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/datasync_location_fsx_windows - add resource #12686

Merged
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
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ func Provider() *schema.Provider {
"aws_datapipeline_pipeline": resourceAwsDataPipelinePipeline(),
"aws_datasync_agent": resourceAwsDataSyncAgent(),
"aws_datasync_location_efs": resourceAwsDataSyncLocationEfs(),
"aws_datasync_location_fsx_windows_file_system": resourceAwsDataSyncLocationFsxWindowsFileSystem(),
"aws_datasync_location_nfs": resourceAwsDataSyncLocationNfs(),
"aws_datasync_location_s3": resourceAwsDataSyncLocationS3(),
"aws_datasync_location_smb": resourceAwsDataSyncLocationSmb(),
Expand Down
218 changes: 218 additions & 0 deletions aws/resource_aws_datasync_location_fsx_windows_file_system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package aws

import (
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/datasync"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsDataSyncLocationFsxWindowsFileSystem() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDataSyncLocationFsxWindowsFileSystemCreate,
Read: resourceAwsDataSyncLocationFsxWindowsFileSystemRead,
Update: resourceAwsDataSyncLocationFsxWindowsFileSystemUpdate,
Delete: resourceAwsDataSyncLocationFsxWindowsFileSystemDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "#")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("Unexpected format of ID (%q), expected DataSyncLocationArn#FsxArn", d.Id())
}

DSArn := idParts[0]
FSxArn := idParts[1]

d.Set("fsx_filesystem_arn", FSxArn)
d.SetId(DSArn)

return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"fsx_filesystem_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArn,
},
"password": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringLenBetween(1, 104),
},
"user": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 104),
},
"domain": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 253),
},
"security_group_arns": {
Type: schema.TypeSet,
Required: true,
ForceNew: true,
MinItems: 1,
MaxItems: 5,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateArn,
},
},
"subdirectory": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 4096),
},
"tags": tagsSchema(),
"uri": {
Type: schema.TypeString,
Computed: true,
},
"creation_time": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAwsDataSyncLocationFsxWindowsFileSystemCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).datasyncconn
fsxArn := d.Get("fsx_filesystem_arn").(string)

input := &datasync.CreateLocationFsxWindowsInput{
FsxFilesystemArn: aws.String(fsxArn),
User: aws.String(d.Get("user").(string)),
Password: aws.String(d.Get("password").(string)),
SecurityGroupArns: expandStringSet(d.Get("security_group_arns").(*schema.Set)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().DatasyncTags(),
}

if v, ok := d.GetOk("subdirectory"); ok {
input.Subdirectory = aws.String(v.(string))
}

if v, ok := d.GetOk("domain"); ok {
input.Domain = aws.String(v.(string))
}

log.Printf("[DEBUG] Creating DataSync Location Fsx Windows File System: %#v", input)
output, err := conn.CreateLocationFsxWindows(input)
if err != nil {
return fmt.Errorf("error creating DataSync Location Fsx Windows File System: %w", err)
}

d.SetId(aws.StringValue(output.LocationArn))

return resourceAwsDataSyncLocationFsxWindowsFileSystemRead(d, meta)
}

func resourceAwsDataSyncLocationFsxWindowsFileSystemRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).datasyncconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

input := &datasync.DescribeLocationFsxWindowsInput{
LocationArn: aws.String(d.Id()),
}

log.Printf("[DEBUG] Reading DataSync Location Fsx Windows: %#v", input)
output, err := conn.DescribeLocationFsxWindows(input)

if isAWSErr(err, datasync.ErrCodeInvalidRequestException, "not found") {
log.Printf("[WARN] DataSync Location Fsx Windows %q not found - removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading DataSync Location Fsx Windows (%s): %w", d.Id(), err)
}

subdirectory, err := dataSyncParseLocationURI(aws.StringValue(output.LocationUri))

if err != nil {
return fmt.Errorf("error parsing Location Fsx Windows File System (%s) URI (%s): %w", d.Id(), aws.StringValue(output.LocationUri), err)
}

d.Set("arn", output.LocationArn)
d.Set("subdirectory", subdirectory)
d.Set("uri", output.LocationUri)
d.Set("user", output.User)
d.Set("domain", output.Domain)

if err := d.Set("security_group_arns", flattenStringSet(output.SecurityGroupArns)); err != nil {
return fmt.Errorf("error setting security_group_arns: %w", err)
}

if err := d.Set("creation_time", output.CreationTime.Format(time.RFC3339)); err != nil {
return fmt.Errorf("error setting creation_time: %w", err)
}

tags, err := keyvaluetags.DatasyncListTags(conn, d.Id())

if err != nil {
return fmt.Errorf("error listing tags for DataSync Location Fsx Windows (%s): %w", d.Id(), err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

return nil
}

func resourceAwsDataSyncLocationFsxWindowsFileSystemUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).datasyncconn

if d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := keyvaluetags.DatasyncUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating DataSync Location Fsx Windows File System (%s) tags: %w", d.Id(), err)
}
}

return resourceAwsDataSyncLocationFsxWindowsFileSystemRead(d, meta)
}

func resourceAwsDataSyncLocationFsxWindowsFileSystemDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).datasyncconn

input := &datasync.DeleteLocationInput{
LocationArn: aws.String(d.Id()),
}

log.Printf("[DEBUG] Deleting DataSync Location Fsx Windows File System: %#v", input)
_, err := conn.DeleteLocation(input)

if isAWSErr(err, datasync.ErrCodeInvalidRequestException, "not found") {
return nil
}

if err != nil {
return fmt.Errorf("error deleting DataSync Location Fsx Windows (%s): %w", d.Id(), err)
}

return nil
}
Loading