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 1 commit
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
1 change: 1 addition & 0 deletions internal/services/storage/blobs.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 Properties for Blob %q (Container %q / Account %q)...", id.BlobName, id.ContainerName, id.AccountName)
secustor marked this conversation as resolved.
Show resolved Hide resolved
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 Properties for Blob %q (Container %q / Account %q): %s", id.BlobName, id.ContainerName, id.AccountName, err)
secustor marked this conversation as resolved.
Show resolved Hide resolved
}
log.Printf("[DEBUG] Updated Properties for Blob %q (Container %q / Account %q).", id.BlobName, id.ContainerName, id.AccountName)
secustor marked this conversation as resolved.
Show resolved Hide resolved
}

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
Original file line number Diff line number Diff line change
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.contentType(data),
secustor marked this conversation as resolved.
Show resolved Hide resolved
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("parallelism", "size", "type"),
{
Config: r.contentTypeUpdated(data),
secustor marked this conversation as resolved.
Show resolved Hide resolved
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) 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 = "no-cache"
}
`, template)
}

func (r StorageBlobResource) template(data acceptance.TestData, accessLevel string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down