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

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/arm/eventhub"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"net/http"
    11  )
    12  
    13  // Default Authorization Rule/Policy created by Azure, used to populate the
    14  // default connection strings and keys
    15  var eventHubNamespaceDefaultAuthorizationRule = "RootManageSharedAccessKey"
    16  
    17  func resourceArmEventHubNamespace() *schema.Resource {
    18  	return &schema.Resource{
    19  		Create: resourceArmEventHubNamespaceCreate,
    20  		Read:   resourceArmEventHubNamespaceRead,
    21  		Update: resourceArmEventHubNamespaceCreate,
    22  		Delete: resourceArmEventHubNamespaceDelete,
    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  				ValidateFunc: validateEventHubNamespaceSku,
    51  			},
    52  
    53  			"capacity": {
    54  				Type:         schema.TypeInt,
    55  				Optional:     true,
    56  				Default:      1,
    57  				ValidateFunc: validateEventHubNamespaceCapacity,
    58  			},
    59  
    60  			"default_primary_connection_string": {
    61  				Type:     schema.TypeString,
    62  				Computed: true,
    63  			},
    64  
    65  			"default_secondary_connection_string": {
    66  				Type:     schema.TypeString,
    67  				Computed: true,
    68  			},
    69  
    70  			"default_primary_key": {
    71  				Type:     schema.TypeString,
    72  				Computed: true,
    73  			},
    74  
    75  			"default_secondary_key": {
    76  				Type:     schema.TypeString,
    77  				Computed: true,
    78  			},
    79  
    80  			"tags": tagsSchema(),
    81  		},
    82  	}
    83  }
    84  
    85  func resourceArmEventHubNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
    86  	client := meta.(*ArmClient)
    87  	namespaceClient := client.eventHubNamespacesClient
    88  	log.Printf("[INFO] preparing arguments for Azure ARM EventHub Namespace creation.")
    89  
    90  	name := d.Get("name").(string)
    91  	location := d.Get("location").(string)
    92  	resGroup := d.Get("resource_group_name").(string)
    93  	sku := d.Get("sku").(string)
    94  	capacity := int32(d.Get("capacity").(int))
    95  	tags := d.Get("tags").(map[string]interface{})
    96  
    97  	parameters := eventhub.NamespaceCreateOrUpdateParameters{
    98  		Location: &location,
    99  		Sku: &eventhub.Sku{
   100  			Name:     eventhub.SkuName(sku),
   101  			Tier:     eventhub.SkuTier(sku),
   102  			Capacity: &capacity,
   103  		},
   104  		Tags: expandTags(tags),
   105  	}
   106  
   107  	_, err := namespaceClient.CreateOrUpdate(resGroup, name, parameters, make(chan struct{}))
   108  	if err != nil {
   109  		return err
   110  	}
   111  
   112  	read, err := namespaceClient.Get(resGroup, name)
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	if read.ID == nil {
   118  		return fmt.Errorf("Cannot read EventHub Namespace %s (resource group %s) ID", name, resGroup)
   119  	}
   120  
   121  	d.SetId(*read.ID)
   122  
   123  	return resourceArmEventHubNamespaceRead(d, meta)
   124  }
   125  
   126  func resourceArmEventHubNamespaceRead(d *schema.ResourceData, meta interface{}) error {
   127  	namespaceClient := meta.(*ArmClient).eventHubNamespacesClient
   128  
   129  	id, err := parseAzureResourceID(d.Id())
   130  	if err != nil {
   131  		return err
   132  	}
   133  	resGroup := id.ResourceGroup
   134  	name := id.Path["namespaces"]
   135  
   136  	resp, err := namespaceClient.Get(resGroup, name)
   137  	if err != nil {
   138  		return fmt.Errorf("Error making Read request on Azure EventHub Namespace %s: %s", name, err)
   139  	}
   140  	if resp.StatusCode == http.StatusNotFound {
   141  		d.SetId("")
   142  		return nil
   143  	}
   144  
   145  	d.Set("name", resp.Name)
   146  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   147  	d.Set("resource_group_name", resGroup)
   148  	d.Set("sku", string(resp.Sku.Name))
   149  	d.Set("capacity", resp.Sku.Capacity)
   150  
   151  	keys, err := namespaceClient.ListKeys(resGroup, name, eventHubNamespaceDefaultAuthorizationRule)
   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 resourceArmEventHubNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
   167  
   168  	namespaceClient := meta.(*ArmClient).eventHubNamespacesClient
   169  
   170  	id, err := parseAzureResourceID(d.Id())
   171  	if err != nil {
   172  		return err
   173  	}
   174  	resGroup := id.ResourceGroup
   175  	name := id.Path["namespaces"]
   176  
   177  	resp, err := namespaceClient.Delete(resGroup, name, make(chan struct{}))
   178  
   179  	if resp.StatusCode != http.StatusNotFound {
   180  		return fmt.Errorf("Error issuing Azure ARM delete request of EventHub Namespace'%s': %s", name, err)
   181  	}
   182  
   183  	return nil
   184  }
   185  
   186  func validateEventHubNamespaceSku(v interface{}, k string) (ws []string, errors []error) {
   187  	value := strings.ToLower(v.(string))
   188  	skus := map[string]bool{
   189  		"basic":    true,
   190  		"standard": true,
   191  	}
   192  
   193  	if !skus[value] {
   194  		errors = append(errors, fmt.Errorf("EventHub Namespace SKU can only be Basic or Standard"))
   195  	}
   196  	return
   197  }
   198  
   199  func validateEventHubNamespaceCapacity(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("EventHub Namespace Capacity can only be 1, 2 or 4"))
   209  	}
   210  	return
   211  }