github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_eventhub_consumer_group.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 resourceArmEventHubConsumerGroup() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceArmEventHubConsumerGroupCreateUpdate,
    16  		Read:   resourceArmEventHubConsumerGroupRead,
    17  		Update: resourceArmEventHubConsumerGroupCreateUpdate,
    18  		Delete: resourceArmEventHubConsumerGroupDelete,
    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  			"eventhub_name": {
    37  				Type:     schema.TypeString,
    38  				Required: true,
    39  				ForceNew: true,
    40  			},
    41  
    42  			"resource_group_name": {
    43  				Type:     schema.TypeString,
    44  				Required: true,
    45  				ForceNew: true,
    46  			},
    47  
    48  			"location": locationSchema(),
    49  
    50  			"user_metadata": {
    51  				Type:     schema.TypeString,
    52  				Optional: true,
    53  			},
    54  		},
    55  	}
    56  }
    57  
    58  func resourceArmEventHubConsumerGroupCreateUpdate(d *schema.ResourceData, meta interface{}) error {
    59  	client := meta.(*ArmClient)
    60  	eventhubClient := client.eventHubConsumerGroupClient
    61  	log.Printf("[INFO] preparing arguments for Azure ARM EventHub Consumer Group creation.")
    62  
    63  	name := d.Get("name").(string)
    64  	namespaceName := d.Get("namespace_name").(string)
    65  	eventHubName := d.Get("eventhub_name").(string)
    66  	location := d.Get("location").(string)
    67  	resGroup := d.Get("resource_group_name").(string)
    68  	userMetaData := d.Get("user_metadata").(string)
    69  
    70  	parameters := eventhub.ConsumerGroupCreateOrUpdateParameters{
    71  		Name:     &name,
    72  		Location: &location,
    73  		ConsumerGroupProperties: &eventhub.ConsumerGroupProperties{
    74  			UserMetadata: &userMetaData,
    75  		},
    76  	}
    77  
    78  	_, err := eventhubClient.CreateOrUpdate(resGroup, namespaceName, eventHubName, name, parameters)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	read, err := eventhubClient.Get(resGroup, namespaceName, eventHubName, name)
    84  
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	if read.ID == nil {
    90  		return fmt.Errorf("Cannot read EventHub Consumer Group %s (resource group %s) ID", name, resGroup)
    91  	}
    92  
    93  	d.SetId(*read.ID)
    94  
    95  	return resourceArmEventHubConsumerGroupRead(d, meta)
    96  }
    97  
    98  func resourceArmEventHubConsumerGroupRead(d *schema.ResourceData, meta interface{}) error {
    99  	eventhubClient := meta.(*ArmClient).eventHubConsumerGroupClient
   100  
   101  	id, err := parseAzureResourceID(d.Id())
   102  	if err != nil {
   103  		return err
   104  	}
   105  	resGroup := id.ResourceGroup
   106  	namespaceName := id.Path["namespaces"]
   107  	eventHubName := id.Path["eventhubs"]
   108  	name := id.Path["consumergroups"]
   109  
   110  	resp, err := eventhubClient.Get(resGroup, namespaceName, eventHubName, name)
   111  	if err != nil {
   112  		return fmt.Errorf("Error making Read request on Azure EventHub Consumer Group %s: %+v", name, err)
   113  	}
   114  	if resp.StatusCode == http.StatusNotFound {
   115  		d.SetId("")
   116  		return nil
   117  	}
   118  
   119  	d.Set("name", name)
   120  	d.Set("eventhub_name", eventHubName)
   121  	d.Set("namespace_name", namespaceName)
   122  	d.Set("resource_group_name", resGroup)
   123  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   124  	d.Set("user_metadata", resp.ConsumerGroupProperties.UserMetadata)
   125  
   126  	return nil
   127  }
   128  
   129  func resourceArmEventHubConsumerGroupDelete(d *schema.ResourceData, meta interface{}) error {
   130  	eventhubClient := meta.(*ArmClient).eventHubConsumerGroupClient
   131  
   132  	id, err := parseAzureResourceID(d.Id())
   133  	if err != nil {
   134  		return err
   135  	}
   136  	resGroup := id.ResourceGroup
   137  	namespaceName := id.Path["namespaces"]
   138  	eventHubName := id.Path["eventhubs"]
   139  	name := id.Path["consumergroups"]
   140  
   141  	resp, err := eventhubClient.Delete(resGroup, namespaceName, eventHubName, name)
   142  
   143  	if resp.StatusCode != http.StatusOK {
   144  		return fmt.Errorf("Error issuing Azure ARM delete request of EventHub Consumer Group '%s': %+v", name, err)
   145  	}
   146  
   147  	return nil
   148  }