diff --git a/azurerm/data_source_servicebus_namespace.go b/azurerm/data_source_servicebus_namespace.go new file mode 100644 index 000000000000..bf8aa1f34820 --- /dev/null +++ b/azurerm/data_source_servicebus_namespace.go @@ -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 +} diff --git a/azurerm/data_source_servicebus_namespace_test.go b/azurerm/data_source_servicebus_namespace_test.go new file mode 100644 index 000000000000..fd30925aa6a3 --- /dev/null +++ b/azurerm/data_source_servicebus_namespace_test.go @@ -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) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index e367edd0b8f4..5af4de2cbf80 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -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(), diff --git a/website/azurerm.erb b/website/azurerm.erb index f4069b9f21c8..0091c754521b 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -235,6 +235,10 @@ azurerm_scheduler_job_collection +