github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/builtin/providers/azurerm/resource_arm_storage_container.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/storage"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceArmStorageContainer() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceArmStorageContainerCreate,
    15  		Read:   resourceArmStorageContainerRead,
    16  		Exists: resourceArmStorageContainerExists,
    17  		Delete: resourceArmStorageContainerDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  			"resource_group_name": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  			"storage_account_name": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  			"container_access_type": &schema.Schema{
    36  				Type:         schema.TypeString,
    37  				Optional:     true,
    38  				ForceNew:     true,
    39  				Default:      "private",
    40  				ValidateFunc: validateArmStorageContainerAccessType,
    41  			},
    42  			"properties": &schema.Schema{
    43  				Type:     schema.TypeMap,
    44  				Computed: true,
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  func validateArmStorageContainerAccessType(v interface{}, k string) (ws []string, errors []error) {
    51  	value := strings.ToLower(v.(string))
    52  	validTypes := map[string]struct{}{
    53  		"private":   struct{}{},
    54  		"blob":      struct{}{},
    55  		"container": struct{}{},
    56  	}
    57  
    58  	if _, ok := validTypes[value]; !ok {
    59  		errors = append(errors, fmt.Errorf("Storage container access type %q is invalid, must be %q, %q or %q", value, "private", "blob", "page"))
    60  	}
    61  	return
    62  }
    63  
    64  func resourceArmStorageContainerCreate(d *schema.ResourceData, meta interface{}) error {
    65  	armClient := meta.(*ArmClient)
    66  
    67  	resourceGroupName := d.Get("resource_group_name").(string)
    68  	storageAccountName := d.Get("storage_account_name").(string)
    69  
    70  	blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroupName, storageAccountName)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	name := d.Get("name").(string)
    76  
    77  	var accessType storage.ContainerAccessType
    78  	if d.Get("container_access_type").(string) == "private" {
    79  		accessType = storage.ContainerAccessType("")
    80  	} else {
    81  		accessType = storage.ContainerAccessType(d.Get("container_access_type").(string))
    82  	}
    83  
    84  	log.Printf("[INFO] Creating container %q in storage account %q.", name, storageAccountName)
    85  	_, err = blobClient.CreateContainerIfNotExists(name, accessType)
    86  	if err != nil {
    87  		return fmt.Errorf("Error creating container %q in storage account %q: %s", name, storageAccountName, err)
    88  	}
    89  
    90  	d.SetId(name)
    91  	return resourceArmStorageContainerRead(d, meta)
    92  }
    93  
    94  // resourceAzureStorageContainerRead does all the necessary API calls to
    95  // read the status of the storage container off Azure.
    96  func resourceArmStorageContainerRead(d *schema.ResourceData, meta interface{}) error {
    97  	armClient := meta.(*ArmClient)
    98  
    99  	resourceGroupName := d.Get("resource_group_name").(string)
   100  	storageAccountName := d.Get("storage_account_name").(string)
   101  
   102  	blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroupName, storageAccountName)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	name := d.Get("name").(string)
   108  	containers, err := blobClient.ListContainers(storage.ListContainersParameters{
   109  		Prefix:  name,
   110  		Timeout: 90,
   111  	})
   112  	if err != nil {
   113  		return fmt.Errorf("Failed to retrieve storage containers in account %q: %s", name, err)
   114  	}
   115  
   116  	var found bool
   117  	for _, cont := range containers.Containers {
   118  		if cont.Name == name {
   119  			found = true
   120  
   121  			props := make(map[string]interface{})
   122  			props["last_modified"] = cont.Properties.LastModified
   123  			props["lease_status"] = cont.Properties.LeaseStatus
   124  			props["lease_state"] = cont.Properties.LeaseState
   125  			props["lease_duration"] = cont.Properties.LeaseDuration
   126  
   127  			d.Set("properties", props)
   128  		}
   129  	}
   130  
   131  	if !found {
   132  		log.Printf("[INFO] Storage container %q does not exist in account %q, removing from state...", name, storageAccountName)
   133  		d.SetId("")
   134  	}
   135  
   136  	return nil
   137  }
   138  
   139  func resourceArmStorageContainerExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   140  	armClient := meta.(*ArmClient)
   141  
   142  	resourceGroupName := d.Get("resource_group_name").(string)
   143  	storageAccountName := d.Get("storage_account_name").(string)
   144  
   145  	blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroupName, storageAccountName)
   146  	if err != nil {
   147  		return false, err
   148  	}
   149  
   150  	name := d.Get("name").(string)
   151  
   152  	log.Printf("[INFO] Checking existence of storage container %q in storage account %q", name, storageAccountName)
   153  	exists, err := blobClient.ContainerExists(name)
   154  	if err != nil {
   155  		return false, fmt.Errorf("Error querying existence of storage container %q in storage account %q: %s", name, storageAccountName, err)
   156  	}
   157  
   158  	if !exists {
   159  		log.Printf("[INFO] Storage container %q does not exist in account %q, removing from state...", name, storageAccountName)
   160  		d.SetId("")
   161  	}
   162  
   163  	return exists, nil
   164  }
   165  
   166  // resourceAzureStorageContainerDelete does all the necessary API calls to
   167  // delete a storage container off Azure.
   168  func resourceArmStorageContainerDelete(d *schema.ResourceData, meta interface{}) error {
   169  	armClient := meta.(*ArmClient)
   170  
   171  	resourceGroupName := d.Get("resource_group_name").(string)
   172  	storageAccountName := d.Get("storage_account_name").(string)
   173  
   174  	blobClient, err := armClient.getBlobStorageClientForStorageAccount(resourceGroupName, storageAccountName)
   175  	if err != nil {
   176  		return err
   177  	}
   178  
   179  	name := d.Get("name").(string)
   180  
   181  	log.Printf("[INFO] Deleting storage container %q in account %q", name, storageAccountName)
   182  	if _, err := blobClient.DeleteContainerIfExists(name); err != nil {
   183  		return fmt.Errorf("Error deleting storage container %q from storage account %q: %s", name, storageAccountName, err)
   184  	}
   185  
   186  	d.SetId("")
   187  	return nil
   188  }