-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: format azurerm resource ID when move resource (#797)
* bugfix: format azurerm resource ID when move resource * add `azurerm_role_definition`
- Loading branch information
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 { | ||
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 | ||
case "azurerm_role_definition": | ||
// input: <role definition id>|<scope> | ||
// output: <role definition id> | ||
azurermIdSplit := strings.Split(azurermId, "|") | ||
if len(azurermIdSplit) != 2 { | ||
return "", fmt.Errorf("invalid id: %s, expected format: <role definition id>|<scope>", azurermId) | ||
} | ||
return azurermIdSplit[0], nil | ||
|
||
// add more cases here as needed | ||
} | ||
// return azure id | ||
return azurermId, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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, | ||
}, | ||
{ | ||
name: "azurerm_role_definition valid", | ||
azurermResourceType: "azurerm_role_definition", | ||
azurermId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Authorization/roleDefinitions/roleDefinitionId|/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg", | ||
expectedAzureId: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Authorization/roleDefinitions/roleDefinitionId", | ||
expectError: false, | ||
}, | ||
} | ||
|
||
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) | ||
} | ||
}) | ||
} | ||
|
||
} |