From 014cd6c24d891e361015a3ed1a349df242e24d70 Mon Sep 17 00:00:00 2001 From: Jon Povey Date: Fri, 20 Apr 2018 17:20:42 +0100 Subject: [PATCH] New Resource: `azurerm_servicebus_rule` (#1124) * Add AzureRM Servicebus rule module, corresponding tests and documentation * Minor fixes to the servicebus rule * Explicitly define the service bus rule in each test * Set correlation_filter to conflict with sql_filter to prevent both from being set at the same time * Raise an error when creating a correlation_filter with no properties set in correlation block * Always return the correlation_filter but only include properties if set * Rename service_bus_rule to service_bus_subscription_rule to relfect azure portal and dependency on service_bus_subscription * Update documentation for service bus subscription rule * Service bus subscription rule: Add validation to rule name, cannot be more than 50 chars * Fixing the descriptions for the name fields --- azurerm/config.go | 13 +- azurerm/provider.go | 1 + ...source_arm_servicebus_subscription_rule.go | 376 ++++++++++++++++++ ...e_arm_servicebus_subscription_rule_test.go | 369 +++++++++++++++++ website/azurerm.erb | 6 +- ...servicebus_subscription_rule.html.markdown | 181 +++++++++ 6 files changed, 941 insertions(+), 5 deletions(-) create mode 100644 azurerm/resource_arm_servicebus_subscription_rule.go create mode 100644 azurerm/resource_arm_servicebus_subscription_rule_test.go create mode 100644 website/docs/r/servicebus_subscription_rule.html.markdown diff --git a/azurerm/config.go b/azurerm/config.go index 2249cbaf9658..06c646de77c9 100644 --- a/azurerm/config.go +++ b/azurerm/config.go @@ -182,10 +182,11 @@ type ArmClient struct { searchServicesClient search.ServicesClient // ServiceBus - serviceBusQueuesClient servicebus.QueuesClient - serviceBusNamespacesClient servicebus.NamespacesClient - serviceBusTopicsClient servicebus.TopicsClient - serviceBusSubscriptionsClient servicebus.SubscriptionsClient + serviceBusQueuesClient servicebus.QueuesClient + serviceBusNamespacesClient servicebus.NamespacesClient + serviceBusTopicsClient servicebus.TopicsClient + serviceBusSubscriptionsClient servicebus.SubscriptionsClient + serviceBusSubscriptionRulesClient servicebus.RulesClient //Scheduler schedulerJobCollectionsClient scheduler.JobCollectionsClient @@ -859,6 +860,10 @@ func (c *ArmClient) registerServiceBusClients(endpoint, subscriptionId string, a subscriptionsClient := servicebus.NewSubscriptionsClientWithBaseURI(endpoint, subscriptionId) c.configureClient(&subscriptionsClient.Client, auth) c.serviceBusSubscriptionsClient = subscriptionsClient + + subscriptionRulesClient := servicebus.NewRulesClientWithBaseURI(endpoint, subscriptionId) + c.configureClient(&subscriptionRulesClient.Client, auth) + c.serviceBusSubscriptionRulesClient = subscriptionRulesClient } func (c *ArmClient) registerSchedulerClients(endpoint, subscriptionId string, auth autorest.Authorizer) { diff --git a/azurerm/provider.go b/azurerm/provider.go index ac2afb83958b..13388cb2e9a4 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -192,6 +192,7 @@ func Provider() terraform.ResourceProvider { "azurerm_servicebus_namespace": resourceArmServiceBusNamespace(), "azurerm_servicebus_queue": resourceArmServiceBusQueue(), "azurerm_servicebus_subscription": resourceArmServiceBusSubscription(), + "azurerm_servicebus_subscription_rule": resourceArmServiceBusSubscriptionRule(), "azurerm_servicebus_topic": resourceArmServiceBusTopic(), "azurerm_servicebus_topic_authorization_rule": resourceArmServiceBusTopicAuthorizationRule(), "azurerm_snapshot": resourceArmSnapshot(), diff --git a/azurerm/resource_arm_servicebus_subscription_rule.go b/azurerm/resource_arm_servicebus_subscription_rule.go new file mode 100644 index 000000000000..fa3218f392bb --- /dev/null +++ b/azurerm/resource_arm_servicebus_subscription_rule.go @@ -0,0 +1,376 @@ +package azurerm + +import ( + "fmt" + "log" + + "github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2017-04-01/servicebus" + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func resourceArmServiceBusSubscriptionRule() *schema.Resource { + return &schema.Resource{ + Create: resourceArmServiceBusSubscriptionRuleCreate, + Read: resourceArmServiceBusSubscriptionRuleRead, + Update: resourceArmServiceBusSubscriptionRuleCreate, + Delete: resourceArmServiceBusSubscriptionRuleDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringLenBetween(1, 50), + }, + + "namespace_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "topic_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "subscription_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "filter_type": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{ + string(servicebus.FilterTypeSQLFilter), + string(servicebus.FilterTypeCorrelationFilter), + }, true), + DiffSuppressFunc: ignoreCaseDiffSuppressFunc, + }, + + "resource_group_name": resourceGroupNameSchema(), + + "sql_filter": { + Type: schema.TypeString, + Optional: true, + }, + + "correlation_filter": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + ConflictsWith: []string{"sql_filter"}, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "correlation_id": { + Type: schema.TypeString, + Optional: true, + }, + "message_id": { + Type: schema.TypeString, + Optional: true, + }, + "to": { + Type: schema.TypeString, + Optional: true, + }, + "reply_to": { + Type: schema.TypeString, + Optional: true, + }, + "label": { + Type: schema.TypeString, + Optional: true, + }, + "session_id": { + Type: schema.TypeString, + Optional: true, + }, + "reply_to_session_id": { + Type: schema.TypeString, + Optional: true, + }, + "content_type": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + }, + + "action": { + Type: schema.TypeString, + Optional: true, + }, + }, + } +} + +func resourceArmServiceBusSubscriptionRuleCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).serviceBusSubscriptionRulesClient + ctx := meta.(*ArmClient).StopContext + log.Printf("[INFO] preparing arguments for Azure ARM Service Bus Subscription Rule creation.") + + name := d.Get("name").(string) + topicName := d.Get("topic_name").(string) + subscriptionName := d.Get("subscription_name").(string) + namespaceName := d.Get("namespace_name").(string) + resourceGroup := d.Get("resource_group_name").(string) + + rule, err := buildAzureRmServiceBusSubscriptionRule(name, d) + if err != nil { + return err + } + + err = validateArmServiceBusSubscriptionRule(name, rule) + if err != nil { + return err + } + + _, err = client.CreateOrUpdate(ctx, resourceGroup, namespaceName, topicName, subscriptionName, name, rule) + if err != nil { + return err + } + + read, err := client.Get(ctx, resourceGroup, namespaceName, topicName, subscriptionName, name) + if err != nil { + return err + } + if read.ID == nil { + return fmt.Errorf("Cannot read Service Bus Subscription Rule %s (resource group %s) ID", name, resourceGroup) + } + + d.SetId(*read.ID) + + return resourceArmServiceBusSubscriptionRuleRead(d, meta) +} + +func resourceArmServiceBusSubscriptionRuleRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).serviceBusSubscriptionRulesClient + ctx := meta.(*ArmClient).StopContext + + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return err + } + + resourceGroup := id.ResourceGroup + namespaceName := id.Path["namespaces"] + topicName := id.Path["topics"] + subscriptionName := id.Path["subscriptions"] + name := id.Path["rules"] + + resp, err := client.Get(ctx, resourceGroup, namespaceName, topicName, subscriptionName, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + d.SetId("") + return nil + } + return fmt.Errorf("Error making Read request on Azure Service Bus Subscription Rule %q (Resource Group %q): %+v", name, resourceGroup, err) + } + + d.Set("name", resp.Name) + d.Set("resource_group_name", resourceGroup) + d.Set("namespace_name", namespaceName) + d.Set("topic_name", topicName) + d.Set("subscription_name", subscriptionName) + + if properties := resp.Ruleproperties; properties != nil { + d.Set("filter_type", properties.FilterType) + + if properties.Action != nil { + d.Set("action", properties.Action.SQLExpression) + } + + if properties.SQLFilter != nil { + d.Set("sql_filter", properties.SQLFilter.SQLExpression) + } + + if err := d.Set("correlation_filter", flattenAzureRmServiceBusCorrelationFilter(properties.CorrelationFilter)); err != nil { + return fmt.Errorf("Error setting `correlation_filter` on Azure Service Bus Subscription Rule (%q): %+v", name, err) + } + } + + return nil +} + +func resourceArmServiceBusSubscriptionRuleDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).serviceBusSubscriptionRulesClient + ctx := meta.(*ArmClient).StopContext + + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return err + } + resourceGroup := id.ResourceGroup + namespaceName := id.Path["namespaces"] + topicName := id.Path["topics"] + subscriptionName := id.Path["subscriptions"] + name := id.Path["rules"] + + _, err = client.Delete(ctx, resourceGroup, namespaceName, topicName, subscriptionName, name) + + return err +} + +func buildAzureRmServiceBusSubscriptionRule(name string, d *schema.ResourceData) (rule servicebus.Rule, err error) { + correlationfilter, err := getAzureRmServiceBusCorrelationFilter(name, d) + if err != nil { + return + } + + filterType := d.Get("filter_type").(string) + rule = servicebus.Rule{ + Ruleproperties: &servicebus.Ruleproperties{ + FilterType: servicebus.FilterType(filterType), + Action: getAzureRmServiceBusAction(d), + SQLFilter: getAzureRmServiceBusSQLFilter(d), + CorrelationFilter: correlationfilter, + }, + } + + return +} + +func getAzureRmServiceBusAction(d *schema.ResourceData) *servicebus.Action { + if action := d.Get("action").(string); action != "" { + serviceBusAction := servicebus.Action{ + SQLExpression: &action, + } + return &serviceBusAction + } + return nil +} + +func getAzureRmServiceBusSQLFilter(d *schema.ResourceData) *servicebus.SQLFilter { + if sqlFilter := d.Get("sql_filter").(string); sqlFilter != "" { + serviceBusSQLFilter := servicebus.SQLFilter{ + SQLExpression: &sqlFilter, + } + return &serviceBusSQLFilter + } + return nil +} + +func getAzureRmServiceBusCorrelationFilter(name string, d *schema.ResourceData) (*servicebus.CorrelationFilter, error) { + if config := d.Get("correlation_filter").([]interface{}); len(config) > 0 { + + if config[0] == nil { + err := fmt.Errorf("Cannot create Service Bus Subscription Rule (%s). At least one property must be set in the `correlation_filter` block", name) + return nil, err + } + + filter := config[0].(map[string]interface{}) + correlationFilter := servicebus.CorrelationFilter{} + + if correlationID := filter["correlation_id"].(string); correlationID != "" { + correlationFilter.CorrelationID = &correlationID + } + + if messageID := filter["message_id"].(string); messageID != "" { + correlationFilter.MessageID = &messageID + } + + if to := filter["to"].(string); to != "" { + correlationFilter.To = &to + } + + if replyTo := filter["reply_to"].(string); replyTo != "" { + correlationFilter.ReplyTo = &replyTo + } + + if label := filter["label"].(string); label != "" { + correlationFilter.Label = &label + } + + if sessionID := filter["session_id"].(string); sessionID != "" { + correlationFilter.SessionID = &sessionID + } + + if replyToSessionID := filter["reply_to_session_id"].(string); replyToSessionID != "" { + correlationFilter.ReplyToSessionID = &replyToSessionID + } + + if contentType := filter["content_type"].(string); contentType != "" { + correlationFilter.ContentType = &contentType + } + + return &correlationFilter, nil + } + + return nil, nil +} + +func flattenAzureRmServiceBusCorrelationFilter(f *servicebus.CorrelationFilter) []interface{} { + filters := make([]interface{}, 0, 1) + if f == nil { + return filters + } + + filter := make(map[string]interface{}) + + if f.CorrelationID != nil { + filter["correlation_id"] = *f.CorrelationID + } + + if f.MessageID != nil { + filter["message_id"] = *f.MessageID + } + + if f.To != nil { + filter["to"] = *f.To + } + + if f.ReplyTo != nil { + filter["reply_to"] = *f.ReplyTo + } + + if f.Label != nil { + filter["label"] = *f.Label + } + + if f.SessionID != nil { + filter["session_id"] = *f.SessionID + } + + if f.ReplyToSessionID != nil { + filter["reply_to_session_id"] = *f.ReplyToSessionID + } + + if f.ContentType != nil { + filter["content_type"] = *f.ContentType + } + + filters = append(filters, filter) + return filters +} + +func validateArmServiceBusSubscriptionRule(name string, rule servicebus.Rule) error { + if rule.Ruleproperties.FilterType == servicebus.FilterTypeSQLFilter { + if rule.Ruleproperties.SQLFilter == nil { + return fmt.Errorf("Cannot create Service Bus Subscription Rule (%s). 'sql_filter' must be specified when 'filter_type' is set to 'SqlFilter'", name) + } + if rule.Ruleproperties.CorrelationFilter != nil { + return fmt.Errorf("Service Bus Subscription Rule (%s) does not support `correlation_filter` when 'filter_type' is set to 'SqlFilter'", name) + } + } + + if rule.Ruleproperties.FilterType == servicebus.FilterTypeCorrelationFilter { + if rule.Ruleproperties.CorrelationFilter == nil { + return fmt.Errorf("Cannot create Service Bus Subscription Rule (%s). 'correlation_filter' must be specified when 'filter_type' is set to 'CorrelationFilter'", name) + } + if rule.Ruleproperties.SQLFilter != nil { + return fmt.Errorf("Service Bus Subscription Rule (%s) does not support `sql_filter` when 'filter_type' is set to 'CorrelationFilter'", name) + } + } + + return nil +} diff --git a/azurerm/resource_arm_servicebus_subscription_rule_test.go b/azurerm/resource_arm_servicebus_subscription_rule_test.go new file mode 100644 index 000000000000..f39c0e75b98c --- /dev/null +++ b/azurerm/resource_arm_servicebus_subscription_rule_test.go @@ -0,0 +1,369 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func TestAccAzureRMServiceBusSubscriptionRule_basicSqlFilter(t *testing.T) { + ri := acctest.RandInt() + config := testAccAzureRMServiceBusSubscriptionRule_basicSqlFilter(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMServiceBusTopicDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusSubscriptionRuleExists(), + ), + }, + }, + }) +} + +func TestAccAzureRMServiceBusSubscriptionRule_basicCorrelationFilter(t *testing.T) { + ri := acctest.RandInt() + config := testAccAzureRMServiceBusSubscriptionRule_basicCorrelationFilter(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMServiceBusTopicDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusSubscriptionRuleExists(), + ), + }, + }, + }) +} + +func TestAccAzureRMServiceBusSubscriptionRule_sqlFilterWithAction(t *testing.T) { + ri := acctest.RandInt() + config := testAccAzureRMServiceBusSubscriptionRule_sqlFilterWithAction(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMServiceBusTopicDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusSubscriptionRuleExists(), + ), + }, + }, + }) +} + +func TestAccAzureRMServiceBusSubscriptionRule_correlationFilterWithAction(t *testing.T) { + ri := acctest.RandInt() + config := testAccAzureRMServiceBusSubscriptionRule_correlationFilterWithAction(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMServiceBusTopicDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusSubscriptionRuleExists(), + ), + }, + }, + }) +} + +func TestAccAzureRMServiceBusSubscriptionRule_sqlFilterUpdated(t *testing.T) { + ri := acctest.RandInt() + location := testLocation() + config := testAccAzureRMServiceBusSubscriptionRule_basicSqlFilter(ri, location) + updatedConfig := testAccAzureRMServiceBusSubscriptionRule_basicSqlFilterUpdated(ri, location) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMServiceBusTopicDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusSubscriptionRuleExists(), + resource.TestCheckResourceAttr(resourceName, "sql_filter", "2=2"), + ), + }, + { + Config: updatedConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusSubscriptionRuleExists(), + resource.TestCheckResourceAttr(resourceName, "sql_filter", "3=3"), + ), + }, + }, + }) +} + +func TestAccAzureRMServiceBusSubscriptionRule_correlationFilterUpdated(t *testing.T) { + ri := acctest.RandInt() + location := testLocation() + config := testAccAzureRMServiceBusSubscriptionRule_correlationFilter(ri, location) + updatedConfig := testAccAzureRMServiceBusSubscriptionRule_correlationFilterUpdated(ri, location) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMServiceBusTopicDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusSubscriptionRuleExists(), + resource.TestCheckResourceAttr(resourceName, "correlation_filter.0.message_id", "test_message_id"), + resource.TestCheckResourceAttr(resourceName, "correlation_filter.0.reply_to", ""), + ), + }, + { + Config: updatedConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusSubscriptionRuleExists(), + resource.TestCheckResourceAttr(resourceName, "correlation_filter.0.message_id", "test_message_id_updated"), + resource.TestCheckResourceAttr(resourceName, "correlation_filter.0.reply_to", "test_reply_to_added"), + ), + }, + }, + }) +} + +func TestAccAzureRMServiceBusSubscriptionRule_updateSqlFilterToCorrelationFilter(t *testing.T) { + ri := acctest.RandInt() + location := testLocation() + config := testAccAzureRMServiceBusSubscriptionRule_basicSqlFilter(ri, location) + updatedConfig := testAccAzureRMServiceBusSubscriptionRule_basicCorrelationFilter(ri, location) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMServiceBusTopicDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusSubscriptionRuleExists(), + ), + }, + { + Config: updatedConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusSubscriptionRuleExists(), + ), + }, + }, + }) +} + +func testCheckAzureRMServiceBusSubscriptionRuleExists() resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Not found: %s", resourceName) + } + + ruleName := rs.Primary.Attributes["name"] + subscriptionName := rs.Primary.Attributes["subscription_name"] + topicName := rs.Primary.Attributes["topic_name"] + namespaceName := rs.Primary.Attributes["namespace_name"] + resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] + if !hasResourceGroup { + return fmt.Errorf("Bad: no resource group found in state for Subscription Rule: %q", ruleName) + } + + client := testAccProvider.Meta().(*ArmClient).serviceBusSubscriptionRulesClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + resp, err := client.Get(ctx, resourceGroup, namespaceName, topicName, subscriptionName, ruleName) + if err != nil { + return fmt.Errorf("Bad: Get on serviceBusRulesClient: %+v", err) + } + + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Bad: Subscription Rule %q (resource group: %q) does not exist", ruleName, resourceGroup) + } + + return nil + } +} + +func testAccAzureRMServiceBusSubscriptionRule_basicSqlFilter(rInt int, location string) string { + sqlFilter := ` + resource "azurerm_servicebus_subscription_rule" "test" { + name = "acctestservicebusrule-%d" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + topic_name = "${azurerm_servicebus_topic.test.name}" + subscription_name = "${azurerm_servicebus_subscription.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" + filter_type = "SqlFilter" + sql_filter = "2=2" + } + ` + + return fmt.Sprintf(testAccAzureRMServiceBusSubscriptionRule_tfTemplate, rInt, location, rInt, rInt, rInt, fmt.Sprintf(sqlFilter, rInt)) +} + +func testAccAzureRMServiceBusSubscriptionRule_basicSqlFilterUpdated(rInt int, location string) string { + sqlFilter := ` + resource "azurerm_servicebus_subscription_rule" "test" { + name = "acctestservicebusrule-%d" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + topic_name = "${azurerm_servicebus_topic.test.name}" + subscription_name = "${azurerm_servicebus_subscription.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" + filter_type = "SqlFilter" + sql_filter = "3=3" + } + ` + return fmt.Sprintf(testAccAzureRMServiceBusSubscriptionRule_tfTemplate, rInt, location, rInt, rInt, rInt, fmt.Sprintf(sqlFilter, rInt)) +} + +func testAccAzureRMServiceBusSubscriptionRule_sqlFilterWithAction(rInt int, location string) string { + sqlFilter := ` + resource "azurerm_servicebus_subscription_rule" "test" { + name = "acctestservicebusrule-%d" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + topic_name = "${azurerm_servicebus_topic.test.name}" + subscription_name = "${azurerm_servicebus_subscription.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" + filter_type = "SqlFilter" + sql_filter = "2=2" + action = "SET Test='true'" + } + ` + return fmt.Sprintf(testAccAzureRMServiceBusSubscriptionRule_tfTemplate, rInt, location, rInt, rInt, rInt, fmt.Sprintf(sqlFilter, rInt)) +} + +func testAccAzureRMServiceBusSubscriptionRule_basicCorrelationFilter(rInt int, location string) string { + correlationFilter := ` + resource "azurerm_servicebus_subscription_rule" "test" { + name = "acctestservicebusrule-%d" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + topic_name = "${azurerm_servicebus_topic.test.name}" + subscription_name = "${azurerm_servicebus_subscription.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" + filter_type = "CorrelationFilter" + + correlation_filter = { + correlation_id = "test_correlation_id" + message_id = "test_message_id" + to = "test_to" + reply_to = "test_reply_to" + label = "test_label" + session_id = "test_session_id" + reply_to_session_id = "test_reply_to_session_id" + content_type = "test_content_type" + } + } + ` + return fmt.Sprintf(testAccAzureRMServiceBusSubscriptionRule_tfTemplate, rInt, location, rInt, rInt, rInt, fmt.Sprintf(correlationFilter, rInt)) +} + +func testAccAzureRMServiceBusSubscriptionRule_correlationFilter(rInt int, location string) string { + correlationFilter := ` + resource "azurerm_servicebus_subscription_rule" "test" { + name = "acctestservicebusrule-%d" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + topic_name = "${azurerm_servicebus_topic.test.name}" + subscription_name = "${azurerm_servicebus_subscription.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" + filter_type = "CorrelationFilter" + + correlation_filter = { + correlation_id = "test_correlation_id" + message_id = "test_message_id" + } + } + ` + return fmt.Sprintf(testAccAzureRMServiceBusSubscriptionRule_tfTemplate, rInt, location, rInt, rInt, rInt, fmt.Sprintf(correlationFilter, rInt)) +} + +func testAccAzureRMServiceBusSubscriptionRule_correlationFilterUpdated(rInt int, location string) string { + correlationFilter := ` + resource "azurerm_servicebus_subscription_rule" "test" { + name = "acctestservicebusrule-%d" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + topic_name = "${azurerm_servicebus_topic.test.name}" + subscription_name = "${azurerm_servicebus_subscription.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" + filter_type = "CorrelationFilter" + + correlation_filter = { + correlation_id = "test_correlation_id" + message_id = "test_message_id_updated" + reply_to = "test_reply_to_added" + } + } + ` + return fmt.Sprintf(testAccAzureRMServiceBusSubscriptionRule_tfTemplate, rInt, location, rInt, rInt, rInt, fmt.Sprintf(correlationFilter, rInt)) +} + +func testAccAzureRMServiceBusSubscriptionRule_correlationFilterWithAction(rInt int, location string) string { + correlationFilter := ` + resource "azurerm_servicebus_subscription_rule" "test" { + name = "acctestservicebusrule-%d" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + topic_name = "${azurerm_servicebus_topic.test.name}" + subscription_name = "${azurerm_servicebus_subscription.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" + action = "SET Test='true'" + filter_type = "CorrelationFilter" + + correlation_filter = { + correlation_id = "test_correlation_id" + message_id = "test_message_id" + } + } + ` + return fmt.Sprintf(testAccAzureRMServiceBusSubscriptionRule_tfTemplate, rInt, location, rInt, rInt, rInt, fmt.Sprintf(correlationFilter, rInt)) +} + +const resourceName = "azurerm_servicebus_subscription_rule.test" + +const testAccAzureRMServiceBusSubscriptionRule_tfTemplate = ` +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 +} + +%s +` diff --git a/website/azurerm.erb b/website/azurerm.erb index a97dd24179b0..f52739eeb4cf 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -511,12 +511,16 @@ > azurerm_servicebus_queue - + > azurerm_servicebus_subscription + > + azurerm_servicebus_subscription_rule + + > azurerm_servicebus_topic diff --git a/website/docs/r/servicebus_subscription_rule.html.markdown b/website/docs/r/servicebus_subscription_rule.html.markdown new file mode 100644 index 000000000000..c3f6627912be --- /dev/null +++ b/website/docs/r/servicebus_subscription_rule.html.markdown @@ -0,0 +1,181 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_servicebus_subscription_rule" +sidebar_current: "docs-azurerm-resource-servicebus-subscription-rule" +description: |- + Create a ServiceBus Subscription Rule. +--- + +# azurerm_servicebus_subscription_rule + +Create a ServiceBus Subscription Rule. + +## Example Usage (SQL Filter) + +```hcl +variable "location" { + description = "Azure datacenter to deploy to." + default = "West US" +} + +variable "servicebus_name" { + description = "Input your unique Azure service bus name" +} + +resource "azurerm_resource_group" "test" { + name = "terraform-servicebus" + location = "${var.location}" +} + +resource "azurerm_servicebus_namespace" "test" { + name = "${var.servicebus_name}" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + sku = "standard" + + tags { + source = "terraform" + } +} + +resource "azurerm_servicebus_topic" "test" { + name = "testTopic" + resource_group_name = "${azurerm_resource_group.test.name}" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + + enable_partitioning = true +} + +resource "azurerm_servicebus_subscription" "test" { + name = "testSubscription" + resource_group_name = "${azurerm_resource_group.test.name}" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + topic_name = "${azurerm_servicebus_topic.test.name}" + max_delivery_count = 1 +} + +resource "azurerm_servicebus_subscription_rule" "test" { + name = "testSubscriptionRule" + resource_group_name = "${azurerm_resource_group.test.name}" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + topic_name = "${azurerm_servicebus_topic.test.name}" + subscription_name = "${azurerm_servicebus_subscription.test.name}" + filter_type = "SqlFilter" + sql_filter = "color = 'red'" +} +``` + +## Example Usage (Correlation Filter) + +```hcl +variable "location" { + description = "Azure datacenter to deploy to." + default = "West US" +} + +variable "servicebus_name" { + description = "Input your unique Azure service bus name" +} + +resource "azurerm_resource_group" "test" { + name = "terraform-servicebus" + location = "${var.location}" +} + +resource "azurerm_servicebus_namespace" "test" { + name = "${var.servicebus_name}" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + sku = "standard" + + tags { + source = "terraform" + } +} + +resource "azurerm_servicebus_topic" "test" { + name = "testTopic" + resource_group_name = "${azurerm_resource_group.test.name}" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + + enable_partitioning = true +} + +resource "azurerm_servicebus_subscription" "test" { + name = "testSubscription" + resource_group_name = "${azurerm_resource_group.test.name}" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + topic_name = "${azurerm_servicebus_topic.test.name}" + max_delivery_count = 1 +} + +resource "azurerm_servicebus_subscription_rule" "test" { + name = "testSubscriptionRule" + resource_group_name = "${azurerm_resource_group.test.name}" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + topic_name = "${azurerm_servicebus_topic.test.name}" + subscription_name = "${azurerm_servicebus_subscription.test.name}" + filter_type = "CorrelationFilter" + correlation_filter = { + correlation_id = "high" + label = "red" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Specifies the name of the ServiceBus Subscription Rule. Changing this forces a new resource to be created. + +* `namespace_name` - (Required) The name of the ServiceBus Namespace in which the ServiceBus Topic exists. Changing this forces a new resource to be created. + +* `topic_name` - (Required) The name of the ServiceBus Topic in which the ServiceBus Subscription exists. Changing this forces a new resource to be created. + +* `subscription_name` - (Required) The name of the ServiceBus Subscription in which this Rule should be created. Changing this forces a new resource to be created. + +* `resource_group_name` - (Required) The name of the resource group in the ServiceBus Namespace exists. Changing this forces a new resource to be created. + +* `filter_type` - (Required) Type of filter to be applied to a BrokeredMessage. Possible values are `SqlFilter` and `CorrelationFilter`. + +* `sql_filter` - (Optional) Represents a filter written in SQL language-based syntax that to be evaluated against a BrokeredMessage. Required when `filter_type` is set to `SqlFilter`. + +* `correlation_filter` - (Optional) A `correlation_filter` block as documented below to be evaluated against a BrokeredMessage. Required when `filter_type` is set to `CorrelationFilter`. + +* `action` - (Optional) Represents set of actions written in SQL language-based syntax that is performed against a BrokeredMessage. + +`correlation_filter` supports the following: + +* `content_type` - (Optional) Content type of the message. + +* `correlation_id` - (Optional) Identifier of the correlation. + +* `label` - (Optional) Application specific label. + +* `message_id` - (Optional) Identifier of the message. + +* `reply_to` - (Optional) Address of the queue to reply to. + +* `reply_to_session_id` - (Optional) Session identifier to reply to. + +* `session_id` - (Optional) Session identifier. + +* `to` - (Optional) Address to send to. + +~> **NOTE:** When creating a subscription rule of type `CorrelationFilter` at least one property must be set in the `correlation_filter` block. + + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ServiceBus Subscription Rule ID. + +## Import + +Service Bus Subscription Rule can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_servicebus_subscription.test /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/microsoft.servicebus/namespaces/sbns1/topics/sntopic1/subscriptions/sbsub1/rules/sbrule1 +```