github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azure/resource_azure_storage_queue.go (about)

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