From 506445ca999180108033bc8a077385fb6cb645a1 Mon Sep 17 00:00:00 2001 From: Xu Wu Date: Fri, 2 Jun 2023 06:37:47 +0800 Subject: [PATCH] `azurerm_express_route_port_authorization`: add a lock when create/update/delete authorization of express route port (#21959) --- .../express_route_port_authorization_resource.go | 10 ++++++++++ .../services/network/express_route_port_resource.go | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/internal/services/network/express_route_port_authorization_resource.go b/internal/services/network/express_route_port_authorization_resource.go index e4a744f783e1..d9c65f73a5dc 100644 --- a/internal/services/network/express_route_port_authorization_resource.go +++ b/internal/services/network/express_route_port_authorization_resource.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/locks" "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/timeouts" @@ -85,6 +86,11 @@ func resourceExpressRoutePortAuthorizationCreate(d *pluginsdk.ResourceData, meta ExpressRoutePortAuthorizationPropertiesFormat: &network.ExpressRoutePortAuthorizationPropertiesFormat{}, } + // can run only one create/update/delete operation of expressRoutePort at the same time + portID := parse.NewExpressRoutePortID(id.SubscriptionId, id.ResourceGroup, id.ExpressRoutePortName) + locks.ByID(portID.ID()) + defer locks.UnlockByID(portID.ID()) + future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ExpressRoutePortName, id.AuthorizationName, properties) if err != nil { return fmt.Errorf("Creating/Updating %s: %+v", id, err) @@ -140,6 +146,10 @@ func resourceExpressRoutePortAuthorizationDelete(d *pluginsdk.ResourceData, meta return err } + portID := parse.NewExpressRoutePortID(id.SubscriptionId, id.ResourceGroup, id.ExpressRoutePortName) + locks.ByID(portID.ID()) + defer locks.UnlockByID(portID.ID()) + future, err := client.Delete(ctx, id.ResourceGroup, id.ExpressRoutePortName, id.AuthorizationName) if err != nil { return fmt.Errorf("deleting %s: %+v", *id, err) diff --git a/internal/services/network/express_route_port_resource.go b/internal/services/network/express_route_port_resource.go index cfdfdcd1c297..77f83c39fd24 100644 --- a/internal/services/network/express_route_port_resource.go +++ b/internal/services/network/express_route_port_resource.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/locks" "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" @@ -227,6 +228,10 @@ func resourceArmExpressRoutePortCreateUpdate(d *pluginsdk.ResourceData, meta int param.ExpressRoutePortPropertiesFormat.BillingType = network.ExpressRoutePortsBillingType(v.(string)) } + // a lock is needed here for subresource express_route_port_authorization needs a lock. + locks.ByID(id.ID()) + defer locks.UnlockByID(id.ID()) + // The link properties can't be specified in first creation. It will result into either error (e.g. setting `adminState`) or being ignored (e.g. setting MACSec) // Hence, if this is a new creation we will do a create-then-update here. if d.IsNewResource() { @@ -320,6 +325,10 @@ func resourceArmExpressRoutePortDelete(d *pluginsdk.ResourceData, meta interface return err } + // a lock is needed here for subresource express_route_port_authorization needs a lock. + locks.ByID(id.ID()) + defer locks.UnlockByID(id.ID()) + future, err := client.Delete(ctx, id.ResourceGroup, id.Name) if err != nil { return fmt.Errorf("deleting Express Route Port %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)