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

New Resource: azurerm_storage_share_file #9406

Merged
merged 13 commits into from
Dec 8, 2020
19 changes: 19 additions & 0 deletions azurerm/internal/services/storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/tombuildsstuff/giovanni/storage/2019-12-12/datalakestore/filesystems"
"github.com/tombuildsstuff/giovanni/storage/2019-12-12/datalakestore/paths"
"github.com/tombuildsstuff/giovanni/storage/2019-12-12/file/directories"
"github.com/tombuildsstuff/giovanni/storage/2019-12-12/file/files"
"github.com/tombuildsstuff/giovanni/storage/2019-12-12/file/shares"
"github.com/tombuildsstuff/giovanni/storage/2019-12-12/queue/queues"
"github.com/tombuildsstuff/giovanni/storage/2019-12-12/table/entities"
Expand Down Expand Up @@ -174,6 +175,24 @@ func (client Client) FileShareDirectoriesClient(ctx context.Context, account acc
return &directoriesClient, nil
}

func (client Client) FileShareFilesClient(ctx context.Context, account accountDetails) (*files.Client, error) {
// NOTE: Files do not support AzureAD Authentication

accountKey, err := account.AccountKey(ctx, client)
if err != nil {
return nil, fmt.Errorf("Error retrieving Account Key: %s", err)
}

storageAuth, err := autorest.NewSharedKeyAuthorizer(account.name, *accountKey, autorest.SharedKeyLite)
if err != nil {
return nil, fmt.Errorf("Error building Authorizer: %+v", err)
}

filesClient := files.NewWithEnvironment(client.Environment)
filesClient.Client.Authorizer = storageAuth
return &filesClient, nil
}

func (client Client) FileSharesClient(ctx context.Context, account accountDetails) (shim.StorageShareWrapper, error) {
// NOTE: Files do not support AzureAD Authentication

Expand Down
1 change: 1 addition & 0 deletions azurerm/internal/services/storage/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
"azurerm_storage_management_policy": resourceArmStorageManagementPolicy(),
"azurerm_storage_queue": resourceArmStorageQueue(),
"azurerm_storage_share": resourceArmStorageShare(),
"azurerm_storage_share_file": resourceArmStorageShareFile(),
"azurerm_storage_share_directory": resourceArmStorageShareDirectory(),
"azurerm_storage_table": resourceArmStorageTable(),
"azurerm_storage_table_entity": resourceArmStorageTableEntity(),
Expand Down
327 changes: 327 additions & 0 deletions azurerm/internal/services/storage/resource_arm_storage_share_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@
package storage

import (
"fmt"
"log"
"os"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"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/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
"github.com/tombuildsstuff/giovanni/storage/2019-12-12/file/files"
)

func resourceArmStorageShareFile() *schema.Resource {
return &schema.Resource{
Create: resourceArmStorageShareFileCreate,
Read: resourceArmStorageShareFileRead,
Update: resourceArmStorageShareFileUpdate,
Delete: resourceArmStorageShareFileDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
// TODO: add validation
},
"share_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"storage_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably replace these two with file_share_id here and determine the rest tbh

"directory_name": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the SDK makes this path - it may be worth doing the same here?

Type: schema.TypeString,
ForceNew: true,
Optional: true,
Default: "",
ValidateFunc: validate.StorageShareDirectoryName,
},

"content_type": {
Type: schema.TypeString,
Optional: true,
Default: "application/octet-stream",
},

"content_encoding": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"content_md5": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"content_disposition": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"source": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
ForceNew: true,
},

"parallelism": {
Type: schema.TypeInt,
Optional: true,
Default: 4,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should be able to remove this since it's unused


"metadata": MetaDataSchema(),
},
}
}

func resourceArmStorageShareFileCreate(d *schema.ResourceData, meta interface{}) error {
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
storageClient := meta.(*clients.Client).Storage

accountName := d.Get("storage_account_name").(string)
shareName := d.Get("share_name").(string)
fileName := d.Get("name").(string)
directoryName := d.Get("directory_name").(string)

account, err := storageClient.FindAccount(ctx, accountName)
if err != nil {
return fmt.Errorf("Error retrieving Account %q for File %q (Share %q): %s", accountName, fileName, shareName, err)
}
if account == nil {
return fmt.Errorf("Unable to locate Storage Account %q!", accountName)
}

client, err := storageClient.FileShareFilesClient(ctx, *account)
if err != nil {
return fmt.Errorf("Error building File Share Directories Client: %s", err)
}

existing, err := client.GetProperties(ctx, accountName, shareName, directoryName, fileName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing File %q (File Share %q / Storage Account %q / Resource Group %q): %s", fileName, shareName, accountName, account.ResourceGroup, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
id := client.GetResourceID(accountName, shareName, directoryName, fileName)
return tf.ImportAsExistsError("azurerm_storage_share_file", id)
}

input := files.CreateInput{
MetaData: ExpandMetaData(d.Get("metadata").(map[string]interface{})),
ContentType: utils.String(d.Get("content_type").(string)),
ContentEncoding: utils.String(d.Get("content_encoding").(string)),
ContentDisposition: utils.String(d.Get("content_disposition").(string)),
}

if v, ok := d.GetOk("content_md5"); ok {
input.ContentMD5 = utils.String(v.(string))
}

var file *os.File
if v, ok := d.GetOk("source"); ok {
file, err = os.Open(v.(string))
if err != nil {
return fmt.Errorf("opening file : %s", err)
}

info, err := file.Stat()
if err != nil {
return fmt.Errorf("'stat'-ing File %q (File Share %q / Account %q): %+v", fileName, shareName, accountName, err)
}

input.ContentLength = info.Size()
}

if _, err := client.Create(ctx, accountName, shareName, directoryName, fileName, input); err != nil {
return fmt.Errorf("creating File %q (File Share %q / Account %q): %+v", fileName, shareName, accountName, err)
}

if file != nil {
if err := client.PutFile(ctx, accountName, shareName, directoryName, fileName, file, 4); err != nil {
return fmt.Errorf("uploading File: %q (File Share %q / Account %q): %+v", fileName, shareName, accountName, err)
}
}

resourceID := client.GetResourceID(accountName, shareName, directoryName, fileName)
d.SetId(resourceID)

return resourceArmStorageShareFileRead(d, meta)
}

func resourceArmStorageShareFileUpdate(d *schema.ResourceData, meta interface{}) error {
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
storageClient := meta.(*clients.Client).Storage

id, err := files.ParseResourceID(d.Id())
if err != nil {
return err
}

account, err := storageClient.FindAccount(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error retrieving Account %q for File %q (Share %q): %s", id.AccountName, id.FileName, id.ShareName, err)
}
if account == nil {
return fmt.Errorf("Unable to locate Storage Account %q!", id.AccountName)
}

client, err := storageClient.FileShareFilesClient(ctx, *account)
if err != nil {
return fmt.Errorf("Error building File Share Directories Client: %s", err)
}

existing, err := client.GetProperties(ctx, id.AccountName, id.ShareName, id.DirectoryName, id.FileName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing File %q (File Share %q / Storage Account %q / Resource Group %q): %s", id.FileName, id.ShareName, id.AccountName, account.ResourceGroup, err)
}
}

if d.HasChange("content_type") || d.HasChange("content_encoding") || d.HasChange("content_disposition") || d.HasChange("content_md5") {
input := files.SetPropertiesInput{
ContentType: utils.String(d.Get("content_type").(string)),
ContentEncoding: utils.String(d.Get("content_encoding").(string)),
ContentDisposition: utils.String(d.Get("content_disposition").(string)),
MetaData: ExpandMetaData(d.Get("metadata").(map[string]interface{})),
}

if v, ok := d.GetOk("content_md5"); ok {
input.ContentMD5 = utils.String(v.(string))
}

if _, err := client.SetProperties(ctx, id.AccountName, id.ShareName, id.DirectoryName, id.FileName, input); err != nil {
return fmt.Errorf("Error creating File %q (File Share %q / Account %q): %+v", id.FileName, id.ShareName, id.AccountName, err)
}
}

/*
metaDataRaw := d.Get("metadata").(map[string]interface{})
metaData := ExpandMetaData(metaDataRaw)

if _, err := client.SetMetaData(ctx, id.AccountName, id.ShareName, id.DirectoryName, id.FileName, metaData); err != nil {
return fmt.Errorf("updating MetaData for File %q (File Share %q / Account %q): %+v", id.FileName, id.ShareName, id.AccountName, err)
}*/

return resourceArmStorageShareFileRead(d, meta)
}

func resourceArmStorageShareFileRead(d *schema.ResourceData, meta interface{}) error {
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
storageClient := meta.(*clients.Client).Storage

id, err := files.ParseResourceID(d.Id())
if err != nil {
return err
}

account, err := storageClient.FindAccount(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error retrieving Account %q for File %q (Share %q): %s", id.AccountName, id.FileName, id.ShareName, err)
}
if account == nil {
log.Printf("[WARN] Unable to determine Resource Group for Storage Share File %q (Share %s, Account %s) - assuming removed & removing from state", id.FileName, id.ShareName, id.AccountName)
d.SetId("")
return nil
}

client, err := storageClient.FileShareFilesClient(ctx, *account)
if err != nil {
return fmt.Errorf("Error building File Share Client for Storage Account %q (Resource Group %q): %s", id.AccountName, account.ResourceGroup, err)
}

props, err := client.GetProperties(ctx, id.AccountName, id.ShareName, id.DirectoryName, id.FileName)
if err != nil {
return fmt.Errorf("Error retrieving Storage Share %q (File Share %q / Account %q / Resource Group %q): %s", id.DirectoryName, id.ShareName, id.AccountName, account.ResourceGroup, err)
}

d.Set("name", id.FileName)
d.Set("directory_name", id.DirectoryName)
d.Set("share_name", id.ShareName)
d.Set("storage_account_name", id.AccountName)

if err := d.Set("metadata", FlattenMetaData(props.MetaData)); err != nil {
return fmt.Errorf("Error setting `metadata`: %s", err)
}
d.Set("content_type", props.ContentType)
d.Set("content_encoding", props.ContentEncoding)
d.Set("content_md5", props.ContentMD5)
d.Set("content_disposition", props.ContentDisposition)

return nil
}

func resourceArmStorageShareFileDelete(d *schema.ResourceData, meta interface{}) error {
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
storageClient := meta.(*clients.Client).Storage

id, err := files.ParseResourceID(d.Id())
if err != nil {
return err
}

account, err := storageClient.FindAccount(ctx, id.AccountName)
if err != nil {
return fmt.Errorf("Error retrieving Account %q for File %q (Share %q): %s", id.AccountName, id.FileName, id.ShareName, err)
}
if account == nil {
return fmt.Errorf("Unable to locate Storage Account %q!", id.AccountName)
}

client, err := storageClient.FileShareFilesClient(ctx, *account)
if err != nil {
return fmt.Errorf("Error building File Share File Client for Storage Account %q (Resource Group %q): %s", id.AccountName, account.ResourceGroup, err)
}

if _, err := client.Delete(ctx, id.AccountName, id.ShareName, id.DirectoryName, id.FileName); err != nil {
return fmt.Errorf("Error deleting Storage Share File %q (File Share %q / Account %q / Resource Group %q): %s", id.FileName, id.ShareName, id.AccountName, account.ResourceGroup, err)
}

return nil
}

/*
func storageShareDirectoryRefreshFunc(ctx context.Context, client *directories.Client, accountName, shareName, directoryName string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Get(ctx, accountName, shareName, directoryName)
if err != nil {
return nil, strconv.Itoa(res.StatusCode), fmt.Errorf("Error retrieving Directory %q (File Share %q / Account %q): %s", directoryName, shareName, accountName, err)
}

return res, strconv.Itoa(res.StatusCode), nil
}
}
*/
Loading