From b0c6ed1b90e6003bbdac9e22ec489cdc79b6ccbc Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Thu, 10 Jun 2021 16:11:25 +0300 Subject: [PATCH 1/3] allow update of data sync smb --- aws/resource_aws_datasync_location_smb.go | 85 +++++++++++-------- ...resource_aws_datasync_location_smb_test.go | 39 ++++++--- 2 files changed, 76 insertions(+), 48 deletions(-) diff --git a/aws/resource_aws_datasync_location_smb.go b/aws/resource_aws_datasync_location_smb.go index 7acf7bb951c6..7da75cb9e2c1 100644 --- a/aws/resource_aws_datasync_location_smb.go +++ b/aws/resource_aws_datasync_location_smb.go @@ -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 == "/" { @@ -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), }, }, @@ -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)) @@ -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{ @@ -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)) @@ -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) @@ -206,6 +203,26 @@ 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") diff --git a/aws/resource_aws_datasync_location_smb_test.go b/aws/resource_aws_datasync_location_smb_test.go index 6e56e2c9c611..6f5696bbd295 100644 --- a/aws/resource_aws_datasync_location_smb_test.go +++ b/aws/resource_aws_datasync_location_smb_test.go @@ -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 @@ -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 { @@ -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 } @@ -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"), @@ -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://.+/`)), + ), + }, }, }) } @@ -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), @@ -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 { From d3c934fecb320a15e9bd454e492ae9270c3dfcf5 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Thu, 10 Jun 2021 16:29:59 +0300 Subject: [PATCH 2/3] fmt --- aws/resource_aws_datasync_location_smb.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_datasync_location_smb.go b/aws/resource_aws_datasync_location_smb.go index 7da75cb9e2c1..ed0a76cb264d 100644 --- a/aws/resource_aws_datasync_location_smb.go +++ b/aws/resource_aws_datasync_location_smb.go @@ -227,7 +227,7 @@ func resourceAwsDataSyncLocationSmbUpdate(d *schema.ResourceData, meta interface 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) @@ -248,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 From 163da71b9c3b0b522ead33cb3622066535bf1fb6 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Thu, 10 Jun 2021 16:33:57 +0300 Subject: [PATCH 3/3] changelog --- .changelog/19753.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changelog/19753.txt diff --git a/.changelog/19753.txt b/.changelog/19753.txt new file mode 100644 index 000000000000..50c2a41b0ce8 --- /dev/null +++ b/.changelog/19753.txt @@ -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`. +``` \ No newline at end of file