github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_servicebus_subscription.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 resourceArmServiceBusSubscription() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceArmServiceBusSubscriptionCreate,
    15  		Read:   resourceArmServiceBusSubscriptionRead,
    16  		Update: resourceArmServiceBusSubscriptionCreate,
    17  		Delete: resourceArmServiceBusSubscriptionDelete,
    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  			"topic_name": {
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  				ForceNew: true,
    39  			},
    40  
    41  			"location": {
    42  				Type:      schema.TypeString,
    43  				Required:  true,
    44  				ForceNew:  true,
    45  				StateFunc: azureRMNormalizeLocation,
    46  			},
    47  
    48  			"resource_group_name": {
    49  				Type:     schema.TypeString,
    50  				Required: true,
    51  				ForceNew: true,
    52  			},
    53  
    54  			"auto_delete_on_idle": {
    55  				Type:     schema.TypeString,
    56  				Optional: true,
    57  				Computed: true,
    58  			},
    59  
    60  			"default_message_ttl": {
    61  				Type:     schema.TypeString,
    62  				Optional: true,
    63  				Computed: true,
    64  			},
    65  
    66  			"lock_duration": {
    67  				Type:     schema.TypeString,
    68  				Optional: true,
    69  				Computed: true,
    70  			},
    71  
    72  			"dead_lettering_on_filter_evaluation_exceptions": {
    73  				Type:     schema.TypeBool,
    74  				Optional: true,
    75  			},
    76  
    77  			"dead_lettering_on_message_expiration": {
    78  				Type:     schema.TypeBool,
    79  				Optional: true,
    80  			},
    81  
    82  			"enable_batched_operations": {
    83  				Type:     schema.TypeBool,
    84  				Optional: true,
    85  			},
    86  
    87  			"max_delivery_count": {
    88  				Type:     schema.TypeInt,
    89  				Required: true,
    90  			},
    91  
    92  			"requires_session": {
    93  				Type:     schema.TypeBool,
    94  				Optional: true,
    95  				// cannot be modified
    96  				ForceNew: true,
    97  			},
    98  		},
    99  	}
   100  }
   101  
   102  func resourceArmServiceBusSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
   103  	client := meta.(*ArmClient).serviceBusSubscriptionsClient
   104  	log.Printf("[INFO] preparing arguments for Azure ARM ServiceBus Subscription creation.")
   105  
   106  	name := d.Get("name").(string)
   107  	topicName := d.Get("topic_name").(string)
   108  	namespaceName := d.Get("namespace_name").(string)
   109  	location := d.Get("location").(string)
   110  	resGroup := d.Get("resource_group_name").(string)
   111  
   112  	parameters := servicebus.SubscriptionCreateOrUpdateParameters{
   113  		Location:   &location,
   114  		Properties: &servicebus.SubscriptionProperties{},
   115  	}
   116  
   117  	if autoDeleteOnIdle := d.Get("auto_delete_on_idle").(string); autoDeleteOnIdle != "" {
   118  		parameters.Properties.AutoDeleteOnIdle = &autoDeleteOnIdle
   119  	}
   120  
   121  	if lockDuration := d.Get("lock_duration").(string); lockDuration != "" {
   122  		parameters.Properties.LockDuration = &lockDuration
   123  	}
   124  
   125  	deadLetteringFilterExceptions := d.Get("dead_lettering_on_filter_evaluation_exceptions").(bool)
   126  	deadLetteringExpiration := d.Get("dead_lettering_on_message_expiration").(bool)
   127  	enableBatchedOps := d.Get("enable_batched_operations").(bool)
   128  	maxDeliveryCount := int32(d.Get("max_delivery_count").(int))
   129  	requiresSession := d.Get("requires_session").(bool)
   130  
   131  	parameters.Properties.DeadLetteringOnFilterEvaluationExceptions = &deadLetteringFilterExceptions
   132  	parameters.Properties.DeadLetteringOnMessageExpiration = &deadLetteringExpiration
   133  	parameters.Properties.EnableBatchedOperations = &enableBatchedOps
   134  	parameters.Properties.MaxDeliveryCount = &maxDeliveryCount
   135  	parameters.Properties.RequiresSession = &requiresSession
   136  
   137  	_, err := client.CreateOrUpdate(resGroup, namespaceName, topicName, name, parameters)
   138  	if err != nil {
   139  		return err
   140  	}
   141  
   142  	read, err := client.Get(resGroup, namespaceName, topicName, name)
   143  	if err != nil {
   144  		return err
   145  	}
   146  	if read.ID == nil {
   147  		return fmt.Errorf("Cannot read ServiceBus Subscription %s (resource group %s) ID", name, resGroup)
   148  	}
   149  
   150  	d.SetId(*read.ID)
   151  
   152  	return resourceArmServiceBusSubscriptionRead(d, meta)
   153  }
   154  
   155  func resourceArmServiceBusSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
   156  	client := meta.(*ArmClient).serviceBusSubscriptionsClient
   157  
   158  	id, err := parseAzureResourceID(d.Id())
   159  	if err != nil {
   160  		return err
   161  	}
   162  	resGroup := id.ResourceGroup
   163  	namespaceName := id.Path["namespaces"]
   164  	topicName := id.Path["topics"]
   165  	name := id.Path["subscriptions"]
   166  
   167  	log.Printf("[INFO] subscriptionID: %s, args: %s, %s, %s, %s", d.Id(), resGroup, namespaceName, topicName, name)
   168  
   169  	resp, err := client.Get(resGroup, namespaceName, topicName, name)
   170  	if err != nil {
   171  		return fmt.Errorf("Error making Read request on Azure ServiceBus Subscription %s: %s", name, err)
   172  	}
   173  	if resp.StatusCode == http.StatusNotFound {
   174  		d.SetId("")
   175  		return nil
   176  	}
   177  
   178  	d.Set("name", resp.Name)
   179  	d.Set("resource_group_name", resGroup)
   180  	d.Set("namespace_name", namespaceName)
   181  	d.Set("topic_name", topicName)
   182  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   183  
   184  	props := resp.Properties
   185  	d.Set("auto_delete_on_idle", props.AutoDeleteOnIdle)
   186  	d.Set("default_message_ttl", props.DefaultMessageTimeToLive)
   187  	d.Set("lock_duration", props.LockDuration)
   188  	d.Set("dead_lettering_on_filter_evaluation_exceptions", props.DeadLetteringOnFilterEvaluationExceptions)
   189  	d.Set("dead_lettering_on_message_expiration", props.DeadLetteringOnMessageExpiration)
   190  	d.Set("enable_batched_operations", props.EnableBatchedOperations)
   191  	d.Set("max_delivery_count", int(*props.MaxDeliveryCount))
   192  	d.Set("requires_session", props.RequiresSession)
   193  
   194  	return nil
   195  }
   196  
   197  func resourceArmServiceBusSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
   198  	client := meta.(*ArmClient).serviceBusSubscriptionsClient
   199  
   200  	id, err := parseAzureResourceID(d.Id())
   201  	if err != nil {
   202  		return err
   203  	}
   204  	resGroup := id.ResourceGroup
   205  	namespaceName := id.Path["namespaces"]
   206  	topicName := id.Path["topics"]
   207  	name := id.Path["subscriptions"]
   208  
   209  	_, err = client.Delete(resGroup, namespaceName, topicName, name)
   210  
   211  	return err
   212  }