Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TechDebt: Move lightsail_domain_entry to common separator #30820

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/30820.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:note
resource/aws_lightsail_domain_entry: The `id` attribute is now comma-delimited
```
56 changes: 33 additions & 23 deletions internal/flex/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ func PointersMapToStringList(pointers map[string]*string) map[string]interface{}
return list
}

// Takes a string of resource attributes separated by the ResourceIdSeparator constant and an expected number of Id Parts
// Takes a string of resource attributes separated by the ResourceIdSeparator constant, an expected number of Id Parts, and a boolean specifying if empty parts are to be allowed
// Returns a list of the resource attributes strings used to construct the unique Id or an error message if the resource id does not parse properly
func ExpandResourceId(id string, partCount int) ([]string, error) {
func ExpandResourceId(id string, partCount int, allowEmptyPart bool) ([]string, error) {
idParts := strings.Split(id, ResourceIdSeparator)

if len(idParts) <= 1 {
Expand All @@ -228,25 +228,26 @@ func ExpandResourceId(id string, partCount int) ([]string, error) {
return nil, fmt.Errorf("unexpected format for ID (%s), expected (%d) parts separated by (%s)", id, partCount, ResourceIdSeparator)
}

var emptyPart bool
emptyParts := make([]int, 0, partCount)
for index, part := range idParts {
if part == "" {
emptyPart = true
emptyParts = append(emptyParts, index)
if !allowEmptyPart {
var emptyPart bool
emptyParts := make([]int, 0, partCount)
for index, part := range idParts {
if part == "" {
emptyPart = true
emptyParts = append(emptyParts, index)
}
}
}

if emptyPart {
return nil, fmt.Errorf("unexpected format for ID (%[1]s), the following id parts indexes are blank (%v)", id, emptyParts)
if emptyPart {
return nil, fmt.Errorf("unexpected format for ID (%[1]s), the following id parts indexes are blank (%v)", id, emptyParts)
}
}

return idParts, nil
}

// Takes a list of the resource attributes as strings used to construct the unique Id and an expected number of Id Parts
// Takes a list of the resource attributes as strings used to construct the unique Id, an expected number of Id Parts, and a boolean specifying if empty parts are to be allowed
// Returns a string of resource attributes separated by the ResourceIdSeparator constant or an error message if the id parts do not parse properly
func FlattenResourceId(idParts []string, partCount int) (string, error) {
func FlattenResourceId(idParts []string, partCount int, allowEmptyPart bool) (string, error) {
if len(idParts) <= 1 {
return "", fmt.Errorf("unexpected format for ID parts (%v), expected more than one part", idParts)
}
Expand All @@ -255,17 +256,19 @@ func FlattenResourceId(idParts []string, partCount int) (string, error) {
return "", fmt.Errorf("unexpected format for ID parts (%v), expected (%d) parts", idParts, partCount)
}

var emptyPart bool
emptyParts := make([]int, 0, len(idParts))
for index, part := range idParts {
if part == "" {
emptyPart = true
emptyParts = append(emptyParts, index)
if !allowEmptyPart {
var emptyPart bool
emptyParts := make([]int, 0, len(idParts))
for index, part := range idParts {
if part == "" {
emptyPart = true
emptyParts = append(emptyParts, index)
}
}
}

if emptyPart {
return "", fmt.Errorf("unexpected format for ID parts (%v), the following id parts indexes are blank (%v)", idParts, emptyParts)
if emptyPart {
return "", fmt.Errorf("unexpected format for ID parts (%v), the following id parts indexes are blank (%v)", idParts, emptyParts)
}
}

return strings.Join(idParts, ResourceIdSeparator), nil
Expand All @@ -276,3 +279,10 @@ func FlattenResourceId(idParts []string, partCount int) (string, error) {
func StringToBoolValue(v *string) bool {
return aws.StringValue(v) == strconv.FormatBool(true)
}

// Takes a string of resource attributes separated by the ResourceIdSeparator constant
// returns the number of parts
func ResourceIdPartCount(id string) int {
idParts := strings.Split(id, ResourceIdSeparator)
return len(idParts)
}
55 changes: 47 additions & 8 deletions internal/flex/flex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestExpandResourceId(t *testing.T) {
t.Parallel()

id := "foo,bar,baz"
got, _ := ExpandResourceId(id, 3)
got, _ := ExpandResourceId(id, 3, false)
want := []string{
"foo",
"bar",
Expand All @@ -114,18 +114,35 @@ func TestExpandResourceIdEmptyPart(t *testing.T) {
t.Parallel()

resourceId := "foo,,baz"
_, err := ExpandResourceId(resourceId, 3)
_, err := ExpandResourceId(resourceId, 3, false)

if !strings.Contains(err.Error(), "format for ID (foo,,baz), the following id parts indexes are blank ([1])") {
t.Fatalf("Expected an error when parsing ResourceId with an empty part")
}
}

func TestExpandResourceIdAllowEmptyPart(t *testing.T) {
t.Parallel()

resourceId := "foo,,baz"
got, _ := ExpandResourceId(resourceId, 3, true)

want := []string{
"foo",
"",
"baz",
}

if !cmp.Equal(got, want) {
t.Errorf("expanded = %v, want = %v", got, want)
}
}

func TestExpandResourceIdIncorrectPartCount(t *testing.T) {
t.Parallel()

resourceId := "foo,bar,baz"
_, err := ExpandResourceId(resourceId, 2)
_, err := ExpandResourceId(resourceId, 2, false)

if !strings.Contains(err.Error(), "unexpected format for ID (foo,bar,baz), expected (2) parts separated by (,)") {
t.Fatalf("Expected an error when parsing ResourceId with incorrect part count")
Expand All @@ -136,7 +153,7 @@ func TestExpandResourceIdSinglePart(t *testing.T) {
t.Parallel()

resourceId := "foo"
_, err := ExpandResourceId(resourceId, 2)
_, err := ExpandResourceId(resourceId, 2, false)

if !strings.Contains(err.Error(), "unexpected format for ID ([foo]), expected more than one part") {
t.Fatalf("Expected an error when parsing ResourceId with single part count")
Expand All @@ -147,7 +164,7 @@ func TestFlattenResourceId(t *testing.T) {
t.Parallel()

idParts := []string{"foo", "bar", "baz"}
got, _ := FlattenResourceId(idParts, 3)
got, _ := FlattenResourceId(idParts, 3, false)
want := "foo,bar,baz"

if !cmp.Equal(got, want) {
Expand All @@ -159,7 +176,7 @@ func TestFlattenResourceIdEmptyPart(t *testing.T) {
t.Parallel()

idParts := []string{"foo", "", "baz"}
_, err := FlattenResourceId(idParts, 3)
_, err := FlattenResourceId(idParts, 3, false)

if !strings.Contains(err.Error(), "unexpected format for ID parts ([foo baz]), the following id parts indexes are blank ([1])") {
t.Fatalf("Expected an error when parsing ResourceId with an empty part")
Expand All @@ -170,7 +187,7 @@ func TestFlattenResourceIdIncorrectPartCount(t *testing.T) {
t.Parallel()

idParts := []string{"foo", "bar", "baz"}
_, err := FlattenResourceId(idParts, 2)
_, err := FlattenResourceId(idParts, 2, false)

if !strings.Contains(err.Error(), "unexpected format for ID parts ([foo bar baz]), expected (2) parts") {
t.Fatalf("Expected an error when parsing ResourceId with incorrect part count")
Expand All @@ -181,13 +198,35 @@ func TestFlattenResourceIdSinglePart(t *testing.T) {
t.Parallel()

idParts := []string{"foo"}
_, err := FlattenResourceId(idParts, 2)
_, err := FlattenResourceId(idParts, 2, false)

if !strings.Contains(err.Error(), "unexpected format for ID parts ([foo]), expected more than one part") {
t.Fatalf("Expected an error when parsing ResourceId with single part count")
}
}

func TestResourceIdPartCount(t *testing.T) {
t.Parallel()

id := "foo,bar,baz"
partCount := ResourceIdPartCount(id)
expectedCount := 3
if partCount != expectedCount {
t.Fatalf("Expected part count of %d.", expectedCount)
}
}

func TestResourceIdPartCountLegacySeparator(t *testing.T) {
t.Parallel()

id := "foo_bar_baz"
partCount := ResourceIdPartCount(id)
expectedCount := 1
if partCount != expectedCount {
t.Fatalf("Expected part count of %d.", expectedCount)
}
}

func TestFlattenTimeStringList(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 2 additions & 2 deletions internal/service/lightsail/bucket_access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func resourceBucketAccessKeyCreate(ctx context.Context, d *schema.ResourceData,
}

idParts := []string{d.Get("bucket_name").(string), *out.AccessKey.AccessKeyId}
id, err := flex.FlattenResourceId(idParts, BucketAccessKeyIdPartsCount)
id, err := flex.FlattenResourceId(idParts, BucketAccessKeyIdPartsCount, false)

if err != nil {
return create.DiagError(names.Lightsail, create.ErrActionFlatteningResourceId, ResBucketAccessKey, d.Get("bucket_name").(string), err)
Expand Down Expand Up @@ -116,7 +116,7 @@ func resourceBucketAccessKeyRead(ctx context.Context, d *schema.ResourceData, me

func resourceBucketAccessKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).LightsailConn()
parts, err := flex.ExpandResourceId(d.Id(), BucketAccessKeyIdPartsCount)
parts, err := flex.ExpandResourceId(d.Id(), BucketAccessKeyIdPartsCount, false)

if err != nil {
return create.DiagError(names.Lightsail, create.ErrActionExpandingResourceId, ResBucketAccessKey, d.Id(), err)
Expand Down
6 changes: 3 additions & 3 deletions internal/service/lightsail/bucket_resource_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func resourceBucketResourceAccessCreate(ctx context.Context, d *schema.ResourceD
}

idParts := []string{d.Get("bucket_name").(string), d.Get("resource_name").(string)}
id, err := flex.FlattenResourceId(idParts, BucketResourceAccessIdPartsCount)
id, err := flex.FlattenResourceId(idParts, BucketResourceAccessIdPartsCount, false)

if err != nil {
return create.DiagError(names.Lightsail, create.ErrActionFlatteningResourceId, ResBucketResourceAccess, d.Get("bucket_name").(string), err)
Expand All @@ -96,7 +96,7 @@ func resourceBucketResourceAccessRead(ctx context.Context, d *schema.ResourceDat
return create.DiagError(names.Lightsail, create.ErrActionReading, ResBucketResourceAccess, d.Id(), err)
}

parts, err := flex.ExpandResourceId(d.Id(), BucketResourceAccessIdPartsCount)
parts, err := flex.ExpandResourceId(d.Id(), BucketResourceAccessIdPartsCount, false)

if err != nil {
return create.DiagError(names.Lightsail, create.ErrActionExpandingResourceId, ResBucketResourceAccess, d.Id(), err)
Expand All @@ -110,7 +110,7 @@ func resourceBucketResourceAccessRead(ctx context.Context, d *schema.ResourceDat

func resourceBucketResourceAccessDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).LightsailConn()
parts, err := flex.ExpandResourceId(d.Id(), BucketResourceAccessIdPartsCount)
parts, err := flex.ExpandResourceId(d.Id(), BucketResourceAccessIdPartsCount, false)

if err != nil {
return create.DiagError(names.Lightsail, create.ErrActionExpandingResourceId, ResBucketResourceAccess, d.Id(), err)
Expand Down
Loading