Skip to content

Commit

Permalink
update service endpoint storage policy to use go-azure-sdk (#26196)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephybun authored Jun 4, 2024
1 parent 1abcf7f commit d052716
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func resourceKubernetesClusterNodePoolCreate(d *pluginsdk.ResourceData, meta int
clustersClient := containersClient.KubernetesClustersClient
poolsClient := containersClient.AgentPoolsClient
subnetClient := meta.(*clients.Client).Network.Client.Subnets
vnetClient := meta.(*clients.Client).Network.VnetClient
vnetClient := meta.(*clients.Client).Network.VirtualNetworks

ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func resourceSubnetNatGatewayAssociation() *pluginsdk.Resource {

func resourceSubnetNatGatewayAssociationCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.Client.Subnets
vnetClient := meta.(*clients.Client).Network.VnetClient
vnetClient := meta.(*clients.Client).Network.VirtualNetworks
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func resourceSubnetNetworkSecurityGroupAssociation() *pluginsdk.Resource {

func resourceSubnetNetworkSecurityGroupAssociationCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.Client.Subnets
vnetClient := meta.(*clients.Client).Network.VnetClient
vnetClient := meta.(*clients.Client).Network.VirtualNetworks
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down
4 changes: 2 additions & 2 deletions internal/services/network/subnet_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func resourceSubnet() *pluginsdk.Resource {
// TODO: refactor the create/flatten functions
func resourceSubnetCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.Client.Subnets
vnetClient := meta.(*clients.Client).Network.VnetClient
vnetClient := meta.(*clients.Client).Network.VirtualNetworks
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand Down Expand Up @@ -447,7 +447,7 @@ func resourceSubnetCreate(d *pluginsdk.ResourceData, meta interface{}) error {

func resourceSubnetUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.Client.Subnets
vnetClient := meta.(*clients.Client).Network.VnetClient
vnetClient := meta.(*clients.Client).Network.VirtualNetworks
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/routetables"
Expand All @@ -17,8 +18,6 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"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 resourceSubnetRouteTableAssociation() *pluginsdk.Resource {
Expand Down Expand Up @@ -58,38 +57,35 @@ func resourceSubnetRouteTableAssociation() *pluginsdk.Resource {

func resourceSubnetRouteTableAssociationCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.Client.Subnets
vnetClient := meta.(*clients.Client).Network.VnetClient
vnetClient := meta.(*clients.Client).Network.VirtualNetworks
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for Subnet <-> Route Table Association creation.")

subnetId := d.Get("subnet_id").(string)
routeTableId := d.Get("route_table_id").(string)

parsedSubnetId, err := commonids.ParseSubnetID(subnetId)
id, err := commonids.ParseSubnetID(d.Get("subnet_id").(string))
if err != nil {
return err
}

parsedRouteTableId, err := routetables.ParseRouteTableID(routeTableId)
routeTableId, err := routetables.ParseRouteTableID(d.Get("route_table_id").(string))
if err != nil {
return err
}

locks.ByName(parsedRouteTableId.RouteTableName, routeTableResourceName)
defer locks.UnlockByName(parsedRouteTableId.RouteTableName, routeTableResourceName)
locks.ByName(routeTableId.RouteTableName, routeTableResourceName)
defer locks.UnlockByName(routeTableId.RouteTableName, routeTableResourceName)

locks.ByName(parsedSubnetId.VirtualNetworkName, VirtualNetworkResourceName)
defer locks.UnlockByName(parsedSubnetId.VirtualNetworkName, VirtualNetworkResourceName)
locks.ByName(id.VirtualNetworkName, VirtualNetworkResourceName)
defer locks.UnlockByName(id.VirtualNetworkName, VirtualNetworkResourceName)

subnet, err := client.Get(ctx, *parsedSubnetId, subnets.DefaultGetOperationOptions())
subnet, err := client.Get(ctx, *id, subnets.DefaultGetOperationOptions())
if err != nil {
if response.WasNotFound(subnet.HttpResponse) {
return fmt.Errorf("Subnet %q (Virtual Network %q / Resource Group %q) was not found!", parsedSubnetId.SubnetName, parsedSubnetId.VirtualNetworkName, parsedSubnetId.ResourceGroupName)
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("retrieving Subnet %q (Virtual Network %q / Resource Group %q): %+v", parsedSubnetId.SubnetName, parsedSubnetId.VirtualNetworkName, parsedSubnetId.ResourceGroupName, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

if model := subnet.Model; model != nil {
Expand All @@ -102,41 +98,41 @@ func resourceSubnetRouteTableAssociationCreate(d *pluginsdk.ResourceData, meta i
}

props.RouteTable = &subnets.RouteTable{
Id: utils.String(routeTableId),
Id: pointer.To(routeTableId.ID()),
}
}
}

if err := client.CreateOrUpdateThenPoll(ctx, *parsedSubnetId, *subnet.Model); err != nil {
return fmt.Errorf("updating Route Table Association for Subnet %q (Virtual Network %q / Resource Group %q): %+v", parsedSubnetId.SubnetName, parsedSubnetId.VirtualNetworkName, parsedSubnetId.ResourceGroupName, err)
if err := client.CreateOrUpdateThenPoll(ctx, *id, *subnet.Model); err != nil {
return fmt.Errorf("updating Route Table Association for %s: %+v", id, err)
}

timeout, _ := ctx.Deadline()

stateConf := &pluginsdk.StateChangeConf{
Pending: []string{string(network.ProvisioningStateUpdating)},
Target: []string{string(network.ProvisioningStateSucceeded)},
Refresh: SubnetProvisioningStateRefreshFunc(ctx, client, *parsedSubnetId),
Pending: []string{string(subnets.ProvisioningStateUpdating)},
Target: []string{string(subnets.ProvisioningStateSucceeded)},
Refresh: SubnetProvisioningStateRefreshFunc(ctx, client, *id),
MinTimeout: 1 * time.Minute,
Timeout: time.Until(timeout),
}
if _, err = stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for provisioning state of subnet for Route Table Association for Subnet %q (Virtual Network %q / Resource Group %q): %+v", parsedSubnetId.SubnetName, parsedSubnetId.VirtualNetworkName, parsedSubnetId.ResourceGroupName, err)
return fmt.Errorf("waiting for provisioning state of Route Table Association for %s: %+v", id, err)
}

vnetId := commonids.NewVirtualNetworkID(parsedSubnetId.SubscriptionId, parsedSubnetId.ResourceGroupName, parsedSubnetId.VirtualNetworkName)
vnetId := commonids.NewVirtualNetworkID(id.SubscriptionId, id.ResourceGroupName, id.VirtualNetworkName)
vnetStateConf := &pluginsdk.StateChangeConf{
Pending: []string{string(network.ProvisioningStateUpdating)},
Target: []string{string(network.ProvisioningStateSucceeded)},
Pending: []string{string(subnets.ProvisioningStateUpdating)},
Target: []string{string(subnets.ProvisioningStateSucceeded)},
Refresh: VirtualNetworkProvisioningStateRefreshFunc(ctx, vnetClient, vnetId),
MinTimeout: 1 * time.Minute,
Timeout: time.Until(timeout),
}
if _, err = vnetStateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for provisioning state of virtual network for Route Table Association for Subnet %q (Virtual Network %q / Resource Group %q): %+v", parsedSubnetId.SubnetName, parsedSubnetId.VirtualNetworkName, parsedSubnetId.ResourceGroupName, err)
return fmt.Errorf("waiting for provisioning state of virtual network for Route Table Association for %s: %+v", id, err)
}

d.SetId(parsedSubnetId.ID())
d.SetId(id.ID())

return resourceSubnetRouteTableAssociationRead(d, meta)
}
Expand All @@ -154,26 +150,26 @@ func resourceSubnetRouteTableAssociationRead(d *pluginsdk.ResourceData, meta int
resp, err := client.Get(ctx, *id, subnets.DefaultGetOperationOptions())
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[DEBUG] Subnet %q (Virtual Network %q / Resource Group %q) could not be found - removing from state!", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName)
log.Printf("[DEBUG] %s could not be found - removing from state!", id)
d.SetId("")
return nil
}
return fmt.Errorf("retrieving Subnet %q (Virtual Network %q / Resource Group %q): %+v", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

model := resp.Model
if model == nil {
return fmt.Errorf("Error: `model` was nil for Subnet %q (Virtual Network %q / Resource Group %q)", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName)
return fmt.Errorf("retrieving %s: `model` was nil", id)
}

props := model.Properties
if props == nil {
return fmt.Errorf("Error: `properties` was nil for Subnet %q (Virtual Network %q / Resource Group %q)", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName)
return fmt.Errorf("retrieving %s: `properties` was nil", id)
}

routeTable := props.RouteTable
if routeTable == nil {
log.Printf("[DEBUG] Subnet %q (Virtual Network %q / Resource Group %q) doesn't have a Route Table - removing from state!", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName)
log.Printf("[DEBUG] %s doesn't have a Route Table - removing from state!", id)
d.SetId("")
return nil
}
Expand All @@ -198,25 +194,25 @@ func resourceSubnetRouteTableAssociationDelete(d *pluginsdk.ResourceData, meta i
read, err := client.Get(ctx, *id, subnets.DefaultGetOperationOptions())
if err != nil {
if response.WasNotFound(read.HttpResponse) {
log.Printf("[DEBUG] Subnet %q (Virtual Network %q / Resource Group %q) could not be found - removing from state!", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName)
log.Printf("[DEBUG] %s could not be found - removing from state!", id)
return nil
}

return fmt.Errorf("retrieving Subnet %q (Virtual Network %q / Resource Group %q): %+v", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

model := read.Model
if model == nil {
return fmt.Errorf("`model` was nil for Subnet %q (Virtual Network %q / Resource Group %q)", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName)
return fmt.Errorf("retrieving %s: `model` was nil", id)
}

props := model.Properties
if props == nil {
return fmt.Errorf("`Properties` was nil for Subnet %q (Virtual Network %q / Resource Group %q)", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName)
return fmt.Errorf("retrieving %s: `properties` was nil", id)
}

if props.RouteTable == nil || props.RouteTable.Id == nil {
log.Printf("[DEBUG] Subnet %q (Virtual Network %q / Resource Group %q) has no Route Table - removing from state!", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName)
log.Printf("[DEBUG] %s has no Route Table - removing from state!", id)
return nil
}

Expand All @@ -236,17 +232,17 @@ func resourceSubnetRouteTableAssociationDelete(d *pluginsdk.ResourceData, meta i
read, err = client.Get(ctx, *id, subnets.DefaultGetOperationOptions())
if err != nil {
if response.WasNotFound(read.HttpResponse) {
log.Printf("[DEBUG] Subnet %q (Virtual Network %q / Resource Group %q) could not be found - removing from state!", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName)
log.Printf("[DEBUG] %s could not be found - removing from state!", id)
return nil
}

return fmt.Errorf("retrieving Subnet %q (Virtual Network %q / Resource Group %q): %+v", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

read.Model.Properties.RouteTable = nil

if err := client.CreateOrUpdateThenPoll(ctx, *id, *read.Model); err != nil {
return fmt.Errorf("removing Route Table Association from Subnet %q (Virtual Network %q / Resource Group %q): %+v", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName, err)
return fmt.Errorf("removing Route Table Association from %s: %+v", id, err)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"testing"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-11-01/subnets"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type SubnetRouteTableAssociationResource struct{}
Expand Down Expand Up @@ -105,42 +105,42 @@ func (SubnetRouteTableAssociationResource) Exists(ctx context.Context, clients *

resp, err := clients.Network.Client.Subnets.Get(ctx, *id, subnets.DefaultGetOperationOptions())
if err != nil {
return nil, fmt.Errorf("reading Subnet Route Table Association (%s): %+v", id, err)
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}

model := resp.Model
if model == nil {
return nil, fmt.Errorf("model was nil for Subnet %q (Virtual Network %q / Resource Group: %q)", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName)
return nil, fmt.Errorf("retrieving %s: `model` was nil", id)
}

props := model.Properties
if props == nil || props.RouteTable == nil {
return nil, fmt.Errorf("properties was nil for Subnet %q (Virtual Network %q / Resource Group: %q)", id.SubnetName, id.VirtualNetworkName, id.ResourceGroupName)
return nil, fmt.Errorf("retrieving %s: `properties` was nil", id)
}

return utils.Bool(props.RouteTable.Id != nil), nil
return pointer.To(props.RouteTable.Id != nil), nil
}

func (SubnetRouteTableAssociationResource) destroy(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) error {
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(15*time.Minute))
defer cancel()

parsedId, err := commonids.ParseSubnetID(state.Attributes["subnet_id"])
id, err := commonids.ParseSubnetID(state.Attributes["subnet_id"])
if err != nil {
return err
}

read, err := client.Network.Client.Subnets.Get(ctx, *parsedId, subnets.DefaultGetOperationOptions())
read, err := client.Network.Client.Subnets.Get(ctx, *id, subnets.DefaultGetOperationOptions())
if err != nil {
if !response.WasNotFound(read.HttpResponse) {
return fmt.Errorf("retrieving Subnet %q (Network %q / Resource Group %q): %+v", parsedId.SubnetName, parsedId.VirtualNetworkName, parsedId.ResourceGroupName, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}
}

read.Model.Properties.RouteTable = nil

if err := client.Network.Client.Subnets.CreateOrUpdateThenPoll(ctx, *parsedId, *read.Model); err != nil {
return fmt.Errorf("updating Subnet %q (Network %q / Resource Group %q): %+v", parsedId.SubnetName, parsedId.VirtualNetworkName, parsedId.ResourceGroupName, err)
if err := client.Network.Client.Subnets.CreateOrUpdateThenPoll(ctx, *id, *read.Model); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

return nil
Expand Down
Loading

0 comments on commit d052716

Please sign in to comment.