-
Notifications
You must be signed in to change notification settings - Fork 72
support customization #923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package services_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/Azure/terraform-provider-azapi/internal/acceptance" | ||
| "github.com/Azure/terraform-provider-azapi/internal/acceptance/check" | ||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| func TestAccGenericResource_customizedKeyVaultKey(t *testing.T) { | ||
| data := acceptance.BuildTestData(t, "azapi_resource", "test") | ||
| r := GenericResource{} | ||
|
|
||
| data.ResourceTest(t, r, []resource.TestStep{ | ||
| { | ||
| Config: r.customizedKeyVaultKey(data), | ||
| Check: resource.ComposeTestCheckFunc( | ||
| check.That(data.ResourceName).ExistsInAzure(r), | ||
| ), | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func (r GenericResource) customizedKeyVaultKey(data acceptance.TestData) string { | ||
| return fmt.Sprintf(` | ||
| %s | ||
|
|
||
|
|
||
| data "azapi_client_config" "current" { | ||
| } | ||
|
|
||
| resource "azapi_resource" "vault" { | ||
| type = "Microsoft.KeyVault/vaults@2023-02-01" | ||
| parent_id = azapi_resource.resourceGroup.id | ||
| name = "acctest%[2]s" | ||
| location = azapi_resource.resourceGroup.location | ||
| body = { | ||
| properties = { | ||
| sku = { | ||
| family = "A" | ||
| name = "standard" | ||
| } | ||
| accessPolicies = [ | ||
| { | ||
| objectId = data.azapi_client_config.current.object_id | ||
| permissions = { | ||
| keys = [ | ||
| "Get", "Create", "Delete", "List", "Restore", "Recover", "UnwrapKey", "WrapKey", "Purge", "Encrypt", "Decrypt", "Sign", "Verify" | ||
| ] | ||
| } | ||
| tenantId = data.azapi_client_config.current.tenant_id | ||
| } | ||
| ] | ||
| enableSoftDelete = true | ||
| enablePurgeProtection = true | ||
| tenantId = data.azapi_client_config.current.tenant_id | ||
| } | ||
| } | ||
| schema_validation_enabled = false | ||
| response_export_values = ["*"] | ||
| } | ||
|
|
||
|
|
||
| resource "azapi_resource" "test" { | ||
| type = "Microsoft.KeyVault/vaults/keys@2023-02-01" | ||
| parent_id = azapi_resource.vault.id | ||
| name = "acctest%[2]s" | ||
| body = { | ||
| properties = { | ||
| keySize = 2048 | ||
| kty = "RSA" | ||
| keyOps = ["encrypt", "decrypt", "sign", "verify", "wrapKey", "unwrapKey"] | ||
| } | ||
| } | ||
| } | ||
| `, r.template(data), data.RandomString) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package customization | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/Azure/terraform-provider-azapi/internal/clients" | ||
| "github.com/Azure/terraform-provider-azapi/internal/services/parse" | ||
| ) | ||
|
|
||
| type Resource interface { | ||
| GetResourceType() string | ||
| CreateFunc() Func | ||
| UpdateFunc() Func | ||
| DeleteFunc() DeleteFunc | ||
| ReadFunc() Func | ||
| } | ||
|
|
||
| type Func func() error | ||
|
|
||
| type DeleteFunc func(ctx context.Context, clients clients.Client, id parse.ResourceId, options clients.RequestOptions) error | ||
|
|
||
| var customizations = make(map[string]Resource) | ||
|
|
||
| func init() { | ||
| var keyVaultKeyCustomization Resource = KeyVaultKeyCustomization{} | ||
| customizations[keyVaultKeyCustomization.GetResourceType()] = keyVaultKeyCustomization | ||
|
|
||
| } | ||
|
|
||
| func GetCustomization(resourceType string) *Resource { | ||
| customization, exists := customizations[resourceType] | ||
| if !exists { | ||
| return nil | ||
| } | ||
| return &customization | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package customization | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/Azure/terraform-provider-azapi/internal/clients" | ||
| "github.com/Azure/terraform-provider-azapi/internal/services/parse" | ||
| "github.com/Azure/terraform-provider-azapi/utils" | ||
| ) | ||
|
|
||
| type KeyVaultKeyCustomization struct { | ||
| } | ||
|
|
||
| func (k KeyVaultKeyCustomization) GetResourceType() string { | ||
| return "Microsoft.KeyVault/vaults/keys" | ||
| } | ||
|
|
||
| func (k KeyVaultKeyCustomization) CreateFunc() Func { | ||
| return nil | ||
| } | ||
|
|
||
| func (k KeyVaultKeyCustomization) UpdateFunc() Func { | ||
| return nil | ||
| } | ||
|
|
||
| func (k KeyVaultKeyCustomization) ReadFunc() Func { | ||
| return nil | ||
| } | ||
|
|
||
| func (k KeyVaultKeyCustomization) DeleteFunc() DeleteFunc { | ||
| return func(ctx context.Context, clients clients.Client, id parse.ResourceId, options clients.RequestOptions) error { | ||
|
|
||
| dataPlaneClient := clients.DataPlaneClient | ||
|
|
||
| path := id.AzureResourceId | ||
| path = strings.TrimPrefix(path, "/") | ||
| path = strings.TrimSuffix(path, "/") | ||
| components := strings.Split(path, "/") | ||
| parts := make(map[string]string) | ||
| for i := 0; i < len(components)-1; i += 2 { | ||
| parts[components[i]] = components[i+1] | ||
| } | ||
|
Comment on lines
+38
to
+45
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps this code should be part of the |
||
|
|
||
| if parts["vaults"] == "" { | ||
| return fmt.Errorf("key vault name is missing in the resource ID: %s", id.AzureResourceId) | ||
| } | ||
|
|
||
| resourceID := fmt.Sprintf("%s.vault.azure.net/keys/%s", parts["vaults"], id.Name) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think hard coding this will be problematic for sovereign cloud: For China cloud: https://vault.azure.cn For US Gov cloud: https://vault.usgovcloudapi.net For Germany: https://vault.microsoftazure.de |
||
|
|
||
| _, err := dataPlaneClient.Action(ctx, resourceID, "", "7.4", http.MethodDelete, nil, options) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two comments here:
|
||
| if err != nil && !utils.ResponseErrorWasNotFound(err) { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| var _ Resource = &KeyVaultKeyCustomization{} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that the interface will store a pointer to the concrete type, consider making this return a
Resourcerather than a*Resource.Also consider returning a bool value, making it similar to the idiomatic go:
if val, ok := GetCustomization(s); ok {}