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

New Data Source: azurerm_servicebus_namespace #2841

Merged
merged 2 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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": {
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
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
34 changes: 34 additions & 0 deletions website/docs/d/servicebus_namespace.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
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.
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved