diff --git a/azurerm/internal/services/eventhub/resource_arm_eventhub.go b/azurerm/internal/services/eventhub/resource_arm_eventhub.go index cd444fde3a61..3b7073e04aa4 100644 --- a/azurerm/internal/services/eventhub/resource_arm_eventhub.go +++ b/azurerm/internal/services/eventhub/resource_arm_eventhub.go @@ -18,6 +18,8 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) +var eventHubResourceName = "azurerm_eventhub" + func resourceArmEventHub() *schema.Resource { return &schema.Resource{ Create: resourceArmEventHubCreateUpdate, diff --git a/azurerm/internal/services/eventhub/resource_arm_eventhub_authorization_rule.go b/azurerm/internal/services/eventhub/resource_arm_eventhub_authorization_rule.go index 8fcf45bc0cf5..b9e8228c908f 100644 --- a/azurerm/internal/services/eventhub/resource_arm_eventhub_authorization_rule.go +++ b/azurerm/internal/services/eventhub/resource_arm_eventhub_authorization_rule.go @@ -12,6 +12,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "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" ) @@ -88,6 +89,12 @@ func resourceArmEventHubAuthorizationRuleCreateUpdate(d *schema.ResourceData, me } } + locks.ByName(eventHubName, eventHubResourceName) + defer locks.UnlockByName(eventHubName, eventHubResourceName) + + locks.ByName(namespaceName, eventHubNamespaceResourceName) + defer locks.UnlockByName(namespaceName, eventHubNamespaceResourceName) + parameters := eventhub.AuthorizationRule{ Name: &name, AuthorizationRuleProperties: &eventhub.AuthorizationRuleProperties{ @@ -177,6 +184,12 @@ func resourceArmEventHubAuthorizationRuleDelete(d *schema.ResourceData, meta int namespaceName := id.Path["namespaces"] eventHubName := id.Path["eventhubs"] + locks.ByName(eventHubName, eventHubResourceName) + defer locks.UnlockByName(eventHubName, eventHubResourceName) + + locks.ByName(namespaceName, eventHubNamespaceResourceName) + defer locks.UnlockByName(namespaceName, eventHubNamespaceResourceName) + resp, err := eventhubClient.DeleteAuthorizationRule(ctx, resourceGroup, namespaceName, eventHubName, name) if resp.StatusCode != http.StatusOK { diff --git a/azurerm/internal/services/eventhub/resource_arm_eventhub_namespace.go b/azurerm/internal/services/eventhub/resource_arm_eventhub_namespace.go index 5ae3bbfa2af6..35b2dbe0c20c 100644 --- a/azurerm/internal/services/eventhub/resource_arm_eventhub_namespace.go +++ b/azurerm/internal/services/eventhub/resource_arm_eventhub_namespace.go @@ -25,6 +25,7 @@ import ( // Default Authorization Rule/Policy created by Azure, used to populate the // default connection strings and keys var eventHubNamespaceDefaultAuthorizationRule = "RootManageSharedAccessKey" +var eventHubNamespaceResourceName = "azurerm_eventhub_namespace" func resourceArmEventHubNamespace() *schema.Resource { return &schema.Resource{ diff --git a/azurerm/internal/services/eventhub/resource_arm_eventhub_namespace_authorization_rule.go b/azurerm/internal/services/eventhub/resource_arm_eventhub_namespace_authorization_rule.go index 7ae9a036c6c5..0bde5790fb4f 100644 --- a/azurerm/internal/services/eventhub/resource_arm_eventhub_namespace_authorization_rule.go +++ b/azurerm/internal/services/eventhub/resource_arm_eventhub_namespace_authorization_rule.go @@ -12,6 +12,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "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" ) @@ -80,6 +81,9 @@ func resourceArmEventHubNamespaceAuthorizationRuleCreateUpdate(d *schema.Resourc } } + locks.ByName(namespaceName, eventHubNamespaceResourceName) + defer locks.UnlockByName(namespaceName, eventHubNamespaceResourceName) + parameters := eventhub.AuthorizationRule{ Name: &name, AuthorizationRuleProperties: &eventhub.AuthorizationRuleProperties{ @@ -166,6 +170,9 @@ func resourceArmEventHubNamespaceAuthorizationRuleDelete(d *schema.ResourceData, resourceGroup := id.ResourceGroup namespaceName := id.Path["namespaces"] + locks.ByName(namespaceName, eventHubNamespaceResourceName) + defer locks.UnlockByName(namespaceName, eventHubNamespaceResourceName) + resp, err := eventhubClient.DeleteAuthorizationRule(ctx, resourceGroup, namespaceName, name) if resp.StatusCode != http.StatusOK { diff --git a/azurerm/internal/services/eventhub/tests/resource_arm_eventhub_authorization_rule_test.go b/azurerm/internal/services/eventhub/tests/resource_arm_eventhub_authorization_rule_test.go index 18cc142c9855..d637f56d6885 100644 --- a/azurerm/internal/services/eventhub/tests/resource_arm_eventhub_authorization_rule_test.go +++ b/azurerm/internal/services/eventhub/tests/resource_arm_eventhub_authorization_rule_test.go @@ -57,6 +57,54 @@ func testAccAzureRMEventHubAuthorizationRule(t *testing.T, listen, send, manage }) } +func TestAccAzureRMEventHubAuthorizationRule_multi(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_eventhub_authorization_rule", "test1") + resourceTwoName := "azurerm_eventhub_authorization_rule.test2" + resourceThreeName := "azurerm_eventhub_authorization_rule.test3" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMEventHubAuthorizationRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAzureRMEventHubAuthorizationRule_multi(data, true, true, true), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMEventHubAuthorizationRuleExists(data.ResourceName), + resource.TestCheckResourceAttr(data.ResourceName, "manage", "false"), + resource.TestCheckResourceAttr(data.ResourceName, "send", "true"), + resource.TestCheckResourceAttr(data.ResourceName, "listen", "true"), + resource.TestCheckResourceAttrSet(data.ResourceName, "primary_connection_string"), + resource.TestCheckResourceAttrSet(data.ResourceName, "secondary_connection_string"), + testCheckAzureRMEventHubAuthorizationRuleExists(resourceTwoName), + resource.TestCheckResourceAttr(resourceTwoName, "manage", "false"), + resource.TestCheckResourceAttr(resourceTwoName, "send", "true"), + resource.TestCheckResourceAttr(resourceTwoName, "listen", "true"), + resource.TestCheckResourceAttrSet(resourceTwoName, "primary_connection_string"), + resource.TestCheckResourceAttrSet(resourceTwoName, "secondary_connection_string"), + testCheckAzureRMEventHubAuthorizationRuleExists(resourceThreeName), + resource.TestCheckResourceAttr(resourceThreeName, "manage", "false"), + resource.TestCheckResourceAttr(resourceThreeName, "send", "true"), + resource.TestCheckResourceAttr(resourceThreeName, "listen", "true"), + resource.TestCheckResourceAttrSet(resourceThreeName, "primary_connection_string"), + resource.TestCheckResourceAttrSet(resourceThreeName, "secondary_connection_string"), + ), + }, + data.ImportStep(), + { + ResourceName: resourceTwoName, + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: resourceThreeName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccAzureRMEventHubAuthorizationRule_requiresImport(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_eventhub_authorization_rule", "test") @@ -214,6 +262,40 @@ resource "azurerm_eventhub_authorization_rule" "test" { `, data.RandomInteger, data.Locations.Primary, listen, send, manage) } +func testAzureRMEventHubAuthorizationRule_multi(data acceptance.TestData, listen, send, manage bool) string { + template := testAccAzureRMEventHubAuthorizationRule_base(data, listen, send, manage) + return fmt.Sprintf(` +%s + +resource "azurerm_eventhub_authorization_rule" "test1" { + name = "acctestruleone-%d" + eventhub_name = azurerm_eventhub.test.name + namespace_name = azurerm_eventhub_namespace.test.name + resource_group_name = azurerm_resource_group.test.name + send = true + listen = true +} + +resource "azurerm_eventhub_authorization_rule" "test2" { + name = "acctestruletwo-%d" + eventhub_name = azurerm_eventhub.test.name + namespace_name = azurerm_eventhub_namespace.test.name + resource_group_name = azurerm_resource_group.test.name + send = true + listen = true +} + +resource "azurerm_eventhub_authorization_rule" "test3" { + name = "acctestrulethree-%d" + eventhub_name = azurerm_eventhub.test.name + namespace_name = azurerm_eventhub_namespace.test.name + resource_group_name = azurerm_resource_group.test.name + send = true + listen = true +} +`, template, data.RandomInteger, data.RandomInteger, data.RandomInteger) +} + func testAccAzureRMEventHubAuthorizationRule_requiresImport(data acceptance.TestData, listen, send, manage bool) string { template := testAccAzureRMEventHubAuthorizationRule_base(data, listen, send, manage) return fmt.Sprintf(` diff --git a/azurerm/internal/services/eventhub/tests/resource_arm_eventhub_namespace_authorization_rule_test.go b/azurerm/internal/services/eventhub/tests/resource_arm_eventhub_namespace_authorization_rule_test.go index bf9030460cbb..5273d778f539 100644 --- a/azurerm/internal/services/eventhub/tests/resource_arm_eventhub_namespace_authorization_rule_test.go +++ b/azurerm/internal/services/eventhub/tests/resource_arm_eventhub_namespace_authorization_rule_test.go @@ -115,6 +115,54 @@ func TestAccAzureRMEventHubNamespaceAuthorizationRule_rightsUpdate(t *testing.T) }) } +func TestAccAzureRMEventHubNamespaceAuthorizationRule_multi(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_eventhub_namespace_authorization_rule", "test1") + resourceTwoName := "azurerm_eventhub_namespace_authorization_rule.test2" + resourceThreeName := "azurerm_eventhub_namespace_authorization_rule.test3" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMEventHubNamespaceAuthorizationRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAzureRMEventHubNamespaceAuthorizationRule_multi(data, true, true, true), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMEventHubNamespaceAuthorizationRuleExists(data.ResourceName), + resource.TestCheckResourceAttr(data.ResourceName, "manage", "false"), + resource.TestCheckResourceAttr(data.ResourceName, "send", "true"), + resource.TestCheckResourceAttr(data.ResourceName, "listen", "true"), + resource.TestCheckResourceAttrSet(data.ResourceName, "primary_connection_string"), + resource.TestCheckResourceAttrSet(data.ResourceName, "secondary_connection_string"), + testCheckAzureRMEventHubNamespaceAuthorizationRuleExists(resourceTwoName), + resource.TestCheckResourceAttr(resourceTwoName, "manage", "false"), + resource.TestCheckResourceAttr(resourceTwoName, "send", "true"), + resource.TestCheckResourceAttr(resourceTwoName, "listen", "true"), + resource.TestCheckResourceAttrSet(resourceTwoName, "primary_connection_string"), + resource.TestCheckResourceAttrSet(resourceTwoName, "secondary_connection_string"), + testCheckAzureRMEventHubNamespaceAuthorizationRuleExists(resourceThreeName), + resource.TestCheckResourceAttr(resourceThreeName, "manage", "false"), + resource.TestCheckResourceAttr(resourceThreeName, "send", "true"), + resource.TestCheckResourceAttr(resourceThreeName, "listen", "true"), + resource.TestCheckResourceAttrSet(resourceThreeName, "primary_connection_string"), + resource.TestCheckResourceAttrSet(resourceThreeName, "secondary_connection_string"), + ), + }, + data.ImportStep(), + { + ResourceName: resourceTwoName, + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: resourceThreeName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testCheckAzureRMEventHubNamespaceAuthorizationRuleDestroy(s *terraform.State) error { client := acceptance.AzureProvider.Meta().(*clients.Client).Eventhub.NamespacesClient ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext @@ -216,3 +264,40 @@ resource "azurerm_eventhub_namespace_authorization_rule" "import" { } `, template) } + +func testAzureRMEventHubNamespaceAuthorizationRule_multi(data acceptance.TestData, listen, send, manage bool) string { + template := testAccAzureRMEventHubNamespaceAuthorizationRule_base(data, listen, send, manage) + return fmt.Sprintf(` +%s + +resource "azurerm_eventhub_namespace_authorization_rule" "test1" { + name = "acctestruleone-%d" + namespace_name = azurerm_eventhub_namespace.test.name + resource_group_name = azurerm_resource_group.test.name + + send = true + listen = true + manage = false +} + +resource "azurerm_eventhub_namespace_authorization_rule" "test2" { + name = "acctestruletwo-%d" + namespace_name = azurerm_eventhub_namespace.test.name + resource_group_name = azurerm_resource_group.test.name + + send = true + listen = true + manage = false +} + +resource "azurerm_eventhub_namespace_authorization_rule" "test3" { + name = "acctestrulethree-%d" + namespace_name = azurerm_eventhub_namespace.test.name + resource_group_name = azurerm_resource_group.test.name + + send = true + listen = true + manage = false +} +`, template, data.RandomInteger, data.RandomInteger, data.RandomInteger) +}