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

feat(storage_blob): implement cache_control #13946

Merged
merged 3 commits into from Nov 2, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/services/storage/blobs.go
Expand Up @@ -27,6 +27,7 @@ type BlobUpload struct {
ContainerName string

BlobType string
CacheControl string
ContentType string
ContentMD5 string
MetaData map[string]string
Expand Down
19 changes: 19 additions & 0 deletions internal/services/storage/storage_blob_resource.go
Expand Up @@ -97,6 +97,11 @@ func resourceStorageBlob() *pluginsdk.Resource {
Default: "application/octet-stream",
},

"cache_control": {
secustor marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeString,
Optional: true,
},

"source": {
Type: pluginsdk.TypeString,
Optional: true,
Expand Down Expand Up @@ -199,6 +204,7 @@ func resourceStorageBlobCreate(d *pluginsdk.ResourceData, meta interface{}) erro
Client: blobsClient,

BlobType: d.Get("type").(string),
CacheControl: d.Get("cache_control").(string),
ContentType: d.Get("content_type").(string),
ContentMD5: contentMD5,
MetaData: ExpandMetaData(metaDataRaw),
Expand Down Expand Up @@ -287,6 +293,18 @@ func resourceStorageBlobUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
log.Printf("[DEBUG] Updated MetaData for Blob %q (Container %q / Account %q).", id.BlobName, id.ContainerName, id.AccountName)
}

if d.HasChange("cache_control") {
log.Printf("[DEBUG] Updating Cache Control for Blob %q (Container %q / Account %q)...", id.BlobName, id.ContainerName, id.AccountName)
input := blobs.SetPropertiesInput{
CacheControl: utils.String(d.Get("cache_control").(string)),
}

if _, err := blobsClient.SetProperties(ctx, id.AccountName, id.ContainerName, id.BlobName, input); err != nil {
return fmt.Errorf("updating Cache Control for Blob %q (Container %q / Account %q): %s", id.BlobName, id.ContainerName, id.AccountName, err)
}
log.Printf("[DEBUG] Updated Cache Control for Blob %q (Container %q / Account %q).", id.BlobName, id.ContainerName, id.AccountName)
}

return resourceStorageBlobRead(d, meta)
}

Expand Down Expand Up @@ -334,6 +352,7 @@ func resourceStorageBlobRead(d *pluginsdk.ResourceData, meta interface{}) error

d.Set("access_tier", string(props.AccessTier))
d.Set("content_type", props.ContentType)
d.Set("cache_control", props.CacheControl)

// Set the ContentMD5 value to md5 hash in hex
contentMD5 := ""
Expand Down
43 changes: 43 additions & 0 deletions internal/services/storage/storage_blob_resource_test.go
Expand Up @@ -243,6 +243,28 @@ func TestAccStorageBlob_blockFromLocalFileWithContentMd5(t *testing.T) {
})
}

func TestAccStorageBlob_cacheControl(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_storage_blob", "test")
r := StorageBlobResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.cacheControl(data, "no-cache"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("parallelism", "size", "type"),
{
Config: r.cacheControl(data, "max-age=3600"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("parallelism", "size", "type"),
})
}

func TestAccStorageBlob_contentType(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_storage_blob", "test")
r := StorageBlobResource{}
Expand Down Expand Up @@ -987,6 +1009,27 @@ resource "azurerm_storage_blob" "test" {
`, template)
}

func (r StorageBlobResource) cacheControl(data acceptance.TestData, cacheControl string) string {
template := r.template(data, "private")
return fmt.Sprintf(`
%s

provider "azurerm" {
features {}
}

resource "azurerm_storage_blob" "test" {
name = "example.ext"
storage_account_name = azurerm_storage_account.test.name
storage_container_name = azurerm_storage_container.test.name
type = "Page"
size = 5120
content_type = "image/png"
cache_control = "%s"
}
`, template, cacheControl)
}

func (r StorageBlobResource) template(data acceptance.TestData, accessLevel string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/storage_blob.html.markdown
Expand Up @@ -60,6 +60,8 @@ The following arguments are supported:

* `access_tier` - (Optional) The access tier of the storage blob. Possible values are `Archive`, `Cool` and `Hot`.

* `cache_control` - (Optional) Controls the [cache control header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) content of the response when blob is requested .

* `content_type` - (Optional) The content type of the storage blob. Cannot be defined if `source_uri` is defined. Defaults to `application/octet-stream`.

* `content_md5` - (Optional) The MD5 sum of the blob contents. Cannot be defined if `source_uri` is defined, or if blob type is Append or Page. Changing this forces a new resource to be created.
Expand Down