github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/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  			},
    59  
    60  			"enable_batched_operations": {
    61  				Type:     schema.TypeBool,
    62  				Optional: true,
    63  			},
    64  
    65  			"enable_express": {
    66  				Type:     schema.TypeBool,
    67  				Optional: true,
    68  			},
    69  
    70  			"enable_filtering_messages_before_publishing": {
    71  				Type:     schema.TypeBool,
    72  				Optional: true,
    73  			},
    74  
    75  			"enable_partitioning": {
    76  				Type:     schema.TypeBool,
    77  				Optional: true,
    78  				ForceNew: true,
    79  			},
    80  
    81  			"max_size_in_megabytes": {
    82  				Type:     schema.TypeInt,
    83  				Optional: true,
    84  				Computed: true,
    85  			},
    86  
    87  			"requires_duplicate_detection": {
    88  				Type:     schema.TypeBool,
    89  				Optional: true,
    90  				ForceNew: true,
    91  			},
    92  
    93  			"support_ordering": {
    94  				Type:     schema.TypeBool,
    95  				Optional: true,
    96  			},
    97  		},
    98  	}
    99  }
   100  
   101  func resourceArmServiceBusTopicCreate(d *schema.ResourceData, meta interface{}) error {
   102  	client := meta.(*ArmClient).serviceBusTopicsClient
   103  	log.Printf("[INFO] preparing arguments for Azure ARM ServiceBus Topic creation.")
   104  
   105  	name := d.Get("name").(string)
   106  	namespaceName := d.Get("namespace_name").(string)
   107  	location := d.Get("location").(string)
   108  	resGroup := d.Get("resource_group_name").(string)
   109  
   110  	parameters := servicebus.TopicCreateOrUpdateParameters{
   111  		Name:            &name,
   112  		Location:        &location,
   113  		TopicProperties: &servicebus.TopicProperties{},
   114  	}
   115  
   116  	if autoDeleteOnIdle := d.Get("auto_delete_on_idle").(string); autoDeleteOnIdle != "" {
   117  		parameters.TopicProperties.AutoDeleteOnIdle = &autoDeleteOnIdle
   118  	}
   119  
   120  	if defaultTTL := d.Get("default_message_ttl").(string); defaultTTL != "" {
   121  		parameters.TopicProperties.DefaultMessageTimeToLive = &defaultTTL
   122  	}
   123  
   124  	if duplicateWindow := d.Get("duplicate_detection_history_time_window").(string); duplicateWindow != "" {
   125  		parameters.TopicProperties.DuplicateDetectionHistoryTimeWindow = &duplicateWindow
   126  	}
   127  
   128  	enableBatchedOps := d.Get("enable_batched_operations").(bool)
   129  	enableExpress := d.Get("enable_express").(bool)
   130  	enableFiltering := d.Get("enable_filtering_messages_before_publishing").(bool)
   131  	enablePartitioning := d.Get("enable_partitioning").(bool)
   132  	maxSize := int64(d.Get("max_size_in_megabytes").(int))
   133  	requiresDuplicateDetection := d.Get("requires_duplicate_detection").(bool)
   134  	supportOrdering := d.Get("support_ordering").(bool)
   135  
   136  	parameters.TopicProperties.EnableBatchedOperations = &enableBatchedOps
   137  	parameters.TopicProperties.EnableExpress = &enableExpress
   138  	parameters.TopicProperties.FilteringMessagesBeforePublishing = &enableFiltering
   139  	parameters.TopicProperties.EnablePartitioning = &enablePartitioning
   140  	parameters.TopicProperties.MaxSizeInMegabytes = &maxSize
   141  	parameters.TopicProperties.RequiresDuplicateDetection = &requiresDuplicateDetection
   142  	parameters.TopicProperties.SupportOrdering = &supportOrdering
   143  
   144  	_, err := client.CreateOrUpdate(resGroup, namespaceName, name, parameters)
   145  	if err != nil {
   146  		return err
   147  	}
   148  
   149  	read, err := client.Get(resGroup, namespaceName, name)
   150  	if err != nil {
   151  		return err
   152  	}
   153  	if read.ID == nil {
   154  		return fmt.Errorf("Cannot read ServiceBus Topic %s (resource group %s) ID", name, resGroup)
   155  	}
   156  
   157  	d.SetId(*read.ID)
   158  
   159  	return resourceArmServiceBusTopicRead(d, meta)
   160  }
   161  
   162  func resourceArmServiceBusTopicRead(d *schema.ResourceData, meta interface{}) error {
   163  	client := meta.(*ArmClient).serviceBusTopicsClient
   164  
   165  	id, err := parseAzureResourceID(d.Id())
   166  	if err != nil {
   167  		return err
   168  	}
   169  	resGroup := id.ResourceGroup
   170  	namespaceName := id.Path["namespaces"]
   171  	name := id.Path["topics"]
   172  
   173  	resp, err := client.Get(resGroup, namespaceName, name)
   174  	if err != nil {
   175  		return fmt.Errorf("Error making Read request on Azure ServiceBus Topic %s: %s", name, err)
   176  	}
   177  	if resp.StatusCode == http.StatusNotFound {
   178  		d.SetId("")
   179  		return nil
   180  	}
   181  
   182  	d.Set("name", resp.Name)
   183  	d.Set("resource_group_name", resGroup)
   184  	d.Set("namespace_name", namespaceName)
   185  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   186  
   187  	props := resp.TopicProperties
   188  	d.Set("auto_delete_on_idle", props.AutoDeleteOnIdle)
   189  	d.Set("default_message_ttl", props.DefaultMessageTimeToLive)
   190  
   191  	if props.DuplicateDetectionHistoryTimeWindow != nil && *props.DuplicateDetectionHistoryTimeWindow != "" {
   192  		d.Set("duplicate_detection_history_time_window", props.DuplicateDetectionHistoryTimeWindow)
   193  	}
   194  
   195  	d.Set("enable_batched_operations", props.EnableBatchedOperations)
   196  	d.Set("enable_express", props.EnableExpress)
   197  	d.Set("enable_filtering_messages_before_publishing", props.FilteringMessagesBeforePublishing)
   198  	d.Set("enable_partitioning", props.EnablePartitioning)
   199  	d.Set("requires_duplicate_detection", props.RequiresDuplicateDetection)
   200  	d.Set("support_ordering", props.SupportOrdering)
   201  
   202  	maxSize := int(*props.MaxSizeInMegabytes)
   203  
   204  	// if the topic is in a premium namespace and partitioning is enabled then the
   205  	// max size returned by the API will be 16 times greater than the value set
   206  	if *props.EnablePartitioning {
   207  		namespace, err := meta.(*ArmClient).serviceBusNamespacesClient.Get(resGroup, namespaceName)
   208  		if err != nil {
   209  			return err
   210  		}
   211  
   212  		if namespace.Sku.Name != servicebus.Premium {
   213  			const partitionCount = 16
   214  			maxSize = int(*props.MaxSizeInMegabytes / partitionCount)
   215  		}
   216  	}
   217  
   218  	d.Set("max_size_in_megabytes", maxSize)
   219  
   220  	return nil
   221  }
   222  
   223  func resourceArmServiceBusTopicDelete(d *schema.ResourceData, meta interface{}) error {
   224  	client := meta.(*ArmClient).serviceBusTopicsClient
   225  
   226  	id, err := parseAzureResourceID(d.Id())
   227  	if err != nil {
   228  		return err
   229  	}
   230  	resGroup := id.ResourceGroup
   231  	namespaceName := id.Path["namespaces"]
   232  	name := id.Path["topics"]
   233  
   234  	_, err = client.Delete(resGroup, namespaceName, name)
   235  
   236  	return err
   237  }