Skip to content

Commit

Permalink
bugfix: format azurerm resource ID when move resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Feb 28, 2025
1 parent eee1806 commit dca7b30
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ENHANCEMENTS:
BUG FIXES:
- Fix a bug that query parameters and headers don't work properly with unknown values
- Fix more edge cases that the provider produced inconsistent result after apply when default output feature is enabled.
- Fix a bug that when moving resource from `azurerm` resource, the id could not be parsed correctly.


## v2.2.0
Expand Down
9 changes: 8 additions & 1 deletion internal/services/azapi_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1094,12 +1094,19 @@ func (r *AzapiResource) MoveState(ctx context.Context) []resource.StateMover {
response.Diagnostics.AddError("Invalid source state", "The source state does not contain an id")
return
}
id, err := parse.ResourceID(requestID)

azureId, err := parse.AzurermIdToAzureId(request.SourceTypeName, requestID)
if err != nil {
response.Diagnostics.AddError("Invalid Resource ID", fmt.Errorf("parsing Resource ID %q: %+v", requestID, err).Error())
return
}

id, err := parse.ResourceID(azureId)
if err != nil {
response.Diagnostics.AddError("Invalid Resource ID", fmt.Errorf("parsing Resource ID %q: %+v", azureId, err).Error())
return
}

state := r.defaultAzapiResourceModel()
state.ID = types.StringValue(id.ID())
state.Name = types.StringValue(id.Name)
Expand Down
23 changes: 23 additions & 0 deletions internal/services/parse/azurerm_resource_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package parse

import (
"fmt"
"strings"
)

// AzurermIdToAzureId converts an azurerm id to an azure resource id
func AzurermIdToAzureId(azurermResourceType string, azurermId string) (string, error) {
switch azurermResourceType {

Check failure on line 10 in internal/services/parse/azurerm_resource_id.go

View workflow job for this annotation

GitHub Actions / golint

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case "azurerm_monitor_diagnostic_setting":
// input: <target id>|<diagnostic setting name>
// output: <target id>/providers/Microsoft.Insights/diagnosticSettings/<diagnostic setting name>
azurermIdSplit := strings.Split(azurermId, "|")
if len(azurermIdSplit) != 2 {
return "", fmt.Errorf("invalid id: %s, expected format: <target id>|<diagnostic setting name>", azurermId)
}
return fmt.Sprintf("%s/providers/Microsoft.Insights/diagnosticSettings/%s", azurermIdSplit[0], azurermIdSplit[1]), nil
// add more cases here as needed
}
// return azure id
return azurermId, nil
}
45 changes: 45 additions & 0 deletions internal/services/parse/azurerm_resource_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package parse_test

import (
"testing"

"github.com/Azure/terraform-provider-azapi/internal/services/parse"
)

func Test_AzurermIdToAzureId(t *testing.T) {
testcases := []struct {
name string
azurermResourceType string
azurermId string
expectedAzureId string
expectError bool
}{
{
name: "azurerm_monitor_diagnostic_setting valid",
azurermResourceType: "azurerm_monitor_diagnostic_setting",
azurermId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm|diagnosticSettingName",
expectedAzureId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm/providers/Microsoft.Insights/diagnosticSettings/diagnosticSettingName",
expectError: false,
},
{
name: "azurerm_monitor_diagnostic_setting invalid",
azurermResourceType: "azurerm_monitor_diagnostic_setting",
azurermId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm|diagnosticSettingName|foo",
expectedAzureId: "",
expectError: true,
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
azureId, err := parse.AzurermIdToAzureId(tc.azurermResourceType, tc.azurermId)
if (err != nil) != tc.expectError {
t.Fatalf("expected error: %v, got: %v", tc.expectError, err)
}
if azureId != tc.expectedAzureId {
t.Fatalf("expected azure id: %s, got: %s", tc.expectedAzureId, azureId)
}
})
}

}

0 comments on commit dca7b30

Please sign in to comment.