Skip to content

Commit

Permalink
ID Parsers: Network (#14954)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry authored Jan 14, 2022
1 parent ed2a623 commit 5361e9c
Show file tree
Hide file tree
Showing 33 changed files with 284 additions and 410 deletions.
21 changes: 8 additions & 13 deletions internal/services/network/bastion_host_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,21 @@ func resourceBastionHost() *pluginsdk.Resource {

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

log.Println("[INFO] preparing arguments for Azure Bastion Host creation.")

resourceGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
id := parse.NewBastionHostID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
location := azure.NormalizeLocation(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Bastion Host %q (Resource Group %q): %s", name, resourceGroup, err)
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

Expand All @@ -135,21 +135,16 @@ func resourceBastionHostCreateUpdate(d *pluginsdk.ResourceData, meta interface{}
Tags: tags.Expand(t),
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters)
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, parameters)
if err != nil {
return fmt.Errorf("creating/updating Bastion Host %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation/update of Bastion Host %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("waiting for creation/update of %s: %+v", id, err)
}

read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("retrieving Bastion Host %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*read.ID)
d.SetId(id.ID())

return resourceBastionHostRead(d, meta)
}
Expand Down
13 changes: 7 additions & 6 deletions internal/services/network/express_route_circuit_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-05-01/network"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand Down Expand Up @@ -123,21 +124,21 @@ func dataSourceExpressRouteCircuit() *pluginsdk.Resource {

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
id := parse.NewExpressRouteCircuitID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, resourceGroup, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Express Route Circuit %q (Resource Group %q) was not found", name, resourceGroup)
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("making Read request on the Express Route Circuit %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("making Read request on %s: %+v", id, err)
}

d.SetId(*resp.ID)
d.SetId(id.ID())

if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
Expand Down
17 changes: 9 additions & 8 deletions internal/services/network/ip_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand Down Expand Up @@ -44,27 +45,27 @@ func dataSourceIpGroup() *pluginsdk.Resource {

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

resourceGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
id := parse.NewIpGroupID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, resourceGroup, name, "")
resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("IP Group %q (Resource Group %q) was not found", name, resourceGroup)
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("making Read request on IP Group %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("making Read request on %s: %+v", id, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("reading request on IP Group %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("reading request on %s: %+v", id, err)
}
d.SetId(*resp.ID)
d.SetId(id.ID())

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand Down
26 changes: 9 additions & 17 deletions internal/services/network/ip_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ func resourceIpGroup() *pluginsdk.Resource {

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

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
id := parse.NewIpGroupID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, name, "")
existing, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing IP Group %q (Resource Group %q): %s", name, resGroup, err)
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

Expand All @@ -87,32 +87,24 @@ func resourceIpGroupCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) er
ipAddresses := d.Get("cidrs").(*pluginsdk.Set).List()

sg := network.IPGroup{
Name: &name,
Name: &id.Name,
Location: &location,
IPGroupPropertiesFormat: &network.IPGroupPropertiesFormat{
IPAddresses: utils.ExpandStringSlice(ipAddresses),
},
Tags: tags.Expand(t),
}

future, err := client.CreateOrUpdate(ctx, resGroup, name, sg)
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, sg)
if err != nil {
return fmt.Errorf("creating/updating IP Group %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for the completion of IP Group %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("waiting for the completion of %s: %+v", id, err)
}

read, err := client.Get(ctx, resGroup, name, "")
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("cannot read IP Group %q (resource group %q) ID", name, resGroup)
}

d.SetId(*read.ID)
d.SetId(id.ID())

return resourceIpGroupRead(d, meta)
}
Expand Down
15 changes: 7 additions & 8 deletions internal/services/network/local_network_gateway_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"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"
Expand Down Expand Up @@ -80,28 +81,26 @@ func dataSourceLocalNetworkGateway() *pluginsdk.Resource {

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
id := parse.NewLocalNetworkGatewayID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, resourceGroup, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("reading the state of Local Network Gateway %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("reading the state of %s: %+v", id, err)
}

if resp.ID != nil {
d.SetId(*resp.ID)
}
d.SetId(id.ID())

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand Down
18 changes: 8 additions & 10 deletions internal/services/network/nat_gateway_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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"
Expand Down Expand Up @@ -79,26 +80,23 @@ func dataSourceNatGateway() *pluginsdk.Resource {

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

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

resp, err := client.Get(ctx, resourceGroup, name, "")
resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Nat Gateway %q (Resource Group %q) was not found", name, resourceGroup)
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("reading Nat Gateway %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("reading %s: %+v", id, err)
}
if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("Cannot read NAT Gateway %q (Resource Group %q) ID", name, resourceGroup)
}
d.SetId(*resp.ID)
d.SetId(id.ID())

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("resource_group_name", id.ResourceGroup)
if sku := resp.Sku; sku != nil {
d.Set("sku_name", resp.Sku.Name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand Down Expand Up @@ -45,28 +46,25 @@ func dataSourceNetworkDDoSProtectionPlan() *pluginsdk.Resource {

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
id := parse.NewDdosProtectionPlanID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, resourceGroup, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("DDoS Protection Plan %q (Resource Group %q) was not found", name, resourceGroup)
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("retrieving DDoS Protection Plan %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("retrieving DDoS Protection Plan %q (Resource Group %q): `id` was nil", name, resourceGroup)
}
d.SetId(*resp.ID)
d.SetId(id.ID())

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand Down
29 changes: 10 additions & 19 deletions internal/services/network/network_ddos_protection_plan_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ func resourceNetworkDDoSProtectionPlan() *pluginsdk.Resource {

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

log.Printf("[INFO] preparing arguments for DDoS protection plan creation")

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
id := parse.NewDdosProtectionPlanID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing DDoS Protection Plan %q (Resource Group %q): %s", name, resourceGroup, err)
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}

Expand All @@ -93,8 +93,8 @@ func resourceNetworkDDoSProtectionPlanCreateUpdate(d *pluginsdk.ResourceData, me
return fmt.Errorf("extracting names of Virtual Network: %+v", err)
}

locks.ByName(name, azureNetworkDDoSProtectionPlanResourceName)
defer locks.UnlockByName(name, azureNetworkDDoSProtectionPlanResourceName)
locks.ByName(id.Name, azureNetworkDDoSProtectionPlanResourceName)
defer locks.UnlockByName(id.Name, azureNetworkDDoSProtectionPlanResourceName)

locks.MultipleByName(vnetsToLock, VirtualNetworkResourceName)
defer locks.UnlockMultipleByName(vnetsToLock, VirtualNetworkResourceName)
Expand All @@ -104,25 +104,16 @@ func resourceNetworkDDoSProtectionPlanCreateUpdate(d *pluginsdk.ResourceData, me
Tags: tags.Expand(t),
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters)
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, parameters)
if err != nil {
return fmt.Errorf("creating/updating DDoS Protection Plan %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation/update of DDoS Protection Plan %q (Resource Group %q): %+v", name, resourceGroup, err)
}

plan, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("retrieving DDoS Protection Plan %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if plan.ID == nil {
return fmt.Errorf("Cannot read DDoS Protection Plan %q (Resource Group %q) ID", name, resourceGroup)
return fmt.Errorf("waiting for creation/update of %s: %+v", id, err)
}

d.SetId(*plan.ID)
d.SetId(id.ID())

return resourceNetworkDDoSProtectionPlanRead(d, meta)
}
Expand Down
Loading

0 comments on commit 5361e9c

Please sign in to comment.