Skip to content

Commit

Permalink
Interim 2
Browse files Browse the repository at this point in the history
  • Loading branch information
favoretti committed Jan 19, 2024
1 parent 07672a7 commit 18967e1
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 135 deletions.
14 changes: 7 additions & 7 deletions internal/services/storage/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (sbu BlobUpload) copy(ctx context.Context) error {
CopySource: sbu.SourceUri,
MetaData: sbu.MetaData,
}
if err := sbu.Client.CopyAndWait(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input, pollingInterval); err != nil {
if err := sbu.Client.CopyAndWait(ctx, sbu.ContainerName, sbu.BlobName, input); err != nil {
return fmt.Errorf("copy/waiting: %s", err)
}

Expand All @@ -108,7 +108,7 @@ func (sbu BlobUpload) createEmptyAppendBlob(ctx context.Context) error {
ContentType: utils.String(sbu.ContentType),
MetaData: sbu.MetaData,
}
if _, err := sbu.Client.PutAppendBlob(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
if _, err := sbu.Client.PutAppendBlob(ctx, sbu.ContainerName, sbu.BlobName, input); err != nil {
return fmt.Errorf("PutAppendBlob: %s", err)
}

Expand All @@ -124,7 +124,7 @@ func (sbu BlobUpload) createEmptyBlockBlob(ctx context.Context) error {
ContentType: utils.String(sbu.ContentType),
MetaData: sbu.MetaData,
}
if _, err := sbu.Client.PutBlockBlob(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
if _, err := sbu.Client.PutBlockBlob(ctx, sbu.ContainerName, sbu.BlobName, input); err != nil {
return fmt.Errorf("PutBlockBlob: %s", err)
}

Expand Down Expand Up @@ -161,7 +161,7 @@ func (sbu BlobUpload) uploadBlockBlob(ctx context.Context) error {
if sbu.ContentMD5 != "" {
input.ContentMD5 = utils.String(sbu.ContentMD5)
}
if err := sbu.Client.PutBlockBlobFromFile(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, file, input); err != nil {
if err := sbu.Client.PutBlockBlobFromFile(ctx, sbu.ContainerName, sbu.BlobName, file, input); err != nil {
return fmt.Errorf("PutBlockBlobFromFile: %s", err)
}

Expand All @@ -178,7 +178,7 @@ func (sbu BlobUpload) createEmptyPageBlob(ctx context.Context) error {
ContentType: utils.String(sbu.ContentType),
MetaData: sbu.MetaData,
}
if _, err := sbu.Client.PutPageBlob(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
if _, err := sbu.Client.PutPageBlob(ctx, sbu.ContainerName, sbu.BlobName, input); err != nil {
return fmt.Errorf("PutPageBlob: %s", err)
}

Expand Down Expand Up @@ -228,7 +228,7 @@ func (sbu BlobUpload) uploadPageBlob(ctx context.Context) error {
ContentType: utils.String(sbu.ContentType),
MetaData: sbu.MetaData,
}
if _, err := sbu.Client.PutPageBlob(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
if _, err := sbu.Client.PutPageBlob(ctx, sbu.ContainerName, sbu.BlobName, input); err != nil {
return fmt.Errorf("PutPageBlob: %s", err)
}

Expand Down Expand Up @@ -373,7 +373,7 @@ func (sbu BlobUpload) blobPageUploadWorker(ctx context.Context, uploadCtx blobPa
Content: chunk,
}

if _, err := sbu.Client.PutPageUpdate(ctx, sbu.AccountName, sbu.ContainerName, sbu.BlobName, input); err != nil {
if _, err := sbu.Client.PutPageUpdate(ctx, sbu.ContainerName, sbu.BlobName, input); err != nil {
uploadCtx.errors <- fmt.Errorf("writing page at offset %d for file %q: %s", page.offset, sbu.Source, err)
uploadCtx.wg.Done()
continue
Expand Down
23 changes: 20 additions & 3 deletions internal/services/storage/migration/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/tombuildsstuff/giovanni/storage/2020-08-04/blob/accounts"
"github.com/tombuildsstuff/giovanni/storage/2020-08-04/file/shares"
)

Expand Down Expand Up @@ -60,10 +61,26 @@ func (s ShareV1ToV2) UpgradeFunc() pluginsdk.StateUpgraderFunc {
shareName := parsedId[0]
accountName := parsedId[2]

environment := meta.(*clients.Client).Account.AzureEnvironment
client := shares.NewWithEnvironment(environment)
storageClient := meta.(*clients.Client).Storage
account, err := storageClient.FindAccount(ctx, accountName)
if err != nil {
return rawState, fmt.Errorf("retrieving Account %q for Share: %s", accountName, err)
}
if account == nil {
return rawState, fmt.Errorf("Unable to locate Storage Account %q!", accountName)
}

domainSuffix, ok := meta.(*clients.Client).Account.Environment.Storage.DomainSuffix()
if !ok {
return rawState, fmt.Errorf("retrieving domain suffix for Storage Accounts")
}

accountId, err := accounts.ParseAccountID(account.ID, *domainSuffix)
if err != nil {
return rawState, fmt.Errorf("parsing account ID %s: %+v", account.ID, err)
}

newResourceId := client.GetResourceID(accountName, shareName)
newResourceId := shares.NewShareID(*accountId, shareName)
log.Printf("[DEBUG] Updating Resource ID from %q to %q", id, newResourceId)

rawState["id"] = newResourceId
Expand Down
67 changes: 0 additions & 67 deletions internal/services/storage/parse/storage_container_data_plane.go

This file was deleted.

21 changes: 16 additions & 5 deletions internal/services/storage/storage_blob_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/giovanni/storage/2020-08-04/blob/accounts"
"github.com/tombuildsstuff/giovanni/storage/2020-08-04/blob/blobs"
)

Expand Down Expand Up @@ -76,6 +77,11 @@ func dataSourceStorageBlobRead(d *pluginsdk.ResourceData, meta interface{}) erro
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

domainSuffix, ok := meta.(*clients.Client).Account.Environment.Storage.DomainSuffix()
if !ok {
return fmt.Errorf("retrieving domain suffix for Storage Accounts")
}

accountName := d.Get("storage_account_name").(string)
containerName := d.Get("storage_container_name").(string)
name := d.Get("name").(string)
Expand All @@ -88,18 +94,23 @@ func dataSourceStorageBlobRead(d *pluginsdk.ResourceData, meta interface{}) erro
return fmt.Errorf("Unable to locate Storage Account %q!", accountName)
}

accountId, err := accounts.ParseAccountID(account.ID, *domainSuffix)
if err != nil {
return fmt.Errorf("parsing account ID %s: %+v", account.ID, err)
}

blobsClient, err := storageClient.BlobsClient(ctx, *account)
if err != nil {
return fmt.Errorf("building Blobs Client: %s", err)
}

id := blobsClient.GetResourceID(accountName, containerName, name)
id := blobs.NewBlobID(*accountId, containerName, name)

log.Printf("[INFO] Retrieving Storage Blob %q (Container %q / Account %q).", name, containerName, accountName)
input := blobs.GetPropertiesInput{}
props, err := blobsClient.GetProperties(ctx, accountName, containerName, name, input)
props, err := blobsClient.GetProperties(ctx, containerName, name, input)
if err != nil {
if utils.ResponseWasNotFound(props.Response) {
if response.WasNotFound(props.HttpResponse.Response) {
return fmt.Errorf("the Blob %q was not found in Container %q / Account %q", name, containerName, accountName)
}

Expand All @@ -125,7 +136,7 @@ func dataSourceStorageBlobRead(d *pluginsdk.ResourceData, meta interface{}) erro

d.Set("type", strings.TrimSuffix(string(props.BlobType), "Blob"))

d.SetId(id)
d.SetId(id.ID())

d.Set("url", id)

Expand Down
17 changes: 14 additions & 3 deletions internal/services/storage/storage_container_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/tombuildsstuff/giovanni/storage/2020-08-04/blob/accounts"
"github.com/tombuildsstuff/giovanni/storage/2020-08-04/blob/containers"
)

func dataSourceStorageContainer() *pluginsdk.Resource {
Expand Down Expand Up @@ -64,6 +65,11 @@ func dataSourceStorageContainerRead(d *pluginsdk.ResourceData, meta interface{})
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

domainSuffix, ok := meta.(*clients.Client).Account.Environment.Storage.DomainSuffix()
if !ok {
return fmt.Errorf("retrieving domain suffix for Storage Accounts")
}

containerName := d.Get("name").(string)
accountName := d.Get("storage_account_name").(string)

Expand All @@ -75,15 +81,20 @@ func dataSourceStorageContainerRead(d *pluginsdk.ResourceData, meta interface{})
return fmt.Errorf("Unable to locate Account %q for Storage Container %q", accountName, containerName)
}

accountId, err := accounts.ParseAccountID(account.ID, *domainSuffix)
if err != nil {
return fmt.Errorf("parsing account ID %s: %+v", account.ID, err)
}

client, err := storageClient.ContainersClient(ctx, *account)
if err != nil {
return fmt.Errorf("building Containers Client for Storage Account %q (Resource Group %q): %s", accountName, account.ResourceGroup, err)
}

id := parse.NewStorageContainerDataPlaneId(accountName, storageClient.Environment.StorageEndpointSuffix, containerName).ID()
id := containers.NewContainerID(*accountId, containerName).ID()
d.SetId(id)

props, err := client.Get(ctx, account.ResourceGroup, accountName, containerName)
props, err := client.Get(ctx, account.ResourceGroup, containerName)
if err != nil {
return fmt.Errorf("retrieving Container %q (Account %q / Resource Group %q): %s", containerName, accountName, account.ResourceGroup, err)
}
Expand Down
38 changes: 25 additions & 13 deletions internal/services/storage/storage_container_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/giovanni/storage/2020-08-04/blob/containers"
)

type StorageContainerResource struct{}
Expand Down Expand Up @@ -187,47 +187,59 @@ func TestAccStorageContainer_web(t *testing.T) {
}

func (r StorageContainerResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.StorageContainerDataPlaneID(state.ID)
domainSuffix, ok := client.Account.Environment.Storage.DomainSuffix()
if !ok {
return nil, fmt.Errorf("retrieving domain suffix for Storage Accounts")
}

id, err := containers.ParseContainerID(state.ID, *domainSuffix)
if err != nil {
return nil, err
}
account, err := client.Storage.FindAccount(ctx, id.AccountName)

account, err := client.Storage.FindAccount(ctx, id.AccountId.AccountName)
if err != nil {
return nil, fmt.Errorf("retrieving Account %q for Container %q: %+v", id.AccountName, id.Name, err)
return nil, fmt.Errorf("retrieving Account %q for Container %q: %+v", id.AccountId.AccountName, id.ContainerName, err)
}
if account == nil {
return nil, fmt.Errorf("unable to locate Storage Account %q", id.AccountName)
return nil, fmt.Errorf("unable to locate Storage Account %q", id.AccountId.AccountName)
}

containersClient, err := client.Storage.ContainersClient(ctx, *account)
if err != nil {
return nil, fmt.Errorf("building Containers Client: %+v", err)
}
prop, err := containersClient.Get(ctx, account.ResourceGroup, id.AccountName, id.Name)
prop, err := containersClient.Get(ctx, account.ResourceGroup, id.ContainerName)
if err != nil {
return nil, fmt.Errorf("retrieving Container %q (Account %q / Resource Group %q): %+v", id.Name, id.AccountName, account.ResourceGroup, err)
return nil, fmt.Errorf("retrieving Container %q (Account %q / Resource Group %q): %+v", id.ContainerName, id.AccountId.AccountName, account.ResourceGroup, err)
}
return utils.Bool(prop != nil), nil
}

func (r StorageContainerResource) Destroy(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.StorageContainerDataPlaneID(state.ID)
domainSuffix, ok := client.Account.Environment.Storage.DomainSuffix()
if !ok {
return nil, fmt.Errorf("retrieving domain suffix for Storage Accounts")
}

id, err := containers.ParseContainerID(state.ID, *domainSuffix)
if err != nil {
return nil, err
}
account, err := client.Storage.FindAccount(ctx, id.AccountName)

account, err := client.Storage.FindAccount(ctx, id.AccountId.AccountName)
if err != nil {
return nil, fmt.Errorf("retrieving Account %q for Container %q: %+v", id.AccountName, id.Name, err)
return nil, fmt.Errorf("retrieving Account %q for Container %q: %+v", id.AccountId.AccountName, id.ContainerName, err)
}
if account == nil {
return nil, fmt.Errorf("unable to locate Storage Account %q", id.AccountName)
return nil, fmt.Errorf("unable to locate Storage Account %q", id.AccountId.AccountName)
}
containersClient, err := client.Storage.ContainersClient(ctx, *account)
if err != nil {
return nil, fmt.Errorf("building Containers Client: %+v", err)
}
if err := containersClient.Delete(ctx, account.ResourceGroup, id.AccountName, id.Name); err != nil {
return nil, fmt.Errorf("deleting Container %q (Account %q / Resource Group %q): %+v", id.Name, id.AccountName, account.ResourceGroup, err)
if err := containersClient.Delete(ctx, account.ResourceGroup, id.ContainerName); err != nil {
return nil, fmt.Errorf("deleting Container %q (Account %q / Resource Group %q): %+v", id.ContainerName, id.AccountId.AccountName, account.ResourceGroup, err)
}
return utils.Bool(true), nil
}
Expand Down
Loading

0 comments on commit 18967e1

Please sign in to comment.