Skip to content

Commit

Permalink
Merge pull request #8041 from neil-yechenwei/fixconsumergroup02
Browse files Browse the repository at this point in the history
Add lock for iothub while operating iothub consumer group
  • Loading branch information
tombuildsstuff authored Aug 10, 2020
2 parents 2ee04ab + 2f24287 commit ea44889
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit ea44889

Please sign in to comment.