From a63450c1727287b921039d2dac8bb2af340e6246 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Fri, 7 Aug 2020 11:24:18 +0800 Subject: [PATCH 1/2] Add lock for iothub while operating iothub consumer group --- .../iothub/iothub_consumer_group_resource.go | 7 ++++ .../iothub_shared_access_policy_resource.go | 2 ++ .../iothub_consumer_group_resource_test.go | 33 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/azurerm/internal/services/iothub/iothub_consumer_group_resource.go b/azurerm/internal/services/iothub/iothub_consumer_group_resource.go index 4c99cecd2ca1..807e4ec2901f 100644 --- a/azurerm/internal/services/iothub/iothub_consumer_group_resource.go +++ b/azurerm/internal/services/iothub/iothub_consumer_group_resource.go @@ -2,6 +2,7 @@ package iothub import ( "fmt" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks" "log" "time" @@ -68,6 +69,9 @@ func resourceArmIotHubConsumerGroupCreate(d *schema.ResourceData, meta interface endpointName := d.Get("eventhub_endpoint_name").(string) resourceGroup := d.Get("resource_group_name").(string) + locks.ByName(iotHubName, IothubResourceName) + defer locks.UnlockByName(iotHubName, IothubResourceName) + if features.ShouldResourcesBeImported() && d.IsNewResource() { existing, err := client.GetEventHubConsumerGroup(ctx, resourceGroup, iotHubName, endpointName, name) if err != nil { @@ -145,6 +149,9 @@ func resourceArmIotHubConsumerGroupDelete(d *schema.ResourceData, meta interface endpointName := id.Path["eventHubEndpoints"] name := id.Path["ConsumerGroups"] + locks.ByName(iotHubName, IothubResourceName) + defer locks.UnlockByName(iotHubName, IothubResourceName) + resp, err := client.DeleteEventHubConsumerGroup(ctx, resourceGroup, iotHubName, endpointName, name) if err != nil { diff --git a/azurerm/internal/services/iothub/iothub_shared_access_policy_resource.go b/azurerm/internal/services/iothub/iothub_shared_access_policy_resource.go index fe394e6196c4..eb7b74aaa14f 100644 --- a/azurerm/internal/services/iothub/iothub_shared_access_policy_resource.go +++ b/azurerm/internal/services/iothub/iothub_shared_access_policy_resource.go @@ -21,6 +21,8 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) +var IothubSharedAccessPolicyResourceName = "azurerm_iothub_shared_access_policy" + func resourceArmIotHubSharedAccessPolicy() *schema.Resource { return &schema.Resource{ Create: resourceArmIotHubSharedAccessPolicyCreateUpdate, diff --git a/azurerm/internal/services/iothub/tests/iothub_consumer_group_resource_test.go b/azurerm/internal/services/iothub/tests/iothub_consumer_group_resource_test.go index b24365044e68..d8b123457560 100644 --- a/azurerm/internal/services/iothub/tests/iothub_consumer_group_resource_test.go +++ b/azurerm/internal/services/iothub/tests/iothub_consumer_group_resource_test.go @@ -73,6 +73,25 @@ func TestAccAzureRMIotHubConsumerGroup_operationsMonitoringEvents(t *testing.T) }) } +func TestAccAzureRMIotHubConsumerGroup_withSharedAccessPolicy(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_iothub_consumer_group", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMIotHubConsumerGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMIotHubConsumerGroup_withSharedAccessPolicy(data, "events"), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMIotHubConsumerGroupExists(data.ResourceName), + ), + }, + data.ImportStep(), + }, + }) +} + func testCheckAzureRMIotHubConsumerGroupDestroy(s *terraform.State) error { client := acceptance.AzureProvider.Meta().(*clients.Client).IoTHub.ResourceClient ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext @@ -176,3 +195,17 @@ resource "azurerm_iothub_consumer_group" "import" { } `, template) } + +func testAccAzureRMIotHubConsumerGroup_withSharedAccessPolicy(data acceptance.TestData, eventName string) string { + template := testAccAzureRMIotHubConsumerGroup_basic(data, eventName) + return fmt.Sprintf(` +%s + +resource "azurerm_iothub_shared_access_policy" "test" { + name = "acctestSharedAccessPolicy" + resource_group_name = azurerm_resource_group.test.name + iothub_name = azurerm_iothub.test.name + service_connect = true +} +`, template) +} From 2f242878667a7a7f1c34cbc0a946500ebebae3ab Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Fri, 7 Aug 2020 11:26:09 +0800 Subject: [PATCH 2/2] update code --- .../internal/services/iothub/iothub_consumer_group_resource.go | 2 +- .../services/iothub/iothub_shared_access_policy_resource.go | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/azurerm/internal/services/iothub/iothub_consumer_group_resource.go b/azurerm/internal/services/iothub/iothub_consumer_group_resource.go index 807e4ec2901f..02850f103e05 100644 --- a/azurerm/internal/services/iothub/iothub_consumer_group_resource.go +++ b/azurerm/internal/services/iothub/iothub_consumer_group_resource.go @@ -2,7 +2,6 @@ package iothub import ( "fmt" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks" "log" "time" @@ -12,6 +11,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/locks" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) diff --git a/azurerm/internal/services/iothub/iothub_shared_access_policy_resource.go b/azurerm/internal/services/iothub/iothub_shared_access_policy_resource.go index eb7b74aaa14f..fe394e6196c4 100644 --- a/azurerm/internal/services/iothub/iothub_shared_access_policy_resource.go +++ b/azurerm/internal/services/iothub/iothub_shared_access_policy_resource.go @@ -21,8 +21,6 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) -var IothubSharedAccessPolicyResourceName = "azurerm_iothub_shared_access_policy" - func resourceArmIotHubSharedAccessPolicy() *schema.Resource { return &schema.Resource{ Create: resourceArmIotHubSharedAccessPolicyCreateUpdate,