2016-09-29 19:07:25 +02:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
|
2016-11-29 16:54:00 +01:00
|
|
|
"location": locationSchema(),
|
2016-09-29 19:07:25 +02:00
|
|
|
|
|
|
|
"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,
|
2016-10-25 17:34:08 +02:00
|
|
|
ForceNew: true,
|
2016-09-29 19:07:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"max_size_in_megabytes": {
|
2016-12-08 17:06:27 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2016-09-29 19:07:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"requires_duplicate_detection": {
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2016-10-25 17:34:08 +02:00
|
|
|
ForceNew: true,
|
2016-09-29 19:07:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"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{
|
2016-12-06 09:39:47 +01:00
|
|
|
Name: &name,
|
|
|
|
Location: &location,
|
|
|
|
TopicProperties: &servicebus.TopicProperties{},
|
2016-09-29 19:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if autoDeleteOnIdle := d.Get("auto_delete_on_idle").(string); autoDeleteOnIdle != "" {
|
2016-12-06 09:39:47 +01:00
|
|
|
parameters.TopicProperties.AutoDeleteOnIdle = &autoDeleteOnIdle
|
2016-09-29 19:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if defaultTTL := d.Get("default_message_ttl").(string); defaultTTL != "" {
|
2016-12-06 09:39:47 +01:00
|
|
|
parameters.TopicProperties.DefaultMessageTimeToLive = &defaultTTL
|
2016-09-29 19:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if duplicateWindow := d.Get("duplicate_detection_history_time_window").(string); duplicateWindow != "" {
|
2016-12-06 09:39:47 +01:00
|
|
|
parameters.TopicProperties.DuplicateDetectionHistoryTimeWindow = &duplicateWindow
|
2016-09-29 19:07:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
parameters.TopicProperties.EnableBatchedOperations = &enableBatchedOps
|
|
|
|
parameters.TopicProperties.EnableExpress = &enableExpress
|
|
|
|
parameters.TopicProperties.FilteringMessagesBeforePublishing = &enableFiltering
|
|
|
|
parameters.TopicProperties.EnablePartitioning = &enablePartitioning
|
|
|
|
parameters.TopicProperties.MaxSizeInMegabytes = &maxSize
|
|
|
|
parameters.TopicProperties.RequiresDuplicateDetection = &requiresDuplicateDetection
|
|
|
|
parameters.TopicProperties.SupportOrdering = &supportOrdering
|
2016-09-29 19:07:25 +02:00
|
|
|
|
|
|
|
_, 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))
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
props := resp.TopicProperties
|
2016-09-29 19:07:25 +02:00
|
|
|
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("requires_duplicate_detection", props.RequiresDuplicateDetection)
|
|
|
|
d.Set("support_ordering", props.SupportOrdering)
|
|
|
|
|
2016-12-08 17:06:27 +01:00
|
|
|
maxSize := int(*props.MaxSizeInMegabytes)
|
|
|
|
|
|
|
|
// if the topic is in a premium namespace and partitioning is enabled then the
|
|
|
|
// max size returned by the API will be 16 times greater than the value set
|
2016-10-25 17:34:08 +02:00
|
|
|
if *props.EnablePartitioning {
|
2016-12-08 17:06:27 +01:00
|
|
|
namespace, err := meta.(*ArmClient).serviceBusNamespacesClient.Get(resGroup, namespaceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if namespace.Sku.Name != servicebus.Premium {
|
|
|
|
const partitionCount = 16
|
|
|
|
maxSize = int(*props.MaxSizeInMegabytes / partitionCount)
|
|
|
|
}
|
2016-10-25 17:34:08 +02:00
|
|
|
}
|
|
|
|
|
2016-12-08 17:06:27 +01:00
|
|
|
d.Set("max_size_in_megabytes", maxSize)
|
|
|
|
|
2016-09-29 19:07:25 +02:00
|
|
|
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
|
|
|
|
}
|