Skip to content

Commit

Permalink
network - update virtual network resources to use `hashicorp/go-azu…
Browse files Browse the repository at this point in the history
…re-sdk` (#26217)

* update remaining virtual network resources to use go-azure-sdk

* replace references to network enums with hashicorp/go-azure-sdk

* fix references to virtual network resource in tests

* conditionally set generation

* update public ip sku to standard to fix tests

* use get shared key endpoint to set shared key

* set shared_key as conditionally computed
  • Loading branch information
stephybun authored Jun 5, 2024
1 parent 4335e31 commit e94a59e
Show file tree
Hide file tree
Showing 17 changed files with 1,072 additions and 1,198 deletions.
108 changes: 0 additions & 108 deletions internal/services/network/azuresdkhacks/network_interface.go

This file was deleted.

21 changes: 0 additions & 21 deletions internal/services/network/edge_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,8 @@ package network

import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/edgezones"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/network/2022-07-01/network"
)

func expandEdgeZone(input string) *network.ExtendedLocation {
normalized := edgezones.Normalize(input)
if normalized == "" {
return nil
}

return &network.ExtendedLocation{
Name: utils.String(normalized),
Type: network.ExtendedLocationTypesEdgeZone,
}
}

func flattenEdgeZone(input *network.ExtendedLocation) string {
if input == nil || input.Type != network.ExtendedLocationTypesEdgeZone || input.Name == nil {
return ""
}
return edgezones.NormalizeNilable(input.Name)
}

func expandEdgeZoneModel(input string) *edgezones.Model {
normalized := edgezones.Normalize(input)
if normalized == "" {
Expand Down
89 changes: 50 additions & 39 deletions internal/services/network/virtual_network_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/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"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-sdk/resource-manager/network/2023-11-01/virtualnetworks"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/network/2022-07-01/network"
)

func dataSourceVirtualNetwork() *pluginsdk.Resource {
Expand All @@ -38,7 +39,7 @@ func dataSourceVirtualNetwork() *pluginsdk.Resource {

"location": commonschema.LocationComputed(),

"tags": tags.SchemaDataSource(),
"tags": commonschema.TagsDataSource(),

"address_space": {
Type: pluginsdk.TypeList,
Expand Down Expand Up @@ -88,59 +89,61 @@ func dataSourceVirtualNetwork() *pluginsdk.Resource {
}

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

id := commonids.NewVirtualNetworkID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
resp, err := client.Get(ctx, id.ResourceGroupName, id.VirtualNetworkName, "")
resp, err := client.Get(ctx, id, virtualnetworks.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: %s was not found", id)
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.SetId(id.ID())

d.Set("location", location.NormalizeNilable(resp.Location))
if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))

if props := resp.VirtualNetworkPropertiesFormat; props != nil {
d.Set("guid", props.ResourceGUID)
if props := model.Properties; props != nil {
d.Set("guid", props.ResourceGuid)

if as := props.AddressSpace; as != nil {
if err := d.Set("address_space", utils.FlattenStringSlice(as.AddressPrefixes)); err != nil {
return fmt.Errorf("setting `address_space`: %v", err)
if as := props.AddressSpace; as != nil {
if err := d.Set("address_space", utils.FlattenStringSlice(as.AddressPrefixes)); err != nil {
return fmt.Errorf("setting `address_space`: %v", err)
}
}
}

if options := props.DhcpOptions; options != nil {
if err := d.Set("dns_servers", utils.FlattenStringSlice(options.DNSServers)); err != nil {
return fmt.Errorf("setting `dns_servers`: %v", err)
if options := props.DhcpOptions; options != nil {
if err := d.Set("dns_servers", utils.FlattenStringSlice(options.DnsServers)); err != nil {
return fmt.Errorf("setting `dns_servers`: %v", err)
}
}
}

if err := d.Set("subnets", flattenVnetSubnetsNames(props.Subnets)); err != nil {
return fmt.Errorf("setting `subnets`: %v", err)
}
if err := d.Set("subnets", flattenVnetSubnetsNames(props.Subnets)); err != nil {
return fmt.Errorf("setting `subnets`: %v", err)
}

if err := d.Set("vnet_peerings", flattenVnetPeerings(props.VirtualNetworkPeerings)); err != nil {
return fmt.Errorf("setting `vnet_peerings`: %v", err)
}
if err := d.Set("vnet_peerings", flattenVnetPeerings(props.VirtualNetworkPeerings)); err != nil {
return fmt.Errorf("setting `vnet_peerings`: %v", err)
}

if err := d.Set("vnet_peerings_addresses", flattenVnetPeeringsdAddressList(props.VirtualNetworkPeerings)); err != nil {
return fmt.Errorf("setting `vnet_peerings_addresses`: %v", err)
}
if err := d.Set("vnet_peerings_addresses", flattenVnetPeeringsdAddressList(props.VirtualNetworkPeerings)); err != nil {
return fmt.Errorf("setting `vnet_peerings_addresses`: %v", err)
}

return tags.FlattenAndSet(d, resp.Tags)
}
return tags.FlattenAndSet(d, model.Tags)
}

return nil
}

func flattenVnetSubnetsNames(input *[]network.Subnet) []interface{} {
func flattenVnetSubnetsNames(input *[]virtualnetworks.Subnet) []interface{} {
subnets := make([]interface{}, 0)

if mysubnets := input; mysubnets != nil {
Expand All @@ -153,32 +156,40 @@ func flattenVnetSubnetsNames(input *[]network.Subnet) []interface{} {
return subnets
}

func flattenVnetPeerings(input *[]network.VirtualNetworkPeering) map[string]interface{} {
func flattenVnetPeerings(input *[]virtualnetworks.VirtualNetworkPeering) map[string]interface{} {
output := make(map[string]interface{})

if peerings := input; peerings != nil {
for _, vnetpeering := range *peerings {
if vnetpeering.Name == nil || vnetpeering.RemoteVirtualNetwork == nil || vnetpeering.RemoteVirtualNetwork.ID == nil {
for _, vnetPeering := range *peerings {
if vnetPeering.Name == nil {
continue
}

key := *vnetpeering.Name
value := *vnetpeering.RemoteVirtualNetwork.ID
value := ""
if props := vnetPeering.Properties; props != nil {
if props.RemoteVirtualNetwork == nil || props.RemoteVirtualNetwork.Id == nil {
continue
}
value = *props.RemoteVirtualNetwork.Id

}
key := *vnetPeering.Name
output[key] = value
}
}

return output
}

func flattenVnetPeeringsdAddressList(input *[]network.VirtualNetworkPeering) []string {
func flattenVnetPeeringsdAddressList(input *[]virtualnetworks.VirtualNetworkPeering) []string {
var output []string
if peerings := input; peerings != nil {
for _, vnetpeering := range *peerings {
for _, addresses := range *vnetpeering.RemoteVirtualNetworkAddressSpace.AddressPrefixes {
if addresses != "" {
output = append(output, addresses)
for _, vnetPeering := range *peerings {
if props := vnetPeering.Properties; props != nil {
for _, addresses := range *props.RemoteVirtualNetworkAddressSpace.AddressPrefixes {
if addresses != "" {
output = append(output, addresses)
}
}
}
}
Expand Down
Loading

0 comments on commit e94a59e

Please sign in to comment.