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

provider/azurerm: expose default keys for servicebus_namespace #9242

Merged
merged 1 commit into from
Oct 6, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions builtin/providers/azurerm/resource_arm_servicebus_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/hashicorp/terraform/helper/schema"
)

// Default Authorization Rule/Policy created by Azure, used to populate the
// default connection strings and keys
var serviceBusNamespaceDefaultAuthorizationRule = "RootManageSharedAccessKey"

func resourceArmServiceBusNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceArmServiceBusNamespaceCreate,
Expand Down Expand Up @@ -55,6 +59,26 @@ func resourceArmServiceBusNamespace() *schema.Resource {
ValidateFunc: validateServiceBusNamespaceCapacity,
},

"default_primary_connection_string": {
Type: schema.TypeString,
Computed: true,
},

"default_secondary_connection_string": {
Type: schema.TypeString,
Computed: true,
},

"default_primary_key": {
Type: schema.TypeString,
Computed: true,
},

"default_secondary_key": {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -123,6 +147,17 @@ func resourceArmServiceBusNamespaceRead(d *schema.ResourceData, meta interface{}
d.Set("name", resp.Name)
d.Set("sku", strings.ToLower(string(resp.Sku.Name)))
d.Set("capacity", resp.Sku.Capacity)

keys, err := namespaceClient.ListKeys(resGroup, name, serviceBusNamespaceDefaultAuthorizationRule)
if err != nil {
log.Printf("[ERROR] Unable to List default keys for Namespace %s: %s", name, err)
} else {
d.Set("default_primary_connection_string", keys.PrimaryConnectionString)
d.Set("default_secondary_connection_string", keys.SecondaryConnectionString)
d.Set("default_primary_key", keys.PrimaryKey)
d.Set("default_secondary_key", keys.SecondaryKey)
}

flattenAndSetTags(d, resp.Tags)

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"net/http"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand Down Expand Up @@ -94,6 +95,33 @@ func TestAccAzureRMServiceBusNamespace_basic(t *testing.T) {
})
}

func TestAccAzureRMServiceBusNamespace_readDefaultKeys(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMServiceBusNamespace_basic, ri, ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMServiceBusNamespaceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMServiceBusNamespaceExists("azurerm_servicebus_namespace.test"),
resource.TestMatchResourceAttr(
"azurerm_servicebus_namespace.test", "default_primary_connection_string", regexp.MustCompile("Endpoint=.+")),
resource.TestMatchResourceAttr(
"azurerm_servicebus_namespace.test", "default_secondary_connection_string", regexp.MustCompile("Endpoint=.+")),
resource.TestMatchResourceAttr(
"azurerm_servicebus_namespace.test", "default_primary_key", regexp.MustCompile(".+")),
resource.TestMatchResourceAttr(
"azurerm_servicebus_namespace.test", "default_secondary_key", regexp.MustCompile(".+")),
),
},
},
})
}

func testCheckAzureRMServiceBusNamespaceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).serviceBusNamespacesClient

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@ The following arguments are supported:
The following attributes are exported:

* `id` - The ServiceBus Namespace ID.

The following attributes are exported only if there is an authorization rule named
`RootManageSharedAccessKey` which is created automatically by Azure.

* `default_primary_connection_string` - The primary connection string for the authorization
rule `RootManageSharedAccessKey`.

* `default_secondary_connection_string` - The secondary connection string for the
authorization rule `RootManageSharedAccessKey`.

* `default_primary_key` - The primary access key for the authorization rule `RootManageSharedAccessKey`.

* `default_secondary_key` - The secondary access key for the authorization rule `RootManageSharedAccessKey`.