github.com/renier/terraform@v0.7.8-0.20161024133817-eb8a9ef5471a/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  			},
    84  
    85  			"max_size_in_megabytes": {
    86  				Type:     schema.TypeInt,
    87  				Optional: true,
    88  				Computed: true,
    89  			},
    90  
    91  			"requires_duplicate_detection": {
    92  				Type:     schema.TypeBool,
    93  				Optional: true,
    94  			},
    95  
    96  			"support_ordering": {
    97  				Type:     schema.TypeBool,
    98  				Optional: true,
    99  			},
   100  		},
   101  	}
   102  }
   103  
   104  func resourceArmServiceBusTopicCreate(d *schema.ResourceData, meta interface{}) error {
   105  	client := meta.(*ArmClient).serviceBusTopicsClient
   106  	log.Printf("[INFO] preparing arguments for Azure ARM ServiceBus Topic creation.")
   107  
   108  	name := d.Get("name").(string)
   109  	namespaceName := d.Get("namespace_name").(string)
   110  	location := d.Get("location").(string)
   111  	resGroup := d.Get("resource_group_name").(string)
   112  
   113  	parameters := servicebus.TopicCreateOrUpdateParameters{
   114  		Name:       &name,
   115  		Location:   &location,
   116  		Properties: &servicebus.TopicProperties{},
   117  	}
   118  
   119  	if autoDeleteOnIdle := d.Get("auto_delete_on_idle").(string); autoDeleteOnIdle != "" {
   120  		parameters.Properties.AutoDeleteOnIdle = &autoDeleteOnIdle
   121  	}
   122  
   123  	if defaultTTL := d.Get("default_message_ttl").(string); defaultTTL != "" {
   124  		parameters.Properties.DefaultMessageTimeToLive = &defaultTTL
   125  	}
   126  
   127  	if duplicateWindow := d.Get("duplicate_detection_history_time_window").(string); duplicateWindow != "" {
   128  		parameters.Properties.DuplicateDetectionHistoryTimeWindow = &duplicateWindow
   129  	}
   130  
   131  	enableBatchedOps := d.Get("enable_batched_operations").(bool)
   132  	enableExpress := d.Get("enable_express").(bool)
   133  	enableFiltering := d.Get("enable_filtering_messages_before_publishing").(bool)
   134  	enablePartitioning := d.Get("enable_partitioning").(bool)
   135  	maxSize := int64(d.Get("max_size_in_megabytes").(int))
   136  	requiresDuplicateDetection := d.Get("requires_duplicate_detection").(bool)
   137  	supportOrdering := d.Get("support_ordering").(bool)
   138  
   139  	parameters.Properties.EnableBatchedOperations = &enableBatchedOps
   140  	parameters.Properties.EnableExpress = &enableExpress
   141  	parameters.Properties.FilteringMessagesBeforePublishing = &enableFiltering
   142  	parameters.Properties.EnablePartitioning = &enablePartitioning
   143  	parameters.Properties.MaxSizeInMegabytes = &maxSize
   144  	parameters.Properties.RequiresDuplicateDetection = &requiresDuplicateDetection
   145  	parameters.Properties.SupportOrdering = &supportOrdering
   146  
   147  	_, err := client.CreateOrUpdate(resGroup, namespaceName, name, parameters)
   148  	if err != nil {
   149  		return err
   150  	}
   151  
   152  	read, err := client.Get(resGroup, namespaceName, name)
   153  	if err != nil {
   154  		return err
   155  	}
   156  	if read.ID == nil {
   157  		return fmt.Errorf("Cannot read ServiceBus Topic %s (resource group %s) ID", name, resGroup)
   158  	}
   159  
   160  	d.SetId(*read.ID)
   161  
   162  	return resourceArmServiceBusTopicRead(d, meta)
   163  }
   164  
   165  func resourceArmServiceBusTopicRead(d *schema.ResourceData, meta interface{}) error {
   166  	client := meta.(*ArmClient).serviceBusTopicsClient
   167  
   168  	id, err := parseAzureResourceID(d.Id())
   169  	if err != nil {
   170  		return err
   171  	}
   172  	resGroup := id.ResourceGroup
   173  	namespaceName := id.Path["namespaces"]
   174  	name := id.Path["topics"]
   175  
   176  	resp, err := client.Get(resGroup, namespaceName, name)
   177  	if err != nil {
   178  		return fmt.Errorf("Error making Read request on Azure ServiceBus Topic %s: %s", name, err)
   179  	}
   180  	if resp.StatusCode == http.StatusNotFound {
   181  		d.SetId("")
   182  		return nil
   183  	}
   184  
   185  	d.Set("name", resp.Name)
   186  	d.Set("resource_group_name", resGroup)
   187  	d.Set("namespace_name", namespaceName)
   188  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   189  
   190  	props := resp.Properties
   191  	d.Set("auto_delete_on_idle", props.AutoDeleteOnIdle)
   192  	d.Set("default_message_ttl", props.DefaultMessageTimeToLive)
   193  
   194  	if props.DuplicateDetectionHistoryTimeWindow != nil && *props.DuplicateDetectionHistoryTimeWindow != "" {
   195  		d.Set("duplicate_detection_history_time_window", props.DuplicateDetectionHistoryTimeWindow)
   196  	}
   197  
   198  	d.Set("enable_batched_operations", props.EnableBatchedOperations)
   199  	d.Set("enable_express", props.EnableExpress)
   200  	d.Set("enable_filtering_messages_before_publishing", props.FilteringMessagesBeforePublishing)
   201  	d.Set("enable_partitioning", props.EnablePartitioning)
   202  	d.Set("max_size_in_megabytes", int(*props.MaxSizeInMegabytes))
   203  	d.Set("requires_duplicate_detection", props.RequiresDuplicateDetection)
   204  	d.Set("support_ordering", props.SupportOrdering)
   205  
   206  	return nil
   207  }
   208  
   209  func resourceArmServiceBusTopicDelete(d *schema.ResourceData, meta interface{}) error {
   210  	client := meta.(*ArmClient).serviceBusTopicsClient
   211  
   212  	id, err := parseAzureResourceID(d.Id())
   213  	if err != nil {
   214  		return err
   215  	}
   216  	resGroup := id.ResourceGroup
   217  	namespaceName := id.Path["namespaces"]
   218  	name := id.Path["topics"]
   219  
   220  	_, err = client.Delete(resGroup, namespaceName, name)
   221  
   222  	return err
   223  }