diff --git a/azurerm/internal/services/servicebus/data_source_servicebus_queue_authorization_rule.go b/azurerm/internal/services/servicebus/data_source_servicebus_queue_authorization_rule.go new file mode 100644 index 000000000000..ce2b1476a871 --- /dev/null +++ b/azurerm/internal/services/servicebus/data_source_servicebus_queue_authorization_rule.go @@ -0,0 +1,132 @@ +package servicebus + +import ( + "fmt" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmServiceBusQueueAuthorizationRule() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmServiceBusQueueAuthorizationRuleRead, + + Timeouts: &schema.ResourceTimeout{ + Read: schema.DefaultTimeout(5 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: azure.ValidateServiceBusAuthorizationRuleName(), + }, + + "namespace_name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validate.ServiceBusNamespaceName, + }, + + "queue_name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: azure.ValidateServiceBusQueueName(), + }, + + "resource_group_name": azure.SchemaResourceGroupName(), + + "listen": { + Type: schema.TypeBool, + Computed: true, + }, + + "send": { + Type: schema.TypeBool, + Computed: true, + }, + + "manage": { + Type: schema.TypeBool, + Computed: true, + }, + + "primary_key": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + + "primary_connection_string": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + + "secondary_key": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + + "secondary_connection_string": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + }, + }, + } +} + +func dataSourceArmServiceBusQueueAuthorizationRuleRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).ServiceBus.QueuesClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + name := d.Get("name").(string) + namespaceName := d.Get("namespace_name").(string) + queueName := d.Get("queue_name").(string) + resourceGroup := d.Get("resource_group_name").(string) + + resp, err := client.GetAuthorizationRule(ctx, resourceGroup, namespaceName, queueName, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("ServiceBus Queue Authorization Rule %q (Resource Group %q / Namespace Name %q) was not found", name, resourceGroup, namespaceName) + } + return fmt.Errorf("Error making Read request on Azure ServiceBus Queue Authorization Rule %s: %+v", name, err) + } + + d.Set("name", name) + d.Set("queue_name", queueName) + d.Set("namespace_name", namespaceName) + d.Set("resource_group_name", resourceGroup) + + if properties := resp.SBAuthorizationRuleProperties; properties != nil { + listen, send, manage := azure.FlattenServiceBusAuthorizationRuleRights(properties.Rights) + d.Set("listen", listen) + d.Set("send", send) + d.Set("manage", manage) + } + + if resp.ID == nil || *resp.ID == "" { + return fmt.Errorf("API returned a nil/empty id for ServiceBus Queue Authorization Rule %q (Resource Group %q): %+v", name, resourceGroup, err) + } + d.SetId(*resp.ID) + + keysResp, err := client.ListKeys(ctx, resourceGroup, namespaceName, queueName, name) + if err != nil { + return fmt.Errorf("Error making Read request on Azure ServiceBus Queue Authorization Rule List Keys %s: %+v", name, err) + } + + d.Set("primary_key", keysResp.PrimaryKey) + d.Set("primary_connection_string", keysResp.PrimaryConnectionString) + d.Set("secondary_key", keysResp.SecondaryKey) + d.Set("secondary_connection_string", keysResp.SecondaryConnectionString) + + return nil +} diff --git a/azurerm/internal/services/servicebus/registration.go b/azurerm/internal/services/servicebus/registration.go index 71be9e539770..743a47706862 100644 --- a/azurerm/internal/services/servicebus/registration.go +++ b/azurerm/internal/services/servicebus/registration.go @@ -25,6 +25,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource { "azurerm_servicebus_namespace": dataSourceArmServiceBusNamespace(), "azurerm_servicebus_namespace_authorization_rule": dataSourceArmServiceBusNamespaceAuthorizationRule(), "azurerm_servicebus_topic_authorization_rule": dataSourceArmServiceBusTopicAuthorizationRule(), + "azurerm_servicebus_queue_authorization_rule": dataSourceArmServiceBusQueueAuthorizationRule(), } } diff --git a/azurerm/internal/services/servicebus/tests/data_source_servicebus_queue_authorization_rule_test.go b/azurerm/internal/services/servicebus/tests/data_source_servicebus_queue_authorization_rule_test.go new file mode 100644 index 000000000000..30c2b4384b3e --- /dev/null +++ b/azurerm/internal/services/servicebus/tests/data_source_servicebus_queue_authorization_rule_test.go @@ -0,0 +1,48 @@ +package tests + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" +) + +func TestAccDataSourceAzureRMServiceBusQueueAuthorizationRule_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_servicebus_queue_authorization_rule", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMServiceBusQueueAuthorizationRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAzureRMServiceBusQueueAuthorizationRule_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusQueueAuthorizationRuleExists(data.ResourceName), + resource.TestCheckResourceAttrSet(data.ResourceName, "id"), + resource.TestCheckResourceAttrSet(data.ResourceName, "name"), + resource.TestCheckResourceAttrSet(data.ResourceName, "namespace_name"), + resource.TestCheckResourceAttrSet(data.ResourceName, "primary_key"), + resource.TestCheckResourceAttrSet(data.ResourceName, "secondary_key"), + resource.TestCheckResourceAttrSet(data.ResourceName, "primary_connection_string"), + resource.TestCheckResourceAttrSet(data.ResourceName, "secondary_connection_string"), + ), + }, + }, + }) +} + +func testAccDataSourceAzureRMServiceBusQueueAuthorizationRule_basic(data acceptance.TestData) string { + template := testAccAzureRMServiceBusQueueAuthorizationRule_base(data, true, true, true) + return fmt.Sprintf(` +%s + +data "azurerm_servicebus_queue_authorization_rule" "test" { + name = azurerm_servicebus_queue_authorization_rule.test.name + namespace_name = azurerm_servicebus_queue_authorization_rule.test.namespace_name + resource_group_name = azurerm_servicebus_queue_authorization_rule.test.resource_group_name + queue_name = azurerm_servicebus_queue_authorization_rule.test.queue_name +} +`, template) +} diff --git a/website/docs/d/servicebus_queue_authorization_rule.html.markdown b/website/docs/d/servicebus_queue_authorization_rule.html.markdown new file mode 100644 index 000000000000..7547e3c1e10f --- /dev/null +++ b/website/docs/d/servicebus_queue_authorization_rule.html.markdown @@ -0,0 +1,58 @@ +--- +subcategory: "Messaging" +layout: "azurerm" +page_title: "Azure Resource Manager: Data Source: azurerm_servicebus_queue_authorization_rule" +description: |- + Gets information about an existing ServiceBus Queue Authorisation Rule within a ServiceBus Queue. +--- + +# Data Source: azurerm_servicebus_queue_authorization_rule + +Use this data source to access information about an existing ServiceBus Queue Authorisation Rule within a ServiceBus Queue. + +## Example Usage + +```hcl +data "azurerm_servicebus_queue_authorization_rule" "example" { + name = "example-tfex_name" + resource_group_name = "example-resources" + queue_name = "example-servicebus_queue" + namespace_name = "example-namespace" +} + +output "id" { + value = data.azurerm_servicebus_queue_authorization_rule.example.id +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `name` - (Required) The name of this ServiceBus Queue Authorisation Rule. + +* `namespace_name` - (Required) The name of the ServiceBus Namespace. + +* `queue_name` - (Required) The name of the ServiceBus Queue. + +* `resource_group_name` - (Required) The name of the Resource Group where the ServiceBus Queue Authorisation Rule exists. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the ServiceBus Queue Authorisation Rule. + +* `primary_key` - The Primary Key for the ServiceBus Queue authorization Rule. + +* `primary_connection_string` - The Primary Connection String for the ServiceBus Queue authorization Rule. + +* `secondary_key` - The Secondary Key for the ServiceBus Queue authorization Rule. + +* `secondary_connection_string` - The Secondary Connection String for the ServiceBus Queue authorization Rule. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `read` - (Defaults to 5 minutes) Used when retrieving the ServiceBus Queue Authorisation Rule. \ No newline at end of file