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

fix: Fix crash during read due to key-only tags in aws_lightsail_instance #37587

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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/37587.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_lightsail_instance: Fix crash when reading a resource that has a key-only tag
```
2 changes: 1 addition & 1 deletion internal/service/lightsail/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTagsInIDElem=ResourceName -ServiceTagsSlice -TagInIDElem=ResourceName -UpdateTags -CreateTags
//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ServiceTagsSlice -TagInIDElem=ResourceName -CreateTags -UpdateTags
//go:generate go run ../../generate/servicepackage/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.

Expand Down
2 changes: 1 addition & 1 deletion internal/service/lightsail/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

// @SDKResource("aws_lightsail_instance", name="Instance")
// @Tags(identifierAttribute="id")
// @Tags(identifierAttribute="id", resourceType="Instance")
func ResourceInstance() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceInstanceCreate,
Expand Down
98 changes: 98 additions & 0 deletions internal/service/lightsail/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,14 @@ func TestAccLightsailInstance_tags(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "bundle_id"),
resource.TestCheckResourceAttrSet(resourceName, "key_pair_name"),
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "tags.Name", "tf-test"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccInstanceConfig_tags2(rName),
Check: resource.ComposeAggregateTestCheckFunc(
Expand All @@ -161,6 +167,60 @@ func TestAccLightsailInstance_tags(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "bundle_id"),
resource.TestCheckResourceAttrSet(resourceName, "key_pair_name"),
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct2),
resource.TestCheckResourceAttr(resourceName, "tags.Name", "tf-test"),
resource.TestCheckResourceAttr(resourceName, "tags.ExtraName", "tf-test"),
),
},
},
})
}

func TestAccLightsailInstance_keyOnlyTags(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_lightsail_instance.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, strings.ToLower(lightsail.ServiceID))
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, strings.ToLower(lightsail.ServiceID)),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckInstanceDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccInstanceConfig_keyOnlyTags1(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckInstanceExists(ctx, resourceName),
resource.TestCheckResourceAttrSet(resourceName, names.AttrAvailabilityZone),
resource.TestCheckResourceAttrSet(resourceName, "blueprint_id"),
resource.TestCheckResourceAttrSet(resourceName, "bundle_id"),
resource.TestCheckResourceAttrSet(resourceName, "key_pair_name"),
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct2),
resource.TestCheckResourceAttr(resourceName, "tags.Name", "tf-test"),
resource.TestCheckResourceAttr(resourceName, "tags.EmptyTag1", ""),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccInstanceConfig_keyOnlyTags2(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckInstanceExists(ctx, resourceName),
resource.TestCheckResourceAttrSet(resourceName, names.AttrAvailabilityZone),
resource.TestCheckResourceAttrSet(resourceName, "blueprint_id"),
resource.TestCheckResourceAttrSet(resourceName, "bundle_id"),
resource.TestCheckResourceAttrSet(resourceName, "key_pair_name"),
resource.TestCheckResourceAttr(resourceName, acctest.CtTagsPercent, acctest.Ct4),
resource.TestCheckResourceAttr(resourceName, "tags.Name", "tf-test"),
resource.TestCheckResourceAttr(resourceName, "tags.ExtraName", "tf-test"),
resource.TestCheckResourceAttr(resourceName, "tags.EmptyTag1", "NotEmptyAnymore"),
resource.TestCheckResourceAttr(resourceName, "tags.EmptyTag2", ""),
),
},
},
Expand Down Expand Up @@ -468,6 +528,44 @@ resource "aws_lightsail_instance" "test" {
`, rName))
}

func testAccInstanceConfig_keyOnlyTags1(rName string) string {
return acctest.ConfigCompose(
testAccInstanceConfigBase(),
fmt.Sprintf(`
resource "aws_lightsail_instance" "test" {
name = "%s"
availability_zone = data.aws_availability_zones.available.names[0]
blueprint_id = "amazon_linux_2"
bundle_id = "nano_3_0"

tags = {
Name = "tf-test"
EmptyTag1 = ""
}
}
`, rName))
}

func testAccInstanceConfig_keyOnlyTags2(rName string) string {
return acctest.ConfigCompose(
testAccInstanceConfigBase(),
fmt.Sprintf(`
resource "aws_lightsail_instance" "test" {
name = "%s"
availability_zone = data.aws_availability_zones.available.names[0]
blueprint_id = "amazon_linux_2"
bundle_id = "nano_3_0"

tags = {
Name = "tf-test",
ExtraName = "tf-test"
EmptyTag1 = "NotEmptyAnymore"
EmptyTag2 = ""
}
}
`, rName))
}

func testAccInstanceConfig_IPAddressType(rName string, rIPAddressType string) string {
return acctest.ConfigCompose(
testAccInstanceConfigBase(),
Expand Down
1 change: 1 addition & 0 deletions internal/service/lightsail/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions internal/service/lightsail/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:build !generate
// +build !generate

package lightsail

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/lightsail"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/types/option"
)

func (p *servicePackage) ListTags(ctx context.Context, meta any, identifier, resourceType string) error {
var (
tags tftags.KeyValueTags
err error
)
switch resourceType {
case "Instance":
tags, err = instanceListTags(ctx, meta.(*conns.AWSClient).LightsailClient(ctx), identifier)

default:
return nil
}

if err != nil {
return err
}

if inContext, ok := tftags.FromContext(ctx); ok {
inContext.TagsOut = option.Some(tags)
}

return nil
}

func instanceListTags(ctx context.Context, client *lightsail.Client, id string) (tftags.KeyValueTags, error) {
out, err := FindInstanceById(ctx, client, id)

if err != nil {
return tftags.New(ctx, nil), err
}

return KeyValueTags(ctx, out.Tags), nil
}
6 changes: 5 additions & 1 deletion internal/tags/key_value_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,11 @@ func (tags KeyValueTags) ResolveDuplicates(ctx context.Context, defaultConfig *D

result := make(map[string]string)
for k, v := range t {
result[k] = v.ValueString()
if v != nil {
acwwat marked this conversation as resolved.
Show resolved Hide resolved
result[k] = v.ValueString()
} else {
result[k] = ""
}
}

configTags := make(map[string]configTag)
Expand Down
9 changes: 9 additions & 0 deletions internal/tags/key_value_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,15 @@ func TestKeyValueTagsMap(t *testing.T) {
"key3": "value3",
},
},
{
name: "empty_value",
tags: New(ctx, map[string]*string{
"key1": testStringPtr(""),
}),
want: map[string]string{
"key1": "",
},
},
{
name: "nil_value",
tags: New(ctx, map[string]*string{
Expand Down
Loading