-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9272 from ritesh-modi/servicebussubscriptiondatas…
…ource added new data source azurerm_servicebus_subscription
- Loading branch information
Showing
5 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
azurerm/internal/services/servicebus/data_source_servicebus_subscription.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
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 dataSourceArmServiceBusSubscription() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmServiceBusSubscriptionRead, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(), | ||
|
||
"namespace_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.ServiceBusNamespaceName, | ||
}, | ||
|
||
"topic_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: azure.ValidateServiceBusTopicName(), | ||
}, | ||
|
||
"auto_delete_on_idle": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"default_message_ttl": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"lock_duration": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"dead_lettering_on_message_expiration": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"dead_lettering_on_filter_evaluation_error": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"enable_batched_operations": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"max_delivery_count": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"requires_session": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"forward_to": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"forward_dead_lettered_messages_to": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmServiceBusSubscriptionRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).ServiceBus.SubscriptionsClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
namespaceName := d.Get("namespace_name").(string) | ||
topicName := d.Get("topic_name").(string) | ||
|
||
existing, err := client.Get(ctx, resourceGroup, namespaceName, topicName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("ServiceBus Subscription %q was not found in Namespace %q in Resource Group %q", name, namespaceName, resourceGroup) | ||
} | ||
|
||
return fmt.Errorf("Error retrieving ServiceBus Subscription %q (Namespace %q, Resource Group %q): %s", name, namespaceName, resourceGroup, err) | ||
} | ||
|
||
d.SetId(*existing.ID) | ||
|
||
if props := existing.SBSubscriptionProperties; props != nil { | ||
d.Set("auto_delete_on_idle", props.AutoDeleteOnIdle) | ||
d.Set("default_message_ttl", props.DefaultMessageTimeToLive) | ||
d.Set("lock_duration", props.LockDuration) | ||
d.Set("dead_lettering_on_message_expiration", props.DeadLetteringOnMessageExpiration) | ||
d.Set("dead_lettering_on_filter_evaluation_error", props.DeadLetteringOnFilterEvaluationExceptions) | ||
d.Set("enable_batched_operations", props.EnableBatchedOperations) | ||
d.Set("requires_session", props.RequiresSession) | ||
d.Set("forward_dead_lettered_messages_to", props.ForwardDeadLetteredMessagesTo) | ||
d.Set("forward_to", props.ForwardTo) | ||
|
||
maxDeliveryCount := 0 | ||
if props.MaxDeliveryCount != nil { | ||
maxDeliveryCount = int(*props.MaxDeliveryCount) | ||
} | ||
|
||
d.Set("max_delivery_count", maxDeliveryCount) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
azurerm/internal/services/servicebus/tests/data_source_servicebus_subscription_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" | ||
) | ||
|
||
func TestAccDataSourceAzureRMServiceBusSubscription_basic(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_servicebus_subscription", "test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.PreCheck(t) }, | ||
Providers: acceptance.SupportedProviders, | ||
CheckDestroy: testCheckAzureRMServiceBusSubscriptionDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAzureRMServiceBusSubscription_basic(data), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMServiceBusSubscriptionExists(data.ResourceName), | ||
resource.TestCheckResourceAttrSet(data.ResourceName, "max_delivery_count"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAzureRMServiceBusSubscription_basic(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_servicebus_namespace" "test" { | ||
name = "acctestservicebusnamespace-%d" | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
sku = "Standard" | ||
} | ||
resource "azurerm_servicebus_topic" "test" { | ||
name = "acctestservicebustopic-%d" | ||
namespace_name = "${azurerm_servicebus_namespace.test.name}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
} | ||
resource "azurerm_servicebus_subscription" "test" { | ||
name = "acctestservicebussubscription-%d" | ||
namespace_name = "${azurerm_servicebus_namespace.test.name}" | ||
topic_name = "${azurerm_servicebus_topic.test.name}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
max_delivery_count = 10 | ||
} | ||
data "azurerm_servicebus_subscription" "test" { | ||
name = azurerm_servicebus_subscription.test.name | ||
resource_group_name = azurerm_resource_group.test.name | ||
namespace_name = azurerm_servicebus_namespace.test.name | ||
topic_name = azurerm_servicebus_topic.test.name | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
subcategory: "Messaging" | ||
layout: "azurerm" | ||
page_title: "Azure Resource Manager: azurerm_servicebus_subscription" | ||
description: |- | ||
Gets information about an existing ServiceBus Subscription. | ||
--- | ||
|
||
# Data Source: azurerm_servicebus_subscription | ||
|
||
Use this data source to access information about an existing ServiceBus Subscription. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "azurerm_servicebus_subscription" "example" { | ||
name = "examplesubscription" | ||
resource_group_name = "exampleresources" | ||
namespace_name = "examplenamespace" | ||
topic_name = "exampletopic" | ||
} | ||
output "servicebus_subscription" { | ||
value = data.azurerm_servicebus_namespace.example | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `name` - Specifies the name of the ServiceBus Subscription. | ||
|
||
* `resource_group_name` - Specifies the name of the Resource Group where the ServiceBus Namespace exists. | ||
|
||
* `namespace_name` - The name of the ServiceBus Namespace. | ||
|
||
* `topic_name` - The name of the ServiceBus Topic. | ||
|
||
## Attributes Reference | ||
|
||
* `max_delivery_count` - The maximum number of deliveries. | ||
|
||
* `auto_delete_on_idle` - The idle interval after which the topic is automatically deleted. | ||
|
||
* `default_message_ttl` - The Default message timespan to live. This is the duration after which the message expires, starting from when the message is sent to Service Bus. This is the default value used when TimeToLive is not set on a message itself. | ||
|
||
* `lock_duration` - The lock duration for the subscription. | ||
|
||
* `dead_lettering_on_message_expiration` - Does the Service Bus Subscription have dead letter support when a message expires? | ||
|
||
* `dead_lettering_on_filter_evaluation_error` - Does the ServiceBus Subscription have dead letter support on filter evaluation exceptions? | ||
|
||
* `enable_batched_operations` - Are batched operations enabled on this ServiceBus Subscription? | ||
|
||
* `requires_session` - Whether or not this ServiceBus Subscription supports session. | ||
|
||
* `forward_to` - The name of a ServiceBus Queue or ServiceBus Topic where messages are automatically forwarded. | ||
|
||
* `forward_dead_lettered_messages_to` - The name of a Queue or Topic to automatically forward Dead Letter messages to. | ||
|
||
## 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 Subscription. |