github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_servicebus_namespace.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"strings"
     8  
     9  	"github.com/Azure/azure-sdk-for-go/arm/servicebus"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  // Default Authorization Rule/Policy created by Azure, used to populate the
    14  // default connection strings and keys
    15  var serviceBusNamespaceDefaultAuthorizationRule = "RootManageSharedAccessKey"
    16  
    17  func resourceArmServiceBusNamespace() *schema.Resource {
    18  	return &schema.Resource{
    19  		Create: resourceArmServiceBusNamespaceCreate,
    20  		Read:   resourceArmServiceBusNamespaceRead,
    21  		Update: resourceArmServiceBusNamespaceCreate,
    22  		Delete: resourceArmServiceBusNamespaceDelete,
    23  		Importer: &schema.ResourceImporter{
    24  			State: schema.ImportStatePassthrough,
    25  		},
    26  
    27  		Schema: map[string]*schema.Schema{
    28  			"name": {
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  
    34  			"location": {
    35  				Type:      schema.TypeString,
    36  				Required:  true,
    37  				ForceNew:  true,
    38  				StateFunc: azureRMNormalizeLocation,
    39  			},
    40  
    41  			"resource_group_name": {
    42  				Type:     schema.TypeString,
    43  				Required: true,
    44  				ForceNew: true,
    45  			},
    46  
    47  			"sku": {
    48  				Type:         schema.TypeString,
    49  				Required:     true,
    50  				ForceNew:     true,
    51  				ValidateFunc: validateServiceBusNamespaceSku,
    52  			},
    53  
    54  			"capacity": {
    55  				Type:         schema.TypeInt,
    56  				Optional:     true,
    57  				ForceNew:     true,
    58  				Default:      1,
    59  				ValidateFunc: validateServiceBusNamespaceCapacity,
    60  			},
    61  
    62  			"default_primary_connection_string": {
    63  				Type:     schema.TypeString,
    64  				Computed: true,
    65  			},
    66  
    67  			"default_secondary_connection_string": {
    68  				Type:     schema.TypeString,
    69  				Computed: true,
    70  			},
    71  
    72  			"default_primary_key": {
    73  				Type:     schema.TypeString,
    74  				Computed: true,
    75  			},
    76  
    77  			"default_secondary_key": {
    78  				Type:     schema.TypeString,
    79  				Computed: true,
    80  			},
    81  
    82  			"tags": tagsSchema(),
    83  		},
    84  	}
    85  }
    86  
    87  func resourceArmServiceBusNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
    88  	client := meta.(*ArmClient)
    89  	namespaceClient := client.serviceBusNamespacesClient
    90  	log.Printf("[INFO] preparing arguments for Azure ARM ServiceBus Namespace creation.")
    91  
    92  	name := d.Get("name").(string)
    93  	location := d.Get("location").(string)
    94  	resGroup := d.Get("resource_group_name").(string)
    95  	sku := d.Get("sku").(string)
    96  	capacity := int32(d.Get("capacity").(int))
    97  	tags := d.Get("tags").(map[string]interface{})
    98  
    99  	parameters := servicebus.NamespaceCreateOrUpdateParameters{
   100  		Location: &location,
   101  		Sku: &servicebus.Sku{
   102  			Name:     servicebus.SkuName(sku),
   103  			Tier:     servicebus.SkuTier(sku),
   104  			Capacity: &capacity,
   105  		},
   106  		Tags: expandTags(tags),
   107  	}
   108  
   109  	_, err := namespaceClient.CreateOrUpdate(resGroup, name, parameters, make(chan struct{}))
   110  	if err != nil {
   111  		return err
   112  	}
   113  
   114  	read, err := namespaceClient.Get(resGroup, name)
   115  	if err != nil {
   116  		return err
   117  	}
   118  
   119  	if read.ID == nil {
   120  		return fmt.Errorf("Cannot read ServiceBus Namespace %s (resource group %s) ID", name, resGroup)
   121  	}
   122  
   123  	d.SetId(*read.ID)
   124  
   125  	return resourceArmServiceBusNamespaceRead(d, meta)
   126  }
   127  
   128  func resourceArmServiceBusNamespaceRead(d *schema.ResourceData, meta interface{}) error {
   129  	namespaceClient := meta.(*ArmClient).serviceBusNamespacesClient
   130  
   131  	id, err := parseAzureResourceID(d.Id())
   132  	if err != nil {
   133  		return err
   134  	}
   135  	resGroup := id.ResourceGroup
   136  	name := id.Path["namespaces"]
   137  
   138  	resp, err := namespaceClient.Get(resGroup, name)
   139  	if err != nil {
   140  		return fmt.Errorf("Error making Read request on Azure ServiceBus Namespace %s: %s", name, err)
   141  	}
   142  	if resp.StatusCode == http.StatusNotFound {
   143  		d.SetId("")
   144  		return nil
   145  	}
   146  
   147  	d.Set("name", resp.Name)
   148  	d.Set("sku", strings.ToLower(string(resp.Sku.Name)))
   149  	d.Set("capacity", resp.Sku.Capacity)
   150  
   151  	keys, err := namespaceClient.ListKeys(resGroup, name, serviceBusNamespaceDefaultAuthorizationRule)
   152  	if err != nil {
   153  		log.Printf("[ERROR] Unable to List default keys for Namespace %s: %s", name, err)
   154  	} else {
   155  		d.Set("default_primary_connection_string", keys.PrimaryConnectionString)
   156  		d.Set("default_secondary_connection_string", keys.SecondaryConnectionString)
   157  		d.Set("default_primary_key", keys.PrimaryKey)
   158  		d.Set("default_secondary_key", keys.SecondaryKey)
   159  	}
   160  
   161  	flattenAndSetTags(d, resp.Tags)
   162  
   163  	return nil
   164  }
   165  
   166  func resourceArmServiceBusNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
   167  	namespaceClient := meta.(*ArmClient).serviceBusNamespacesClient
   168  
   169  	id, err := parseAzureResourceID(d.Id())
   170  	if err != nil {
   171  		return err
   172  	}
   173  	resGroup := id.ResourceGroup
   174  	name := id.Path["namespaces"]
   175  
   176  	resp, err := namespaceClient.Delete(resGroup, name, make(chan struct{}))
   177  
   178  	if resp.StatusCode != http.StatusNotFound {
   179  		return fmt.Errorf("Error issuing Azure ARM delete request of ServiceBus Namespace'%s': %s", name, err)
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  func validateServiceBusNamespaceSku(v interface{}, k string) (ws []string, errors []error) {
   186  	value := strings.ToLower(v.(string))
   187  	skus := map[string]bool{
   188  		"basic":    true,
   189  		"standard": true,
   190  		"premium":  true,
   191  	}
   192  
   193  	if !skus[value] {
   194  		errors = append(errors, fmt.Errorf("ServiceBus Namespace SKU can only be Basic, Standard or Premium"))
   195  	}
   196  	return
   197  }
   198  
   199  func validateServiceBusNamespaceCapacity(v interface{}, k string) (ws []string, errors []error) {
   200  	value := v.(int)
   201  	capacities := map[int]bool{
   202  		1: true,
   203  		2: true,
   204  		4: true,
   205  	}
   206  
   207  	if !capacities[value] {
   208  		errors = append(errors, fmt.Errorf("ServiceBus Namespace Capacity can only be 1, 2 or 4"))
   209  	}
   210  	return
   211  }