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 2 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
28 changes: 16 additions & 12 deletions azurerm/resource_arm_notification_hub_authorization_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/Azure/azure-sdk-for-go/services/notificationhubs/mgmt/2017-04-01/notificationhubs"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
Expand Down Expand Up @@ -106,21 +107,24 @@ func resourceArmNotificationHubAuthorizationRuleCreateUpdate(d *schema.ResourceD
},
}

if _, err := client.CreateOrUpdateAuthorizationRule(ctx, resourceGroup, namespaceName, notificationHubName, name, parameters); err != nil {
return fmt.Errorf("Error creating Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q): %+v", name, notificationHubName, namespaceName, resourceGroup, err)
}
return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
andrey-dubnik marked this conversation as resolved.
Show resolved Hide resolved

read, err := client.GetAuthorizationRule(ctx, resourceGroup, namespaceName, notificationHubName, name)
if err != nil {
return fmt.Errorf("Error retrieving Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q): %+v", name, notificationHubName, namespaceName, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q) ID", name, notificationHubName, namespaceName, resourceGroup)
}
if _, err := client.CreateOrUpdateAuthorizationRule(ctx, resourceGroup, namespaceName, notificationHubName, name, parameters); err != nil {
return resource.NonRetryableError(fmt.Errorf("Error creating Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q): %+v", name, notificationHubName, namespaceName, resourceGroup, err))
}

read, err := client.GetAuthorizationRule(ctx, resourceGroup, namespaceName, notificationHubName, name)
if err != nil {
return resource.RetryableError(fmt.Errorf("Expected instance of the authorization rule %q (Notification Hub %q / Namespace %q / Resource Group %q) to be created but was in non existent state, retrying", name, notificationHubName, namespaceName, resourceGroup))
}
if read.ID == nil {
return resource.NonRetryableError(fmt.Errorf("Cannot read Authorization Rule %q (Notification Hub %q / Namespace %q / Resource Group %q) ID", name, notificationHubName, namespaceName, resourceGroup))
}

d.SetId(*read.ID)
d.SetId(*read.ID)

return resourceArmNotificationHubAuthorizationRuleRead(d, meta)
return resource.NonRetryableError(resourceArmNotificationHubAuthorizationRuleRead(d, meta))
})
}

func resourceArmNotificationHubAuthorizationRuleRead(d *schema.ResourceData, meta interface{}) error {
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