From e893cd24da84d119aaaeda10468604281a06376c Mon Sep 17 00:00:00 2001 From: magodo Date: Fri, 10 Jun 2022 13:43:00 +0800 Subject: [PATCH] Support storage file share (#150) --- internal/armtemplate/armtemplate.go | 4 +- internal/armtemplate/armtemplate_hack.go | 20 ++++++++-- internal/test/case_storage_file_share.go | 49 ++++++++++++++++++++++++ internal/test/e2e_test.go | 7 ++++ 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 internal/test/case_storage_file_share.go diff --git a/internal/armtemplate/armtemplate.go b/internal/armtemplate/armtemplate.go index 8fc043a..7791a62 100644 --- a/internal/armtemplate/armtemplate.go +++ b/internal/armtemplate/armtemplate.go @@ -144,9 +144,11 @@ type FQTemplate struct { } type FQResource struct { + // The Id is representing the TF resource ID. Id string Properties interface{} - DependsOn []string + // The IDs in the DependsOn are TF resource IDs. + DependsOn []string } func (tpl FQTemplate) DependencyInfo() (map[string][]string, error) { diff --git a/internal/armtemplate/armtemplate_hack.go b/internal/armtemplate/armtemplate_hack.go index e82ca00..e00a8f5 100644 --- a/internal/armtemplate/armtemplate_hack.go +++ b/internal/armtemplate/armtemplate_hack.go @@ -135,6 +135,8 @@ func (res ResourceId) ProviderId(sub, rg string, b *client.ClientBuilder) (strin "Microsoft.KeyVault/vaults/secrets", "Microsoft.KeyVault/vaults/certificates": return res.providerIdForKeyVaultNestedItems(sub, rg, b) + case "Microsoft.Storage/storageAccounts/fileServices/shares": + return res.providerIdForStoragFileShare(sub, rg, b) default: return res.ID(sub, rg), nil } @@ -163,7 +165,7 @@ func (res ResourceId) providerIdForKeyVaultNestedItems(sub, rg string, b *client } segs := strings.Split(res.Name, "/") if len(segs) != 2 { - return "", fmt.Errorf("malformed resource name %q for %q", res.Type, res.Name) + return "", fmt.Errorf("malformed resource name %q for %q", res.Name, res.Type) } resp, err := client.Get(ctx, rg, segs[0], segs[1], nil) if err != nil { @@ -180,7 +182,7 @@ func (res ResourceId) providerIdForKeyVaultNestedItems(sub, rg string, b *client } segs := strings.Split(res.Name, "/") if len(segs) != 2 { - return "", fmt.Errorf("malformed resource name %q for %q", res.Type, res.Name) + return "", fmt.Errorf("malformed resource name %q for %q", res.Name, res.Type) } resp, err := client.Get(ctx, rg, segs[0], segs[1], nil) if err != nil { @@ -200,7 +202,7 @@ func (res ResourceId) providerIdForKeyVaultNestedItems(sub, rg string, b *client } segs := strings.Split(res.Name, "/") if len(segs) != 2 { - return "", fmt.Errorf("malformed resource name %q for %q", res.Type, res.Name) + return "", fmt.Errorf("malformed resource name %q for %q", res.Name, res.Type) } resp, err := client.Get(ctx, rg, segs[0], segs[1], nil) if err != nil { @@ -216,3 +218,15 @@ func (res ResourceId) providerIdForKeyVaultNestedItems(sub, rg string, b *client } panic("never reach here") } + +func (res ResourceId) providerIdForStoragFileShare(sub, rg string, b *client.ClientBuilder) (string, error) { + // See issue: https://github.com/Azure/aztfy/issues/130 + + // Normally we should retrieve the data plane URL of the storage file share via API. While there is no such attribute in its mgmt plane API model. + // Therefore, we simply construct the data plane URL via its mgmt plane resource ID. + segs := strings.Split(res.Name, "/") + if len(segs) != 3 { + return "", fmt.Errorf("malformed resource name %q for %q", res.Name, res.Type) + } + return fmt.Sprintf("https://%s.file.core.windows.net/%s", segs[0], segs[2]), nil +} diff --git a/internal/test/case_storage_file_share.go b/internal/test/case_storage_file_share.go new file mode 100644 index 0000000..d1929bd --- /dev/null +++ b/internal/test/case_storage_file_share.go @@ -0,0 +1,49 @@ +package test + +import ( + "encoding/json" + "fmt" + + "github.com/Azure/aztfy/internal/resmap" +) + +type CaseStorageFileShare struct{} + +func (CaseStorageFileShare) Tpl(d Data) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} +resource "azurerm_resource_group" "test" { + name = "%[1]s" + location = "WestEurope" +} + +resource "azurerm_storage_account" "test" { + name = "aztfy%[2]s" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + account_tier = "Standard" + account_replication_type = "LRS" +} +resource "azurerm_storage_share" "test" { + name = "aztfy%[2]s" + storage_account_name = azurerm_storage_account.test.name + quota = 5 +} +`, d.RandomRgName(), d.RandomStringOfLength(8)) +} + +func (CaseStorageFileShare) ResourceMapping(d Data) (resmap.ResourceMapping, error) { + rm := fmt.Sprintf(`{ +"/subscriptions/%[1]s/resourceGroups/%[2]s": "azurerm_resource_group.test", +"/subscriptions/%[1]s/resourceGroups/%[2]s/providers/Microsoft.Storage/storageAccounts/aztfy%[3]s": "azurerm_storage_account.test", +"https://aztfy%[3]s.file.core.windows.net/aztfy%[3]s": "azurerm_storage_share.test" +} +`, d.subscriptionId, d.RandomRgName(), d.RandomStringOfLength(8)) + m := resmap.ResourceMapping{} + if err := json.Unmarshal([]byte(rm), &m); err != nil { + return nil, err + } + return m, nil +} diff --git a/internal/test/e2e_test.go b/internal/test/e2e_test.go index 9918f9d..24e9368 100644 --- a/internal/test/e2e_test.go +++ b/internal/test/e2e_test.go @@ -135,3 +135,10 @@ func TestFunctionAppSlot(t *testing.T) { c, d := CaseFunctionAppSlot{}, NewData() runCase(t, d, c) } + +func TestStorageFileShare(t *testing.T) { + t.Parallel() + precheck(t) + c, d := CaseStorageFileShare{}, NewData() + runCase(t, d, c) +}