github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/azure/resource_azure_storage_queue.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  // resourceAzureStorageQueue returns the *schema.Resource associated
    11  // to a storage queue on Azure.
    12  func resourceAzureStorageQueue() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAzureStorageQueueCreate,
    15  		Read:   resourceAzureStorageQueueRead,
    16  		Delete: resourceAzureStorageQueueDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": &schema.Schema{
    20  				Type:        schema.TypeString,
    21  				Required:    true,
    22  				ForceNew:    true,
    23  				Description: parameterDescriptions["name"],
    24  			},
    25  			"storage_service_name": &schema.Schema{
    26  				Type:        schema.TypeString,
    27  				Required:    true,
    28  				ForceNew:    true,
    29  				Description: parameterDescriptions["storage_service_name"],
    30  			},
    31  		},
    32  	}
    33  }
    34  
    35  // resourceAzureStorageQueueCreate does all the necessary API calls to
    36  // create a storage queue on Azure.
    37  func resourceAzureStorageQueueCreate(d *schema.ResourceData, meta interface{}) error {
    38  	azureClient := meta.(*Client)
    39  	storServName := d.Get("storage_service_name").(string)
    40  	queueClient, err := azureClient.getStorageServiceQueueClient(storServName)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	// create the queue:
    46  	log.Println("Sending Storage Queue creation request to Azure.")
    47  	name := d.Get("name").(string)
    48  	err = queueClient.CreateQueue(name)
    49  	if err != nil {
    50  		return fmt.Errorf("Error creation Storage Queue on Azure: %s", err)
    51  	}
    52  
    53  	d.SetId(name)
    54  	return nil
    55  }
    56  
    57  // resourceAzureStorageQueueRead does all the necessary API calls to
    58  // read the state of the storage queue off Azure.
    59  func resourceAzureStorageQueueRead(d *schema.ResourceData, meta interface{}) error {
    60  	azureClient := meta.(*Client)
    61  	storServName := d.Get("storage_service_name").(string)
    62  	queueClient, err := azureClient.getStorageServiceQueueClient(storServName)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	// check for queue's existence:
    68  	log.Println("[INFO] Sending Storage Queue existence query to Azure.")
    69  	name := d.Get("name").(string)
    70  	exists, err := queueClient.QueueExists(name)
    71  	if err != nil {
    72  		return fmt.Errorf("Error checking for Storage Queue existence: %s", err)
    73  	}
    74  
    75  	// If the queue has been deleted in the meantime;
    76  	// untrack the resource from the schema.
    77  	if !exists {
    78  		d.SetId("")
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  // resourceAzureStorageQueueDelete does all the necessary API calls to
    85  // delete the storage queue off Azure.
    86  func resourceAzureStorageQueueDelete(d *schema.ResourceData, meta interface{}) error {
    87  	azureClient := meta.(*Client)
    88  	storServName := d.Get("storage_service_name").(string)
    89  	queueClient, err := azureClient.getStorageServiceQueueClient(storServName)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	// issue the deletion of the storage queue:
    95  	log.Println("[INFO] Sending Storage Queue deletion request to Azure.")
    96  	name := d.Get("name").(string)
    97  	err = queueClient.DeleteQueue(name)
    98  	if err != nil {
    99  		return fmt.Errorf("Error deleting Storage queue off Azure: %s", err)
   100  	}
   101  
   102  	return nil
   103  }