Skip to content

Commit

Permalink
Provider function parse_resource_id: Populate the resource_name f…
Browse files Browse the repository at this point in the history
…or resource group and subscription
  • Loading branch information
magodo committed Dec 18, 2024
1 parent 6eb4009 commit 939ca38
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
8 changes: 6 additions & 2 deletions internal/provider/function/parse_resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
120 changes: 120 additions & 0 deletions internal/provider/function/parse_resource_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 939ca38

Please sign in to comment.