diff --git a/azurerm/resource_arm_storage_blob.go b/azurerm/resource_arm_storage_blob.go index 9dd53a057da1..db005454b934 100644 --- a/azurerm/resource_arm_storage_blob.go +++ b/azurerm/resource_arm_storage_blob.go @@ -114,6 +114,15 @@ func resourceArmStorageBlob() *schema.Resource { ForceNew: true, ValidateFunc: validation.IntAtLeast(1), }, + + "metadata": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validate.NoEmptyStrings, + }, + }, }, } } @@ -201,6 +210,13 @@ func resourceArmStorageBlobCreate(d *schema.ResourceData, meta interface{}) erro } } + blob.Metadata = expandStorageAccountBlobMetadata(d) + + opts := &storage.SetBlobMetadataOptions{} + if err := blob.SetMetadata(opts); err != nil { + return fmt.Errorf("Error setting metadata for storage blob on Azure: %s", err) + } + d.SetId(id) return resourceArmStorageBlobRead(d, meta) } @@ -564,6 +580,15 @@ func resourceArmStorageBlobUpdate(d *schema.ResourceData, meta interface{}) erro return fmt.Errorf("Error setting properties of blob %s (container %s, storage account %s): %+v", id.blobName, id.containerName, id.storageAccountName, err) } + if d.HasChange("metadata") { + blob.Metadata = expandStorageAccountBlobMetadata(d) + + opts := &storage.SetBlobMetadataOptions{} + if err := blob.SetMetadata(opts); err != nil { + return fmt.Errorf("Error setting metadata for storage blob on Azure: %s", err) + } + } + return nil } @@ -615,6 +640,12 @@ func resourceArmStorageBlobRead(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("Error getting properties of blob %s (container %s, storage account %s): %+v", id.blobName, id.containerName, id.storageAccountName, err) } + metadataOptions := &storage.GetBlobMetadataOptions{} + err = blob.GetMetadata(metadataOptions) + if err != nil { + return fmt.Errorf("Error getting metadata of blob %s (container %s, storage account %s): %+v", id.blobName, id.containerName, id.storageAccountName, err) + } + d.Set("name", id.blobName) d.Set("storage_container_name", id.containerName) d.Set("storage_account_name", id.storageAccountName) @@ -632,6 +663,7 @@ func resourceArmStorageBlobRead(d *schema.ResourceData, meta interface{}) error log.Printf("[INFO] URL for %q is empty", id.blobName) } d.Set("url", u) + d.Set("metadata", flattenStorageAccountBlobMetadata(blob.Metadata)) return nil } @@ -730,3 +762,23 @@ func determineResourceGroupForStorageAccount(accountName string, client *ArmClie return nil, nil } + +func expandStorageAccountBlobMetadata(d *schema.ResourceData) storage.BlobMetadata { + blobMetadata := make(map[string]string) + + blobMetadataRaw := d.Get("metadata").(map[string]interface{}) + for key, value := range blobMetadataRaw { + blobMetadata[key] = value.(string) + } + return storage.BlobMetadata(blobMetadata) +} + +func flattenStorageAccountBlobMetadata(in storage.BlobMetadata) map[string]interface{} { + blobMetadata := make(map[string]interface{}) + + for key, value := range in { + blobMetadata[key] = value + } + + return blobMetadata +} diff --git a/azurerm/resource_arm_storage_blob_test.go b/azurerm/resource_arm_storage_blob_test.go index 33035fee4eeb..28a447ded0c7 100644 --- a/azurerm/resource_arm_storage_blob_test.go +++ b/azurerm/resource_arm_storage_blob_test.go @@ -31,6 +31,9 @@ func TestAccAzureRMStorageBlob_basic(t *testing.T) { Config: testAccAzureRMStorageBlob_basic(ri, rs, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMStorageBlobExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "metadata.%", "2"), + resource.TestCheckResourceAttr(resourceName, "metadata.test", "value1"), + resource.TestCheckResourceAttr(resourceName, "metadata.test2", "value2"), ), }, { @@ -491,6 +494,11 @@ resource "azurerm_storage_blob" "test" { type = "page" size = 5120 + + metadata { + test = "value1" + test2 = "value2" + } } `, rInt, location, rString) } diff --git a/website/docs/r/storage_blob.html.markdown b/website/docs/r/storage_blob.html.markdown index 2a37a1ab0d45..8d66ba48366a 100644 --- a/website/docs/r/storage_blob.html.markdown +++ b/website/docs/r/storage_blob.html.markdown @@ -75,6 +75,8 @@ The following arguments are supported: * `attempts` - (Optional) The number of attempts to make per page or block when uploading. Defaults to `1`. +* `metadata` - (Optional) A map of custom blob metadata. + ## Attributes Reference The following attributes are exported in addition to the arguments listed above: