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

r/storage_blob: support for access_tier, append blobs and source_content #4238

Merged
merged 7 commits into from
Sep 5, 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
70 changes: 57 additions & 13 deletions azurerm/internal/services/storage/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
"strings"
Expand All @@ -24,25 +25,35 @@ type BlobUpload struct {
BlobName string
ContainerName string

BlobType string
ContentType string
MetaData map[string]string
Parallelism int
Size int
Source string
SourceUri string
BlobType string
ContentType string
MetaData map[string]string
Parallelism int
Size int
Source string
SourceContent string
SourceUri string
}

func (sbu BlobUpload) Create(ctx context.Context) error {
if sbu.SourceUri != "" {
return sbu.copy(ctx)
}

blobType := strings.ToLower(sbu.BlobType)

// TODO: new feature for 'append' blobs?
if blobType == "append" {
if sbu.Source != "" || sbu.SourceContent != "" || sbu.SourceUri != "" {
return fmt.Errorf("A source cannot be specified for an Append blob")
}

return sbu.createEmptyAppendBlob(ctx)
}

if blobType == "block" {
if sbu.SourceUri != "" {
return sbu.copy(ctx)
}

if sbu.SourceContent != "" {
return sbu.uploadBlockBlobFromContent(ctx)
}
if sbu.Source != "" {
return sbu.uploadBlockBlob(ctx)
}
Expand All @@ -51,6 +62,12 @@ func (sbu BlobUpload) Create(ctx context.Context) error {
}

if blobType == "page" {
if sbu.SourceUri != "" {
return sbu.copy(ctx)
}
if sbu.SourceContent != "" {
return fmt.Errorf("`source_content` cannot be specified for a Page blob")
}
if sbu.Source != "" {
return sbu.uploadPageBlob(ctx)
}
Expand All @@ -73,6 +90,18 @@ func (sbu BlobUpload) copy(ctx context.Context) error {
return nil
}

func (sbu BlobUpload) createEmptyAppendBlob(ctx context.Context) error {
input := blobs.PutAppendBlobInput{
ContentType: utils.String(sbu.ContentType),
MetaData: sbu.MetaData,
}
if _, err := sbu.Client.PutAppendBlob(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
return fmt.Errorf("Error PutAppendBlob: %s", err)
}

return nil
}

func (sbu BlobUpload) createEmptyBlockBlob(ctx context.Context) error {
input := blobs.PutBlockBlobInput{
ContentType: utils.String(sbu.ContentType),
Expand All @@ -85,6 +114,22 @@ func (sbu BlobUpload) createEmptyBlockBlob(ctx context.Context) error {
return nil
}

func (sbu BlobUpload) uploadBlockBlobFromContent(ctx context.Context) error {
tmpFile, err := ioutil.TempFile(os.TempDir(), "upload-")
if err != nil {
return fmt.Errorf("Error creating temporary file: %s", err)
}
defer os.Remove(tmpFile.Name())

if _, err = tmpFile.Write([]byte(sbu.SourceContent)); err != nil {
return fmt.Errorf("Error writing Source Content to Temp File: %s", err)
}
defer tmpFile.Close()

sbu.Source = tmpFile.Name()
return sbu.uploadBlockBlob(ctx)
}

func (sbu BlobUpload) uploadBlockBlob(ctx context.Context) error {
file, err := os.Open(sbu.Source)
if err != nil {
Expand Down Expand Up @@ -147,7 +192,6 @@ func (sbu BlobUpload) uploadPageBlob(ctx context.Context) error {
ContentType: utils.String(sbu.ContentType),
MetaData: sbu.MetaData,
}
// TODO: access tiers?
if _, err := sbu.Client.PutPageBlob(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
return fmt.Errorf("Error PutPageBlob: %s", err)
}
Expand Down
94 changes: 63 additions & 31 deletions azurerm/resource_arm_storage_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
Expand Down Expand Up @@ -37,24 +38,29 @@ func resourceArmStorageBlob() *schema.Resource {
},

"storage_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
// TODO: add validation
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmStorageAccountName,
},

"storage_container_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
// TODO: add validation
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmStorageContainerName,
},

"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"block", "page"}, true),
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: suppress.CaseDifference, // TODO: remove in 2.0
ValidateFunc: validation.StringInSlice([]string{
"Append",
"Block",
"Page",
}, true),
},

"size": {
Expand All @@ -65,6 +71,17 @@ func resourceArmStorageBlob() *schema.Resource {
ValidateFunc: validate.IntDivisibleBy(512),
},

"access_tier": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
string(blobs.Archive),
string(blobs.Cool),
string(blobs.Hot),
}, false),
},

"content_type": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -75,14 +92,21 @@ func resourceArmStorageBlob() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"source_uri"},
ConflictsWith: []string{"source_uri", "source_content"},
},

"source_content": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"source", "source_uri"},
},

"source_uri": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"source"},
ConflictsWith: []string{"source", "source_content"},
},

"url": {
Expand Down Expand Up @@ -159,13 +183,14 @@ func resourceArmStorageBlobCreate(d *schema.ResourceData, meta interface{}) erro
BlobName: name,
Client: blobsClient,

BlobType: d.Get("type").(string),
ContentType: d.Get("content_type").(string),
MetaData: storage.ExpandMetaData(metaDataRaw),
Parallelism: d.Get("parallelism").(int),
Size: d.Get("size").(int),
Source: d.Get("source").(string),
SourceUri: d.Get("source_uri").(string),
BlobType: d.Get("type").(string),
ContentType: d.Get("content_type").(string),
MetaData: storage.ExpandMetaData(metaDataRaw),
Parallelism: d.Get("parallelism").(int),
Size: d.Get("size").(int),
Source: d.Get("source").(string),
SourceContent: d.Get("source_content").(string),
SourceUri: d.Get("source_uri").(string),
}
if err := blobInput.Create(ctx); err != nil {
return fmt.Errorf("Error creating Blob %q (Container %q / Account %q): %s", name, containerName, accountName, err)
Expand Down Expand Up @@ -199,7 +224,17 @@ func resourceArmStorageBlobUpdate(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("Error building Blobs Client: %s", err)
}

// TODO: changing the access tier
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("Error 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") {
log.Printf("[DEBUG] Updating Properties for Blob %q (Container %q / Account %q)...", id.BlobName, id.ContainerName, id.AccountName)
Expand Down Expand Up @@ -269,23 +304,20 @@ func resourceArmStorageBlobRead(d *schema.ResourceData, meta interface{}) error
d.Set("storage_account_name", id.AccountName)
d.Set("resource_group_name", resourceGroup)

d.Set("access_tier", string(props.AccessTier))
d.Set("content_type", props.ContentType)
d.Set("type", strings.TrimSuffix(string(props.BlobType), "Blob"))
d.Set("url", d.Id())

if err := d.Set("metadata", storage.FlattenMetaData(props.MetaData)); err != nil {
return fmt.Errorf("Error setting `metadata`: %+v", err)
}
// The CopySource is only returned if the blob hasn't been modified (e.g. metadata configured etc)
// as such, we need to conditionally set this to ensure it's trackable if possible
if props.CopySource != "" {
d.Set("source_uri", props.CopySource)
}

blobType := strings.ToLower(strings.Replace(string(props.BlobType), "Blob", "", 1))
d.Set("type", blobType)

d.Set("url", d.Id())

if err := d.Set("metadata", storage.FlattenMetaData(props.MetaData)); err != nil {
return fmt.Errorf("Error setting `metadata`: %+v", err)
}

return nil
}

Expand Down
Loading