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_notification_hub_authorization_rule fix to be able to create multiple rules in one go #4087

Merged
merged 5 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions azurerm/resource_arm_notification_hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

var notificationHubResourceName = "azurerm_notification_hub"

const apnsProductionName = "Production"
const apnsProductionEndpoint = "https://api.push.apple.com:443/3/device"
const apnsSandboxName = "Sandbox"
Expand Down
13 changes: 13 additions & 0 deletions azurerm/resource_arm_notification_hub_authorization_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ func resourceArmNotificationHubAuthorizationRuleCreateUpdate(d *schema.ResourceD
}
}

azureRMLockByName(notificationHubName, notificationHubResourceName)
defer azureRMUnlockByName(notificationHubName, notificationHubResourceName)

azureRMLockByName(namespaceName, notificationHubNamespaceResourceName)
defer azureRMUnlockByName(namespaceName, notificationHubNamespaceResourceName)

parameters := notificationhubs.SharedAccessAuthorizationRuleCreateOrUpdateParameters{
Properties: &notificationhubs.SharedAccessAuthorizationRuleProperties{
Rights: expandNotificationHubAuthorizationRuleRights(manage, send, listen),
Expand All @@ -121,6 +127,7 @@ func resourceArmNotificationHubAuthorizationRuleCreateUpdate(d *schema.ResourceD
d.SetId(*read.ID)

return resourceArmNotificationHubAuthorizationRuleRead(d, meta)

}

func resourceArmNotificationHubAuthorizationRuleRead(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -183,6 +190,12 @@ func resourceArmNotificationHubAuthorizationRuleDelete(d *schema.ResourceData, m
notificationHubName := id.Path["notificationHubs"]
name := id.Path["AuthorizationRules"]

azureRMLockByName(notificationHubName, notificationHubResourceName)
defer azureRMUnlockByName(notificationHubName, notificationHubResourceName)

azureRMLockByName(namespaceName, notificationHubNamespaceResourceName)
defer azureRMUnlockByName(namespaceName, notificationHubNamespaceResourceName)

resp, err := client.DeleteAuthorizationRule(ctx, resourceGroup, namespaceName, notificationHubName, name)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
Expand Down
90 changes: 90 additions & 0 deletions azurerm/resource_arm_notification_hub_authorization_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,61 @@ func TestAccAzureRMNotificationHubAuthorizationRule_send(t *testing.T) {
})
}

func TestAccAzureRMNotificationHubAuthorizationRule_multi(t *testing.T) {
resourceOneName := "azurerm_notification_hub_authorization_rule.test1"
resourceTwoName := "azurerm_notification_hub_authorization_rule.test2"
resourceThreeName := "azurerm_notification_hub_authorization_rule.test2"
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMNotificationHubAuthorizationRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAzureRMNotificationHubAuthorizationRule_multi(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNotificationHubAuthorizationRuleExists(resourceOneName),
resource.TestCheckResourceAttr(resourceOneName, "manage", "false"),
resource.TestCheckResourceAttr(resourceOneName, "send", "true"),
resource.TestCheckResourceAttr(resourceOneName, "listen", "true"),
resource.TestCheckResourceAttrSet(resourceOneName, "primary_access_key"),
resource.TestCheckResourceAttrSet(resourceOneName, "secondary_access_key"),
testCheckAzureRMNotificationHubAuthorizationRuleExists(resourceTwoName),
resource.TestCheckResourceAttr(resourceTwoName, "manage", "false"),
resource.TestCheckResourceAttr(resourceTwoName, "send", "true"),
resource.TestCheckResourceAttr(resourceTwoName, "listen", "true"),
resource.TestCheckResourceAttrSet(resourceTwoName, "primary_access_key"),
resource.TestCheckResourceAttrSet(resourceTwoName, "secondary_access_key"),
testCheckAzureRMNotificationHubAuthorizationRuleExists(resourceThreeName),
resource.TestCheckResourceAttr(resourceThreeName, "manage", "false"),
resource.TestCheckResourceAttr(resourceThreeName, "send", "true"),
resource.TestCheckResourceAttr(resourceThreeName, "listen", "true"),
resource.TestCheckResourceAttrSet(resourceThreeName, "primary_access_key"),
resource.TestCheckResourceAttrSet(resourceThreeName, "secondary_access_key"),
),
},
{
ResourceName: resourceOneName,
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: resourceTwoName,
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: resourceThreeName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMNotificationHubAuthorizationRule_updated(t *testing.T) {
resourceName := "azurerm_notification_hub_authorization_rule.test"

Expand Down Expand Up @@ -271,6 +326,41 @@ resource "azurerm_notification_hub_authorization_rule" "test" {
`, template, ri)
}

func testAzureRMNotificationHubAuthorizationRule_multi(ri int, location string) string {
template := testAzureRMNotificationHubAuthorizationRule_template(ri, location)
return fmt.Sprintf(`
%s

resource "azurerm_notification_hub_authorization_rule" "test1" {
name = "acctestruleone-%d"
notification_hub_name = "${azurerm_notification_hub.test.name}"
namespace_name = "${azurerm_notification_hub_namespace.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
send = true
listen = true
}

resource "azurerm_notification_hub_authorization_rule" "test2" {
name = "acctestruletwo-%d"
notification_hub_name = "${azurerm_notification_hub.test.name}"
namespace_name = "${azurerm_notification_hub_namespace.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
send = true
listen = true
}

resource "azurerm_notification_hub_authorization_rule" "test3" {
name = "acctestrulethree-%d"
notification_hub_name = "${azurerm_notification_hub.test.name}"
namespace_name = "${azurerm_notification_hub_namespace.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
send = true
listen = true
}

`, template, ri, ri, ri)
}

func testAzureRMNotificationHubAuthorizationRule_manage(ri int, location string) string {
template := testAzureRMNotificationHubAuthorizationRule_template(ri, location)
return fmt.Sprintf(`
Expand Down
2 changes: 2 additions & 0 deletions azurerm/resource_arm_notification_hub_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

var notificationHubNamespaceResourceName = "azurerm_notification_hub_namespace"

func resourceArmNotificationHubNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceArmNotificationHubNamespaceCreateUpdate,
Expand Down