From 5361e9cc0772dd69929a47c21575b5be0d832830 Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Thu, 13 Jan 2022 17:27:26 -0800 Subject: [PATCH] ID Parsers: Network (#14954) --- .../services/network/bastion_host_resource.go | 21 +++---- .../express_route_circuit_data_source.go | 13 +++-- .../services/network/ip_group_data_source.go | 17 +++--- .../services/network/ip_group_resource.go | 26 +++------ .../local_network_gateway_data_source.go | 15 +++-- .../network/nat_gateway_data_source.go | 18 +++--- ...etwork_ddos_protection_plan_data_source.go | 18 +++--- .../network_ddos_protection_plan_resource.go | 29 ++++------ .../network_security_group_data_source.go | 17 +++--- .../network_security_group_resource.go | 30 ++++------ .../network/network_security_rule_resource.go | 31 ++++------ .../network/network_watcher_data_source.go | 17 +++--- .../private_link_service_data_source.go | 21 +++---- .../services/network/public_ip_data_source.go | 13 +++-- .../network/public_ip_prefix_data_source.go | 13 +++-- .../network/route_filter_data_source.go | 17 +++--- .../services/network/route_filter_resource.go | 27 +++------ .../network/route_table_data_source.go | 17 +++--- .../services/network/route_table_resource.go | 27 +++------ ...subnet_route_table_association_resource.go | 7 +-- .../virtual_hub_bgp_connection_resource.go | 29 ++++------ .../network/virtual_hub_data_source.go | 14 ++--- .../network/virtual_hub_ip_resource.go | 31 ++++------ .../services/network/virtual_hub_resource.go | 8 +-- .../virtual_hub_route_table_resource.go | 29 ++++------ ..._hub_security_partner_provider_resource.go | 25 +++----- ..._network_gateway_connection_data_source.go | 16 +++--- ...ual_network_gateway_connection_resource.go | 57 +++++++------------ .../virtual_network_gateway_data_source.go | 15 ++--- .../virtual_network_peering_resource.go | 23 +++----- .../network/virtual_wan_data_source.go | 18 +++--- .../services/network/vpn_gateway_resource.go | 22 +++---- ...application_firewall_policy_data_source.go | 13 +++-- 33 files changed, 284 insertions(+), 410 deletions(-) diff --git a/internal/services/network/bastion_host_resource.go b/internal/services/network/bastion_host_resource.go index f95747a37211..6d4c9752abda 100644 --- a/internal/services/network/bastion_host_resource.go +++ b/internal/services/network/bastion_host_resource.go @@ -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) } } @@ -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) } diff --git a/internal/services/network/express_route_circuit_data_source.go b/internal/services/network/express_route_circuit_data_source.go index 5baca7de3093..b565aff07537 100644 --- a/internal/services/network/express_route_circuit_data_source.go +++ b/internal/services/network/express_route_circuit_data_source.go @@ -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" @@ -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)) diff --git a/internal/services/network/ip_group_data_source.go b/internal/services/network/ip_group_data_source.go index 0421bdd3bbc1..6360b039c4d2 100644 --- a/internal/services/network/ip_group_data_source.go +++ b/internal/services/network/ip_group_data_source.go @@ -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" @@ -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)) } diff --git a/internal/services/network/ip_group_resource.go b/internal/services/network/ip_group_resource.go index e6d23af29782..f87ec9a8adba 100644 --- a/internal/services/network/ip_group_resource.go +++ b/internal/services/network/ip_group_resource.go @@ -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) } } @@ -87,7 +87,7 @@ 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), @@ -95,24 +95,16 @@ func resourceIpGroupCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) er 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) } diff --git a/internal/services/network/local_network_gateway_data_source.go b/internal/services/network/local_network_gateway_data_source.go index 12461ca5c42c..0d02044fbae6 100644 --- a/internal/services/network/local_network_gateway_data_source.go +++ b/internal/services/network/local_network_gateway_data_source.go @@ -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" @@ -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)) } diff --git a/internal/services/network/nat_gateway_data_source.go b/internal/services/network/nat_gateway_data_source.go index f4cccc59b9ab..4a1c727b681c 100644 --- a/internal/services/network/nat_gateway_data_source.go +++ b/internal/services/network/nat_gateway_data_source.go @@ -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" @@ -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) } diff --git a/internal/services/network/network_ddos_protection_plan_data_source.go b/internal/services/network/network_ddos_protection_plan_data_source.go index ec420114698e..eb1df65919e2 100644 --- a/internal/services/network/network_ddos_protection_plan_data_source.go +++ b/internal/services/network/network_ddos_protection_plan_data_source.go @@ -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" @@ -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)) } diff --git a/internal/services/network/network_ddos_protection_plan_resource.go b/internal/services/network/network_ddos_protection_plan_resource.go index 9da5ea9466d6..82c89c34b357 100644 --- a/internal/services/network/network_ddos_protection_plan_resource.go +++ b/internal/services/network/network_ddos_protection_plan_resource.go @@ -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) } } @@ -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) @@ -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) } diff --git a/internal/services/network/network_security_group_data_source.go b/internal/services/network/network_security_group_data_source.go index 4b37b96c1426..f41e29362ad7 100644 --- a/internal/services/network/network_security_group_data_source.go +++ b/internal/services/network/network_security_group_data_source.go @@ -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" @@ -137,27 +138,27 @@ func dataSourceNetworkSecurityGroup() *pluginsdk.Resource { func dataSourceNetworkSecurityGroupRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.SecurityGroupClient + 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.NewNetworkSecurityGroupID(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: Network Security 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 Network Security 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 Network Security 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)) } diff --git a/internal/services/network/network_security_group_resource.go b/internal/services/network/network_security_group_resource.go index b89f06a82168..552f0af5a551 100644 --- a/internal/services/network/network_security_group_resource.go +++ b/internal/services/network/network_security_group_resource.go @@ -182,17 +182,17 @@ func resourceNetworkSecurityGroup() *pluginsdk.Resource { func resourceNetworkSecurityGroupCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.SecurityGroupClient + 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.NewNetworkSecurityGroupID(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 Network Security Group %q (Resource Group %q): %s", name, resGroup, err) + return fmt.Errorf("checking for presence of existing %s: %s", id, err) } } @@ -209,11 +209,11 @@ func resourceNetworkSecurityGroupCreateUpdate(d *pluginsdk.ResourceData, meta in return fmt.Errorf("Building list of Network Security Group Rules: %+v", sgErr) } - locks.ByName(name, networkSecurityGroupResourceName) - defer locks.UnlockByName(name, networkSecurityGroupResourceName) + locks.ByName(id.Name, networkSecurityGroupResourceName) + defer locks.UnlockByName(id.Name, networkSecurityGroupResourceName) sg := network.SecurityGroup{ - Name: &name, + Name: &id.Name, Location: &location, SecurityGroupPropertiesFormat: &network.SecurityGroupPropertiesFormat{ SecurityRules: &sgRules, @@ -221,24 +221,16 @@ func resourceNetworkSecurityGroupCreateUpdate(d *pluginsdk.ResourceData, meta in 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 NSG %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 NSG %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 NSG %q (resource group %q) ID", name, resGroup) - } - - d.SetId(*read.ID) + d.SetId(id.ID()) return resourceNetworkSecurityGroupRead(d, meta) } diff --git a/internal/services/network/network_security_rule_resource.go b/internal/services/network/network_security_rule_resource.go index b1c13444ce7b..26a23cbb449e 100644 --- a/internal/services/network/network_security_rule_resource.go +++ b/internal/services/network/network_security_rule_resource.go @@ -175,18 +175,17 @@ func resourceNetworkSecurityRule() *pluginsdk.Resource { func resourceNetworkSecurityRuleCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.SecurityRuleClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - nsgName := d.Get("network_security_group_name").(string) - resGroup := d.Get("resource_group_name").(string) + id := parse.NewSecurityRuleID(subscriptionId, d.Get("resource_group_name").(string), d.Get("network_security_group_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resGroup, nsgName, name) + existing, err := client.Get(ctx, id.ResourceGroup, id.NetworkSecurityGroupName, id.Name) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Rule %q (Network Security Group %q / Resource Group %q): %s", name, nsgName, resGroup, err) + return fmt.Errorf("checking for presence of existing %s: %s", id, err) } } @@ -205,12 +204,12 @@ func resourceNetworkSecurityRuleCreateUpdate(d *pluginsdk.ResourceData, meta int protocol := d.Get("protocol").(string) if !meta.(*clients.Client).Features.Network.RelaxedLocking { - locks.ByName(nsgName, networkSecurityGroupResourceName) - defer locks.UnlockByName(nsgName, networkSecurityGroupResourceName) + locks.ByName(id.NetworkSecurityGroupName, networkSecurityGroupResourceName) + defer locks.UnlockByName(id.NetworkSecurityGroupName, networkSecurityGroupResourceName) } rule := network.SecurityRule{ - Name: &name, + Name: &id.Name, SecurityRulePropertiesFormat: &network.SecurityRulePropertiesFormat{ SourcePortRange: &sourcePortRange, DestinationPortRange: &destinationPortRange, @@ -290,24 +289,16 @@ func resourceNetworkSecurityRuleCreateUpdate(d *pluginsdk.ResourceData, meta int rule.DestinationApplicationSecurityGroups = &destinationApplicationSecurityGroups } - future, err := client.CreateOrUpdate(ctx, resGroup, nsgName, name, rule) + future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.NetworkSecurityGroupName, id.Name, rule) if err != nil { - return fmt.Errorf("Creating/Updating Network Security Rule %q (NSG %q / Resource Group %q): %+v", name, nsgName, 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 completion of Network Security Rule %q (NSG %q / Resource Group %q): %+v", name, nsgName, resGroup, err) - } - - read, err := client.Get(ctx, resGroup, nsgName, name) - if err != nil { - return fmt.Errorf("making Read request on Network Security Rule %q (NSG %q / Resource Group %q): %+v", name, nsgName, resGroup, err) - } - if read.ID == nil { - return fmt.Errorf("Cannot read Network Security Rule %s (NSG %q / resource group %s) ID", name, nsgName, resGroup) + return fmt.Errorf("waiting for completion of %s: %+v", id, err) } - d.SetId(*read.ID) + d.SetId(id.ID()) return resourceNetworkSecurityRuleRead(d, meta) } diff --git a/internal/services/network/network_watcher_data_source.go b/internal/services/network/network_watcher_data_source.go index 6b8b84b9fcdc..c1c77cb5f51c 100644 --- a/internal/services/network/network_watcher_data_source.go +++ b/internal/services/network/network_watcher_data_source.go @@ -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/tf/validation" @@ -37,24 +38,24 @@ func dataSourceNetworkWatcher() *pluginsdk.Resource { func dataSourceNetworkWatcherRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.WatcherClient + 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.NewNetworkWatcherID(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: Network Watcher %q (Resource Group %q) was not found", name, resourceGroup) + return fmt.Errorf("%swas not found", id) } - return fmt.Errorf("making Read request on Network Watcher %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()) - d.Set("name", name) - d.Set("resource_group_name", resourceGroup) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) if location := resp.Location; location != nil { d.Set("location", azure.NormalizeLocation(*location)) } diff --git a/internal/services/network/private_link_service_data_source.go b/internal/services/network/private_link_service_data_source.go index 606199619445..c5cc1dcf3a88 100644 --- a/internal/services/network/private_link_service_data_source.go +++ b/internal/services/network/private_link_service_data_source.go @@ -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/services/network/validate" "github.com/hashicorp/terraform-provider-azurerm/internal/tags" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" @@ -97,25 +98,22 @@ func dataSourcePrivateLinkService() *pluginsdk.Resource { func dataSourcePrivateLinkServiceRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.PrivateLinkServiceClient + 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.NewPrivateLinkServiceID(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: Private Link Service %q (Resource Group %q) was not found", name, resourceGroup) + return fmt.Errorf("%s was not found", id) } - return fmt.Errorf("reading Private Link Service %q (Resource Group %q): %+v", name, resourceGroup, err) - } - if resp.ID == nil { - return fmt.Errorf("Cannot read ID for Private Link Service %q (Resource Group %q)", name, resourceGroup) + return fmt.Errorf("reading %s: %+v", id, err) } d.Set("name", resp.Name) - d.Set("resource_group_name", resourceGroup) + d.Set("resource_group_name", id.ResourceGroup) d.Set("location", azure.NormalizeLocation(*resp.Location)) if props := resp.PrivateLinkServiceProperties; props != nil { @@ -145,10 +143,7 @@ func dataSourcePrivateLinkServiceRead(d *pluginsdk.ResourceData, meta interface{ } } - if resp.ID == nil || *resp.ID == "" { - return fmt.Errorf("API returns a nil/empty id on Private Link Service %q (Resource Group %q): %+v", name, resourceGroup, err) - } - d.SetId(*resp.ID) + d.SetId(id.ID()) return tags.FlattenAndSet(d, resp.Tags) } diff --git a/internal/services/network/public_ip_data_source.go b/internal/services/network/public_ip_data_source.go index 07bd2b654a7e..074ca7049693 100644 --- a/internal/services/network/public_ip_data_source.go +++ b/internal/services/network/public_ip_data_source.go @@ -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/tf/validation" @@ -89,21 +90,21 @@ func dataSourcePublicIP() *pluginsdk.Resource { func dataSourcePublicIPRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.PublicIPsClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - resGroup := d.Get("resource_group_name").(string) - name := d.Get("name").(string) + id := parse.NewPublicIpAddressID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) - resp, err := client.Get(ctx, resGroup, name, "") + resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "") if err != nil { if utils.ResponseWasNotFound(resp.Response) { - return fmt.Errorf("Error: Public IP %q (Resource Group %q) was not found", name, resGroup) + return fmt.Errorf("%s was not found", id) } - return fmt.Errorf("making Read request on Azure public ip %s: %s", name, err) + return fmt.Errorf("making Read request on %s: %s", id, err) } - d.SetId(*resp.ID) + d.SetId(id.ID()) d.Set("zones", resp.Zones) diff --git a/internal/services/network/public_ip_prefix_data_source.go b/internal/services/network/public_ip_prefix_data_source.go index 3138a4b2a35c..9db85a175714 100644 --- a/internal/services/network/public_ip_prefix_data_source.go +++ b/internal/services/network/public_ip_prefix_data_source.go @@ -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" @@ -54,20 +55,20 @@ func dataSourcePublicIpPrefix() *pluginsdk.Resource { func dataSourcePublicIpPrefixRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.PublicIPPrefixesClient + 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) - resp, err := client.Get(ctx, resourceGroup, name, "") + id := parse.NewPublicIpPrefixID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) + resp, err := client.Get(ctx, id.ResourceGroup, id.PublicIPPrefixeName, "") if err != nil { if utils.ResponseWasNotFound(resp.Response) { - return fmt.Errorf("Error: Public IP prefix %q was not found in Resource Group %q", name, resourceGroup) + return fmt.Errorf("%s was not found", id) } - return fmt.Errorf("retrieving Public IP Prefix %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("retrieving %s: %+v", id, err) } - d.SetId(*resp.ID) + d.SetId(id.ID()) d.Set("zones", resp.Zones) if location := resp.Location; location != nil { d.Set("location", azure.NormalizeLocation(*location)) diff --git a/internal/services/network/route_filter_data_source.go b/internal/services/network/route_filter_data_source.go index 8d3d8ddb0c71..faa3317c8224 100644 --- a/internal/services/network/route_filter_data_source.go +++ b/internal/services/network/route_filter_data_source.go @@ -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/tags" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" @@ -71,24 +72,24 @@ func dataSourceRouteFilter() *pluginsdk.Resource { func dataSourceRouteFilterRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.RouteFiltersClient + 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.NewRouteFilterID(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("Route Filter %q (Resource Group %q) was not found", name, resourceGroup) + return fmt.Errorf("%s was not found", id) } - return fmt.Errorf("making Read request on Azure Route Filter %q: %+v", name, err) + return fmt.Errorf("making Read request on %s: %+v", id, err) } - d.SetId(*resp.ID) + d.SetId(id.ID()) - d.Set("name", name) - d.Set("resource_group_name", resourceGroup) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) if location := resp.Location; location != nil { d.Set("location", azure.NormalizeLocation(*location)) } diff --git a/internal/services/network/route_filter_resource.go b/internal/services/network/route_filter_resource.go index cd6cd7e4ac4f..5c3ca64f031a 100644 --- a/internal/services/network/route_filter_resource.go +++ b/internal/services/network/route_filter_resource.go @@ -98,21 +98,21 @@ func resourceRouteFilter() *pluginsdk.Resource { func resourceRouteFilterCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.RouteFiltersClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() log.Printf("[INFO] preparing arguments for Route Filter create/update.") - name := d.Get("name").(string) + id := parse.NewRouteFilterID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) location := azure.NormalizeLocation(d.Get("location").(string)) - resourceGroup := d.Get("resource_group_name").(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 Route Filter %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("checking for presence of existing %s: %+v", id, err) } } @@ -122,7 +122,7 @@ func resourceRouteFilterCreateUpdate(d *pluginsdk.ResourceData, meta interface{} } routeSet := network.RouteFilter{ - Name: &name, + Name: &id.Name, Location: &location, RouteFilterPropertiesFormat: &network.RouteFilterPropertiesFormat{ Rules: expandRouteFilterRules(d), @@ -130,25 +130,16 @@ func resourceRouteFilterCreateUpdate(d *pluginsdk.ResourceData, meta interface{} Tags: tags.Expand(t), } - future, err := client.CreateOrUpdate(ctx, resourceGroup, name, routeSet) + future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, routeSet) if err != nil { - return fmt.Errorf("creating/updating Route Filter %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 completion of Route Filter %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("waiting for completion of %s: %+v", id, err) } - read, err := client.Get(ctx, resourceGroup, name, "") - if err != nil { - return fmt.Errorf("retrieving Route Filter %q (Resource Group %q): %+v", name, resourceGroup, err) - } - - if read.ID == nil { - return fmt.Errorf("ID was nil for Route Filter %q (Resource Group %q)", name, resourceGroup) - } - - d.SetId(*read.ID) + d.SetId(id.ID()) return resourceRouteFilterRead(d, meta) } diff --git a/internal/services/network/route_table_data_source.go b/internal/services/network/route_table_data_source.go index 8f45f1781637..9eb5cd57fdea 100644 --- a/internal/services/network/route_table_data_source.go +++ b/internal/services/network/route_table_data_source.go @@ -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/tags" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" @@ -75,24 +76,24 @@ func dataSourceRouteTable() *pluginsdk.Resource { func dataSourceRouteTableRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.RouteTablesClient + 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.NewRouteTableID(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: Route Table %q (Resource Group %q) was not found", name, resourceGroup) + return fmt.Errorf("%s was not found", id) } - return fmt.Errorf("making Read request on Azure Route Table %q: %+v", name, err) + return fmt.Errorf("making Read request on %s: %+v", id, err) } - d.SetId(*resp.ID) + d.SetId(id.ID()) - d.Set("name", name) - d.Set("resource_group_name", resourceGroup) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) if location := resp.Location; location != nil { d.Set("location", azure.NormalizeLocation(*location)) } diff --git a/internal/services/network/route_table_resource.go b/internal/services/network/route_table_resource.go index 9278717dd443..353485b82cd4 100644 --- a/internal/services/network/route_table_resource.go +++ b/internal/services/network/route_table_resource.go @@ -114,21 +114,21 @@ func resourceRouteTable() *pluginsdk.Resource { func resourceRouteTableCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.RouteTablesClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() log.Printf("[INFO] preparing arguments for AzureRM Route Table creation.") - name := d.Get("name").(string) + id := parse.NewRouteTableID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) location := azure.NormalizeLocation(d.Get("location").(string)) - resourceGroup := d.Get("resource_group_name").(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 Route Table %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("checking for presence of existing %s: %+v", id, err) } } @@ -138,7 +138,7 @@ func resourceRouteTableCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) } routeSet := network.RouteTable{ - Name: &name, + Name: &id.Name, Location: &location, RouteTablePropertiesFormat: &network.RouteTablePropertiesFormat{ Routes: expandRouteTableRoutes(d), @@ -147,25 +147,16 @@ func resourceRouteTableCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) Tags: tags.Expand(t), } - future, err := client.CreateOrUpdate(ctx, resourceGroup, name, routeSet) + future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, routeSet) if err != nil { - return fmt.Errorf("Creating/Updating Route Table %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 completion of Route Table %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("waiting for completion of %s: %+v", id, err) } - read, err := client.Get(ctx, resourceGroup, name, "") - if err != nil { - return fmt.Errorf("retrieving Route Table %q (Resource Group %q): %+v", name, resourceGroup, err) - } - - if read.ID == nil { - return fmt.Errorf("ID was nil for Route Table %q (Resource Group %q)", name, resourceGroup) - } - - d.SetId(*read.ID) + d.SetId(id.ID()) return resourceRouteTableRead(d, meta) } diff --git a/internal/services/network/subnet_route_table_association_resource.go b/internal/services/network/subnet_route_table_association_resource.go index 2afe1d84681a..c31d00b9366d 100644 --- a/internal/services/network/subnet_route_table_association_resource.go +++ b/internal/services/network/subnet_route_table_association_resource.go @@ -139,12 +139,7 @@ func resourceSubnetRouteTableAssociationCreate(d *pluginsdk.ResourceData, meta i return fmt.Errorf("waiting for provisioning state of virtual network for Route Table Association for Subnet %q (Virtual Network %q / Resource Group %q): %+v", subnetName, virtualNetworkName, resourceGroup, err) } - read, err := client.Get(ctx, resourceGroup, virtualNetworkName, subnetName, "") - if err != nil { - return fmt.Errorf("retrieving Subnet %q (Virtual Network %q / Resource Group %q): %+v", subnetName, virtualNetworkName, resourceGroup, err) - } - - d.SetId(*read.ID) + d.SetId(parsedSubnetId.ID()) return resourceSubnetRouteTableAssociationRead(d, meta) } diff --git a/internal/services/network/virtual_hub_bgp_connection_resource.go b/internal/services/network/virtual_hub_bgp_connection_resource.go index a619022ed5e1..d5f219dfda22 100644 --- a/internal/services/network/virtual_hub_bgp_connection_resource.go +++ b/internal/services/network/virtual_hub_bgp_connection_resource.go @@ -71,21 +71,21 @@ func resourceVirtualHubBgpConnectionCreate(d *pluginsdk.ResourceData, meta inter ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := parse.VirtualHubID(d.Get("virtual_hub_id").(string)) + virtHubId, err := parse.VirtualHubID(d.Get("virtual_hub_id").(string)) if err != nil { return err } - locks.ByName(id.Name, virtualHubResourceName) - defer locks.UnlockByName(id.Name, virtualHubResourceName) + locks.ByName(virtHubId.Name, virtualHubResourceName) + defer locks.UnlockByName(virtHubId.Name, virtualHubResourceName) - name := d.Get("name").(string) + id := parse.NewBgpConnectionID(virtHubId.SubscriptionId, virtHubId.ResourceGroup, virtHubId.Name, d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, id.ResourceGroup, id.Name, name) + existing, err := client.Get(ctx, id.ResourceGroup, id.VirtualHubName, id.Name) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for present of existing Virtual Hub Bgp Connection %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) + return fmt.Errorf("checking for presence of existing %s: %+v", id, err) } } @@ -102,25 +102,16 @@ func resourceVirtualHubBgpConnectionCreate(d *pluginsdk.ResourceData, meta inter }, } - future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, name, parameters) + future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.VirtualHubName, id.Name, parameters) if err != nil { - return fmt.Errorf("creating/updating Virtual Hub Bgp Connection %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) + return fmt.Errorf("creating/updating %s: %+v", id, err) } if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("waiting on creating/updating future for Virtual Hub Bgp Connection %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) + return fmt.Errorf("waiting on creating/updating future for %s: %+v", id, err) } - resp, err := client.Get(ctx, id.ResourceGroup, id.Name, name) - if err != nil { - return fmt.Errorf("retrieving Virtual Hub Bgp Connection %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) - } - - if resp.ID == nil || *resp.ID == "" { - return fmt.Errorf("empty or nil ID returned for Virtual Hub Bgp Connection %q (Resource Group %q / Virtual Hub %q) ID", name, id.ResourceGroup, id.Name) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceVirtualHubBgpConnectionRead(d, meta) } diff --git a/internal/services/network/virtual_hub_data_source.go b/internal/services/network/virtual_hub_data_source.go index 68604ead6fc5..7b9f4c0baf8b 100644 --- a/internal/services/network/virtual_hub_data_source.go +++ b/internal/services/network/virtual_hub_data_source.go @@ -55,24 +55,24 @@ func dataSourceVirtualHub() *pluginsdk.Resource { func dataSourceVirtualHubRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.VirtualHubClient + 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.NewVirtualHubID(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: Virtual Hub %q (Resource Group %q) was not found", name, resourceGroup) + return fmt.Errorf("%s was not found", id) } - return fmt.Errorf("reading Virtual Hub %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("reading %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)) } diff --git a/internal/services/network/virtual_hub_ip_resource.go b/internal/services/network/virtual_hub_ip_resource.go index 89538fd90807..13d5326ff7af 100644 --- a/internal/services/network/virtual_hub_ip_resource.go +++ b/internal/services/network/virtual_hub_ip_resource.go @@ -89,21 +89,21 @@ func resourceVirtualHubIPCreateUpdate(d *pluginsdk.ResourceData, meta interface{ ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := parse.VirtualHubID(d.Get("virtual_hub_id").(string)) + virtHubId, err := parse.VirtualHubID(d.Get("virtual_hub_id").(string)) if err != nil { return err } - locks.ByName(id.Name, virtualHubResourceName) - defer locks.UnlockByName(id.Name, virtualHubResourceName) + locks.ByName(virtHubId.Name, virtualHubResourceName) + defer locks.UnlockByName(virtHubId.Name, virtualHubResourceName) - name := d.Get("name").(string) + id := parse.NewVirtualHubIpConfigurationID(virtHubId.SubscriptionId, virtHubId.ResourceGroup, virtHubId.Name, d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, id.ResourceGroup, id.Name, name) + existing, err := client.Get(ctx, id.ResourceGroup, id.VirtualHubName, id.IpConfigurationName) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for present of existing Virtual Hub IP %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) + return fmt.Errorf("checking for presence of %s: %+v", id, err) } } @@ -117,7 +117,7 @@ func resourceVirtualHubIPCreateUpdate(d *pluginsdk.ResourceData, meta interface{ } parameters := network.HubIPConfiguration{ - Name: utils.String(d.Get("name").(string)), + Name: utils.String(id.IpConfigurationName), HubIPConfigurationPropertiesFormat: &network.HubIPConfigurationPropertiesFormat{ Subnet: &network.Subnet{ ID: utils.String(d.Get("subnet_id").(string)), @@ -139,25 +139,16 @@ func resourceVirtualHubIPCreateUpdate(d *pluginsdk.ResourceData, meta interface{ } } - future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, name, parameters) + future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.VirtualHubName, id.IpConfigurationName, parameters) if err != nil { - return fmt.Errorf("creating/updating Virtual Hub IP %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) + return fmt.Errorf("creating/updating %s: %+v", id, err) } if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("waiting on creating/updating future for Virtual Hub IP %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) + return fmt.Errorf("waiting on creating/updating future for %s: %+v", id, err) } - resp, err := client.Get(ctx, id.ResourceGroup, id.Name, name) - if err != nil { - return fmt.Errorf("retrieving Virtual Hub IP %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) - } - - if resp.ID == nil || *resp.ID == "" { - return fmt.Errorf("empty or nil ID returned for Virtual Hub IP %q (Resource Group %q / Virtual Hub %q) ID", name, id.ResourceGroup, id.Name) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceVirtualHubIPRead(d, meta) } diff --git a/internal/services/network/virtual_hub_resource.go b/internal/services/network/virtual_hub_resource.go index 8e6ecf121610..4192dec909a7 100644 --- a/internal/services/network/virtual_hub_resource.go +++ b/internal/services/network/virtual_hub_resource.go @@ -186,16 +186,12 @@ func resourceVirtualHubCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) ContinuousTargetOccurence: 3, Timeout: time.Until(timeout), } - respRaw, err := stateConf.WaitForStateContext(ctx) + _, err = stateConf.WaitForStateContext(ctx) if err != nil { return fmt.Errorf("waiting for %s provisioning route: %+v", id, err) } - resp := respRaw.(network.VirtualHub) - if resp.ID == nil { - return fmt.Errorf("cannot read %s ID", id) - } - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceVirtualHubRead(d, meta) } diff --git a/internal/services/network/virtual_hub_route_table_resource.go b/internal/services/network/virtual_hub_route_table_resource.go index ec3b42000398..7706c8ef89fd 100644 --- a/internal/services/network/virtual_hub_route_table_resource.go +++ b/internal/services/network/virtual_hub_route_table_resource.go @@ -117,21 +117,21 @@ func resourceVirtualHubRouteTableCreateUpdate(d *pluginsdk.ResourceData, meta in ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := parse.VirtualHubID(d.Get("virtual_hub_id").(string)) + virtHubId, err := parse.VirtualHubID(d.Get("virtual_hub_id").(string)) if err != nil { return err } - locks.ByName(id.Name, virtualHubResourceName) - defer locks.UnlockByName(id.Name, virtualHubResourceName) + locks.ByName(virtHubId.Name, virtualHubResourceName) + defer locks.UnlockByName(virtHubId.Name, virtualHubResourceName) - name := d.Get("name").(string) + id := parse.NewHubRouteTableID(virtHubId.SubscriptionId, virtHubId.ResourceGroup, virtHubId.Name, d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, id.ResourceGroup, id.Name, name) + existing, err := client.Get(ctx, id.ResourceGroup, id.VirtualHubName, id.Name) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for present of existing HubRouteTable %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) + return fmt.Errorf("checking for presence of %s: %+v", id, err) } } @@ -148,25 +148,16 @@ func resourceVirtualHubRouteTableCreateUpdate(d *pluginsdk.ResourceData, meta in }, } - future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, name, parameters) + future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.VirtualHubName, id.Name, parameters) if err != nil { - return fmt.Errorf("creating/updating HubRouteTable %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) + return fmt.Errorf("creating/updating %s: %+v", id, err) } if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("waiting on creating/updating future for HubRouteTable %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) + return fmt.Errorf("waiting on creating/updating future for %s: %+v", id, err) } - resp, err := client.Get(ctx, id.ResourceGroup, id.Name, name) - if err != nil { - return fmt.Errorf("retrieving HubRouteTable %q (Resource Group %q / Virtual Hub %q): %+v", name, id.ResourceGroup, id.Name, err) - } - - if resp.ID == nil || *resp.ID == "" { - return fmt.Errorf("empty or nil ID returned for HubRouteTable %q (Resource Group %q / Virtual Hub %q) ID", name, id.ResourceGroup, id.Name) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceVirtualHubRouteTableRead(d, meta) } diff --git a/internal/services/network/virtual_hub_security_partner_provider_resource.go b/internal/services/network/virtual_hub_security_partner_provider_resource.go index 562de3c2ad00..4be4d396b5a5 100644 --- a/internal/services/network/virtual_hub_security_partner_provider_resource.go +++ b/internal/services/network/virtual_hub_security_partner_provider_resource.go @@ -74,16 +74,16 @@ func resourceVirtualHubSecurityPartnerProvider() *pluginsdk.Resource { func resourceVirtualHubSecurityPartnerProviderCreate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.SecurityPartnerProviderClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewSecurityPartnerProviderID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) - 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 present of existing Security Partner Provider %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("checking for present of existing %s: %+v", id, err) } } @@ -105,25 +105,16 @@ func resourceVirtualHubSecurityPartnerProviderCreate(d *pluginsdk.ResourceData, } } - 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 Security Partner Provider %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("creating %s: %+v", id, err) } if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("waiting on creating future for Security Partner Provider %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("waiting on creating future for %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, name) - if err != nil { - return fmt.Errorf("retrieving Security Partner Provider %q (Resource Group %q): %+v", name, resourceGroup, err) - } - - if resp.ID == nil || *resp.ID == "" { - return fmt.Errorf("empty or nil ID returned for Security Partner Provider %q (Resource Group %q) ID", name, resourceGroup) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceVirtualHubSecurityPartnerProviderRead(d, meta) } diff --git a/internal/services/network/virtual_network_gateway_connection_data_source.go b/internal/services/network/virtual_network_gateway_connection_data_source.go index d1d5b935e69a..624d2289e896 100644 --- a/internal/services/network/virtual_network_gateway_connection_data_source.go +++ b/internal/services/network/virtual_network_gateway_connection_data_source.go @@ -4,6 +4,8 @@ import ( "fmt" "time" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse" + "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" @@ -189,25 +191,25 @@ func dataSourceVirtualNetworkGatewayConnection() *pluginsdk.Resource { func dataSourceVirtualNetworkGatewayConnectionRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.VnetGatewayConnectionsClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - resGroup := d.Get("resource_group_name").(string) + id := parse.NewNetworkGatewayConnectionID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) - resp, err := client.Get(ctx, resGroup, name) + resp, err := client.Get(ctx, id.ResourceGroup, id.ConnectionName) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - return fmt.Errorf("Virtual Network Gateway Connection %q (Resource Group %q) was not found", name, resGroup) + return fmt.Errorf("%s was not found", id) } - return fmt.Errorf("making Read request on AzureRM Virtual Network Gateway Connection %q (Resource Group %q): %+v", name, resGroup, err) + return fmt.Errorf("making Read request on %s: %+v", id, err) } - d.SetId(*resp.ID) + d.SetId(id.ID()) d.Set("name", resp.Name) - d.Set("resource_group_name", resGroup) + d.Set("resource_group_name", id.ResourceGroup) if location := resp.Location; location != nil { d.Set("location", azure.NormalizeLocation(*location)) } diff --git a/internal/services/network/virtual_network_gateway_connection_resource.go b/internal/services/network/virtual_network_gateway_connection_resource.go index 1e8cf7398c52..6aab5f64327c 100644 --- a/internal/services/network/virtual_network_gateway_connection_resource.go +++ b/internal/services/network/virtual_network_gateway_connection_resource.go @@ -308,19 +308,19 @@ func resourceVirtualNetworkGatewayConnection() *pluginsdk.Resource { func resourceVirtualNetworkGatewayConnectionCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.VnetGatewayConnectionsClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() log.Printf("[INFO] preparing arguments for AzureRM Virtual Network Gateway Connection creation.") - name := d.Get("name").(string) - resGroup := d.Get("resource_group_name").(string) + id := parse.NewNetworkGatewayConnectionID(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.ConnectionName) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Virtual Network Gateway Connection %q (Resource Group %q): %s", name, resGroup, err) + return fmt.Errorf("checking for presence of existing %s: %s", id, err) } } @@ -338,42 +338,34 @@ func resourceVirtualNetworkGatewayConnectionCreateUpdate(d *pluginsdk.ResourceDa } connection := network.VirtualNetworkGatewayConnection{ - Name: &name, + Name: &id.ConnectionName, Location: &location, Tags: tags.Expand(t), VirtualNetworkGatewayConnectionPropertiesFormat: properties, } - future, err := client.CreateOrUpdate(ctx, resGroup, name, connection) + future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ConnectionName, connection) if err != nil { - return fmt.Errorf("Creating/Updating AzureRM Virtual Network Gateway Connection %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 completion of Virtual Network Gateway Connection %q (Resource Group %q): %+v", name, resGroup, err) + return fmt.Errorf("waiting for completion of %s: %+v", id, err) } if properties.SharedKey != nil && !d.IsNewResource() { - future, err := client.SetSharedKey(ctx, resGroup, name, network.ConnectionSharedKey{ + future, err := client.SetSharedKey(ctx, id.ResourceGroup, id.ConnectionName, network.ConnectionSharedKey{ Value: properties.SharedKey, }) if err != nil { - return fmt.Errorf("Updating Shared Key for Virtual Network Gateway Connection %q (Resource Group %q): %+v", name, resGroup, err) + return fmt.Errorf("updating Shared Key for %s: %+v", id, err) } if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("Waiting for updating Shared Key for Virtual Network Gateway Connection %q (Resource Group %q): %+v", name, resGroup, err) + return fmt.Errorf("waiting for updating Shared Key for %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 AzureRM Virtual Network Gateway Connection %q (resource group %q) ID", name, resGroup) - } - - d.SetId(*read.ID) + d.SetId(id.ID()) return resourceVirtualNetworkGatewayConnectionRead(d, meta) } @@ -383,24 +375,24 @@ func resourceVirtualNetworkGatewayConnectionRead(d *pluginsdk.ResourceData, meta ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - resGroup, name, err := resourceGroupAndVirtualNetworkGatewayConnectionFromId(d.Id()) + id, err := parse.NetworkGatewayConnectionID(d.Id()) if err != nil { return err } - resp, err := client.Get(ctx, resGroup, name) + resp, err := client.Get(ctx, id.ResourceGroup, id.ConnectionName) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") return nil } - return fmt.Errorf("making Read request on AzureRM Virtual Network Gateway Connection %q: %+v", name, err) + return fmt.Errorf("making Read request on %s: %+v", id, err) } conn := *resp.VirtualNetworkGatewayConnectionPropertiesFormat d.Set("name", resp.Name) - d.Set("resource_group_name", resGroup) + d.Set("resource_group_name", id.ResourceGroup) if location := resp.Location; location != nil { d.Set("location", azure.NormalizeLocation(*location)) } @@ -482,18 +474,18 @@ func resourceVirtualNetworkGatewayConnectionDelete(d *pluginsdk.ResourceData, me ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - resGroup, name, err := resourceGroupAndVirtualNetworkGatewayConnectionFromId(d.Id()) + id, err := parse.NetworkGatewayConnectionID(d.Id()) if err != nil { return err } - future, err := client.Delete(ctx, resGroup, name) + future, err := client.Delete(ctx, id.ResourceGroup, id.ConnectionName) if err != nil { - return fmt.Errorf("Deleting Virtual Network Gateway Connection %q (Resource Group %q): %+v", name, resGroup, err) + return fmt.Errorf("deleting %s: %+v", id, err) } if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("waiting for deletion of Virtual Network Gateway Connection %q (Resource Group %q): %+v", name, resGroup, err) + return fmt.Errorf("waiting for deletion of %s: %+v", id, err) } return nil @@ -621,15 +613,6 @@ func getVirtualNetworkGatewayConnectionProperties(d *pluginsdk.ResourceData) (*n return props, nil } -func resourceGroupAndVirtualNetworkGatewayConnectionFromId(virtualNetworkGatewayConnectionId string) (string, string, error) { - id, err := parse.NetworkGatewayConnectionID(virtualNetworkGatewayConnectionId) - if err != nil { - return "", "", err - } - - return id.ResourceGroup, id.ConnectionName, nil -} - func expandVirtualNetworkGatewayConnectionIpsecPolicies(schemaIpsecPolicies []interface{}) *[]network.IpsecPolicy { ipsecPolicies := make([]network.IpsecPolicy, 0, len(schemaIpsecPolicies)) diff --git a/internal/services/network/virtual_network_gateway_data_source.go b/internal/services/network/virtual_network_gateway_data_source.go index 8a1a442a5c39..3ff9a2ce0366 100644 --- a/internal/services/network/virtual_network_gateway_data_source.go +++ b/internal/services/network/virtual_network_gateway_data_source.go @@ -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/tags" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" @@ -232,25 +233,25 @@ func dataSourceVirtualNetworkGateway() *pluginsdk.Resource { func dataSourceVirtualNetworkGatewayRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.VnetGatewayClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - resGroup := d.Get("resource_group_name").(string) + id := parse.NewVirtualNetworkGatewayID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) - resp, err := client.Get(ctx, resGroup, name) + resp, err := client.Get(ctx, id.ResourceGroup, id.Name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - return fmt.Errorf("Virtual Network Gateway %q (Resource Group %q) was not found", name, resGroup) + return fmt.Errorf("%s was not found", id) } - return fmt.Errorf("making Read request on AzureRM Virtual Network Gateway %q (Resource Group %q): %+v", name, resGroup, err) + return fmt.Errorf("making Read request on %s: %+v", id, err) } - d.SetId(*resp.ID) + d.SetId(id.ID()) d.Set("name", resp.Name) - d.Set("resource_group_name", resGroup) + d.Set("resource_group_name", id.ResourceGroup) if location := resp.Location; location != nil { d.Set("location", azure.NormalizeLocation(*location)) } diff --git a/internal/services/network/virtual_network_peering_resource.go b/internal/services/network/virtual_network_peering_resource.go index 7ca56866207f..0dd9a15de2ef 100644 --- a/internal/services/network/virtual_network_peering_resource.go +++ b/internal/services/network/virtual_network_peering_resource.go @@ -89,20 +89,19 @@ func resourceVirtualNetworkPeering() *pluginsdk.Resource { func resourceVirtualNetworkPeeringCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.VnetPeeringsClient + subscriptionId := meta.(*clients.Client).Account.SubscriptionId ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() log.Printf("[INFO] preparing arguments for Azure ARM virtual network peering creation.") - name := d.Get("name").(string) - vnetName := d.Get("virtual_network_name").(string) - resGroup := d.Get("resource_group_name").(string) + id := parse.NewVirtualNetworkPeeringID(subscriptionId, d.Get("resource_group_name").(string), d.Get("virtual_network_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resGroup, vnetName, name) + existing, err := client.Get(ctx, id.ResourceGroup, id.VirtualNetworkName, id.Name) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Peering %q (Virtual Network %q / Resource Group %q): %s", name, vnetName, resGroup, err) + return fmt.Errorf("checking for presence of existing %s: %s", id, err) } } @@ -112,26 +111,18 @@ func resourceVirtualNetworkPeeringCreateUpdate(d *pluginsdk.ResourceData, meta i } peer := network.VirtualNetworkPeering{ - Name: &name, + Name: &id.Name, VirtualNetworkPeeringPropertiesFormat: getVirtualNetworkPeeringProperties(d), } peerMutex.Lock() defer peerMutex.Unlock() - if err := pluginsdk.Retry(300*time.Second, retryVnetPeeringsClientCreateUpdate(d, resGroup, vnetName, name, peer, meta)); err != nil { + if err := pluginsdk.Retry(300*time.Second, retryVnetPeeringsClientCreateUpdate(d, id.ResourceGroup, id.VirtualNetworkName, id.Name, peer, meta)); err != nil { return err } - read, err := client.Get(ctx, resGroup, vnetName, name) - if err != nil { - return err - } - if read.ID == nil { - return fmt.Errorf("Cannot read ID of Virtual Network Peering %q (resource group %q)", name, resGroup) - } - - d.SetId(*read.ID) + d.SetId(id.ID()) return resourceVirtualNetworkPeeringRead(d, meta) } diff --git a/internal/services/network/virtual_wan_data_source.go b/internal/services/network/virtual_wan_data_source.go index 3a1630ead77a..ab3569e62065 100644 --- a/internal/services/network/virtual_wan_data_source.go +++ b/internal/services/network/virtual_wan_data_source.go @@ -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/tags" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" @@ -68,27 +69,24 @@ func dataSourceVirtualWan() *pluginsdk.Resource { func dataSourceVirtualWanRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.VirtualWanClient + 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.NewVirtualWanID(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: Virtual Wan %q (Resource Group %q) was not found", name, resourceGroup) + return fmt.Errorf("%s was not found", id) } - return fmt.Errorf("reading Virtual Wan %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("API returns a nil/empty id on Virtual Wan %q (resource group %q): %+v", name, resourceGroup, 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)) } diff --git a/internal/services/network/vpn_gateway_resource.go b/internal/services/network/vpn_gateway_resource.go index 9d9002dbd4ab..ed1e9ebf9711 100644 --- a/internal/services/network/vpn_gateway_resource.go +++ b/internal/services/network/vpn_gateway_resource.go @@ -268,7 +268,7 @@ func resourceVPNGatewayCreate(d *pluginsdk.ResourceData, meta interface{}) error } } - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceVPNGatewayRead(d, meta) } @@ -278,15 +278,17 @@ func resourceVPNGatewayUpdate(d *pluginsdk.ResourceData, meta interface{}) error ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id, err := parse.VpnGatewayID(d.Id()) + if err != nil { + return err + } - locks.ByName(name, VPNGatewayResourceName) - defer locks.UnlockByName(name, VPNGatewayResourceName) + locks.ByName(id.Name, VPNGatewayResourceName) + defer locks.UnlockByName(id.Name, VPNGatewayResourceName) - existing, err := client.Get(ctx, resourceGroup, name) + existing, err := client.Get(ctx, id.ResourceGroup, id.Name) if err != nil { - return fmt.Errorf("retrieving for presence of existing VPN Gateway %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("retrieving for presence of existing %s: %+v", id, err) } if d.HasChange("scale_unit") { @@ -314,10 +316,10 @@ func resourceVPNGatewayUpdate(d *pluginsdk.ResourceData, meta interface{}) error } } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, name, existing); err != nil { - return fmt.Errorf("creating VPN Gateway %q (Resource Group %q): %+v", name, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, existing); err != nil { + return fmt.Errorf("creating %s: %+v", id, err) } - if err := waitForCompletion(d, ctx, client, resourceGroup, name); err != nil { + if err := waitForCompletion(d, ctx, client, id.ResourceGroup, id.Name); err != nil { return err } diff --git a/internal/services/network/web_application_firewall_policy_data_source.go b/internal/services/network/web_application_firewall_policy_data_source.go index 65480332013e..ef228a2bd061 100644 --- a/internal/services/network/web_application_firewall_policy_data_source.go +++ b/internal/services/network/web_application_firewall_policy_data_source.go @@ -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/tf/validation" @@ -39,23 +40,23 @@ func dataWebApplicationFirewallPolicy() *pluginsdk.Resource { func dataSourceWebApplicationFirewallPolicy(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Network.WebApplicationFirewallPoliciesClient + 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) - resp, err := client.Get(ctx, resourceGroup, name) + id := parse.NewApplicationGatewayWebApplicationFirewallPolicyID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) + resp, err := client.Get(ctx, id.ResourceGroup, id.Name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - return fmt.Errorf("Error: Web Application Firewall Policy %q was not found", name) + return fmt.Errorf("%s was not found", id) } return 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)) }