-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19859 from hashicorp/b-aws_datasync_location_s3-o…
…utposts-location-arn r/aws_datasync_location_s3: Correctly parse S3 on Outposts location URI
- Loading branch information
Showing
10 changed files
with
208 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:bug | ||
resource/aws_datasync_location_s3: Correctly parse S3 on Outposts location URI | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package datasync | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/aws/aws-sdk-go/aws/arn" | ||
) | ||
|
||
var ( | ||
locationURIPattern = regexp.MustCompile(`^(efs|nfs|s3|smb|fsxw)://(.+)$`) | ||
locationURIGlobalIDAndSubdirPattern = regexp.MustCompile(`^([a-zA-Z0-9.\-]+)(/.*)$`) | ||
s3OutpostsAccessPointARNResourcePattern = regexp.MustCompile(`^outpost/.*/accesspoint/.*?(/.*)$`) | ||
) | ||
|
||
// 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) { | ||
submatches := locationURIPattern.FindStringSubmatch(uri) | ||
|
||
if len(submatches) != 3 { | ||
return "", fmt.Errorf("location URI (%s) does not match pattern %q", uri, locationURIPattern) | ||
} | ||
|
||
globalIDAndSubdir := submatches[2] | ||
parsedARN, err := arn.Parse(globalIDAndSubdir) | ||
|
||
if err == nil { | ||
submatches = s3OutpostsAccessPointARNResourcePattern.FindStringSubmatch(parsedARN.Resource) | ||
|
||
if len(submatches) != 2 { | ||
return "", fmt.Errorf("location URI S3 on Outposts access point ARN resource (%s) does not match pattern %q", parsedARN.Resource, s3OutpostsAccessPointARNResourcePattern) | ||
} | ||
|
||
return submatches[1], nil | ||
} | ||
|
||
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[2], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package datasync_test | ||
|
||
import ( | ||
"testing" | ||
|
||
tfdatasync "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/datasync" | ||
) | ||
|
||
func TestSubdirectoryFromLocationURI(t *testing.T) { | ||
testCases := []struct { | ||
TestName string | ||
InputURI string | ||
ExpectedError bool | ||
ExpectedSubdirectory string | ||
}{ | ||
{ | ||
TestName: "empty URI", | ||
InputURI: "", | ||
ExpectedError: true, | ||
}, | ||
{ | ||
TestName: "invalid URI scheme", | ||
InputURI: "test://testing/", | ||
ExpectedError: true, | ||
}, | ||
{ | ||
TestName: "S3 bucket URI no bucket name (1)", | ||
InputURI: "s3://", | ||
ExpectedError: true, | ||
}, | ||
{ | ||
TestName: "S3 bucket URI no bucket name (2)", | ||
InputURI: "s3:///", | ||
ExpectedError: true, | ||
}, | ||
{ | ||
TestName: "S3 bucket URI top level", | ||
InputURI: "s3://bucket/", | ||
ExpectedSubdirectory: "/", | ||
}, | ||
{ | ||
TestName: "S3 bucket URI one level", | ||
InputURI: "s3://bucket/my-folder-1/", | ||
ExpectedSubdirectory: "/my-folder-1/", | ||
}, | ||
{ | ||
TestName: "S3 bucket URI two levels", | ||
InputURI: "s3://bucket/my-folder-1/my-folder-2", | ||
ExpectedSubdirectory: "/my-folder-1/my-folder-2", | ||
}, | ||
{ | ||
TestName: "S3 Outposts ARN URI top level", | ||
InputURI: "s3://arn:aws:s3-outposts:eu-west-3:123456789012:outpost/op-YYYYYYYYYY/accesspoint/my-access-point/", | ||
ExpectedSubdirectory: "/", | ||
}, | ||
{ | ||
TestName: "S3 Outposts ARN URI one level", | ||
InputURI: "s3://arn:aws:s3-outposts:eu-west-3:123456789012:outpost/op-YYYYYYYYYY/accesspoint/my-access-point/my-folder-1/", | ||
ExpectedSubdirectory: "/my-folder-1/", | ||
}, | ||
{ | ||
TestName: "S3 Outposts ARN URI two levels", | ||
InputURI: "s3://arn:aws:s3-outposts:eu-west-3:123456789012:outpost/op-YYYYYYYYYY/accesspoint/my-access-point/my-folder-1/my-folder-2", | ||
ExpectedSubdirectory: "/my-folder-1/my-folder-2", | ||
}, | ||
{ | ||
TestName: "EFS URI top level", | ||
InputURI: "efs://us-west-2.fs-abcdef01/", | ||
ExpectedSubdirectory: "/", | ||
}, | ||
{ | ||
TestName: "EFS URI one level", | ||
InputURI: "efs://us-west-2.fs-abcdef01/my-folder-1/", | ||
ExpectedSubdirectory: "/my-folder-1/", | ||
}, | ||
{ | ||
TestName: "EFS URI two levels", | ||
InputURI: "efs://us-west-2.fs-abcdef01/my-folder-1/my-folder-2", | ||
ExpectedSubdirectory: "/my-folder-1/my-folder-2", | ||
}, | ||
{ | ||
TestName: "NFS URI top level", | ||
InputURI: "nfs://example.com/", | ||
ExpectedSubdirectory: "/", | ||
}, | ||
{ | ||
TestName: "NFS URI one level", | ||
InputURI: "nfs://example.com/my-folder-1/", | ||
ExpectedSubdirectory: "/my-folder-1/", | ||
}, | ||
{ | ||
TestName: "NFS URI two levels", | ||
InputURI: "nfs://example.com/my-folder-1/my-folder-2", | ||
ExpectedSubdirectory: "/my-folder-1/my-folder-2", | ||
}, | ||
{ | ||
TestName: "SMB URI top level", | ||
InputURI: "smb://192.168.1.1/", | ||
ExpectedSubdirectory: "/", | ||
}, | ||
{ | ||
TestName: "SMB URI one level", | ||
InputURI: "smb://192.168.1.1/my-folder-1/", | ||
ExpectedSubdirectory: "/my-folder-1/", | ||
}, | ||
{ | ||
TestName: "SMB URI two levels", | ||
InputURI: "smb://192.168.1.1/my-folder-1/my-folder-2", | ||
ExpectedSubdirectory: "/my-folder-1/my-folder-2", | ||
}, | ||
{ | ||
TestName: "FSx Windows URI top level", | ||
InputURI: "fsxw://us-west-2.fs-abcdef012345678901/", | ||
ExpectedSubdirectory: "/", | ||
}, | ||
{ | ||
TestName: "FSx Windows URI one level", | ||
InputURI: "fsxw://us-west-2.fs-abcdef012345678901/my-folder-1/", | ||
ExpectedSubdirectory: "/my-folder-1/", | ||
}, | ||
{ | ||
TestName: "FSx Windows URI two levels", | ||
InputURI: "fsxw://us-west-2.fs-abcdef012345678901/my-folder-1/my-folder-2", | ||
ExpectedSubdirectory: "/my-folder-1/my-folder-2", | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.TestName, func(t *testing.T) { | ||
got, err := tfdatasync.SubdirectoryFromLocationURI(testCase.InputURI) | ||
|
||
if err == nil && testCase.ExpectedError { | ||
t.Fatalf("expected error") | ||
} | ||
|
||
if err != nil && !testCase.ExpectedError { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
|
||
if got != testCase.ExpectedSubdirectory { | ||
t.Errorf("got %s, expected %s", got, testCase.ExpectedSubdirectory) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters