Skip to content

Commit

Permalink
azurerm_eventhub_namespace_authorization_rule - lock so multiple reso…
Browse files Browse the repository at this point in the history
…urce updates won't clash (#6701)

Fixes #4893
  • Loading branch information
nickmhankins committed May 6, 2020
1 parent 63028d8 commit 221f39e
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
2 changes: 2 additions & 0 deletions azurerm/internal/services/eventhub/resource_arm_eventhub.go
Expand Up @@ -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,
Expand Down
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
Expand Up @@ -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{
Expand Down
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
Expand Up @@ -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")

Expand Down Expand Up @@ -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(`
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit 221f39e

Please sign in to comment.