Skip to content

Commit

Permalink
Merge pull request #19753 from DrFaust92/r/datasync_location_smb
Browse files Browse the repository at this point in the history
r/datasync_location_smb - support update
  • Loading branch information
ewbankkit authored Jun 10, 2021
2 parents 1ac5e4d + 163da71 commit 4fae3df
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 50 deletions.
7 changes: 7 additions & 0 deletions .changelog/19753.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_datasync_location_smb: Add support for updating.
```

```release-note:enhancement
resource/aws_datasync_location_smb: Add plan time validation for `domain`, `agent_arns`, `password`, `server_hostname`, `subdirectory`, and `user`.
```
89 changes: 53 additions & 36 deletions aws/resource_aws_datasync_location_smb.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,52 +29,49 @@ func resourceAwsDataSyncLocationSmb() *schema.Resource {
"agent_arns": {
Type: schema.TypeSet,
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateArn,
},
},
"domain": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 253),
},
"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),
Type: schema.TypeString,
Default: datasync.SmbVersionAutomatic,
Optional: true,
ValidateFunc: validation.StringInSlice(datasync.SmbVersion_Values(), false),
},
},
},
},
"password": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
Sensitive: true,
ValidateFunc: validation.StringLenBetween(1, 104),
},
"server_hostname": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
"subdirectory": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 4096),
/*// Ignore missing trailing slash
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if new == "/" {
Expand All @@ -94,9 +91,9 @@ func resourceAwsDataSyncLocationSmb() *schema.Resource {
Computed: true,
},
"user": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 104),
},
},

Expand Down Expand Up @@ -126,7 +123,7 @@ func resourceAwsDataSyncLocationSmbCreate(d *schema.ResourceData, meta interface
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)
return fmt.Errorf("error creating DataSync Location SMB: %w", err)
}

d.SetId(aws.StringValue(output.LocationArn))
Expand All @@ -153,7 +150,7 @@ func resourceAwsDataSyncLocationSmbRead(d *schema.ResourceData, meta interface{}
}

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

tagsInput := &datasync.ListTagsForResourceInput{
Expand All @@ -164,13 +161,13 @@ func resourceAwsDataSyncLocationSmbRead(d *schema.ResourceData, meta interface{}
tagsOutput, err := conn.ListTagsForResource(tagsInput)

if err != nil {
return fmt.Errorf("error reading DataSync Location SMB (%s) tags: %s", d.Id(), err)
return fmt.Errorf("error reading DataSync Location SMB (%s) tags: %w", 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)
return fmt.Errorf("error parsing Location SMB (%s) URI (%s): %w", d.Id(), aws.StringValue(output.LocationUri), err)
}

d.Set("agent_arns", flattenStringSet(output.AgentArns))
Expand All @@ -180,7 +177,7 @@ func resourceAwsDataSyncLocationSmbRead(d *schema.ResourceData, meta interface{}
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)
return fmt.Errorf("error setting mount_options: %w", err)
}

d.Set("subdirectory", subdirectory)
Expand All @@ -206,11 +203,31 @@ func resourceAwsDataSyncLocationSmbRead(d *schema.ResourceData, meta interface{}
func resourceAwsDataSyncLocationSmbUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).datasyncconn

if d.HasChangesExcept("tags_all", "tags") {
input := &datasync.UpdateLocationSmbInput{
LocationArn: aws.String(d.Id()),
AgentArns: expandStringSet(d.Get("agent_arns").(*schema.Set)),
MountOptions: expandDataSyncSmbMountOptions(d.Get("mount_options").([]interface{})),
Password: aws.String(d.Get("password").(string)),
Subdirectory: aws.String(d.Get("subdirectory").(string)),
User: aws.String(d.Get("user").(string)),
}

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

_, err := conn.UpdateLocationSmb(input)
if err != nil {
return fmt.Errorf("error updating DataSync Location SMB (%s): %w", d.Id(), err)
}
}

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

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 fmt.Errorf("error updating Datasync SMB location (%s) tags: %w", d.Id(), err)
}
}
return resourceAwsDataSyncLocationSmbRead(d, meta)
Expand All @@ -231,7 +248,7 @@ func resourceAwsDataSyncLocationSmbDelete(d *schema.ResourceData, meta interface
}

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

return nil
Expand Down
39 changes: 25 additions & 14 deletions aws/resource_aws_datasync_location_smb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func init() {
func testSweepDataSyncLocationSmbs(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
return fmt.Errorf("error getting client: %w", err)
}
conn := client.(*AWSClient).datasyncconn

Expand All @@ -39,7 +39,7 @@ func testSweepDataSyncLocationSmbs(region string) error {
}

if err != nil {
return fmt.Errorf("Error retrieving DataSync Location SMBs: %s", err)
return fmt.Errorf("Error retrieving DataSync Location SMBs: %w", err)
}

if len(output.Locations) == 0 {
Expand All @@ -54,12 +54,11 @@ func testSweepDataSyncLocationSmbs(region string) error {
continue
}
log.Printf("[INFO] Deleting DataSync Location SMB: %s", uri)
input := &datasync.DeleteLocationInput{
LocationArn: location.LocationArn,
}

_, err := conn.DeleteLocation(input)

r := resourceAwsDataSyncLocationSmb()
d := r.Data(nil)
d.SetId(aws.StringValue(location.LocationArn))
err = r.Delete(d, client)
if isAWSErr(err, "InvalidRequestException", "not found") {
continue
}
Expand Down Expand Up @@ -92,10 +91,9 @@ func TestAccAWSDataSyncLocationSmb_basic(t *testing.T) {
CheckDestroy: testAccCheckAWSDataSyncLocationSmbDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDataSyncLocationSmbConfig(rName),
Config: testAccAWSDataSyncLocationSmbConfig(rName, "/test/"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDataSyncLocationSmbExists(resourceName, &locationSmb1),

testAccMatchResourceAttrRegionalARN(resourceName, "arn", "datasync", regexp.MustCompile(`location/loc-.+`)),
resource.TestCheckResourceAttr(resourceName, "agent_arns.#", "1"),
resource.TestCheckResourceAttr(resourceName, "mount_options.#", "1"),
Expand All @@ -111,6 +109,19 @@ func TestAccAWSDataSyncLocationSmb_basic(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password", "server_hostname"},
},
{
Config: testAccAWSDataSyncLocationSmbConfig(rName, "/test2/"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDataSyncLocationSmbExists(resourceName, &locationSmb1),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "datasync", regexp.MustCompile(`location/loc-.+`)),
resource.TestCheckResourceAttr(resourceName, "agent_arns.#", "1"),
resource.TestCheckResourceAttr(resourceName, "mount_options.#", "1"),
resource.TestCheckResourceAttr(resourceName, "mount_options.0.version", "AUTOMATIC"),
resource.TestCheckResourceAttr(resourceName, "user", "Guest"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestMatchResourceAttr(resourceName, "uri", regexp.MustCompile(`^smb://.+/`)),
),
},
},
})
}
Expand All @@ -127,7 +138,7 @@ func TestAccAWSDataSyncLocationSmb_disappears(t *testing.T) {
CheckDestroy: testAccCheckAWSDataSyncLocationSmbDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDataSyncLocationSmbConfig(rName),
Config: testAccAWSDataSyncLocationSmbConfig(rName, "/test/"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDataSyncLocationSmbExists(resourceName, &locationSmb1),
testAccCheckAWSDataSyncLocationSmbDisappears(&locationSmb1),
Expand Down Expand Up @@ -357,16 +368,16 @@ resource "aws_datasync_agent" "test" {
`, rName))
}

func testAccAWSDataSyncLocationSmbConfig(rName string) string {
return testAccAWSDataSyncLocationSmbConfigBase(rName) + `
func testAccAWSDataSyncLocationSmbConfig(rName, dir string) string {
return testAccAWSDataSyncLocationSmbConfigBase(rName) + fmt.Sprintf(`
resource "aws_datasync_location_smb" "test" {
agent_arns = [aws_datasync_agent.test.arn]
password = "ZaphodBeeblebroxPW"
server_hostname = aws_instance.test.public_ip
subdirectory = "/test/"
subdirectory = %[1]q
user = "Guest"
}
`
`, dir)
}

func testAccAWSDataSyncLocationSmbConfigTags1(rName, key1, value1 string) string {
Expand Down

0 comments on commit 4fae3df

Please sign in to comment.