Skip to content

Commit

Permalink
fix: Add missing type-specific args for aws_datasync_location_*
Browse files Browse the repository at this point in the history
resources
  • Loading branch information
acwwat committed Mar 4, 2024
1 parent 2dc8341 commit 5363e66
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 48 deletions.
14 changes: 14 additions & 0 deletions internal/service/datasync/location_efs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package datasync

import (
"context"
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"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/diag"
Expand Down Expand Up @@ -170,11 +172,23 @@ func resourceLocationEFSRead(ctx context.Context, d *schema.ResourceData, meta i
}

uri := aws.StringValue(output.LocationUri)
globalId, err := globalIdFromLocationURI(uri)
if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}
subdirectory, err := subdirectoryFromLocationURI(uri)
if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}

locationArn, err := arn.Parse(d.Id())
if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}
globalIdParts := strings.Split(globalId, ".") // Global ID format for EFS location is <region>.<efs_file_system_id>
efsFileSystemArn := fmt.Sprintf("arn:%s:elasticfilesystem:%s:%s:file-system/%s", locationArn.Partition, globalIdParts[0], locationArn.AccountID, globalIdParts[1])
d.Set("efs_file_system_arn", efsFileSystemArn)

d.Set("access_point_arn", output.AccessPointArn)
d.Set("arn", output.LocationArn)
if err := d.Set("ec2_config", flattenEC2Config(output.Ec2Config)); err != nil {
Expand Down
28 changes: 12 additions & 16 deletions internal/service/datasync/location_efs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ func TestAccDataSyncLocationEFS_basic(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"efs_file_system_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand All @@ -79,10 +78,9 @@ func TestAccDataSyncLocationEFS_accessPointARN(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"efs_file_system_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down Expand Up @@ -133,10 +131,9 @@ func TestAccDataSyncLocationEFS_subdirectory(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"efs_file_system_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand All @@ -163,10 +160,9 @@ func TestAccDataSyncLocationEFS_tags(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"efs_file_system_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccLocationEFSConfig_tags2(rName, "key1", "value1updated", "key2", "value2"),
Expand Down
5 changes: 5 additions & 0 deletions internal/service/datasync/location_nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func resourceLocationNFSRead(ctx context.Context, d *schema.ResourceData, meta i
}

uri := aws.StringValue(output.LocationUri)
serverHostName, err := globalIdFromLocationURI(uri)
if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}
subdirectory, err := subdirectoryFromLocationURI(uri)
if err != nil {
return sdkdiag.AppendFromErr(diags, err)
Expand All @@ -165,6 +169,7 @@ func resourceLocationNFSRead(ctx context.Context, d *schema.ResourceData, meta i
if err := d.Set("on_prem_config", flattenOnPremConfig(output.OnPremConfig)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting on_prem_config: %s", err)
}
d.Set("server_hostname", serverHostName)
d.Set("subdirectory", subdirectory)
d.Set("uri", uri)

Expand Down
35 changes: 15 additions & 20 deletions internal/service/datasync/location_nfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ func TestAccDataSyncLocationNFS_basic(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"server_hostname"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand All @@ -77,10 +76,9 @@ func TestAccDataSyncLocationNFS_mountOptions(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"server_hostname"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccLocationNFSConfig_mountOptions(rName, "NFS4_1"),
Expand Down Expand Up @@ -138,10 +136,9 @@ func TestAccDataSyncLocationNFS_AgentARNs_multiple(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"server_hostname"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand All @@ -167,10 +164,9 @@ func TestAccDataSyncLocationNFS_subdirectory(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"server_hostname"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccLocationNFSConfig_subdirectory(rName, "/subdirectory2/"),
Expand Down Expand Up @@ -204,10 +200,9 @@ func TestAccDataSyncLocationNFS_tags(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"server_hostname"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccLocationNFSConfig_tags2(rName, "key1", "value1updated", "key2", "value2"),
Expand Down
13 changes: 13 additions & 0 deletions internal/service/datasync/location_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package datasync

import (
"context"
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"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/diag"
Expand Down Expand Up @@ -172,11 +174,22 @@ func resourceLocationS3Read(ctx context.Context, d *schema.ResourceData, meta in
}

uri := aws.StringValue(output.LocationUri)
s3BucketName, err := globalIdFromLocationURI(aws.StringValue(output.LocationUri))
if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}
subdirectory, err := subdirectoryFromLocationURI(aws.StringValue(output.LocationUri))
if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}

locationArn, err := arn.Parse(d.Id())
if err != nil {
return sdkdiag.AppendFromErr(diags, err)
}
s3BucketArn := fmt.Sprintf("arn:%s:s3:::%s", locationArn.Partition, s3BucketName)
d.Set("s3_bucket_arn", s3BucketArn)

d.Set("agent_arns", aws.StringValueSlice(output.AgentArns))
d.Set("arn", output.LocationArn)
if err := d.Set("s3_config", flattenS3Config(output.S3Config)); err != nil {
Expand Down
21 changes: 9 additions & 12 deletions internal/service/datasync/location_s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ func TestAccDataSyncLocationS3_basic(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"s3_bucket_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down Expand Up @@ -87,10 +86,9 @@ func TestAccDataSyncLocationS3_storageClass(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"s3_bucket_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
Expand Down Expand Up @@ -141,10 +139,9 @@ func TestAccDataSyncLocationS3_tags(t *testing.T) {
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"s3_bucket_arn"},
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccLocationS3Config_tags2(rName, "key1", "value1updated", "key2", "value2"),
Expand Down
20 changes: 20 additions & 0 deletions internal/service/datasync/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ var (
s3OutpostsAccessPointARNResourcePattern = regexache.MustCompile(`^outpost/.*/accesspoint/.*?(/.*)$`)
)

// globalIDFromLocationURI extracts the global ID from a location URI.
// https://docs.aws.amazon.com/datasync/latest/userguide/API_LocationListEntry.html#DataSync-Type-LocationListEntry-LocationUri
func globalIdFromLocationURI(uri string) (string, error) {
submatches := locationURIPattern.FindStringSubmatch(uri)

if len(submatches) != 3 {
return "", fmt.Errorf("location URI (%s) does not match pattern %q", uri, locationURIPattern)
}

globalIDAndSubdir := submatches[2]

submatches = locationURIGlobalIDAndSubdirPattern.FindStringSubmatch(globalIDAndSubdir)

if len(submatches) != 3 {
return "", fmt.Errorf("location URI global ID and subdirectory (%s) does not match pattern %q", globalIDAndSubdir, locationURIGlobalIDAndSubdirPattern)
}

return submatches[1], nil
}

// subdirectoryFromLocationURI extracts the subdirectory from a location URI.
// https://docs.aws.amazon.com/datasync/latest/userguide/API_LocationListEntry.html#DataSync-Type-LocationListEntry-LocationUri
func subdirectoryFromLocationURI(uri string) (string, error) {
Expand Down

0 comments on commit 5363e66

Please sign in to comment.