diff --git a/builtin/providers/azurerm/config.go b/builtin/providers/azurerm/config.go index e740ea13e..0fccb590b 100644 --- a/builtin/providers/azurerm/config.go +++ b/builtin/providers/azurerm/config.go @@ -69,6 +69,7 @@ type ArmClient struct { trafficManagerEndpointsClient trafficmanager.EndpointsClient serviceBusNamespacesClient servicebus.NamespacesClient + serviceBusTopicsClient servicebus.TopicsClient } func withRequestLogging() autorest.SendDecorator { @@ -352,6 +353,12 @@ func (c *Config) getArmClient() (*ArmClient, error) { sbnc.Sender = autorest.CreateSender(withRequestLogging()) client.serviceBusNamespacesClient = sbnc + sbtc := servicebus.NewTopicsClient(c.SubscriptionID) + setUserAgent(&sbtc.Client) + sbtc.Authorizer = spt + sbtc.Sender = autorest.CreateSender(withRequestLogging()) + client.serviceBusTopicsClient = sbtc + return &client, nil } diff --git a/builtin/providers/azurerm/import_arm_servicebus_topic_test.go b/builtin/providers/azurerm/import_arm_servicebus_topic_test.go new file mode 100644 index 000000000..b5a933c65 --- /dev/null +++ b/builtin/providers/azurerm/import_arm_servicebus_topic_test.go @@ -0,0 +1,33 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAzureRMServiceBusTopic_importBasic(t *testing.T) { + resourceName := "azurerm_servicebus_topic.test" + + ri := acctest.RandInt() + config := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMServiceBusTopicDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: config, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/builtin/providers/azurerm/provider.go b/builtin/providers/azurerm/provider.go index d0317c979..4a3b3ac35 100644 --- a/builtin/providers/azurerm/provider.go +++ b/builtin/providers/azurerm/provider.go @@ -57,6 +57,7 @@ func Provider() terraform.ResourceProvider { "azurerm_route": resourceArmRoute(), "azurerm_route_table": resourceArmRouteTable(), "azurerm_servicebus_namespace": resourceArmServiceBusNamespace(), + "azurerm_servicebus_topic": resourceArmServiceBusTopic(), "azurerm_storage_account": resourceArmStorageAccount(), "azurerm_storage_blob": resourceArmStorageBlob(), "azurerm_storage_container": resourceArmStorageContainer(), diff --git a/builtin/providers/azurerm/resource_arm_servicebus_topic.go b/builtin/providers/azurerm/resource_arm_servicebus_topic.go new file mode 100644 index 000000000..9d2eb6229 --- /dev/null +++ b/builtin/providers/azurerm/resource_arm_servicebus_topic.go @@ -0,0 +1,223 @@ +package azurerm + +import ( + "fmt" + "log" + "net/http" + + "github.com/Azure/azure-sdk-for-go/arm/servicebus" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceArmServiceBusTopic() *schema.Resource { + return &schema.Resource{ + Create: resourceArmServiceBusTopicCreate, + Read: resourceArmServiceBusTopicRead, + Update: resourceArmServiceBusTopicCreate, + Delete: resourceArmServiceBusTopicDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "namespace_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "location": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + StateFunc: azureRMNormalizeLocation, + }, + + "resource_group_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "auto_delete_on_idle": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + + "default_message_ttl": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + + "duplicate_detection_history_time_window": { + Type: schema.TypeString, + Optional: true, + }, + + "enable_batched_operations": { + Type: schema.TypeBool, + Optional: true, + }, + + "enable_express": { + Type: schema.TypeBool, + Optional: true, + }, + + "enable_filtering_messages_before_publishing": { + Type: schema.TypeBool, + Optional: true, + }, + + "enable_partitioning": { + Type: schema.TypeBool, + Optional: true, + }, + + "max_size_in_megabytes": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + + "requires_duplicate_detection": { + Type: schema.TypeBool, + Optional: true, + }, + + "support_ordering": { + Type: schema.TypeBool, + Optional: true, + }, + }, + } +} + +func resourceArmServiceBusTopicCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).serviceBusTopicsClient + log.Printf("[INFO] preparing arguments for Azure ARM ServiceBus Topic creation.") + + name := d.Get("name").(string) + namespaceName := d.Get("namespace_name").(string) + location := d.Get("location").(string) + resGroup := d.Get("resource_group_name").(string) + + parameters := servicebus.TopicCreateOrUpdateParameters{ + Name: &name, + Location: &location, + Properties: &servicebus.TopicProperties{}, + } + + if autoDeleteOnIdle := d.Get("auto_delete_on_idle").(string); autoDeleteOnIdle != "" { + parameters.Properties.AutoDeleteOnIdle = &autoDeleteOnIdle + } + + if defaultTTL := d.Get("default_message_ttl").(string); defaultTTL != "" { + parameters.Properties.DefaultMessageTimeToLive = &defaultTTL + } + + if duplicateWindow := d.Get("duplicate_detection_history_time_window").(string); duplicateWindow != "" { + parameters.Properties.DuplicateDetectionHistoryTimeWindow = &duplicateWindow + } + + enableBatchedOps := d.Get("enable_batched_operations").(bool) + enableExpress := d.Get("enable_express").(bool) + enableFiltering := d.Get("enable_filtering_messages_before_publishing").(bool) + enablePartitioning := d.Get("enable_partitioning").(bool) + maxSize := int64(d.Get("max_size_in_megabytes").(int)) + requiresDuplicateDetection := d.Get("requires_duplicate_detection").(bool) + supportOrdering := d.Get("support_ordering").(bool) + + parameters.Properties.EnableBatchedOperations = &enableBatchedOps + parameters.Properties.EnableExpress = &enableExpress + parameters.Properties.FilteringMessagesBeforePublishing = &enableFiltering + parameters.Properties.EnablePartitioning = &enablePartitioning + parameters.Properties.MaxSizeInMegabytes = &maxSize + parameters.Properties.RequiresDuplicateDetection = &requiresDuplicateDetection + parameters.Properties.SupportOrdering = &supportOrdering + + _, err := client.CreateOrUpdate(resGroup, namespaceName, name, parameters) + if err != nil { + return err + } + + read, err := client.Get(resGroup, namespaceName, name) + if err != nil { + return err + } + if read.ID == nil { + return fmt.Errorf("Cannot read ServiceBus Topic %s (resource group %s) ID", name, resGroup) + } + + d.SetId(*read.ID) + + return resourceArmServiceBusTopicRead(d, meta) +} + +func resourceArmServiceBusTopicRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).serviceBusTopicsClient + + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return err + } + resGroup := id.ResourceGroup + namespaceName := id.Path["namespaces"] + name := id.Path["topics"] + + resp, err := client.Get(resGroup, namespaceName, name) + if err != nil { + return fmt.Errorf("Error making Read request on Azure ServiceBus Topic %s: %s", name, err) + } + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return nil + } + + d.Set("name", resp.Name) + d.Set("resource_group_name", resGroup) + d.Set("namespace_name", namespaceName) + d.Set("location", azureRMNormalizeLocation(*resp.Location)) + + props := resp.Properties + d.Set("auto_delete_on_idle", props.AutoDeleteOnIdle) + d.Set("default_message_ttl", props.DefaultMessageTimeToLive) + + if props.DuplicateDetectionHistoryTimeWindow != nil && *props.DuplicateDetectionHistoryTimeWindow != "" { + d.Set("duplicate_detection_history_time_window", props.DuplicateDetectionHistoryTimeWindow) + } + + d.Set("enable_batched_operations", props.EnableBatchedOperations) + d.Set("enable_express", props.EnableExpress) + d.Set("enable_filtering_messages_before_publishing", props.FilteringMessagesBeforePublishing) + d.Set("enable_partitioning", props.EnablePartitioning) + d.Set("max_size_in_megabytes", int(*props.MaxSizeInMegabytes)) + d.Set("requires_duplicate_detection", props.RequiresDuplicateDetection) + d.Set("support_ordering", props.SupportOrdering) + + return nil +} + +func resourceArmServiceBusTopicDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).serviceBusTopicsClient + + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return err + } + resGroup := id.ResourceGroup + namespaceName := id.Path["namespaces"] + name := id.Path["topics"] + + _, err = client.Delete(resGroup, namespaceName, name) + + return err +} diff --git a/builtin/providers/azurerm/resource_arm_servicebus_topic_test.go b/builtin/providers/azurerm/resource_arm_servicebus_topic_test.go new file mode 100644 index 000000000..9b3a5f3b9 --- /dev/null +++ b/builtin/providers/azurerm/resource_arm_servicebus_topic_test.go @@ -0,0 +1,161 @@ +package azurerm + +import ( + "fmt" + "net/http" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAzureRMServiceBusTopic_basic(t *testing.T) { + ri := acctest.RandInt() + config := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMServiceBusTopicDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusTopicExists("azurerm_servicebus_topic.test"), + ), + }, + }, + }) +} + +func TestAccAzureRMServiceBusTopic_update(t *testing.T) { + ri := acctest.RandInt() + preConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri) + postConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_update, ri, ri, ri) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMServiceBusTopicDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: preConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMServiceBusTopicExists("azurerm_servicebus_topic.test"), + ), + }, + resource.TestStep{ + Config: postConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "azurerm_servicebus_topic.test", "enable_batched_operations", "true"), + resource.TestCheckResourceAttr( + "azurerm_servicebus_topic.test", "enable_express", "true"), + ), + }, + }, + }) +} + +func testCheckAzureRMServiceBusTopicDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*ArmClient).serviceBusTopicsClient + + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_servicebus_topic" { + continue + } + + name := rs.Primary.Attributes["name"] + namespaceName := rs.Primary.Attributes["namespace_name"] + resourceGroup := rs.Primary.Attributes["resource_group_name"] + + resp, err := client.Get(resourceGroup, namespaceName, name) + if err != nil { + if resp.StatusCode == http.StatusNotFound { + return nil + } + return err + } + + if resp.StatusCode != http.StatusNotFound { + return fmt.Errorf("ServiceBus Topic still exists:\n%#v", resp.Properties) + } + } + + return nil +} + +func testCheckAzureRMServiceBusTopicExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + // Ensure we have enough information in state to look up in API + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + topicName := rs.Primary.Attributes["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 topic: %s", topicName) + } + + client := testAccProvider.Meta().(*ArmClient).serviceBusTopicsClient + + resp, err := client.Get(resourceGroup, namespaceName, topicName) + if err != nil { + return fmt.Errorf("Bad: Get on serviceBusTopicsClient: %s", err) + } + + if resp.StatusCode == http.StatusNotFound { + return fmt.Errorf("Bad: Topic %q (resource group: %q) does not exist", namespaceName, resourceGroup) + } + + return nil + } +} + +var testAccAzureRMServiceBusTopic_basic = ` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "West US" +} + +resource "azurerm_servicebus_namespace" "test" { + name = "acctestservicebusnamespace-%d" + location = "West US" + resource_group_name = "${azurerm_resource_group.test.name}" + sku = "standard" +} + +resource "azurerm_servicebus_topic" "test" { + name = "acctestservicebustopic-%d" + location = "West US" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +` + +var testAccAzureRMServiceBusTopic_update = ` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "West US" +} + +resource "azurerm_servicebus_namespace" "test" { + name = "acctestservicebusnamespace-%d" + location = "West US" + resource_group_name = "${azurerm_resource_group.test.name}" + sku = "standard" +} + +resource "azurerm_servicebus_topic" "test" { + name = "acctestservicebustopic-%d" + location = "West US" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" + enable_batched_operations = true + enable_express = true +} +` diff --git a/website/source/docs/providers/azurerm/r/servicebus_topic.html.markdown b/website/source/docs/providers/azurerm/r/servicebus_topic.html.markdown new file mode 100644 index 000000000..2a4ed5bb1 --- /dev/null +++ b/website/source/docs/providers/azurerm/r/servicebus_topic.html.markdown @@ -0,0 +1,104 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_servicebus_topic" +sidebar_current: "docs-azurerm-resource-servicebus-topic" +description: |- + Create a ServiceBus Topic. +--- + +# azurerm\_servicebus\_topic + +Create a ServiceBus Topic. + +**Note** Topics can only be created in Namespaces with an SKU or `standard` or +higher. + +## Example Usage + +``` +resource "azurerm_resource_group" "test" { + name = "resourceGroup1" + location = "West US" +} + +resource "azurerm_servicebus_namespace" "test" { + name = "acceptanceTestServiceBusNamespace" + location = "West US" + resource_group_name = "${azurerm_resource_group.test.name}" + sku = "standard" + + tags { + environment = "Production" + } +} + +resource "azurerm_servicebus_topic" "test" { + name = "testTopic" + location = "West US" + resource_group_name = "${azurerm_resource_group.test.name}" + namespace_name = "${azurerm_servicebus_namespace.test.name}" + + enable_partitioning = true +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Specifies the name of the ServiceBus Topic resource. Changing this forces a + new resource to be created. + +* `namespace_name` - (Required) The name of the ServiceBus Namespace to create + this topic in. Changing this forces a new resource to be created. + +* `location` - (Required) Specifies the supported Azure location where the resource exists. + Changing this forces a new resource to be created. + +* `resource_group_name` - (Required) The name of the resource group in which to + create the namespace. Changing this forces a new resource to be created. + +* `auto_delete_on_idle` - (Optional) The idle interval after which the + Topic is automatically deleted, minimum of 5 minutes. Provided in the [TimeSpan](#timespan-format) + format. + +* `default_message_ttl` - (Optional) The TTL of messages sent to this topic if no + TTL value is set on the message itself. Provided in the [TimeSpan](#timespan-format) + format. + +* `duplicate_detection_history_time_window` - (Optional) The duration during which + duplicates can be detected. Provided in the [TimeSpan](#timespan-format) format. + +* `enable_batched_operations` - (Optional) Boolean flag which controls if server-side + batched operations are enabled. Defaults to false. + +* `enable_express` - (Optional) Boolean flag which controls whether Express Entities + are enabled. An express topic holds a message in memory temporarily before writing + it to persistent storage. Defaults to false. + +* `enable_filtering_messages_before_publishing` - (Optional) Boolean flag which + controls whether messages should be filtered before publishing. Defaults to + false. + +* `enable_partitioning` - (Optional) Boolean flag which controls whether to enable + the topic to be partitioned across multiple message brokers. Defaults to false. + +* `max_size_in_megabytes` - (Optional) Integer value which controls the size of + memory allocated for the topic. + +* `requires_duplicate_detection` - (Optional) Boolean flag which controls whether + the Topic requires duplicate detection. Defaults to false. + +* `support_ordering` - (Optional) Boolean flag which controls whether the Topic + supports ordering. Defaults to false. + +### TimeSpan Format + +Some arguments for this resource are required in the TimeSpan format which is +used to represent a lengh of time. The supported format is documented [here](https://msdn.microsoft.com/en-us/library/se73z7b9(v=vs.110).aspx#Anchor_2) + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ServiceBus Topic ID. diff --git a/website/source/layouts/azurerm.erb b/website/source/layouts/azurerm.erb index 1f6840aeb..308011c83 100644 --- a/website/source/layouts/azurerm.erb +++ b/website/source/layouts/azurerm.erb @@ -144,6 +144,10 @@ > azurerm_servicebus_namespace + + > + azurerm_servicebus_topic +