Skip to content

Commit

Permalink
Merge pull request #24200 from DrFaust92/data-sync-fsx
Browse files Browse the repository at this point in the history
r/datasync_location_fsx_openzfs_file_system - new resource
  • Loading branch information
ewbankkit authored Apr 13, 2022
2 parents fade619 + ddfec40 commit 8393fa9
Show file tree
Hide file tree
Showing 8 changed files with 750 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/#24200.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_datasync_location_fsx_openzfs_file_system
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,7 @@ func Provider() *schema.Provider {
"aws_datasync_agent": datasync.ResourceAgent(),
"aws_datasync_location_efs": datasync.ResourceLocationEFS(),
"aws_datasync_location_fsx_lustre_file_system": datasync.ResourceLocationFSxLustreFileSystem(),
"aws_datasync_location_fsx_openzfs_file_system": datasync.ResourceLocationFSxOpenZfsFileSystem(),
"aws_datasync_location_fsx_windows_file_system": datasync.ResourceLocationFSxWindowsFileSystem(),
"aws_datasync_location_hdfs": datasync.ResourceLocationHdfs(),
"aws_datasync_location_nfs": datasync.ResourceLocationNFS(),
Expand Down
25 changes: 25 additions & 0 deletions internal/service/datasync/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,28 @@ func FindFsxLustreLocationByARN(conn *datasync.DataSync, arn string) (*datasync.

return output, nil
}

func FindFsxOpenZfsLocationByARN(conn *datasync.DataSync, arn string) (*datasync.DescribeLocationFsxOpenZfsOutput, error) {
input := &datasync.DescribeLocationFsxOpenZfsInput{
LocationArn: aws.String(arn),
}

output, err := conn.DescribeLocationFsxOpenZfs(input)

if tfawserr.ErrMessageContains(err, datasync.ErrCodeInvalidRequestException, "not found") {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}
298 changes: 298 additions & 0 deletions internal/service/datasync/location_fsx_openzfs_file_system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
package datasync

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

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/datasync"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func ResourceLocationFSxOpenZfsFileSystem() *schema.Resource {
return &schema.Resource{
Create: resourceLocationFSxOpenZfsFileSystemCreate,
Read: resourceLocationFSxOpenZfsFileSystemRead,
Update: resourceLocationFSxOpenZfsFileSystemUpdate,
Delete: resourceLocationFSxOpenZfsFileSystemDelete,
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: verify.ValidARN,
},
"protocol": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"nfs": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"mount_options": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"version": {
Type: schema.TypeString,
Default: datasync.NfsVersionAutomatic,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(datasync.NfsVersion_Values(), false),
},
},
},
},
},
},
},
},
},
},
"security_group_arns": {
Type: schema.TypeSet,
Required: true,
ForceNew: true,
MinItems: 1,
MaxItems: 5,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: verify.ValidARN,
},
},
"subdirectory": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 4096),
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
"uri": {
Type: schema.TypeString,
Computed: true,
},
"creation_time": {
Type: schema.TypeString,
Computed: true,
},
},

CustomizeDiff: verify.SetTagsDiff,
}
}

func resourceLocationFSxOpenZfsFileSystemCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).DataSyncConn
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))
fsxArn := d.Get("fsx_filesystem_arn").(string)

input := &datasync.CreateLocationFsxOpenZfsInput{
FsxFilesystemArn: aws.String(fsxArn),
Protocol: expandDataSyncProtocol(d.Get("protocol").([]interface{})),
SecurityGroupArns: flex.ExpandStringSet(d.Get("security_group_arns").(*schema.Set)),
Tags: Tags(tags.IgnoreAWS()),
}

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

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

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

return resourceLocationFSxOpenZfsFileSystemRead(d, meta)
}

func resourceLocationFSxOpenZfsFileSystemRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).DataSyncConn
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

output, err := FindFsxOpenZfsLocationByARN(conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] DataSync Location Fsx OpenZfs (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

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

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

if err != nil {
return err
}

d.Set("arn", output.LocationArn)
d.Set("subdirectory", subdirectory)
d.Set("uri", output.LocationUri)

if err := d.Set("security_group_arns", flex.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)
}

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

tags, err := ListTags(conn, d.Id())

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

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

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

return nil
}

func resourceLocationFSxOpenZfsFileSystemUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).DataSyncConn

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

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

return resourceLocationFSxOpenZfsFileSystemRead(d, meta)
}

func resourceLocationFSxOpenZfsFileSystemDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).DataSyncConn

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

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

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

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

return nil
}

func expandDataSyncProtocol(l []interface{}) *datasync.FsxProtocol {
if len(l) == 0 || l[0] == nil {
return nil
}

m := l[0].(map[string]interface{})

Protocol := &datasync.FsxProtocol{
NFS: expandDataSyncNFS(m["nfs"].([]interface{})),
}

return Protocol
}

func flattenDataSyncProtocol(protocol *datasync.FsxProtocol) []interface{} {
if protocol == nil {
return []interface{}{}
}

m := map[string]interface{}{
"nfs": flattenDataSyncNFS(protocol.NFS),
}

return []interface{}{m}
}

func expandDataSyncNFS(l []interface{}) *datasync.FsxProtocolNfs {
if len(l) == 0 || l[0] == nil {
return nil
}

m := l[0].(map[string]interface{})

Protocol := &datasync.FsxProtocolNfs{
MountOptions: expandDataSyncNfsMountOptions(m["mount_options"].([]interface{})),
}

return Protocol
}

func flattenDataSyncNFS(nfs *datasync.FsxProtocolNfs) []interface{} {
if nfs == nil {
return []interface{}{}
}

m := map[string]interface{}{
"mount_options": flattenDataSyncNfsMountOptions(nfs.MountOptions),
}

return []interface{}{m}
}
Loading

0 comments on commit 8393fa9

Please sign in to comment.