Skip to content

Commit

Permalink
Allow blob upload in archive tier (#22250)
Browse files Browse the repository at this point in the history
* Allow blob upload in archive tier

Changed the order in which the access tier change is done, to avoid issues when blobs are uploaded in the archive tier.

* add test

* fmt

---------

Co-authored-by: magodo <[email protected]>
  • Loading branch information
ppodgorsek and magodo authored Jan 25, 2024
1 parent a9979d5 commit 141640f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
24 changes: 12 additions & 12 deletions internal/services/storage/storage_blob_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,6 @@ func resourceStorageBlobUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
return fmt.Errorf("building Blobs Client: %s", err)
}

if d.HasChange("access_tier") {
// this is only applicable for Gen2/BlobStorage accounts
log.Printf("[DEBUG] Updating Access Tier for Blob %q (Container %q / Account %q)...", id.BlobName, id.ContainerName, id.AccountName)
accessTier := blobs.AccessTier(d.Get("access_tier").(string))

if _, err := blobsClient.SetTier(ctx, id.AccountName, id.ContainerName, id.BlobName, accessTier); err != nil {
return fmt.Errorf("updating Access Tier for Blob %q (Container %q / Account %q): %s", id.BlobName, id.ContainerName, id.AccountName, err)
}

log.Printf("[DEBUG] Updated Access Tier for Blob %q (Container %q / Account %q).", id.BlobName, id.ContainerName, id.AccountName)
}

if d.HasChange("content_type") || d.HasChange("cache_control") {
log.Printf("[DEBUG] Updating Properties for Blob %q (Container %q / Account %q)...", id.BlobName, id.ContainerName, id.AccountName)
input := blobs.SetPropertiesInput{
Expand Down Expand Up @@ -309,6 +297,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("access_tier") {
// this is only applicable for Gen2/BlobStorage accounts
log.Printf("[DEBUG] Updating Access Tier for Blob %q (Container %q / Account %q)...", id.BlobName, id.ContainerName, id.AccountName)
accessTier := blobs.AccessTier(d.Get("access_tier").(string))

if _, err := blobsClient.SetTier(ctx, id.AccountName, id.ContainerName, id.BlobName, accessTier); err != nil {
return fmt.Errorf("updating Access Tier for Blob %q (Container %q / Account %q): %s", id.BlobName, id.ContainerName, id.AccountName, err)
}

log.Printf("[DEBUG] Updated Access Tier for Blob %q (Container %q / Account %q).", id.BlobName, id.ContainerName, id.AccountName)
}

return resourceStorageBlobRead(d, meta)
}

Expand Down
33 changes: 33 additions & 0 deletions internal/services/storage/storage_blob_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,21 @@ func TestAccStorageBlob_update(t *testing.T) {
})
}

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

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.archive(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("parallelism", "size", "type", "source_content"),
})
}

func (r StorageBlobResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := blobs.ParseResourceID(state.ID)
if err != nil {
Expand Down Expand Up @@ -1152,6 +1167,24 @@ resource "azurerm_storage_container" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString, accessLevel)
}

func (r StorageBlobResource) archive(data acceptance.TestData) string {
template := r.template(data, "private")
return fmt.Sprintf(`
provider "azurerm" {}
%s
resource "azurerm_storage_blob" "test" {
name = "rick.morty"
storage_account_name = azurerm_storage_account.test.name
storage_container_name = azurerm_storage_container.test.name
type = "Block"
source_content = "Wubba Lubba Dub Dub"
access_tier = "Archive"
}
`, template)
}

func populateTempFile(input *os.File) error {
if err := input.Truncate(25*1024*1024 + 512); err != nil {
return fmt.Errorf("Failed to truncate file to 25M")
Expand Down

0 comments on commit 141640f

Please sign in to comment.