github.com/anuaimi/terraform@v0.6.4-0.20150904235404-2bf9aec61da8/builtin/providers/azure/resource_azure_storage_blob.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  // resourceAzureStorageBlob returns the *schema.Resource associated
    11  // with a storage blob on Azure.
    12  func resourceAzureStorageBlob() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceAzureStorageBlobCreate,
    15  		Read:   resourceAzureStorageBlobRead,
    16  		Update: resourceAzureStorageBlobUpdate,
    17  		Exists: resourceAzureStorageBlobExists,
    18  		Delete: resourceAzureStorageBlobDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": &schema.Schema{
    22  				Type:        schema.TypeString,
    23  				Required:    true,
    24  				ForceNew:    true,
    25  				Description: parameterDescriptions["name"],
    26  			},
    27  			"type": &schema.Schema{
    28  				Type:        schema.TypeString,
    29  				Required:    true,
    30  				ForceNew:    true,
    31  				Description: parameterDescriptions["type"],
    32  			},
    33  			"size": &schema.Schema{
    34  				Type:     schema.TypeInt,
    35  				Required: true,
    36  				ForceNew: true,
    37  				DefaultFunc: func() (interface{}, error) {
    38  					return int64(0), nil
    39  				},
    40  			},
    41  			"storage_container_name": &schema.Schema{
    42  				Type:        schema.TypeString,
    43  				Required:    true,
    44  				ForceNew:    true,
    45  				Description: parameterDescriptions["storage_container_name"],
    46  			},
    47  			"storage_service_name": {
    48  				Type:        schema.TypeString,
    49  				Required:    true,
    50  				ForceNew:    true,
    51  				Description: parameterDescriptions["storage_service_name"],
    52  			},
    53  			"url": &schema.Schema{
    54  				Type:        schema.TypeString,
    55  				Computed:    true,
    56  				Description: parameterDescriptions["url"],
    57  			},
    58  		},
    59  	}
    60  }
    61  
    62  // resourceAzureStorageBlobCreate does all the necessary API calls to
    63  // create the storage blob on Azure.
    64  func resourceAzureStorageBlobCreate(d *schema.ResourceData, meta interface{}) error {
    65  	azureClient := meta.(*Client)
    66  	storName := d.Get("storage_service_name").(string)
    67  
    68  	blobClient, err := azureClient.getStorageServiceBlobClient(storName)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	log.Println("[INFO] Issuing create on Azure storage blob.")
    74  	name := d.Get("name").(string)
    75  	blobType := d.Get("type").(string)
    76  	cont := d.Get("storage_container_name").(string)
    77  	switch blobType {
    78  	case "BlockBlob":
    79  		err = blobClient.CreateBlockBlob(cont, name)
    80  	case "PageBlob":
    81  		size := int64(d.Get("size").(int))
    82  		err = blobClient.PutPageBlob(cont, name, size)
    83  	default:
    84  		err = fmt.Errorf("Invalid blob type specified; see parameter desciptions for more info.")
    85  	}
    86  	if err != nil {
    87  		return fmt.Errorf("Error creating storage blob on Azure: %s", err)
    88  	}
    89  
    90  	d.SetId(name)
    91  	return resourceAzureStorageBlobRead(d, meta)
    92  }
    93  
    94  // resourceAzureStorageBlobRead does all the necessary API calls to
    95  // read the status of the storage blob off Azure.
    96  func resourceAzureStorageBlobRead(d *schema.ResourceData, meta interface{}) error {
    97  	azureClient := meta.(*Client)
    98  
    99  	// check for it's existence:
   100  	exists, err := resourceAzureStorageBlobExists(d, meta)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	// if it exists; read relevant information:
   106  	if exists {
   107  		storName := d.Get("storage_service_name").(string)
   108  
   109  		blobClient, err := azureClient.getStorageServiceBlobClient(storName)
   110  		if err != nil {
   111  			return err
   112  		}
   113  
   114  		name := d.Get("name").(string)
   115  		cont := d.Get("storage_container_name").(string)
   116  		url := blobClient.GetBlobURL(cont, name)
   117  		d.Set("url", url)
   118  	}
   119  
   120  	// NOTE: no need to unset the ID here, as resourceAzureStorageBlobExists
   121  	// already should have done so if it were required.
   122  	return nil
   123  }
   124  
   125  // resourceAzureStorageBlobUpdate does all the necessary API calls to
   126  // update a blob on Azure.
   127  func resourceAzureStorageBlobUpdate(d *schema.ResourceData, meta interface{}) error {
   128  	// NOTE: although empty as most paramters have ForceNew set; this is
   129  	// still required in case of changes to the storage_service_key
   130  
   131  	// run the ExistsFunc beforehand to ensure the resource's existence nonetheless:
   132  	_, err := resourceAzureStorageBlobExists(d, meta)
   133  	return err
   134  }
   135  
   136  // resourceAzureStorageBlobExists does all the necessary API calls to
   137  // check for the existence of the blob on Azure.
   138  func resourceAzureStorageBlobExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   139  	azureClient := meta.(*Client)
   140  	storName := d.Get("storage_service_name").(string)
   141  
   142  	blobClient, err := azureClient.getStorageServiceBlobClient(storName)
   143  	if err != nil {
   144  		return false, err
   145  	}
   146  
   147  	log.Println("[INFO] Querying Azure for storage blob's existence.")
   148  	name := d.Get("name").(string)
   149  	cont := d.Get("storage_container_name").(string)
   150  	exists, err := blobClient.BlobExists(cont, name)
   151  	if err != nil {
   152  		return false, fmt.Errorf("Error whilst checking for Azure storage blob's existence: %s", err)
   153  	}
   154  
   155  	// if not found; it means it was deleted in the meantime and
   156  	// we must remove it from the schema.
   157  	if !exists {
   158  		d.SetId("")
   159  	}
   160  
   161  	return exists, nil
   162  }
   163  
   164  // resourceAzureStorageBlobDelete does all the necessary API calls to
   165  // delete the blob off Azure.
   166  func resourceAzureStorageBlobDelete(d *schema.ResourceData, meta interface{}) error {
   167  	azureClient := meta.(*Client)
   168  	storName := d.Get("storage_service_name").(string)
   169  
   170  	blobClient, err := azureClient.getStorageServiceBlobClient(storName)
   171  	if err != nil {
   172  		return err
   173  	}
   174  
   175  	log.Println("[INFO] Issuing storage blob delete command off Azure.")
   176  	name := d.Get("name").(string)
   177  	cont := d.Get("storage_container_name").(string)
   178  	if _, err = blobClient.DeleteBlobIfExists(cont, name); err != nil {
   179  		return fmt.Errorf("Error whilst deleting storage blob: %s", err)
   180  	}
   181  
   182  	d.SetId("")
   183  	return nil
   184  }