Skip to content

Commit

Permalink
Blob metadata (#3206)
Browse files Browse the repository at this point in the history
Resolves #1400
  • Loading branch information
Lucretius authored and katbyte committed Apr 9, 2019
1 parent 86a55a5 commit e6fbb47
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
52 changes: 52 additions & 0 deletions azurerm/resource_arm_storage_blob.go
Expand Up @@ -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,
},
},
},
}
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions azurerm/resource_arm_storage_blob_test.go
Expand Up @@ -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"),
),
},
{
Expand Down Expand Up @@ -491,6 +494,11 @@ resource "azurerm_storage_blob" "test" {
type = "page"
size = 5120
metadata {
test = "value1"
test2 = "value2"
}
}
`, rInt, location, rString)
}
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/storage_blob.html.markdown
Expand Up @@ -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:
Expand Down

0 comments on commit e6fbb47

Please sign in to comment.