Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_eventhub_namespace_authorization_rule - lock so multiple resource updates won't clash #6701

Merged
merged 4 commits into from May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -58,6 +58,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) {
if !features.ShouldResourcesBeImported() {
t.Skip("Skipping since resources aren't required to be imported")
Expand Down Expand Up @@ -220,6 +268,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 @@ -121,6 +121,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 @@ -222,3 +270,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)
}