From 05a8abd51c54a01cc370ec07114de5b4b6656b83 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 8 Aug 2023 19:33:33 +0200 Subject: [PATCH] d/network_service_tag: refactoring to use `hashicorp/go-azure-sdk` --- internal/services/network/client/client.go | 5 -- .../network_service_tags_data_source.go | 49 ++++++++----------- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/internal/services/network/client/client.go b/internal/services/network/client/client.go index 85d0ea022b81..a7b8b9c15fed 100644 --- a/internal/services/network/client/client.go +++ b/internal/services/network/client/client.go @@ -44,7 +44,6 @@ type Client struct { ServiceAssociationLinkClient *network.ServiceAssociationLinksClient ServiceEndpointPoliciesClient *network.ServiceEndpointPoliciesClient ServiceEndpointPolicyDefinitionsClient *network.ServiceEndpointPolicyDefinitionsClient - ServiceTagsClient *network.ServiceTagsClient SubnetsClient *network.SubnetsClient NatGatewayClient *network.NatGatewaysClient VirtualHubBgpConnectionClient *network.VirtualHubBgpConnectionClient @@ -148,9 +147,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) { ServiceEndpointPolicyDefinitionsClient := network.NewServiceEndpointPolicyDefinitionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&ServiceEndpointPolicyDefinitionsClient.Client, o.ResourceManagerAuthorizer) - ServiceTagsClient := network.NewServiceTagsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) - o.ConfigureClient(&ServiceTagsClient.Client, o.ResourceManagerAuthorizer) - SubnetsClient := network.NewSubnetsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&SubnetsClient.Client, o.ResourceManagerAuthorizer) @@ -225,7 +221,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) { SecurityPartnerProviderClient: &SecurityPartnerProviderClient, ServiceEndpointPoliciesClient: &ServiceEndpointPoliciesClient, ServiceEndpointPolicyDefinitionsClient: &ServiceEndpointPolicyDefinitionsClient, - ServiceTagsClient: &ServiceTagsClient, SubnetsClient: &SubnetsClient, NatGatewayClient: &NatGatewayClient, VirtualHubBgpConnectionClient: &VirtualHubBgpConnectionClient, diff --git a/internal/services/network/network_service_tags_data_source.go b/internal/services/network/network_service_tags_data_source.go index 6415ea5deeed..f4b953b8133d 100644 --- a/internal/services/network/network_service_tags_data_source.go +++ b/internal/services/network/network_service_tags_data_source.go @@ -11,7 +11,7 @@ import ( "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" "github.com/hashicorp/go-azure-helpers/resourcemanager/location" - "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" + "github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-04-01/servicetags" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/internal/timeouts" @@ -33,12 +33,7 @@ func dataSourceNetworkServiceTags() *pluginsdk.Resource { Required: true, }, - "location_filter": { - Type: pluginsdk.TypeString, - Optional: true, - StateFunc: azure.NormalizeLocation, - DiffSuppressFunc: location.DiffSuppressFunc, - }, + "location_filter": commonschema.LocationOptional(), "address_prefixes": { Type: pluginsdk.TypeList, @@ -68,34 +63,34 @@ func dataSourceNetworkServiceTags() *pluginsdk.Resource { } func dataSourceNetworkServiceTagsRead(d *pluginsdk.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).Network.ServiceTagsClient + client := meta.(*clients.Client).Network.ServiceTags + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - location := azure.NormalizeLocation(d.Get("location")) - res, err := client.List(ctx, location) + locationId := servicetags.NewLocationID(subscriptionId, location.Normalize(d.Get("location").(string))) + resp, err := client.ServiceTagsList(ctx, locationId) if err != nil { return fmt.Errorf("listing network service tags: %+v", err) } - if res.Values == nil { - return fmt.Errorf("unexpected nil value for service tag information") + if resp.Model == nil || resp.Model.Values == nil { + return fmt.Errorf("listing network service tags: `model.Values` was nil") } service := d.Get("service").(string) - locationFilter := azure.NormalizeLocation(d.Get("location_filter")) - - for _, sti := range *res.Values { - if sti.Name == nil || !isServiceTagOf(*sti.Name, service) { + locationFilter := location.Normalize(d.Get("location_filter").(string)) + for _, value := range *resp.Model.Values { + if value.Name == nil || !isServiceTagOf(*value.Name, service) { continue } - if props := sti.Properties; props != nil { + if props := value.Properties; props != nil { if props.Region == nil { continue } - if azure.NormalizeLocation(*props.Region) == locationFilter { + if location.NormalizeNilable(props.Region) == locationFilter { addressPrefixes := make([]string, 0) if props.AddressPrefixes != nil { addressPrefixes = *props.AddressPrefixes @@ -105,8 +100,8 @@ func dataSourceNetworkServiceTagsRead(d *pluginsdk.ResourceData, meta interface{ return fmt.Errorf("setting `address_prefixes`: %+v", err) } - var IPv4 []string - var IPv6 []string + var ipv4Cidrs []string + var ipv6Cidrs []string for _, prefix := range addressPrefixes { ip, ipNet, err := net.ParseCIDR(prefix) @@ -115,31 +110,29 @@ func dataSourceNetworkServiceTagsRead(d *pluginsdk.ResourceData, meta interface{ } if ip.To4() != nil { - IPv4 = append(IPv4, ipNet.String()) + ipv4Cidrs = append(ipv4Cidrs, ipNet.String()) } else { - IPv6 = append(IPv6, ipNet.String()) + ipv6Cidrs = append(ipv6Cidrs, ipNet.String()) } } - err = d.Set("ipv4_cidrs", IPv4) + err = d.Set("ipv4_cidrs", ipv4Cidrs) if err != nil { return fmt.Errorf("setting `ipv4_cidrs`: %+v", err) } - err = d.Set("ipv6_cidrs", IPv6) + err = d.Set("ipv6_cidrs", ipv6Cidrs) if err != nil { return fmt.Errorf("setting `ipv6_cidrs`: %+v", err) } - if sti.ID == nil { - return fmt.Errorf("unexcepted nil ID for service tag") - } + d.SetId(fmt.Sprintf("%s-%s", locationId.LocationName, service)) - d.SetId(*sti.ID) return nil } } } + errSuffix := "globally" if locationFilter != "" { errSuffix = "for region " + locationFilter