Skip to content

Commit

Permalink
Support storage file share (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo authored Jun 10, 2022
1 parent f957fc1 commit e893cd2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
4 changes: 3 additions & 1 deletion internal/armtemplate/armtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 17 additions & 3 deletions internal/armtemplate/armtemplate_hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
49 changes: 49 additions & 0 deletions internal/test/case_storage_file_share.go
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions internal/test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit e893cd2

Please sign in to comment.