github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azurerm/resource_arm_eventhub_namespace.go (about)

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