Skip to content

Commit

Permalink
update nat gateway data source to use hashicorp/go-azure-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
stephybun committed May 8, 2024
1 parent 92812db commit 3ff232d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 46 deletions.
61 changes: 38 additions & 23 deletions internal/services/network/nat_gateway_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-helpers/resourcemanager/zones"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/natgateways"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceNatGateway() *pluginsdk.Resource {
Expand Down Expand Up @@ -78,43 +79,57 @@ func dataSourceNatGateway() *pluginsdk.Resource {
}

func dataSourceNatGatewayRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.NatGatewayClient
client := meta.(*clients.Client).Network.Client.NatGateways
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewNatGatewayID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := natgateways.NewNatGatewayID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
resp, err := client.Get(ctx, id, natgateways.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("reading %s: %+v", id, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}
d.SetId(id.ID())

d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)
if sku := resp.Sku; sku != nil {
d.Set("sku_name", resp.Sku.Name)
}
d.Set("name", id.NatGatewayName)
d.Set("resource_group_name", id.ResourceGroupName)

d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("zones", zones.FlattenUntyped(resp.Zones))
if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))
d.Set("sku_name", pointer.From(model.Sku))

Check failure on line 103 in internal/services/network/nat_gateway_data_source.go

View workflow job for this annotation

GitHub Actions / tflint

R004: ResourceData.Set() incompatible value type: struct{Name *github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/natgateways.NatGatewaySkuName "json:\"name,omitempty\""}
d.Set("zones", zones.FlattenUntyped(model.Zones))
if props := model.Properties; props != nil {
d.Set("idle_timeout_in_minutes", props.IdleTimeoutInMinutes)
d.Set("resource_guid", props.ResourceGuid)

if props := resp.NatGatewayPropertiesFormat; props != nil {
d.Set("idle_timeout_in_minutes", props.IdleTimeoutInMinutes)
d.Set("resource_guid", props.ResourceGUID)
if err := d.Set("public_ip_address_ids", flattenNetworkSubResourceID(props.PublicIPAddresses)); err != nil {
return fmt.Errorf("setting `public_ip_address_ids`: %+v", err)
}

if err := d.Set("public_ip_address_ids", flattenNetworkSubResourceID(props.PublicIPAddresses)); err != nil {
return fmt.Errorf("setting `public_ip_address_ids`: %+v", err)
if err := d.Set("public_ip_prefix_ids", flattenNetworkSubResourceID(props.PublicIPPrefixes)); err != nil {
return fmt.Errorf("setting `public_ip_prefix_ids`: %+v", err)
}
}
return tags.FlattenAndSet(d, model.Tags)
}
return nil
}

func flattenNetworkSubResourceID(input *[]natgateways.SubResource) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

if err := d.Set("public_ip_prefix_ids", flattenNetworkSubResourceID(props.PublicIPPrefixes)); err != nil {
return fmt.Errorf("setting `public_ip_prefix_ids`: %+v", err)
for _, item := range *input {
if item.Id != nil {
results = append(results, *item.Id)
}
}

return tags.FlattenAndSet(d, resp.Tags)
return results
}
23 changes: 0 additions & 23 deletions internal/services/network/network_subresource.go

This file was deleted.

0 comments on commit 3ff232d

Please sign in to comment.