github.com/keshavdv/terraform@v0.7.0-rc2.0.20160711232630-d69256dcb425/builtin/providers/azurerm/resource_arm_storage_blob.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func resourceArmStorageBlob() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceArmStorageBlobCreate, 14 Read: resourceArmStorageBlobRead, 15 Exists: resourceArmStorageBlobExists, 16 Delete: resourceArmStorageBlobDelete, 17 18 Schema: map[string]*schema.Schema{ 19 "name": { 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 }, 24 "resource_group_name": { 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 }, 29 "storage_account_name": { 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 }, 34 "storage_container_name": { 35 Type: schema.TypeString, 36 Required: true, 37 ForceNew: true, 38 }, 39 "type": { 40 Type: schema.TypeString, 41 Required: true, 42 ForceNew: true, 43 ValidateFunc: validateArmStorageBlobType, 44 }, 45 "size": { 46 Type: schema.TypeInt, 47 Optional: true, 48 ForceNew: true, 49 Default: 0, 50 ValidateFunc: validateArmStorageBlobSize, 51 }, 52 "url": { 53 Type: schema.TypeString, 54 Computed: true, 55 }, 56 }, 57 } 58 } 59 60 func validateArmStorageBlobSize(v interface{}, k string) (ws []string, errors []error) { 61 value := v.(int) 62 63 if value%512 != 0 { 64 errors = append(errors, fmt.Errorf("Blob Size %q is invalid, must be a multiple of 512", value)) 65 } 66 67 return 68 } 69 70 func validateArmStorageBlobType(v interface{}, k string) (ws []string, errors []error) { 71 value := strings.ToLower(v.(string)) 72 validTypes := map[string]struct{}{ 73 "blob": struct{}{}, 74 "page": struct{}{}, 75 } 76 77 if _, ok := validTypes[value]; !ok { 78 errors = append(errors, fmt.Errorf("Blob type %q is invalid, must be %q or %q", value, "blob", "page")) 79 } 80 return 81 } 82 83 func resourceArmStorageBlobCreate(d *schema.ResourceData, meta interface{}) error { 84 armClient := meta.(*ArmClient) 85 86 resourceGroupName := d.Get("resource_group_name").(string) 87 storageAccountName := d.Get("storage_account_name").(string) 88 89 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroupName, storageAccountName) 90 if err != nil { 91 return err 92 } 93 if !accountExists { 94 return fmt.Errorf("Storage Account %q Not Found", storageAccountName) 95 } 96 97 name := d.Get("name").(string) 98 blobType := d.Get("type").(string) 99 cont := d.Get("storage_container_name").(string) 100 101 log.Printf("[INFO] Creating blob %q in storage account %q", name, storageAccountName) 102 switch strings.ToLower(blobType) { 103 case "block": 104 err = blobClient.CreateBlockBlob(cont, name) 105 case "page": 106 size := int64(d.Get("size").(int)) 107 err = blobClient.PutPageBlob(cont, name, size, map[string]string{}) 108 } 109 if err != nil { 110 return fmt.Errorf("Error creating storage blob on Azure: %s", err) 111 } 112 113 d.SetId(name) 114 return resourceArmStorageBlobRead(d, meta) 115 } 116 117 func resourceArmStorageBlobRead(d *schema.ResourceData, meta interface{}) error { 118 armClient := meta.(*ArmClient) 119 120 resourceGroupName := d.Get("resource_group_name").(string) 121 storageAccountName := d.Get("storage_account_name").(string) 122 123 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroupName, storageAccountName) 124 if err != nil { 125 return err 126 } 127 if !accountExists { 128 log.Printf("[DEBUG] Storage account %q not found, removing blob %q from state", storageAccountName, d.Id()) 129 d.SetId("") 130 return nil 131 } 132 133 exists, err := resourceArmStorageBlobExists(d, meta) 134 if err != nil { 135 return err 136 } 137 138 if !exists { 139 // Exists already removed this from state 140 return nil 141 } 142 143 name := d.Get("name").(string) 144 storageContainerName := d.Get("storage_container_name").(string) 145 146 url := blobClient.GetBlobURL(storageContainerName, name) 147 if url == "" { 148 log.Printf("[INFO] URL for %q is empty", name) 149 } 150 d.Set("url", url) 151 152 return nil 153 } 154 155 func resourceArmStorageBlobExists(d *schema.ResourceData, meta interface{}) (bool, error) { 156 armClient := meta.(*ArmClient) 157 158 resourceGroupName := d.Get("resource_group_name").(string) 159 storageAccountName := d.Get("storage_account_name").(string) 160 161 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroupName, storageAccountName) 162 if err != nil { 163 return false, err 164 } 165 if !accountExists { 166 log.Printf("[DEBUG] Storage account %q not found, removing blob %q from state", storageAccountName, d.Id()) 167 d.SetId("") 168 return false, nil 169 } 170 171 name := d.Get("name").(string) 172 storageContainerName := d.Get("storage_container_name").(string) 173 174 log.Printf("[INFO] Checking for existence of storage blob %q.", name) 175 exists, err := blobClient.BlobExists(storageContainerName, name) 176 if err != nil { 177 return false, fmt.Errorf("error testing existence of storage blob %q: %s", name, err) 178 } 179 180 if !exists { 181 log.Printf("[INFO] Storage blob %q no longer exists, removing from state...", name) 182 d.SetId("") 183 } 184 185 return exists, nil 186 } 187 188 func resourceArmStorageBlobDelete(d *schema.ResourceData, meta interface{}) error { 189 armClient := meta.(*ArmClient) 190 191 resourceGroupName := d.Get("resource_group_name").(string) 192 storageAccountName := d.Get("storage_account_name").(string) 193 194 blobClient, accountExists, err := armClient.getBlobStorageClientForStorageAccount(resourceGroupName, storageAccountName) 195 if err != nil { 196 return err 197 } 198 if !accountExists { 199 log.Printf("[INFO]Storage Account %q doesn't exist so the blob won't exist", storageAccountName) 200 return nil 201 } 202 203 name := d.Get("name").(string) 204 storageContainerName := d.Get("storage_container_name").(string) 205 206 log.Printf("[INFO] Deleting storage blob %q", name) 207 if _, err = blobClient.DeleteBlobIfExists(storageContainerName, name, map[string]string{}); err != nil { 208 return fmt.Errorf("Error deleting storage blob %q: %s", name, err) 209 } 210 211 d.SetId("") 212 return nil 213 }