Skip to content

Commit 9c25dcb

Browse files
Resource Identity and List : voiceservices (hashicorp#31862)
1 parent ac5e769 commit 9c25dcb

13 files changed

+674
-84
lines changed

internal/services/voiceservices/registration.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,8 @@ func (r Registration) EphemeralResources() []func() ephemeral.EphemeralResource
5858
}
5959

6060
func (r Registration) ListResources() []sdk.FrameworkListWrappedResource {
61-
return []sdk.FrameworkListWrappedResource{}
61+
return []sdk.FrameworkListWrappedResource{
62+
CommunicationsGatewayListResource{},
63+
CommunicationsGatewayTestLineListResource{},
64+
}
6265
}

internal/services/voiceservices/voice_services_communications_gateway_resource.go

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ import (
1515
"github.com/hashicorp/go-azure-helpers/lang/response"
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/voiceservices/2023-04-03/communicationsgateways"
1920
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
2021
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
2122
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
2223
)
2324

25+
//go:generate go run ../../tools/generator-tests resourceidentity -resource-name voice_services_communications_gateway -service-package-name voiceservices -properties "name,resource_group_name" -known-values "subscription_id:data.Subscriptions.Primary"
26+
2427
type CommunicationsGatewayModel struct {
2528
Name string `tfschema:"name"`
2629
ResourceGroupName string `tfschema:"resource_group_name"`
@@ -50,9 +53,15 @@ type PrimaryRegionPropertiesModel struct{}
5053

5154
type CommunicationsGatewayResource struct{}
5255

53-
var _ sdk.ResourceWithUpdate = CommunicationsGatewayResource{}
56+
var (
57+
_ sdk.ResourceWithUpdate = CommunicationsGatewayResource{}
58+
_ sdk.ResourceWithCustomizeDiff = CommunicationsGatewayResource{}
59+
_ sdk.ResourceWithIdentity = CommunicationsGatewayResource{}
60+
)
5461

55-
var _ sdk.ResourceWithCustomizeDiff = CommunicationsGatewayResource{}
62+
func (r CommunicationsGatewayResource) Identity() resourceids.ResourceId {
63+
return &communicationsgateways.CommunicationsGatewayId{}
64+
}
5665

5766
func (r CommunicationsGatewayResource) ResourceType() string {
5867
return "azurerm_voice_services_communications_gateway"
@@ -296,6 +305,9 @@ func (r CommunicationsGatewayResource) Create() sdk.ResourceFunc {
296305
}
297306

298307
metadata.SetID(id)
308+
if err := pluginsdk.SetResourceIdentityData(metadata.ResourceData, &id); err != nil {
309+
return err
310+
}
299311

300312
return nil
301313
},
@@ -410,63 +422,57 @@ func (r CommunicationsGatewayResource) Read() sdk.ResourceFunc {
410422
return fmt.Errorf("retrieving %s: model was nil", id)
411423
}
412424

413-
state := CommunicationsGatewayModel{
414-
Name: id.CommunicationsGatewayName,
415-
ResourceGroupName: id.ResourceGroupName,
416-
Location: location.Normalize(model.Location),
417-
}
425+
return r.flatten(metadata, id, model)
426+
},
427+
}
428+
}
418429

419-
if properties := model.Properties; properties != nil {
420-
state.Connectivity = string(properties.Connectivity)
430+
func (r CommunicationsGatewayResource) flatten(metadata sdk.ResourceMetaData, id *communicationsgateways.CommunicationsGatewayId, model *communicationsgateways.CommunicationsGateway) error {
431+
state := CommunicationsGatewayModel{
432+
Name: id.CommunicationsGatewayName,
433+
ResourceGroupName: id.ResourceGroupName,
434+
Location: location.Normalize(model.Location),
435+
}
421436

422-
codecsValue := ""
423-
if len(properties.Codecs) > 0 {
424-
codecsValue = string(properties.Codecs[0])
425-
}
426-
state.Codecs = codecsValue
437+
if properties := model.Properties; properties != nil {
438+
state.Connectivity = string(properties.Connectivity)
427439

428-
state.E911Type = properties.E911Type
440+
codecsValue := ""
441+
if len(properties.Codecs) > 0 {
442+
codecsValue = string(properties.Codecs[0])
443+
}
444+
state.Codecs = codecsValue
429445

430-
state.Platforms = flattenCommunicationsPlatformModel(properties.Platforms)
446+
state.E911Type = properties.E911Type
431447

432-
state.ServiceLocation = flattenServiceRegionPropertiesModel(&properties.ServiceLocations)
448+
state.Platforms = flattenCommunicationsPlatformModel(properties.Platforms)
433449

434-
if properties.AutoGeneratedDomainNameLabelScope != nil {
435-
state.AutoGeneratedDomainNameLabelScope = *properties.AutoGeneratedDomainNameLabelScope
436-
}
450+
state.ServiceLocation = flattenServiceRegionPropertiesModel(&properties.ServiceLocations)
437451

438-
if properties.ApiBridge != nil && *properties.ApiBridge != nil {
439-
apiBridgeValue, err := json.Marshal(*properties.ApiBridge)
440-
if err != nil {
441-
return fmt.Errorf("marshalling value for ApiBridge: %+v", err)
442-
}
443-
state.ApiBridge = string(apiBridgeValue)
444-
}
452+
state.AutoGeneratedDomainNameLabelScope = pointer.From(properties.AutoGeneratedDomainNameLabelScope)
445453

446-
if properties.EmergencyDialStrings != nil {
447-
state.EmergencyDialStrings = *properties.EmergencyDialStrings
448-
}
454+
if properties.ApiBridge != nil && *properties.ApiBridge != nil {
455+
apiBridgeValue, err := json.Marshal(*properties.ApiBridge)
456+
if err != nil {
457+
return fmt.Errorf("marshalling value for ApiBridge: %+v", err)
458+
}
459+
state.ApiBridge = string(apiBridgeValue)
460+
}
449461

450-
onPremMcpEnabled := false
451-
if properties.OnPremMcpEnabled != nil {
452-
onPremMcpEnabled = *properties.OnPremMcpEnabled
453-
}
454-
state.OnPremMcpEnabled = onPremMcpEnabled
462+
state.EmergencyDialStrings = pointer.From(properties.EmergencyDialStrings)
455463

456-
v := ""
457-
if properties.TeamsVoicemailPilotNumber != nil {
458-
v = *properties.TeamsVoicemailPilotNumber
459-
}
460-
state.MicrosoftTeamsVoicemailPilotNumber = v
461-
}
464+
state.OnPremMcpEnabled = pointer.From(properties.OnPremMcpEnabled)
462465

463-
if model.Tags != nil {
464-
state.Tags = *model.Tags
465-
}
466+
state.MicrosoftTeamsVoicemailPilotNumber = pointer.From(properties.TeamsVoicemailPilotNumber)
467+
}
466468

467-
return metadata.Encode(&state)
468-
},
469+
state.Tags = pointer.From(model.Tags)
470+
471+
if err := pluginsdk.SetResourceIdentityData(metadata.ResourceData, id); err != nil {
472+
return err
469473
}
474+
475+
return metadata.Encode(&state)
470476
}
471477

472478
func (r CommunicationsGatewayResource) Delete() sdk.ResourceFunc {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright IBM Corp. 2014, 2025
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package voiceservices_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+
customstatecheck "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/statecheck"
14+
)
15+
16+
func TestAccVoiceServicesCommunicationsGateway_resourceIdentity(t *testing.T) {
17+
data := acceptance.BuildTestData(t, "azurerm_voice_services_communications_gateway", "test")
18+
r := VoiceServicesCommunicationsGatewayResource{}
19+
20+
checkedFields := map[string]struct{}{
21+
"subscription_id": {},
22+
"name": {},
23+
"resource_group_name": {},
24+
}
25+
26+
data.ResourceIdentityTest(t, []acceptance.TestStep{
27+
{
28+
Config: r.basic(data),
29+
ConfigStateChecks: []statecheck.StateCheck{
30+
customstatecheck.ExpectAllIdentityFieldsAreChecked("azurerm_voice_services_communications_gateway.test", checkedFields),
31+
statecheck.ExpectIdentityValue("azurerm_voice_services_communications_gateway.test", tfjsonpath.New("subscription_id"), knownvalue.StringExact(data.Subscriptions.Primary)),
32+
statecheck.ExpectIdentityValueMatchesStateAtPath("azurerm_voice_services_communications_gateway.test", tfjsonpath.New("name"), tfjsonpath.New("name")),
33+
statecheck.ExpectIdentityValueMatchesStateAtPath("azurerm_voice_services_communications_gateway.test", tfjsonpath.New("resource_group_name"), tfjsonpath.New("resource_group_name")),
34+
},
35+
},
36+
data.ImportBlockWithResourceIdentityStep(false),
37+
data.ImportBlockWithIDStep(false),
38+
}, false)
39+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright IBM Corp. 2014, 2025
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package voiceservices
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/hashicorp/go-azure-helpers/lang/pointer"
11+
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
12+
"github.com/hashicorp/go-azure-sdk/resource-manager/voiceservices/2023-04-03/communicationsgateways"
13+
"github.com/hashicorp/terraform-plugin-framework/list"
14+
"github.com/hashicorp/terraform-plugin-framework/resource"
15+
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
16+
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
17+
)
18+
19+
type CommunicationsGatewayListResource struct{}
20+
21+
var _ sdk.FrameworkListWrappedResource = new(CommunicationsGatewayListResource)
22+
23+
func (CommunicationsGatewayListResource) ResourceFunc() *pluginsdk.Resource {
24+
return sdk.WrappedResource(CommunicationsGatewayResource{})
25+
}
26+
27+
func (CommunicationsGatewayListResource) Metadata(_ context.Context, _ resource.MetadataRequest, response *resource.MetadataResponse) {
28+
response.TypeName = CommunicationsGatewayResource{}.ResourceType()
29+
}
30+
31+
func (CommunicationsGatewayListResource) List(ctx context.Context, request list.ListRequest, stream *list.ListResultsStream, metadata sdk.ResourceMetadata) {
32+
client := metadata.Client.VoiceServices.CommunicationsGatewaysClient
33+
34+
var data sdk.DefaultListModel
35+
diags := request.Config.Get(ctx, &data)
36+
if diags.HasError() {
37+
stream.Results = list.ListResultsStreamDiagnostics(diags)
38+
return
39+
}
40+
41+
var results []communicationsgateways.CommunicationsGateway
42+
43+
subscriptionID := metadata.SubscriptionId
44+
if !data.SubscriptionId.IsNull() {
45+
subscriptionID = data.SubscriptionId.ValueString()
46+
}
47+
48+
r := CommunicationsGatewayResource{}
49+
50+
switch {
51+
case !data.ResourceGroupName.IsNull():
52+
resp, err := client.ListByResourceGroupComplete(ctx, commonids.NewResourceGroupID(subscriptionID, data.ResourceGroupName.ValueString()))
53+
if err != nil {
54+
sdk.SetResponseErrorDiagnostic(stream, fmt.Sprintf("listing `%s`", r.ResourceType()), err)
55+
return
56+
}
57+
58+
results = resp.Items
59+
default:
60+
resp, err := client.ListBySubscriptionComplete(ctx, commonids.NewSubscriptionID(subscriptionID))
61+
if err != nil {
62+
sdk.SetResponseErrorDiagnostic(stream, fmt.Sprintf("listing `%s`", r.ResourceType()), err)
63+
return
64+
}
65+
66+
results = resp.Items
67+
}
68+
69+
stream.Results = func(push func(list.ListResult) bool) {
70+
for _, gateway := range results {
71+
result := request.NewListResult(ctx)
72+
result.DisplayName = pointer.From(gateway.Name)
73+
74+
id, err := communicationsgateways.ParseCommunicationsGatewayID(pointer.From(gateway.Id))
75+
if err != nil {
76+
sdk.SetErrorDiagnosticAndPushListResult(result, push, "parsing Communications Gateway ID", err)
77+
return
78+
}
79+
80+
rmd := sdk.NewResourceMetaData(metadata.Client, r)
81+
rmd.SetID(id)
82+
83+
if err := r.flatten(rmd, id, &gateway); err != nil {
84+
sdk.SetErrorDiagnosticAndPushListResult(result, push, fmt.Sprintf("encoding `%s` resource data", r.ResourceType()), err)
85+
return
86+
}
87+
88+
sdk.EncodeListResult(ctx, rmd.ResourceData, &result)
89+
if result.Diagnostics.HasError() {
90+
push(result)
91+
return
92+
}
93+
94+
if !push(result) {
95+
return
96+
}
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)