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