github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_servicebus_topic.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/arm/servicebus"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceArmServiceBusTopic() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceArmServiceBusTopicCreate,
    15  		Read:   resourceArmServiceBusTopicRead,
    16  		Update: resourceArmServiceBusTopicCreate,
    17  		Delete: resourceArmServiceBusTopicDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": {
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"namespace_name": {
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  				ForceNew: true,
    33  			},
    34  
    35  			"location": locationSchema(),
    36  
    37  			"resource_group_name": {
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  				ForceNew: true,
    41  			},
    42  
    43  			"auto_delete_on_idle": {
    44  				Type:     schema.TypeString,
    45  				Optional: true,
    46  				Computed: true,
    47  			},
    48  
    49  			"default_message_ttl": {
    50  				Type:     schema.TypeString,
    51  				Optional: true,
    52  				Computed: true,
    53  			},
    54  
    55  			"duplicate_detection_history_time_window": {
    56  				Type:     schema.TypeString,
    57  				Optional: true,
    58  				Computed: true,
    59  			},
    60  
    61  			"enable_batched_operations": {
    62  				Type:     schema.TypeBool,
    63  				Optional: true,
    64  			},
    65  
    66  			"enable_express": {
    67  				Type:     schema.TypeBool,
    68  				Optional: true,
    69  			},
    70  
    71  			"enable_filtering_messages_before_publishing": {
    72  				Type:     schema.TypeBool,
    73  				Optional: true,
    74  			},
    75  
    76  			"enable_partitioning": {
    77  				Type:     schema.TypeBool,
    78  				Optional: true,
    79  				ForceNew: true,
    80  			},
    81  
    82  			"max_size_in_megabytes": {
    83  				Type:     schema.TypeInt,
    84  				Optional: true,
    85  				Computed: true,
    86  			},
    87  
    88  			"requires_duplicate_detection": {
    89  				Type:     schema.TypeBool,
    90  				Optional: true,
    91  				ForceNew: true,
    92  			},
    93  
    94  			"support_ordering": {
    95  				Type:     schema.TypeBool,
    96  				Optional: true,
    97  			},
    98  		},
    99  	}
   100  }
   101  
   102  func resourceArmServiceBusTopicCreate(d *schema.ResourceData, meta interface{}) error {
   103  	client := meta.(*ArmClient).serviceBusTopicsClient
   104  	log.Printf("[INFO] preparing arguments for Azure ARM ServiceBus Topic creation.")
   105  
   106  	name := d.Get("name").(string)
   107  	namespaceName := d.Get("namespace_name").(string)
   108  	location := d.Get("location").(string)
   109  	resGroup := d.Get("resource_group_name").(string)
   110  
   111  	parameters := servicebus.TopicCreateOrUpdateParameters{
   112  		Name:            &name,
   113  		Location:        &location,
   114  		TopicProperties: &servicebus.TopicProperties{},
   115  	}
   116  
   117  	if autoDeleteOnIdle := d.Get("auto_delete_on_idle").(string); autoDeleteOnIdle != "" {
   118  		parameters.TopicProperties.AutoDeleteOnIdle = &autoDeleteOnIdle
   119  	}
   120  
   121  	if defaultTTL := d.Get("default_message_ttl").(string); defaultTTL != "" {
   122  		parameters.TopicProperties.DefaultMessageTimeToLive = &defaultTTL
   123  	}
   124  
   125  	if duplicateWindow := d.Get("duplicate_detection_history_time_window").(string); duplicateWindow != "" {
   126  		parameters.TopicProperties.DuplicateDetectionHistoryTimeWindow = &duplicateWindow
   127  	}
   128  
   129  	enableBatchedOps := d.Get("enable_batched_operations").(bool)
   130  	enableExpress := d.Get("enable_express").(bool)
   131  	enableFiltering := d.Get("enable_filtering_messages_before_publishing").(bool)
   132  	enablePartitioning := d.Get("enable_partitioning").(bool)
   133  	maxSize := int64(d.Get("max_size_in_megabytes").(int))
   134  	requiresDuplicateDetection := d.Get("requires_duplicate_detection").(bool)
   135  	supportOrdering := d.Get("support_ordering").(bool)
   136  
   137  	parameters.TopicProperties.EnableBatchedOperations = &enableBatchedOps
   138  	parameters.TopicProperties.EnableExpress = &enableExpress
   139  	parameters.TopicProperties.FilteringMessagesBeforePublishing = &enableFiltering
   140  	parameters.TopicProperties.EnablePartitioning = &enablePartitioning
   141  	parameters.TopicProperties.MaxSizeInMegabytes = &maxSize
   142  	parameters.TopicProperties.RequiresDuplicateDetection = &requiresDuplicateDetection
   143  	parameters.TopicProperties.SupportOrdering = &supportOrdering
   144  
   145  	_, err := client.CreateOrUpdate(resGroup, namespaceName, name, parameters)
   146  	if err != nil {
   147  		return err
   148  	}
   149  
   150  	read, err := client.Get(resGroup, namespaceName, name)
   151  	if err != nil {
   152  		return err
   153  	}
   154  	if read.ID == nil {
   155  		return fmt.Errorf("Cannot read ServiceBus Topic %s (resource group %s) ID", name, resGroup)
   156  	}
   157  
   158  	d.SetId(*read.ID)
   159  
   160  	return resourceArmServiceBusTopicRead(d, meta)
   161  }
   162  
   163  func resourceArmServiceBusTopicRead(d *schema.ResourceData, meta interface{}) error {
   164  	client := meta.(*ArmClient).serviceBusTopicsClient
   165  
   166  	id, err := parseAzureResourceID(d.Id())
   167  	if err != nil {
   168  		return err
   169  	}
   170  	resGroup := id.ResourceGroup
   171  	namespaceName := id.Path["namespaces"]
   172  	name := id.Path["topics"]
   173  
   174  	resp, err := client.Get(resGroup, namespaceName, name)
   175  	if err != nil {
   176  		return fmt.Errorf("Error making Read request on Azure ServiceBus Topic %s: %+v", name, err)
   177  	}
   178  	if resp.StatusCode == http.StatusNotFound {
   179  		d.SetId("")
   180  		return nil
   181  	}
   182  
   183  	d.Set("name", resp.Name)
   184  	d.Set("resource_group_name", resGroup)
   185  	d.Set("namespace_name", namespaceName)
   186  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   187  
   188  	props := resp.TopicProperties
   189  	d.Set("auto_delete_on_idle", props.AutoDeleteOnIdle)
   190  	d.Set("default_message_ttl", props.DefaultMessageTimeToLive)
   191  
   192  	if props.DuplicateDetectionHistoryTimeWindow != nil && *props.DuplicateDetectionHistoryTimeWindow != "" {
   193  		d.Set("duplicate_detection_history_time_window", props.DuplicateDetectionHistoryTimeWindow)
   194  	}
   195  
   196  	d.Set("enable_batched_operations", props.EnableBatchedOperations)
   197  	d.Set("enable_express", props.EnableExpress)
   198  	d.Set("enable_filtering_messages_before_publishing", props.FilteringMessagesBeforePublishing)
   199  	d.Set("enable_partitioning", props.EnablePartitioning)
   200  	d.Set("requires_duplicate_detection", props.RequiresDuplicateDetection)
   201  	d.Set("support_ordering", props.SupportOrdering)
   202  
   203  	maxSize := int(*props.MaxSizeInMegabytes)
   204  
   205  	// if the topic is in a premium namespace and partitioning is enabled then the
   206  	// max size returned by the API will be 16 times greater than the value set
   207  	if *props.EnablePartitioning {
   208  		namespace, err := meta.(*ArmClient).serviceBusNamespacesClient.Get(resGroup, namespaceName)
   209  		if err != nil {
   210  			return err
   211  		}
   212  
   213  		if namespace.Sku.Name != servicebus.Premium {
   214  			const partitionCount = 16
   215  			maxSize = int(*props.MaxSizeInMegabytes / partitionCount)
   216  		}
   217  	}
   218  
   219  	d.Set("max_size_in_megabytes", maxSize)
   220  
   221  	return nil
   222  }
   223  
   224  func resourceArmServiceBusTopicDelete(d *schema.ResourceData, meta interface{}) error {
   225  	client := meta.(*ArmClient).serviceBusTopicsClient
   226  
   227  	id, err := parseAzureResourceID(d.Id())
   228  	if err != nil {
   229  		return err
   230  	}
   231  	resGroup := id.ResourceGroup
   232  	namespaceName := id.Path["namespaces"]
   233  	name := id.Path["topics"]
   234  
   235  	_, err = client.Delete(resGroup, namespaceName, name)
   236  
   237  	return err
   238  }