github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azure/resource_azure_storage_blob.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 // resourceAzureStorageBlob returns the *schema.Resource associated 12 // with a storage blob on Azure. 13 func resourceAzureStorageBlob() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAzureStorageBlobCreate, 16 Read: resourceAzureStorageBlobRead, 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 78 container := blobClient.GetContainerReference(cont) 79 blob := container.GetBlobReference(name) 80 81 switch blobType { 82 case "BlockBlob": 83 options := &storage.PutBlobOptions{} 84 err = blob.CreateBlockBlob(options) 85 case "PageBlob": 86 size := int64(d.Get("size").(int)) 87 options := &storage.PutBlobOptions{} 88 blob.Properties.ContentLength = size 89 err = blob.PutPageBlob(options) 90 default: 91 err = fmt.Errorf("Invalid blob type specified; see parameter desciptions for more info.") 92 } 93 if err != nil { 94 return fmt.Errorf("Error creating storage blob on Azure: %s", err) 95 } 96 97 d.SetId(name) 98 return resourceAzureStorageBlobRead(d, meta) 99 } 100 101 // resourceAzureStorageBlobRead does all the necessary API calls to 102 // read the status of the storage blob off Azure. 103 func resourceAzureStorageBlobRead(d *schema.ResourceData, meta interface{}) error { 104 azureClient := meta.(*Client) 105 106 // check for it's existence: 107 exists, err := resourceAzureStorageBlobExists(d, meta) 108 if err != nil { 109 return err 110 } 111 112 // if it exists; read relevant information: 113 if exists { 114 storName := d.Get("storage_service_name").(string) 115 116 blobClient, err := azureClient.getStorageServiceBlobClient(storName) 117 if err != nil { 118 return err 119 } 120 121 name := d.Get("name").(string) 122 cont := d.Get("storage_container_name").(string) 123 container := blobClient.GetContainerReference(cont) 124 blob := container.GetBlobReference(name) 125 url := blob.GetURL() 126 d.Set("url", url) 127 } 128 129 // NOTE: no need to unset the ID here, as resourceAzureStorageBlobExists 130 // already should have done so if it were required. 131 return nil 132 } 133 134 // resourceAzureStorageBlobExists does all the necessary API calls to 135 // check for the existence of the blob on Azure. 136 func resourceAzureStorageBlobExists(d *schema.ResourceData, meta interface{}) (bool, error) { 137 azureClient := meta.(*Client) 138 storName := d.Get("storage_service_name").(string) 139 140 blobClient, err := azureClient.getStorageServiceBlobClient(storName) 141 if err != nil { 142 return false, err 143 } 144 145 log.Println("[INFO] Querying Azure for storage blob's existence.") 146 name := d.Get("name").(string) 147 cont := d.Get("storage_container_name").(string) 148 container := blobClient.GetContainerReference(cont) 149 blob := container.GetBlobReference(name) 150 exists, err := blob.Exists() 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 179 container := blobClient.GetContainerReference(cont) 180 blob := container.GetBlobReference(name) 181 182 options := &storage.DeleteBlobOptions{} 183 _, err = blob.DeleteIfExists(options) 184 if err != nil { 185 return fmt.Errorf("Error whilst deleting storage blob: %s", err) 186 } 187 188 d.SetId("") 189 return nil 190 }