|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go/aws" |
| 10 | + "github.com/aws/aws-sdk-go/service/datasync" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 13 | + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" |
| 14 | +) |
| 15 | + |
| 16 | +func resourceAwsDataSyncLocationFsxWindowsFileSystem() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Create: resourceAwsDataSyncLocationFsxWindowsFileSystemCreate, |
| 19 | + Read: resourceAwsDataSyncLocationFsxWindowsFileSystemRead, |
| 20 | + Update: resourceAwsDataSyncLocationFsxWindowsFileSystemUpdate, |
| 21 | + Delete: resourceAwsDataSyncLocationFsxWindowsFileSystemDelete, |
| 22 | + Importer: &schema.ResourceImporter{ |
| 23 | + State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 24 | + idParts := strings.Split(d.Id(), "#") |
| 25 | + if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { |
| 26 | + return nil, fmt.Errorf("Unexpected format of ID (%q), expected DataSyncLocationArn#FsxArn", d.Id()) |
| 27 | + } |
| 28 | + |
| 29 | + DSArn := idParts[0] |
| 30 | + FSxArn := idParts[1] |
| 31 | + |
| 32 | + d.Set("fsx_filesystem_arn", FSxArn) |
| 33 | + d.SetId(DSArn) |
| 34 | + |
| 35 | + return []*schema.ResourceData{d}, nil |
| 36 | + }, |
| 37 | + }, |
| 38 | + |
| 39 | + Schema: map[string]*schema.Schema{ |
| 40 | + "arn": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + }, |
| 44 | + "fsx_filesystem_arn": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + ForceNew: true, |
| 48 | + ValidateFunc: validateArn, |
| 49 | + }, |
| 50 | + "password": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Required: true, |
| 53 | + ForceNew: true, |
| 54 | + Sensitive: true, |
| 55 | + ValidateFunc: validation.StringLenBetween(1, 104), |
| 56 | + }, |
| 57 | + "user": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Required: true, |
| 60 | + ForceNew: true, |
| 61 | + ValidateFunc: validation.StringLenBetween(1, 104), |
| 62 | + }, |
| 63 | + "domain": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Optional: true, |
| 66 | + ForceNew: true, |
| 67 | + ValidateFunc: validation.StringLenBetween(1, 253), |
| 68 | + }, |
| 69 | + "security_group_arns": { |
| 70 | + Type: schema.TypeSet, |
| 71 | + Required: true, |
| 72 | + ForceNew: true, |
| 73 | + MinItems: 1, |
| 74 | + MaxItems: 5, |
| 75 | + Elem: &schema.Schema{ |
| 76 | + Type: schema.TypeString, |
| 77 | + ValidateFunc: validateArn, |
| 78 | + }, |
| 79 | + }, |
| 80 | + "subdirectory": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Optional: true, |
| 83 | + Computed: true, |
| 84 | + ForceNew: true, |
| 85 | + ValidateFunc: validation.StringLenBetween(1, 4096), |
| 86 | + }, |
| 87 | + "tags": tagsSchema(), |
| 88 | + "uri": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Computed: true, |
| 91 | + }, |
| 92 | + "creation_time": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func resourceAwsDataSyncLocationFsxWindowsFileSystemCreate(d *schema.ResourceData, meta interface{}) error { |
| 101 | + conn := meta.(*AWSClient).datasyncconn |
| 102 | + fsxArn := d.Get("fsx_filesystem_arn").(string) |
| 103 | + |
| 104 | + input := &datasync.CreateLocationFsxWindowsInput{ |
| 105 | + FsxFilesystemArn: aws.String(fsxArn), |
| 106 | + User: aws.String(d.Get("user").(string)), |
| 107 | + Password: aws.String(d.Get("password").(string)), |
| 108 | + SecurityGroupArns: expandStringSet(d.Get("security_group_arns").(*schema.Set)), |
| 109 | + Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().DatasyncTags(), |
| 110 | + } |
| 111 | + |
| 112 | + if v, ok := d.GetOk("subdirectory"); ok { |
| 113 | + input.Subdirectory = aws.String(v.(string)) |
| 114 | + } |
| 115 | + |
| 116 | + if v, ok := d.GetOk("domain"); ok { |
| 117 | + input.Domain = aws.String(v.(string)) |
| 118 | + } |
| 119 | + |
| 120 | + log.Printf("[DEBUG] Creating DataSync Location Fsx Windows File System: %#v", input) |
| 121 | + output, err := conn.CreateLocationFsxWindows(input) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("error creating DataSync Location Fsx Windows File System: %w", err) |
| 124 | + } |
| 125 | + |
| 126 | + d.SetId(aws.StringValue(output.LocationArn)) |
| 127 | + |
| 128 | + return resourceAwsDataSyncLocationFsxWindowsFileSystemRead(d, meta) |
| 129 | +} |
| 130 | + |
| 131 | +func resourceAwsDataSyncLocationFsxWindowsFileSystemRead(d *schema.ResourceData, meta interface{}) error { |
| 132 | + conn := meta.(*AWSClient).datasyncconn |
| 133 | + ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig |
| 134 | + |
| 135 | + input := &datasync.DescribeLocationFsxWindowsInput{ |
| 136 | + LocationArn: aws.String(d.Id()), |
| 137 | + } |
| 138 | + |
| 139 | + log.Printf("[DEBUG] Reading DataSync Location Fsx Windows: %#v", input) |
| 140 | + output, err := conn.DescribeLocationFsxWindows(input) |
| 141 | + |
| 142 | + if isAWSErr(err, datasync.ErrCodeInvalidRequestException, "not found") { |
| 143 | + log.Printf("[WARN] DataSync Location Fsx Windows %q not found - removing from state", d.Id()) |
| 144 | + d.SetId("") |
| 145 | + return nil |
| 146 | + } |
| 147 | + |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("error reading DataSync Location Fsx Windows (%s): %w", d.Id(), err) |
| 150 | + } |
| 151 | + |
| 152 | + subdirectory, err := dataSyncParseLocationURI(aws.StringValue(output.LocationUri)) |
| 153 | + |
| 154 | + if err != nil { |
| 155 | + return fmt.Errorf("error parsing Location Fsx Windows File System (%s) URI (%s): %w", d.Id(), aws.StringValue(output.LocationUri), err) |
| 156 | + } |
| 157 | + |
| 158 | + d.Set("arn", output.LocationArn) |
| 159 | + d.Set("subdirectory", subdirectory) |
| 160 | + d.Set("uri", output.LocationUri) |
| 161 | + d.Set("user", output.User) |
| 162 | + d.Set("domain", output.Domain) |
| 163 | + |
| 164 | + if err := d.Set("security_group_arns", flattenStringSet(output.SecurityGroupArns)); err != nil { |
| 165 | + return fmt.Errorf("error setting security_group_arns: %w", err) |
| 166 | + } |
| 167 | + |
| 168 | + if err := d.Set("creation_time", output.CreationTime.Format(time.RFC3339)); err != nil { |
| 169 | + return fmt.Errorf("error setting creation_time: %w", err) |
| 170 | + } |
| 171 | + |
| 172 | + tags, err := keyvaluetags.DatasyncListTags(conn, d.Id()) |
| 173 | + |
| 174 | + if err != nil { |
| 175 | + return fmt.Errorf("error listing tags for DataSync Location Fsx Windows (%s): %w", d.Id(), err) |
| 176 | + } |
| 177 | + |
| 178 | + if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { |
| 179 | + return fmt.Errorf("error setting tags: %w", err) |
| 180 | + } |
| 181 | + |
| 182 | + return nil |
| 183 | +} |
| 184 | + |
| 185 | +func resourceAwsDataSyncLocationFsxWindowsFileSystemUpdate(d *schema.ResourceData, meta interface{}) error { |
| 186 | + conn := meta.(*AWSClient).datasyncconn |
| 187 | + |
| 188 | + if d.HasChange("tags") { |
| 189 | + o, n := d.GetChange("tags") |
| 190 | + |
| 191 | + if err := keyvaluetags.DatasyncUpdateTags(conn, d.Id(), o, n); err != nil { |
| 192 | + return fmt.Errorf("error updating DataSync Location Fsx Windows File System (%s) tags: %w", d.Id(), err) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return resourceAwsDataSyncLocationFsxWindowsFileSystemRead(d, meta) |
| 197 | +} |
| 198 | + |
| 199 | +func resourceAwsDataSyncLocationFsxWindowsFileSystemDelete(d *schema.ResourceData, meta interface{}) error { |
| 200 | + conn := meta.(*AWSClient).datasyncconn |
| 201 | + |
| 202 | + input := &datasync.DeleteLocationInput{ |
| 203 | + LocationArn: aws.String(d.Id()), |
| 204 | + } |
| 205 | + |
| 206 | + log.Printf("[DEBUG] Deleting DataSync Location Fsx Windows File System: %#v", input) |
| 207 | + _, err := conn.DeleteLocation(input) |
| 208 | + |
| 209 | + if isAWSErr(err, datasync.ErrCodeInvalidRequestException, "not found") { |
| 210 | + return nil |
| 211 | + } |
| 212 | + |
| 213 | + if err != nil { |
| 214 | + return fmt.Errorf("error deleting DataSync Location Fsx Windows (%s): %w", d.Id(), err) |
| 215 | + } |
| 216 | + |
| 217 | + return nil |
| 218 | +} |
0 commit comments