github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 Exists: resourceAzureStorageBlobExists, 17 Delete: resourceAzureStorageBlobDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "name": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 Description: parameterDescriptions["name"], 25 }, 26 "type": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 Description: parameterDescriptions["type"], 31 }, 32 "size": &schema.Schema{ 33 Type: schema.TypeInt, 34 Required: true, 35 ForceNew: true, 36 DefaultFunc: func() (interface{}, error) { 37 return int64(0), nil 38 }, 39 }, 40 "storage_container_name": &schema.Schema{ 41 Type: schema.TypeString, 42 Required: true, 43 ForceNew: true, 44 Description: parameterDescriptions["storage_container_name"], 45 }, 46 "storage_service_name": { 47 Type: schema.TypeString, 48 Required: true, 49 ForceNew: true, 50 Description: parameterDescriptions["storage_service_name"], 51 }, 52 "url": &schema.Schema{ 53 Type: schema.TypeString, 54 Computed: true, 55 Description: parameterDescriptions["url"], 56 }, 57 }, 58 } 59 } 60 61 // resourceAzureStorageBlobCreate does all the necessary API calls to 62 // create the storage blob on Azure. 63 func resourceAzureStorageBlobCreate(d *schema.ResourceData, meta interface{}) error { 64 azureClient := meta.(*Client) 65 storName := d.Get("storage_service_name").(string) 66 67 blobClient, err := azureClient.getStorageServiceBlobClient(storName) 68 if err != nil { 69 return err 70 } 71 72 log.Println("[INFO] Issuing create on Azure storage blob.") 73 name := d.Get("name").(string) 74 blobType := d.Get("type").(string) 75 cont := d.Get("storage_container_name").(string) 76 switch blobType { 77 case "BlockBlob": 78 err = blobClient.CreateBlockBlob(cont, name) 79 case "PageBlob": 80 size := int64(d.Get("size").(int)) 81 err = blobClient.PutPageBlob(cont, name, size, map[string]string{}) 82 default: 83 err = fmt.Errorf("Invalid blob type specified; see parameter desciptions for more info.") 84 } 85 if err != nil { 86 return fmt.Errorf("Error creating storage blob on Azure: %s", err) 87 } 88 89 d.SetId(name) 90 return resourceAzureStorageBlobRead(d, meta) 91 } 92 93 // resourceAzureStorageBlobRead does all the necessary API calls to 94 // read the status of the storage blob off Azure. 95 func resourceAzureStorageBlobRead(d *schema.ResourceData, meta interface{}) error { 96 azureClient := meta.(*Client) 97 98 // check for it's existence: 99 exists, err := resourceAzureStorageBlobExists(d, meta) 100 if err != nil { 101 return err 102 } 103 104 // if it exists; read relevant information: 105 if exists { 106 storName := d.Get("storage_service_name").(string) 107 108 blobClient, err := azureClient.getStorageServiceBlobClient(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 // resourceAzureStorageBlobExists does all the necessary API calls to 125 // check for the existence of the blob on Azure. 126 func resourceAzureStorageBlobExists(d *schema.ResourceData, meta interface{}) (bool, error) { 127 azureClient := meta.(*Client) 128 storName := d.Get("storage_service_name").(string) 129 130 blobClient, err := azureClient.getStorageServiceBlobClient(storName) 131 if err != nil { 132 return false, err 133 } 134 135 log.Println("[INFO] Querying Azure for storage blob's existence.") 136 name := d.Get("name").(string) 137 cont := d.Get("storage_container_name").(string) 138 exists, err := blobClient.BlobExists(cont, name) 139 if err != nil { 140 return false, fmt.Errorf("Error whilst checking for Azure storage blob's existence: %s", err) 141 } 142 143 // if not found; it means it was deleted in the meantime and 144 // we must remove it from the schema. 145 if !exists { 146 d.SetId("") 147 } 148 149 return exists, nil 150 } 151 152 // resourceAzureStorageBlobDelete does all the necessary API calls to 153 // delete the blob off Azure. 154 func resourceAzureStorageBlobDelete(d *schema.ResourceData, meta interface{}) error { 155 azureClient := meta.(*Client) 156 storName := d.Get("storage_service_name").(string) 157 158 blobClient, err := azureClient.getStorageServiceBlobClient(storName) 159 if err != nil { 160 return err 161 } 162 163 log.Println("[INFO] Issuing storage blob delete command off Azure.") 164 name := d.Get("name").(string) 165 cont := d.Get("storage_container_name").(string) 166 if _, err = blobClient.DeleteBlobIfExists(cont, name, make(map[string]string)); err != nil { 167 return fmt.Errorf("Error whilst deleting storage blob: %s", err) 168 } 169 170 d.SetId("") 171 return nil 172 }