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