Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blob metadata #3206

Merged
merged 8 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions azurerm/resource_arm_storage_blob.go
Original file line number Diff line number Diff line change
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,
},
Lucretius marked this conversation as resolved.
Show resolved Hide resolved
},
},
}
}
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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