github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/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  	mgmtClient := meta.(*Client).mgmtClient
    66  	storName := d.Get("storage_service_name").(string)
    67  
    68  	blobClient, err := getStorageServiceBlobClient(mgmtClient, 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  	// check for it's existence:
    98  	exists, err := resourceAzureStorageBlobExists(d, meta)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	// if it exists; read relevant information:
   104  	if exists {
   105  		mgmtClient := meta.(*Client).mgmtClient
   106  		storName := d.Get("storage_service_name").(string)
   107  
   108  		blobClient, err := getStorageServiceBlobClient(mgmtClient, storName)
   109  		if err != nil {
   110  			return err
   111  		}
   112  
   113  		name := d.Get("name").(string)
   114  		cont := d.Get("storage_container_name").(string)
   115  		url := blobClient.GetBlobURL(cont, name)
   116  		d.Set("url", url)
   117  	}
   118  
   119  	// NOTE: no need to unset the ID here, as resourceAzureStorageBlobExists
   120  	// already should have done so if it were required.
   121  	return nil
   122  }
   123  
   124  // resourceAzureStorageBlobUpdate does all the necessary API calls to
   125  // update a blob on Azure.
   126  func resourceAzureStorageBlobUpdate(d *schema.ResourceData, meta interface{}) error {
   127  	// NOTE: although empty as most paramters have ForceNew set; this is
   128  	// still required in case of changes to the storage_service_key
   129  
   130  	// run the ExistsFunc beforehand to ensure the resource's existence nonetheless:
   131  	_, err := resourceAzureStorageBlobExists(d, meta)
   132  	return err
   133  }
   134  
   135  // resourceAzureStorageBlobExists does all the necessary API calls to
   136  // check for the existence of the blob on Azure.
   137  func resourceAzureStorageBlobExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   138  	mgmtClient := meta.(*Client).mgmtClient
   139  	storName := d.Get("storage_service_name").(string)
   140  
   141  	blobClient, err := getStorageServiceBlobClient(mgmtClient, storName)
   142  	if err != nil {
   143  		return false, err
   144  	}
   145  
   146  	log.Println("[INFO] Querying Azure for storage blob's existence.")
   147  	name := d.Get("name").(string)
   148  	cont := d.Get("storage_container_name").(string)
   149  	exists, err := blobClient.BlobExists(cont, name)
   150  	if err != nil {
   151  		return false, fmt.Errorf("Error whilst checking for Azure storage blob's existence: %s", err)
   152  	}
   153  
   154  	// if not found; it means it was deleted in the meantime and
   155  	// we must remove it from the schema.
   156  	if !exists {
   157  		d.SetId("")
   158  	}
   159  
   160  	return exists, nil
   161  }
   162  
   163  // resourceAzureStorageBlobDelete does all the necessary API calls to
   164  // delete the blob off Azure.
   165  func resourceAzureStorageBlobDelete(d *schema.ResourceData, meta interface{}) error {
   166  	mgmtClient := meta.(*Client).mgmtClient
   167  	storName := d.Get("storage_service_name").(string)
   168  
   169  	blobClient, err := getStorageServiceBlobClient(mgmtClient, storName)
   170  	if err != nil {
   171  		return err
   172  	}
   173  
   174  	log.Println("[INFO] Issuing storage blob delete command off Azure.")
   175  	name := d.Get("name").(string)
   176  	cont := d.Get("storage_container_name").(string)
   177  	if _, err = blobClient.DeleteBlobIfExists(cont, name); err != nil {
   178  		return fmt.Errorf("Error whilst deleting storage blob: %s", err)
   179  	}
   180  
   181  	d.SetId("")
   182  	return nil
   183  }