github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/builtin/providers/azurerm/resource_arm_eventhub.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"net/http"
     8  
     9  	"github.com/Azure/azure-sdk-for-go/arm/eventhub"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceArmEventHub() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceArmEventHubCreate,
    16  		Read:   resourceArmEventHubRead,
    17  		Update: resourceArmEventHubCreate,
    18  		Delete: resourceArmEventHubDelete,
    19  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"name": {
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  
    30  			"namespace_name": {
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  
    36  			"resource_group_name": {
    37  				Type:     schema.TypeString,
    38  				Required: true,
    39  				ForceNew: true,
    40  			},
    41  
    42  			"location": {
    43  				Type:     schema.TypeString,
    44  				Required: true,
    45  				ForceNew: true,
    46  			},
    47  
    48  			"partition_count": {
    49  				Type:         schema.TypeInt,
    50  				Required:     true,
    51  				ValidateFunc: validateEventHubPartitionCount,
    52  			},
    53  
    54  			"message_retention": {
    55  				Type:         schema.TypeInt,
    56  				Required:     true,
    57  				ValidateFunc: validateEventHubMessageRetentionCount,
    58  			},
    59  
    60  			"partition_ids": {
    61  				Type:     schema.TypeSet,
    62  				Elem:     &schema.Schema{Type: schema.TypeString},
    63  				Set:      schema.HashString,
    64  				Computed: true,
    65  			},
    66  		},
    67  	}
    68  }
    69  
    70  func resourceArmEventHubCreate(d *schema.ResourceData, meta interface{}) error {
    71  	client := meta.(*ArmClient)
    72  	eventhubClient := client.eventHubClient
    73  	log.Printf("[INFO] preparing arguments for Azure ARM EventHub creation.")
    74  
    75  	name := d.Get("name").(string)
    76  	namespaceName := d.Get("namespace_name").(string)
    77  	location := d.Get("location").(string)
    78  	resGroup := d.Get("resource_group_name").(string)
    79  	partitionCount := int64(d.Get("partition_count").(int))
    80  	messageRetention := int64(d.Get("message_retention").(int))
    81  
    82  	parameters := eventhub.CreateOrUpdateParameters{
    83  		Location: &location,
    84  		Properties: &eventhub.Properties{
    85  			PartitionCount:         &partitionCount,
    86  			MessageRetentionInDays: &messageRetention,
    87  		},
    88  	}
    89  
    90  	_, err := eventhubClient.CreateOrUpdate(resGroup, namespaceName, name, parameters)
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	read, err := eventhubClient.Get(resGroup, namespaceName, name)
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	if read.ID == nil {
   101  		return fmt.Errorf("Cannot read EventHub %s (resource group %s) ID", name, resGroup)
   102  	}
   103  
   104  	d.SetId(*read.ID)
   105  
   106  	return resourceArmEventHubRead(d, meta)
   107  }
   108  
   109  func resourceArmEventHubRead(d *schema.ResourceData, meta interface{}) error {
   110  	eventhubClient := meta.(*ArmClient).eventHubClient
   111  
   112  	id, err := parseAzureResourceID(d.Id())
   113  	if err != nil {
   114  		return err
   115  	}
   116  	resGroup := id.ResourceGroup
   117  	namespaceName := id.Path["namespaces"]
   118  	name := id.Path["eventhubs"]
   119  
   120  	resp, err := eventhubClient.Get(resGroup, namespaceName, name)
   121  	if err != nil {
   122  		return fmt.Errorf("Error making Read request on Azure EventHub %s: %s", name, err)
   123  	}
   124  	if resp.StatusCode == http.StatusNotFound {
   125  		d.SetId("")
   126  		return nil
   127  	}
   128  
   129  	d.Set("name", resp.Name)
   130  	d.Set("namespace_name", namespaceName)
   131  	d.Set("resource_group_name", resGroup)
   132  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   133  
   134  	d.Set("partition_count", resp.Properties.PartitionCount)
   135  	d.Set("message_retention", resp.Properties.MessageRetentionInDays)
   136  	d.Set("partition_ids", resp.Properties.PartitionIds)
   137  
   138  	return nil
   139  }
   140  
   141  func resourceArmEventHubDelete(d *schema.ResourceData, meta interface{}) error {
   142  	eventhubClient := meta.(*ArmClient).eventHubClient
   143  
   144  	id, err := parseAzureResourceID(d.Id())
   145  	if err != nil {
   146  		return err
   147  	}
   148  	resGroup := id.ResourceGroup
   149  	namespaceName := id.Path["namespaces"]
   150  	name := id.Path["eventhubs"]
   151  
   152  	resp, err := eventhubClient.Delete(resGroup, namespaceName, name)
   153  
   154  	if resp.StatusCode != http.StatusOK {
   155  		return fmt.Errorf("Error issuing Azure ARM delete request of EventHub'%s': %s", name, err)
   156  	}
   157  
   158  	return nil
   159  }
   160  
   161  func validateEventHubPartitionCount(v interface{}, k string) (ws []string, errors []error) {
   162  	value := v.(int)
   163  
   164  	if !(32 >= value && value >= 2) {
   165  		errors = append(errors, fmt.Errorf("EventHub Partition Count has to be between 2 and 32"))
   166  	}
   167  	return
   168  }
   169  
   170  func validateEventHubMessageRetentionCount(v interface{}, k string) (ws []string, errors []error) {
   171  	value := v.(int)
   172  
   173  	if !(7 >= value && value >= 1) {
   174  		errors = append(errors, fmt.Errorf("EventHub Retention Count has to be between 1 and 7"))
   175  	}
   176  	return
   177  }