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

add support for updating tags in apprunner vpc connection #27345

Merged
merged 10 commits into from
Oct 24, 2022
3 changes: 3 additions & 0 deletions .changelog/27345.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_apprunner_vpc_connector: Add ability to update tags
```
24 changes: 23 additions & 1 deletion internal/service/apprunner/vpc_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func ResourceVPCConnector() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceVPCConnectorCreate,
ReadWithoutTimeout: resourceVPCConnectorRead,
UpdateWithoutTimeout: resourceVPCConnectorUpdate,
DeleteWithoutTimeout: resourceVPCConnectorDelete,

Importer: &schema.ResourceImporter{
Expand Down Expand Up @@ -51,7 +53,8 @@ func ResourceVPCConnector() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"tags": tftags.TagsSchemaComputed(),
"tags": tftags.TagsSchemaComputed(),
"tags_all": tftags.TagsSchemaComputed(),
"status": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -61,6 +64,7 @@ func ResourceVPCConnector() *schema.Resource {
Computed: true,
},
},
CustomizeDiff: verify.SetTagsDiff,
}
}

Expand Down Expand Up @@ -167,9 +171,27 @@ func resourceVPCConnectorRead(ctx context.Context, d *schema.ResourceData, meta
return diag.FromErr(fmt.Errorf("error setting tags: %w", err))
}

if err := d.Set("tags_all", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.FromErr(fmt.Errorf("error setting tags_all: %w", err))
}

return nil
}

func resourceVPCConnectorUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).AppRunnerConn

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := UpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return diag.FromErr(fmt.Errorf("error updating App Runner vpc Connector (%s) tags: %s", d.Get("arn").(string), err))
kpena027 marked this conversation as resolved.
Show resolved Hide resolved
}
}

return resourceVPCConnectorRead(ctx, d, meta)
}

func resourceVPCConnectorDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).AppRunnerConn

Expand Down
77 changes: 43 additions & 34 deletions internal/service/apprunner/vpc_connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ func TestAccAppRunnerVPCConnector_tags(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccVPCConnectorConfig_tags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckVPCConnectorExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccVPCConnectorConfig_tags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckVPCConnectorExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}
Expand Down Expand Up @@ -157,7 +174,7 @@ func testAccCheckVPCConnectorExists(n string) resource.TestCheckFunc {
}
}

func testAccVPCConnectorConfig_basic(rName string) string {
func testAccVPCConnectorConfig_base(rName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
Expand All @@ -167,24 +184,27 @@ resource "aws_vpc" "test" {
}
}

resource "aws_security_group" "test" {
vpc_id = aws_vpc.test.id
name = %[1]q
resource "aws_subnet" "test" {
cidr_block = "10.1.1.0/24"
vpc_id = aws_vpc.test.id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there specific changes being made here? Or is this just changing the order of the resources in the function?


tags = {
Name = %[1]q
}
}

resource "aws_subnet" "test" {
cidr_block = "10.1.1.0/24"
vpc_id = aws_vpc.test.id
resource "aws_security_group" "test" {
vpc_id = aws_vpc.test.id

tags = {
Name = %[1]q
}
}
`, rName)
}

func testAccVPCConnectorConfig_basic(rName string) string {
return testAccVPCConnectorConfig_base(rName) + fmt.Sprintf(`
kpena027 marked this conversation as resolved.
Show resolved Hide resolved
resource "aws_apprunner_vpc_connector" "test" {
vpc_connector_name = %[1]q
subnets = [aws_subnet.test.id]
Expand All @@ -194,33 +214,7 @@ resource "aws_apprunner_vpc_connector" "test" {
}

func testAccVPCConnectorConfig_tags1(rName string, tagKey1 string, tagValue1 string) string {
kpena027 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"

tags = {
Name = %[1]q
}
}

resource "aws_security_group" "test" {
vpc_id = aws_vpc.test.id
name = %[1]q

tags = {
Name = %[1]q
}
}

resource "aws_subnet" "test" {
cidr_block = "10.1.1.0/24"
vpc_id = aws_vpc.test.id

tags = {
Name = %[1]q
}
}

return testAccVPCConnectorConfig_base(rName) + fmt.Sprintf(`
resource "aws_apprunner_vpc_connector" "test" {
vpc_connector_name = %[1]q
subnets = [aws_subnet.test.id]
Expand All @@ -233,6 +227,21 @@ resource "aws_apprunner_vpc_connector" "test" {
`, rName, tagKey1, tagValue1)
}

func testAccVPCConnectorConfig_tags2(rName string, tagKey1 string, tagValue1 string, tagKey2 string, tagValue2 string) string {
kpena027 marked this conversation as resolved.
Show resolved Hide resolved
return testAccVPCConnectorConfig_base(rName) + fmt.Sprintf(`
kpena027 marked this conversation as resolved.
Show resolved Hide resolved
resource "aws_apprunner_vpc_connector" "test" {
vpc_connector_name = %[1]q
subnets = [aws_subnet.test.id]
security_groups = [aws_security_group.test.id]

tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

func testAccPreCheckVPCConnector(t *testing.T) {
conn := acctest.Provider.Meta().(*conns.AWSClient).AppRunnerConn
ctx := context.Background()
Expand Down