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

aws_datasync_location_smb addition #10381

Merged
merged 7 commits into from
Feb 5, 2020
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
26 changes: 26 additions & 0 deletions aws/datasync.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ func expandDataSyncEc2Config(l []interface{}) *datasync.Ec2Config {
return ec2Config
}

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

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

smbMountOptions := &datasync.SmbMountOptions{
Version: aws.String(m["version"].(string)),
}

return smbMountOptions
}

func expandDataSyncOnPremConfig(l []interface{}) *datasync.OnPremConfig {
if len(l) == 0 || l[0] == nil {
return nil
Expand Down Expand Up @@ -99,6 +113,18 @@ func flattenDataSyncEc2Config(ec2Config *datasync.Ec2Config) []interface{} {
return []interface{}{m}
}

func flattenDataSyncSmbMountOptions(mountOptions *datasync.SmbMountOptions) []interface{} {
if mountOptions == nil {
return []interface{}{}
}

m := map[string]interface{}{
"version": aws.StringValue(mountOptions.Version),
}

return []interface{}{m}
}

func flattenDataSyncOnPremConfig(onPremConfig *datasync.OnPremConfig) []interface{} {
if onPremConfig == nil {
return []interface{}{}
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ func Provider() terraform.ResourceProvider {
"aws_datasync_location_efs": resourceAwsDataSyncLocationEfs(),
"aws_datasync_location_nfs": resourceAwsDataSyncLocationNfs(),
"aws_datasync_location_s3": resourceAwsDataSyncLocationS3(),
"aws_datasync_location_smb": resourceAwsDataSyncLocationSmb(),
"aws_datasync_task": resourceAwsDataSyncTask(),
"aws_dax_cluster": resourceAwsDaxCluster(),
"aws_dax_parameter_group": resourceAwsDaxParameterGroup(),
Expand Down
224 changes: 224 additions & 0 deletions aws/resource_aws_datasync_location_smb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package aws

import (
"fmt"
"log"

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

func resourceAwsDataSyncLocationSmb() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDataSyncLocationSmbCreate,
Read: resourceAwsDataSyncLocationSmbRead,
Update: resourceAwsDataSyncLocationSmbUpdate,
Delete: resourceAwsDataSyncLocationSmbDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"agent_arns": {
Type: schema.TypeSet,
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"domain": {
bflad marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"mount_options": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
DiffSuppressFunc: suppressMissingOptionalConfigurationBlock,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"version": {
Type: schema.TypeString,
Default: datasync.SmbVersionAutomatic,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
datasync.SmbVersionAutomatic,
datasync.SmbVersionSmb2,
datasync.SmbVersionSmb3,
}, false),
},
},
},
},
"password": {
bflad marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
Sensitive: true,
ForceNew: true,
},
"server_hostname": {
bflad marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"subdirectory": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
/*// Ignore missing trailing slash
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if new == "/" {
return false
}
if strings.TrimSuffix(old, "/") == strings.TrimSuffix(new, "/") {
return true
}
return false
},
*/
},
"tags": tagsSchema(),
"uri": {
Type: schema.TypeString,
Computed: true,
},
"user": {
bflad marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

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

input := &datasync.CreateLocationSmbInput{
AgentArns: expandStringSet(d.Get("agent_arns").(*schema.Set)),
MountOptions: expandDataSyncSmbMountOptions(d.Get("mount_options").([]interface{})),
Password: aws.String(d.Get("password").(string)),
ServerHostname: aws.String(d.Get("server_hostname").(string)),
Subdirectory: aws.String(d.Get("subdirectory").(string)),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().DatasyncTags(),
User: aws.String(d.Get("user").(string)),
}

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

log.Printf("[DEBUG] Creating DataSync Location SMB: %s", input)
output, err := conn.CreateLocationSmb(input)
if err != nil {
return fmt.Errorf("error creating DataSync Location SMB: %s", err)
}

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

return resourceAwsDataSyncLocationSmbRead(d, meta)
}

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

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

log.Printf("[DEBUG] Reading DataSync Location SMB: %s", input)
output, err := conn.DescribeLocationSmb(input)

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

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

tagsInput := &datasync.ListTagsForResourceInput{
ResourceArn: output.LocationArn,
}

log.Printf("[DEBUG] Reading DataSync Location SMB tags: %s", tagsInput)
tagsOutput, err := conn.ListTagsForResource(tagsInput)

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

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

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

d.Set("agent_arns", schema.NewSet(schema.HashString, flattenStringList(output.AgentArns)))

d.Set("arn", output.LocationArn)

d.Set("domain", output.Domain)

if err := d.Set("mount_options", flattenDataSyncSmbMountOptions(output.MountOptions)); err != nil {
return fmt.Errorf("error setting mount_options: %s", err)
}

d.Set("subdirectory", subdirectory)

if err := d.Set("tags", keyvaluetags.DatasyncKeyValueTags(tagsOutput.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("user", output.User)

d.Set("uri", output.LocationUri)

return nil
}

func resourceAwsDataSyncLocationSmbUpdate(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 SMB location (%s) tags: %s", d.Id(), err)
}
}
return resourceAwsDataSyncLocationSmbRead(d, meta)
}

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

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

log.Printf("[DEBUG] Deleting DataSync Location SMB: %s", input)
_, err := conn.DeleteLocation(input)

if isAWSErr(err, "InvalidRequestException", "not found") {
return nil
}

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

return nil
}
Loading