Skip to content

Commit d734cc2

Browse files
authored
azurerm_service_plan: support resource identity (hashicorp#31558)
1 parent 09fa898 commit d734cc2

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

internal/services/appservice/service_plan_resource.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
1616
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
1717
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
18+
"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
1819
"github.com/hashicorp/go-azure-sdk/resource-manager/web/2023-12-01/appserviceplans"
1920
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
2021
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/helpers"
@@ -26,14 +27,21 @@ import (
2627
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
2728
)
2829

30+
//go:generate go run ../../tools/generator-tests resourceidentity -resource-name service_plan -service-package-name appservice -properties "resource_group_name,name" -known-values "subscription_id:data.Subscriptions.Primary"
31+
2932
type ServicePlanResource struct{}
3033

3134
var (
3235
_ sdk.ResourceWithUpdate = ServicePlanResource{}
3336
_ sdk.ResourceWithStateMigration = ServicePlanResource{}
3437
_ sdk.ResourceWithCustomizeDiff = ServicePlanResource{}
38+
_ sdk.ResourceWithIdentity = ServicePlanResource{}
3539
)
3640

41+
func (r ServicePlanResource) Identity() resourceids.ResourceId {
42+
return &commonids.AppServicePlanId{}
43+
}
44+
3745
type OSType string
3846

3947
const (
@@ -214,6 +222,9 @@ func (r ServicePlanResource) Create() sdk.ResourceFunc {
214222
}
215223

216224
metadata.SetID(id)
225+
if err := pluginsdk.SetResourceIdentityData(metadata.ResourceData, &id); err != nil {
226+
return err
227+
}
217228

218229
return nil
219230
},
@@ -283,6 +294,10 @@ func (r ServicePlanResource) Read() sdk.ResourceFunc {
283294
state.Tags = pointer.From(model.Tags)
284295
}
285296

297+
if err := pluginsdk.SetResourceIdentityData(metadata.ResourceData, id); err != nil {
298+
return err
299+
}
300+
286301
return metadata.Encode(&state)
287302
},
288303
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright IBM Corp. 2014, 2025
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package appservice_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
10+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
11+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
12+
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
13+
)
14+
15+
func TestAccServicePlan_resourceIdentity(t *testing.T) {
16+
data := acceptance.BuildTestData(t, "azurerm_service_plan", "test")
17+
r := ServicePlanResource{}
18+
19+
data.ResourceIdentityTest(t, []acceptance.TestStep{
20+
{
21+
Config: r.basic(data),
22+
ConfigStateChecks: []statecheck.StateCheck{
23+
statecheck.ExpectIdentityValue("azurerm_service_plan.test", tfjsonpath.New("subscription_id"), knownvalue.StringExact(data.Subscriptions.Primary)),
24+
statecheck.ExpectIdentityValueMatchesStateAtPath("azurerm_service_plan.test", tfjsonpath.New("name"), tfjsonpath.New("name")),
25+
statecheck.ExpectIdentityValueMatchesStateAtPath("azurerm_service_plan.test", tfjsonpath.New("resource_group_name"), tfjsonpath.New("resource_group_name")),
26+
},
27+
},
28+
data.ImportBlockWithResourceIdentityStep(false),
29+
data.ImportBlockWithIDStep(false),
30+
}, false)
31+
}

0 commit comments

Comments
 (0)