Skip to content

Commit

Permalink
Merge pull request #2841 from terraform-providers/f/data-source-servi…
Browse files Browse the repository at this point in the history
…cbus-namespace

New Data Source: `azurerm_servicebus_namespace`
  • Loading branch information
tombuildsstuff authored Feb 7, 2019
2 parents 5e4c19c + ce78dc6 commit 41bafb3
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 0 deletions.
105 changes: 105 additions & 0 deletions azurerm/data_source_servicebus_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package azurerm

import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmServiceBusNamespace() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmServiceBusNamespaceRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"resource_group_name": resourceGroupNameForDataSourceSchema(),

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

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

"capacity": {
Type: schema.TypeInt,
Computed: true,
},

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

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

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

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

"tags": tagsForDataSourceSchema(),
},
}
}

func dataSourceArmServiceBusNamespaceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).serviceBusNamespacesClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("ServiceBus Namespace %q was not found in Resource Group %q", name, resourceGroup)
}

return fmt.Errorf("Error retrieving ServiceBus Namespace %q (Resource Group %q): %s", name, resourceGroup, err)
}

d.SetId(*resp.ID)

if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

if sku := resp.Sku; sku != nil {
d.Set("sku", string(sku.Name))
d.Set("capacity", sku.Capacity)
}

keys, err := client.ListKeys(ctx, resourceGroup, name, serviceBusNamespaceDefaultAuthorizationRule)
if err != nil {
log.Printf("[WARN] Unable to List default keys for Namespace %q (Resource Group %q): %+v", name, resourceGroup, 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)
}

return nil
}
87 changes: 87 additions & 0 deletions azurerm/data_source_servicebus_namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceAzureRMServiceBusNamespace_basic(t *testing.T) {
dataSourceName := "data.azurerm_servicebus_namespace.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMServiceBusNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMServiceBusNamespace_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMServiceBusNamespaceExists(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "location"),
resource.TestCheckResourceAttrSet(dataSourceName, "sku"),
resource.TestCheckResourceAttrSet(dataSourceName, "capacity"),
resource.TestCheckResourceAttrSet(dataSourceName, "default_primary_connection_string"),
resource.TestCheckResourceAttrSet(dataSourceName, "default_secondary_connection_string"),
resource.TestCheckResourceAttrSet(dataSourceName, "default_primary_key"),
resource.TestCheckResourceAttrSet(dataSourceName, "default_secondary_key"),
),
},
},
})
}

func TestAccDataSourceAzureRMServiceBusNamespace_premium(t *testing.T) {
dataSourceName := "data.azurerm_servicebus_namespace.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMServiceBusNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMServiceBusNamespace_premium(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMServiceBusNamespaceExists(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "location"),
resource.TestCheckResourceAttrSet(dataSourceName, "sku"),
resource.TestCheckResourceAttrSet(dataSourceName, "capacity"),
resource.TestCheckResourceAttrSet(dataSourceName, "default_primary_connection_string"),
resource.TestCheckResourceAttrSet(dataSourceName, "default_secondary_connection_string"),
resource.TestCheckResourceAttrSet(dataSourceName, "default_primary_key"),
resource.TestCheckResourceAttrSet(dataSourceName, "default_secondary_key"),
),
},
},
})
}

func testAccDataSourceAzureRMServiceBusNamespace_basic(rInt int, location string) string {
template := testAccAzureRMServiceBusNamespace_basic(rInt, location)
return fmt.Sprintf(`
%s
data "azurerm_servicebus_namespace" "test" {
name = "${azurerm_servicebus_namespace.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, template)
}

func testAccDataSourceAzureRMServiceBusNamespace_premium(rInt int, location string) string {
template := testAccAzureRMServiceBusNamespace_premium(rInt, location)
return fmt.Sprintf(`
%s
data "azurerm_servicebus_namespace" "test" {
name = "${azurerm_servicebus_namespace.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, template)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_role_definition": dataSourceArmRoleDefinition(),
"azurerm_route_table": dataSourceArmRouteTable(),
"azurerm_scheduler_job_collection": dataSourceArmSchedulerJobCollection(),
"azurerm_servicebus_namespace": dataSourceArmServiceBusNamespace(),
"azurerm_shared_image_gallery": dataSourceArmSharedImageGallery(),
"azurerm_shared_image_version": dataSourceArmSharedImageVersion(),
"azurerm_shared_image": dataSourceArmSharedImage(),
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@
<a href="/docs/providers/azurerm/d/scheduler_job_collection.html">azurerm_scheduler_job_collection</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-servicebus-namespace") %>>
<a href="/docs/providers/azurerm/d/servicebus_namespace.html">azurerm_servicebus_namespace</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-shared-image-x") %>>
<a href="/docs/providers/azurerm/d/shared_image.html">azurerm_shared_image</a>
</li>
Expand Down
53 changes: 53 additions & 0 deletions website/docs/d/servicebus_namespace.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_servicebus_namespace"
sidebar_current: "docs-azurerm-datasource-servicebus-namespace"
description: |-
Gets information about an existing ServiceBus Namespace.
---

# Data Source: azurerm_servicebus_namespace

Use this data source to access information about an existing ServiceBus Namespace.

## Example Usage

```hcl
data "azurerm_servicebus_namespace" "test" {
name = "examplenamespace"
resource_group_name = "example-resources"
}
output "location" {
value = "${data.azurerm_servicebus_namespace.test.location}"
}
```

## Argument Reference

* `name` - (Required) Specifies the name of the ServiceBus Namespace.

* `resource_group_name` - (Required) Specifies the name of the Resource Group where the ServiceBus Namespace exists.

## Attributes Reference

* `location` - The location of the Resource Group in which the ServiceBus Namespace exists.

* `sku` - The Tier used for the ServiceBus Namespace.

* `capacity` - The capacity of the ServiceBus Namespace.

* `tags` - A mapping of tags assigned to the resource.

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`.

0 comments on commit 41bafb3

Please sign in to comment.