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