diff --git a/internal/provider/function/parse_resource_id.go b/internal/provider/function/parse_resource_id.go index 716e89a8fb61..ef315a9e15e0 100644 --- a/internal/provider/function/parse_resource_id.go +++ b/internal/provider/function/parse_resource_id.go @@ -109,14 +109,18 @@ func (p ParseResourceIDFunction) Run(ctx context.Context, request function.RunRe for k, v := range s { switch v.Type { case resourceids.ResourceGroupSegmentType: - output["resource_group_name"] = types.StringValue(parsed.Parsed[v.Name]) + resourceGroupName := types.StringValue(parsed.Parsed[v.Name]) + output["resource_group_name"] = resourceGroupName + output["resource_name"] = resourceGroupName case resourceids.ResourceProviderSegmentType: output["resource_provider"] = types.StringPointerValue(v.FixedValue) fullResourceType = pointer.From(v.FixedValue) case resourceids.SubscriptionIdSegmentType: - output["subscription_id"] = types.StringValue(parsed.Parsed["subscriptionId"]) + subscriptionId := types.StringValue(parsed.Parsed["subscriptionId"]) + output["subscription_id"] = subscriptionId + output["resource_name"] = subscriptionId case resourceids.StaticSegmentType: switch { diff --git a/internal/provider/function/parse_resource_id_test.go b/internal/provider/function/parse_resource_id_test.go index 6b8f596f436f..a8672c5aa74a 100644 --- a/internal/provider/function/parse_resource_id_test.go +++ b/internal/provider/function/parse_resource_id_test.go @@ -42,6 +42,54 @@ func TestProviderFunctionParseResourceID_basic(t *testing.T) { }) } +func TestProviderFunctionParseResourceID_subscription(t *testing.T) { + t.Parallel() + + resource.UnitTest(t, resource.TestCase{ + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))), + }, + ProtoV5ProviderFactories: framework.ProtoV5ProviderFactoriesInit(context.Background(), "azurerm"), + Steps: []resource.TestStep{ + { + Config: testParseSubscriptionResourceIdOutput("/subscriptions/12345678-1234-9876-4563-123456789012"), + Check: acceptance.ComposeTestCheckFunc( + acceptance.TestCheckOutput("subscription_id", "12345678-1234-9876-4563-123456789012"), + acceptance.TestCheckOutput("resource_group_name", ""), + acceptance.TestCheckOutput("resource_type", "subscriptions"), + acceptance.TestCheckOutput("resource_name", "12345678-1234-9876-4563-123456789012"), + acceptance.TestCheckOutput("resource_scope", ""), + acceptance.TestCheckOutput("full_resource_type", "/subscriptions"), + ), + }, + }, + }) +} + +func TestProviderFunctionParseResourceID_resourceGroup(t *testing.T) { + t.Parallel() + + resource.UnitTest(t, resource.TestCase{ + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipBelow(version.Must(version.NewVersion("1.8.0-beta1"))), + }, + ProtoV5ProviderFactories: framework.ProtoV5ProviderFactoriesInit(context.Background(), "azurerm"), + Steps: []resource.TestStep{ + { + Config: testParseResourceGroupResourceIdOutput("/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1"), + Check: acceptance.ComposeTestCheckFunc( + acceptance.TestCheckOutput("subscription_id", "12345678-1234-9876-4563-123456789012"), + acceptance.TestCheckOutput("resource_group_name", "resGroup1"), + acceptance.TestCheckOutput("resource_type", "resourceGroups"), + acceptance.TestCheckOutput("resource_name", "resGroup1"), + acceptance.TestCheckOutput("resource_scope", ""), + acceptance.TestCheckOutput("full_resource_type", "/resourceGroups"), + ), + }, + }, + }) +} + func TestProviderFunctionParseResourceID_scopedAtSubscription(t *testing.T) { t.Parallel() @@ -139,6 +187,78 @@ output "full_resource_type" { `, id) } +func testParseSubscriptionResourceIdOutput(id string) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +locals { + parsed_id = provider::azurerm::parse_resource_id("%s") +} + +output "resource_name" { + value = local.parsed_id["resource_name"] +} + +output "resource_scope" { + value = local.parsed_id["resource_scope"] +} + +output "resource_group_name" { + value = local.parsed_id["resource_group_name"] +} + +output "resource_type" { + value = local.parsed_id["resource_type"] +} + +output "subscription_id" { + value = local.parsed_id["subscription_id"] +} + +output "full_resource_type" { + value = local.parsed_id["full_resource_type"] +} +`, id) +} + +func testParseResourceGroupResourceIdOutput(id string) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +locals { + parsed_id = provider::azurerm::parse_resource_id("%s") +} + +output "resource_name" { + value = local.parsed_id["resource_name"] +} + +output "resource_scope" { + value = local.parsed_id["resource_scope"] +} + +output "resource_group_name" { + value = local.parsed_id["resource_group_name"] +} + +output "resource_type" { + value = local.parsed_id["resource_type"] +} + +output "subscription_id" { + value = local.parsed_id["subscription_id"] +} + +output "full_resource_type" { + value = local.parsed_id["full_resource_type"] +} +`, id) +} + func testParseScopedResourceIdOutput(id string) string { return fmt.Sprintf(` provider "azurerm" {