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