github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azure/resource_azure_storage_container.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 // resourceAzureStorageContainer returns the *schema.Resource associated 12 // to a storage container on Azure. 13 func resourceAzureStorageContainer() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAzureStorageContainerCreate, 16 Read: resourceAzureStorageContainerRead, 17 Exists: resourceAzureStorageContainerExists, 18 Delete: resourceAzureStorageContainerDelete, 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 "storage_service_name": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 Description: parameterDescriptions["storage_service_name"], 32 }, 33 "container_access_type": &schema.Schema{ 34 Type: schema.TypeString, 35 Required: true, 36 ForceNew: true, 37 Description: parameterDescriptions["container_access_type"], 38 }, 39 "properties": &schema.Schema{ 40 Type: schema.TypeMap, 41 Computed: true, 42 Elem: schema.TypeString, 43 Description: parameterDescriptions["properties"], 44 }, 45 }, 46 } 47 } 48 49 // resourceAzureStorageContainerCreate does all the necessary API calls to 50 // create the storage container on Azure. 51 func resourceAzureStorageContainerCreate(d *schema.ResourceData, meta interface{}) error { 52 azureClient := meta.(*Client) 53 storName := d.Get("storage_service_name").(string) 54 55 blobClient, err := azureClient.getStorageServiceBlobClient(storName) 56 if err != nil { 57 return err 58 } 59 60 log.Println("[INFO] Creating storage container on Azure.") 61 name := d.Get("name").(string) 62 accessType := storage.ContainerAccessType(d.Get("container_access_type").(string)) 63 err = blobClient.CreateContainer(name, accessType) 64 if err != nil { 65 return fmt.Errorf("Failed to create storage container on Azure: %s", err) 66 } 67 68 d.SetId(name) 69 return resourceAzureStorageContainerRead(d, meta) 70 } 71 72 // resourceAzureStorageContainerRead does all the necessary API calls to 73 // read the status of the storage container off Azure. 74 func resourceAzureStorageContainerRead(d *schema.ResourceData, meta interface{}) error { 75 azureClient := meta.(*Client) 76 storName := d.Get("storage_service_name").(string) 77 78 blobClient, err := azureClient.getStorageServiceBlobClient(storName) 79 if err != nil { 80 return err 81 } 82 83 log.Println("[INFO] Querying Azure for storage containers.") 84 name := d.Get("name").(string) 85 containers, err := blobClient.ListContainers(storage.ListContainersParameters{ 86 Prefix: name, 87 Timeout: 90, 88 }) 89 if err != nil { 90 return fmt.Errorf("Failed to query Azure for its storage containers: %s", err) 91 } 92 93 // search for our storage container and update its stats: 94 var found bool 95 // loop just to make sure we got the right container: 96 for _, cont := range containers.Containers { 97 if cont.Name == name { 98 found = true 99 100 props := make(map[string]interface{}) 101 props["last_modified"] = cont.Properties.LastModified 102 props["lease_status"] = cont.Properties.LeaseStatus 103 props["lease_state"] = cont.Properties.LeaseState 104 props["lease_duration"] = cont.Properties.LeaseDuration 105 106 d.Set("properties", props) 107 } 108 } 109 110 // if not found; it means the resource has been deleted 111 // in the meantime; so we must untrack it: 112 if !found { 113 d.SetId("") 114 } 115 116 return nil 117 } 118 119 // resourceAzureStorageContainerExists does all the necessary API calls to 120 // check if the storage container already exists on Azure. 121 func resourceAzureStorageContainerExists(d *schema.ResourceData, meta interface{}) (bool, error) { 122 azureClient := meta.(*Client) 123 storName := d.Get("storage_service_name").(string) 124 125 blobClient, err := azureClient.getStorageServiceBlobClient(storName) 126 if err != nil { 127 return false, err 128 } 129 130 log.Println("[INFO] Checking existence of storage container on Azure.") 131 name := d.Get("name").(string) 132 exists, err := blobClient.ContainerExists(name) 133 if err != nil { 134 return false, fmt.Errorf("Failed to query for Azure storage container existence: %s", err) 135 } 136 137 // if it does not exist; untrack the resource: 138 if !exists { 139 d.SetId("") 140 } 141 return exists, nil 142 } 143 144 // resourceAzureStorageContainerDelete does all the necessary API calls to 145 // delete a storage container off Azure. 146 func resourceAzureStorageContainerDelete(d *schema.ResourceData, meta interface{}) error { 147 azureClient := meta.(*Client) 148 storName := d.Get("storage_service_name").(string) 149 150 blobClient, err := azureClient.getStorageServiceBlobClient(storName) 151 if err != nil { 152 return err 153 } 154 155 log.Println("[INFO] Issuing Azure storage container deletion call.") 156 name := d.Get("name").(string) 157 if _, err := blobClient.DeleteContainerIfExists(name); err != nil { 158 return fmt.Errorf("Failed deleting storage container off Azure: %s", err) 159 } 160 161 d.SetId("") 162 return nil 163 }