Skip to content

Commit 996d80f

Browse files
committed
Fix azapi_resource move from azurerm_storage_share by converting /fileshares/ to /shares/
1 parent 377baeb commit 996d80f

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ENHANCEMENTS:
2424
BUG FIXES:
2525
- Fix validation logic for properties that are both `ReadOnly` and `Required`.
2626
- Fix panic when using `sensitive_body_version` with empty `sensitive_body` (GH-999).
27+
- Fix `azapi_resource` resource move from `azurerm_storage_share` by converting `/fileshares/` to `/shares/` in resource ID.
2728

2829
## v2.7.0
2930

internal/services/azapi_resource.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,7 @@ func (r *AzapiResource) tagsWithDefaultTags(config types.Map, state *AzapiResour
13701370
// into the corresponding ARM ID. Some azurerm resources expose management-plane IDs using alternative attribute names.
13711371
// Supported cases:
13721372
// - azurerm_storage_container: uses `resource_manager_id`
1373+
// - azurerm_storage_share: uses `resource_manager_id`
13731374
// - azurerm_key_vault_secret: uses `resource_versionless_id`
13741375
// - azurerm_key_vault_key: uses `resource_versionless_id`
13751376
//
@@ -1389,6 +1390,17 @@ func deriveAzureArmIdFromAzurermState(ctx context.Context, azurermType, primaryI
13891390
}
13901391
return "", fmt.Errorf("unable to derive ARM resource ID for storage container: missing attribute 'resource_manager_id' in source state")
13911392
}
1393+
case "azurerm_storage_share":
1394+
if isHTTPS {
1395+
if sourceState != nil {
1396+
var armId string
1397+
if err := sourceState.GetAttribute(ctx, path.Root("resource_manager_id"), &armId); err == nil && armId != "" {
1398+
// Convert /fileshares/ to /shares/ in the resource ID
1399+
return parse.AzurermIdToAzureId(azurermType, armId)
1400+
}
1401+
}
1402+
return "", fmt.Errorf("unable to derive ARM resource ID for storage share: missing attribute 'resource_manager_id' in source state")
1403+
}
13921404
case "azurerm_key_vault_secret":
13931405
if isHTTPS {
13941406
if sourceState != nil {

internal/services/azapi_resource_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,31 @@ func TestAccGenericResource_moveStorageContainer(t *testing.T) {
700700
})
701701
}
702702

703+
func TestAccGenericResource_moveStorageShare(t *testing.T) {
704+
data := acceptance.BuildTestData(t, "azapi_resource", "test")
705+
r := GenericResource{}
706+
data.ResourceTest(t, r, []resource.TestStep{
707+
{
708+
Config: r.moveStorageShareSetup(data),
709+
ExternalProviders: externalProvidersAzurerm(),
710+
},
711+
{
712+
Config: r.moveStorageShareStartMoving(data),
713+
Check: resource.ComposeTestCheckFunc(
714+
check.That(data.ResourceName).ExistsInAzure(r),
715+
),
716+
ExternalProviders: externalProvidersAzurerm(),
717+
},
718+
{
719+
Config: r.moveStorageShareUpdate(data),
720+
Check: resource.ComposeTestCheckFunc(
721+
check.That(data.ResourceName).ExistsInAzure(r),
722+
),
723+
ExternalProviders: externalProvidersAzurerm(),
724+
},
725+
})
726+
}
727+
703728
func TestAccGenericResource_moveKeyVaultSecret(t *testing.T) {
704729
data := acceptance.BuildTestData(t, "azapi_resource", "test")
705730
r := GenericResource{}
@@ -2749,6 +2774,128 @@ resource "azapi_resource" "test" {
27492774
`, r.template(data), data.RandomString)
27502775
}
27512776

2777+
func (r GenericResource) moveStorageShareSetup(data acceptance.TestData) string {
2778+
return fmt.Sprintf(`
2779+
%s
2780+
2781+
provider "azurerm" {
2782+
features {}
2783+
}
2784+
2785+
resource "azurerm_storage_account" "sa" {
2786+
name = "acctestsa%[2]s"
2787+
location = azapi_resource.resourceGroup.location
2788+
resource_group_name = azapi_resource.resourceGroup.name
2789+
account_tier = "Standard"
2790+
account_replication_type = "LRS"
2791+
}
2792+
2793+
resource "azurerm_storage_share" "share" {
2794+
name = "acctestshare%[2]s"
2795+
storage_account_name = azurerm_storage_account.sa.name
2796+
quota = 50
2797+
}
2798+
`, r.template(data), data.RandomString)
2799+
}
2800+
2801+
func (r GenericResource) moveStorageShareStartMoving(data acceptance.TestData) string {
2802+
return fmt.Sprintf(`
2803+
%s
2804+
2805+
provider "azurerm" {
2806+
features {}
2807+
}
2808+
2809+
moved {
2810+
from = azurerm_storage_share.share
2811+
to = azapi_resource.test
2812+
}
2813+
2814+
resource "azurerm_storage_account" "sa" {
2815+
name = "acctestsa%[2]s"
2816+
location = azapi_resource.resourceGroup.location
2817+
resource_group_name = azapi_resource.resourceGroup.name
2818+
account_tier = "Standard"
2819+
account_replication_type = "LRS"
2820+
}
2821+
2822+
//resource "azurerm_storage_share" "share" {
2823+
// name = "acctestshare%[2]s"
2824+
// storage_account_name = azurerm_storage_account.sa.name
2825+
// quota = 50
2826+
//}
2827+
2828+
data "azapi_resource_id" "fileService" {
2829+
type = "Microsoft.Storage/storageAccounts/fileServices@2023-05-01"
2830+
parent_id = azurerm_storage_account.sa.id
2831+
name = "default"
2832+
}
2833+
2834+
resource "azapi_resource" "test" {
2835+
type = "Microsoft.Storage/storageAccounts/fileServices/shares@2023-05-01"
2836+
parent_id = data.azapi_resource_id.fileService.id
2837+
name = "acctestshare%[2]s"
2838+
body = {
2839+
properties = {
2840+
shareQuota = 50
2841+
}
2842+
}
2843+
ignore_casing = false
2844+
schema_validation_enabled = true
2845+
ignore_missing_property = true
2846+
}
2847+
`, r.template(data), data.RandomString)
2848+
}
2849+
2850+
func (r GenericResource) moveStorageShareUpdate(data acceptance.TestData) string {
2851+
return fmt.Sprintf(`
2852+
%s
2853+
2854+
provider "azurerm" {
2855+
features {}
2856+
}
2857+
2858+
moved {
2859+
from = azurerm_storage_share.share
2860+
to = azapi_resource.test
2861+
}
2862+
2863+
resource "azurerm_storage_account" "sa" {
2864+
name = "acctestsa%[2]s"
2865+
location = azapi_resource.resourceGroup.location
2866+
resource_group_name = azapi_resource.resourceGroup.name
2867+
account_tier = "Standard"
2868+
account_replication_type = "LRS"
2869+
}
2870+
2871+
//resource "azurerm_storage_share" "share" {
2872+
// name = "acctestshare%[2]s"
2873+
// storage_account_name = azurerm_storage_account.sa.name
2874+
// quota = 50
2875+
//}
2876+
2877+
data "azapi_resource_id" "fileService" {
2878+
type = "Microsoft.Storage/storageAccounts/fileServices@2023-05-01"
2879+
parent_id = azurerm_storage_account.sa.id
2880+
name = "default"
2881+
}
2882+
2883+
resource "azapi_resource" "test" {
2884+
type = "Microsoft.Storage/storageAccounts/fileServices/shares@2023-05-01"
2885+
parent_id = data.azapi_resource_id.fileService.id
2886+
name = "acctestshare%[2]s"
2887+
body = {
2888+
properties = {
2889+
shareQuota = 100
2890+
}
2891+
}
2892+
ignore_casing = false
2893+
schema_validation_enabled = true
2894+
ignore_missing_property = true
2895+
}
2896+
`, r.template(data), data.RandomString)
2897+
}
2898+
27522899
func (r GenericResource) moveKeyVaultSecretSetup(data acceptance.TestData) string {
27532900
return fmt.Sprintf(`
27542901
%s

internal/services/parse/azurerm_resource_id.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func AzurermIdToAzureId(azurermResourceType string, azurermId string) (string, e
2424
return "", fmt.Errorf("invalid id: %s, expected format: <role definition id>|<scope>", azurermId)
2525
}
2626
return azurermIdSplit[0], nil
27+
case "azurerm_storage_share":
28+
return strings.Replace(azurermId, "/fileshares/", "/shares/", 1), nil
2729

2830
// add more cases here as needed
2931
}

internal/services/parse/azurerm_resource_id_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ func Test_AzurermIdToAzureId(t *testing.T) {
3535
expectedAzureId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Authorization/roleDefinitions/roleDefinitionId",
3636
expectError: false,
3737
},
38+
{
39+
name: "azurerm_storage_share",
40+
azurermResourceType: "azurerm_storage_share",
41+
azurermId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Storage/storageAccounts/sa/fileServices/default/fileshares/share",
42+
expectedAzureId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Storage/storageAccounts/sa/fileServices/default/shares/share",
43+
expectError: false,
44+
},
3845
}
3946

4047
for _, tc := range testcases {

0 commit comments

Comments
 (0)