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 tags attribute for aws_appmesh_virtual_router resource #9249

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
1 change: 1 addition & 0 deletions aws/resource_aws_appmesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestAccAWSAppmesh(t *testing.T) {
},
"VirtualRouter": {
"basic": testAccAwsAppmeshVirtualRouter_basic,
"tags": testAccAwsAppmeshVirtualRouter_tags,
},
"VirtualService": {
"virtualNode": testAccAwsAppmeshVirtualService_virtualNode,
Expand Down
23 changes: 23 additions & 0 deletions aws/resource_aws_appmesh_virtual_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func resourceAwsAppmeshVirtualRouter() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchema(),
},
}
}
Expand All @@ -121,6 +123,7 @@ func resourceAwsAppmeshVirtualRouterCreate(d *schema.ResourceData, meta interfac
MeshName: aws.String(d.Get("mesh_name").(string)),
VirtualRouterName: aws.String(d.Get("name").(string)),
Spec: expandAppmeshVirtualRouterSpec(d.Get("spec").([]interface{})),
Tags: tagsFromMapAppmesh(d.Get("tags").(map[string]interface{})),
}

log.Printf("[DEBUG] Creating App Mesh virtual router: %#v", req)
Expand Down Expand Up @@ -165,6 +168,16 @@ func resourceAwsAppmeshVirtualRouterRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("error setting spec: %s", err)
}

err = saveTagsAppmesh(conn, d, aws.StringValue(resp.VirtualRouter.Metadata.Arn))
if isAWSErr(err, appmesh.ErrCodeNotFoundException, "") {
log.Printf("[WARN] App Mesh virtual router (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("error saving tags: %s", err)
}

return nil
}

Expand All @@ -186,6 +199,16 @@ func resourceAwsAppmeshVirtualRouterUpdate(d *schema.ResourceData, meta interfac
}
}

err := setTagsAppmesh(conn, d, d.Get("arn").(string))
if isAWSErr(err, appmesh.ErrCodeNotFoundException, "") {
log.Printf("[WARN] App Mesh virtual router (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return resourceAwsAppmeshVirtualRouterRead(d, meta)
}

Expand Down
83 changes: 83 additions & 0 deletions aws/resource_aws_appmesh_virtual_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,62 @@ func testAccAwsAppmeshVirtualRouter_basic(t *testing.T) {
})
}

func testAccAwsAppmeshVirtualRouter_tags(t *testing.T) {
var vr appmesh.VirtualRouterData
resourceName := "aws_appmesh_virtual_router.foo"
meshName := fmt.Sprintf("tf-test-mesh-%d", acctest.RandInt())
vrName := fmt.Sprintf("tf-test-router-%d", acctest.RandInt())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAppmeshVirtualRouterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAppmeshVirtualRouterConfig_tags(meshName, vrName, "foo", "bar", "good", "bad"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppmeshVirtualRouterExists(
resourceName, &vr),
resource.TestCheckResourceAttr(
resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(
resourceName, "tags.foo", "bar"),
resource.TestCheckResourceAttr(
resourceName, "tags.good", "bad"),
),
},
{
Config: testAccAppmeshVirtualRouterConfig_tags(meshName, vrName, "foo2", "bar", "good", "bad2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppmeshVirtualRouterExists(
resourceName, &vr),
resource.TestCheckResourceAttr(
resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(
resourceName, "tags.foo2", "bar"),
resource.TestCheckResourceAttr(
resourceName, "tags.good", "bad2"),
),
},
{
Config: testAccAppmeshVirtualRouterConfig_basic(meshName, vrName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppmeshVirtualRouterExists(
resourceName, &vr),
resource.TestCheckResourceAttr(
resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportStateId: fmt.Sprintf("%s/%s", meshName, vrName),
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAppmeshVirtualRouterDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).appmeshconn

Expand Down Expand Up @@ -244,3 +300,30 @@ resource "aws_appmesh_virtual_router" "foo" {
}
`, meshName, vrName)
}

func testAccAppmeshVirtualRouterConfig_tags(meshName, vrName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_appmesh_mesh" "foo" {
name = "%[1]s"
}

resource "aws_appmesh_virtual_router" "foo" {
name = "%[2]s"
mesh_name = "${aws_appmesh_mesh.foo.id}"

spec {
listener {
port_mapping {
port = 8080
protocol = "http"
}
}
}

tags = {
%[3]s = %[4]q
%[5]s = %[6]q
}
}
`, meshName, vrName, tagKey1, tagValue1, tagKey2, tagValue2)
}
1 change: 1 addition & 0 deletions website/docs/r/appmesh_virtual_router.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ The following arguments are supported:
* `name` - (Required) The name to use for the virtual router.
* `mesh_name` - (Required) The name of the service mesh in which to create the virtual router.
* `spec` - (Required) The virtual router specification to apply.
* `tags` - (Optional) A mapping of tags to assign to the resource.

The `spec` object supports the following:

Expand Down