github.com/richardmarshall/terraform@v0.9.5-0.20170429023105-15704cc6ee35/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": locationSchema(),
    43  
    44  			"partition_count": {
    45  				Type:         schema.TypeInt,
    46  				Required:     true,
    47  				ValidateFunc: validateEventHubPartitionCount,
    48  			},
    49  
    50  			"message_retention": {
    51  				Type:         schema.TypeInt,
    52  				Required:     true,
    53  				ValidateFunc: validateEventHubMessageRetentionCount,
    54  			},
    55  
    56  			"partition_ids": {
    57  				Type:     schema.TypeSet,
    58  				Elem:     &schema.Schema{Type: schema.TypeString},
    59  				Set:      schema.HashString,
    60  				Computed: true,
    61  			},
    62  		},
    63  	}
    64  }
    65  
    66  func resourceArmEventHubCreate(d *schema.ResourceData, meta interface{}) error {
    67  	client := meta.(*ArmClient)
    68  	eventhubClient := client.eventHubClient
    69  	log.Printf("[INFO] preparing arguments for Azure ARM EventHub creation.")
    70  
    71  	name := d.Get("name").(string)
    72  	namespaceName := d.Get("namespace_name").(string)
    73  	location := d.Get("location").(string)
    74  	resGroup := d.Get("resource_group_name").(string)
    75  	partitionCount := int64(d.Get("partition_count").(int))
    76  	messageRetention := int64(d.Get("message_retention").(int))
    77  
    78  	parameters := eventhub.CreateOrUpdateParameters{
    79  		Location: &location,
    80  		Properties: &eventhub.Properties{
    81  			PartitionCount:         &partitionCount,
    82  			MessageRetentionInDays: &messageRetention,
    83  		},
    84  	}
    85  
    86  	_, err := eventhubClient.CreateOrUpdate(resGroup, namespaceName, name, parameters)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	read, err := eventhubClient.Get(resGroup, namespaceName, name)
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	if read.ID == nil {
    97  		return fmt.Errorf("Cannot read EventHub %s (resource group %s) ID", name, resGroup)
    98  	}
    99  
   100  	d.SetId(*read.ID)
   101  
   102  	return resourceArmEventHubRead(d, meta)
   103  }
   104  
   105  func resourceArmEventHubRead(d *schema.ResourceData, meta interface{}) error {
   106  	eventhubClient := meta.(*ArmClient).eventHubClient
   107  
   108  	id, err := parseAzureResourceID(d.Id())
   109  	if err != nil {
   110  		return err
   111  	}
   112  	resGroup := id.ResourceGroup
   113  	namespaceName := id.Path["namespaces"]
   114  	name := id.Path["eventhubs"]
   115  
   116  	resp, err := eventhubClient.Get(resGroup, namespaceName, name)
   117  	if err != nil {
   118  		return fmt.Errorf("Error making Read request on Azure EventHub %s: %s", name, err)
   119  	}
   120  	if resp.StatusCode == http.StatusNotFound {
   121  		d.SetId("")
   122  		return nil
   123  	}
   124  
   125  	d.Set("name", resp.Name)
   126  	d.Set("namespace_name", namespaceName)
   127  	d.Set("resource_group_name", resGroup)
   128  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   129  
   130  	d.Set("partition_count", resp.Properties.PartitionCount)
   131  	d.Set("message_retention", resp.Properties.MessageRetentionInDays)
   132  	d.Set("partition_ids", resp.Properties.PartitionIds)
   133  
   134  	return nil
   135  }
   136  
   137  func resourceArmEventHubDelete(d *schema.ResourceData, meta interface{}) error {
   138  	eventhubClient := meta.(*ArmClient).eventHubClient
   139  
   140  	id, err := parseAzureResourceID(d.Id())
   141  	if err != nil {
   142  		return err
   143  	}
   144  	resGroup := id.ResourceGroup
   145  	namespaceName := id.Path["namespaces"]
   146  	name := id.Path["eventhubs"]
   147  
   148  	resp, err := eventhubClient.Delete(resGroup, namespaceName, name)
   149  
   150  	if resp.StatusCode != http.StatusOK {
   151  		return fmt.Errorf("Error issuing Azure ARM delete request of EventHub'%s': %s", name, err)
   152  	}
   153  
   154  	return nil
   155  }
   156  
   157  func validateEventHubPartitionCount(v interface{}, k string) (ws []string, errors []error) {
   158  	value := v.(int)
   159  
   160  	if !(32 >= value && value >= 2) {
   161  		errors = append(errors, fmt.Errorf("EventHub Partition Count has to be between 2 and 32"))
   162  	}
   163  	return
   164  }
   165  
   166  func validateEventHubMessageRetentionCount(v interface{}, k string) (ws []string, errors []error) {
   167  	value := v.(int)
   168  
   169  	if !(7 >= value && value >= 1) {
   170  		errors = append(errors, fmt.Errorf("EventHub Retention Count has to be between 1 and 7"))
   171  	}
   172  	return
   173  }