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_route resource #9206

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
23 changes: 23 additions & 0 deletions aws/resource_aws_appmesh_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ func resourceAwsAppmeshRoute() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchema(),
},
}
}
Expand All @@ -187,6 +189,7 @@ func resourceAwsAppmeshRouteCreate(d *schema.ResourceData, meta interface{}) err
RouteName: aws.String(d.Get("name").(string)),
VirtualRouterName: aws.String(d.Get("virtual_router_name").(string)),
Spec: expandAppmeshRouteSpec(d.Get("spec").([]interface{})),
Tags: tagsFromMapAppmesh(d.Get("tags").(map[string]interface{})),
}

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

err = saveTagsAppmesh(conn, d, aws.StringValue(resp.Route.Metadata.Arn))
if isAWSErr(err, appmesh.ErrCodeNotFoundException, "") {
log.Printf("[WARN] App Mesh route (%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 @@ -255,6 +268,16 @@ func resourceAwsAppmeshRouteUpdate(d *schema.ResourceData, meta interface{}) err
}
}

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

return resourceAwsAppmeshRouteRead(d, meta)
}

Expand Down
241 changes: 120 additions & 121 deletions aws/resource_aws_appmesh_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,62 @@ func testAccAwsAppmeshRoute_tcpRoute(t *testing.T) {
})
}

func testAccAwsAppmeshRoute_tags(t *testing.T) {
var r appmesh.RouteData
resourceName := "aws_appmesh_route.foo"
meshName := fmt.Sprintf("tf-test-mesh-%d", acctest.RandInt())
vrName := fmt.Sprintf("tf-test-router-%d", acctest.RandInt())
vn1Name := fmt.Sprintf("tf-test-node-%d", acctest.RandInt())
vn2Name := fmt.Sprintf("tf-test-node-%d", acctest.RandInt())
rName := fmt.Sprintf("tf-test-route-%d", acctest.RandInt())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAppmeshRouteDestroy,
Steps: []resource.TestStep{
{
Config: testAccAppmeshRouteConfigWithTags(meshName, vrName, vn1Name, vn2Name, rName, "foo", "bar", "good", "bad"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppmeshRouteExists(resourceName, &r),
resource.TestCheckResourceAttr(
resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(
resourceName, "tags.foo", "bar"),
resource.TestCheckResourceAttr(
resourceName, "tags.good", "bad"),
),
},
{
Config: testAccAppmeshRouteConfigWithTags(meshName, vrName, vn1Name, vn2Name, rName, "foo2", "bar", "good", "bad2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppmeshRouteExists(resourceName, &r),
resource.TestCheckResourceAttr(
resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(
resourceName, "tags.foo2", "bar"),
resource.TestCheckResourceAttr(
resourceName, "tags.good", "bad2"),
),
},
{
Config: testAccAppmeshRouteConfig_httpRoute(meshName, vrName, vn1Name, vn2Name, rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppmeshRouteExists(resourceName, &r),
resource.TestCheckResourceAttr(
resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportStateId: fmt.Sprintf("%s/%s/%s", meshName, vrName, rName),
ImportState: true,
ImportStateVerify: true,
},
},
})
}

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

Expand Down Expand Up @@ -268,42 +324,47 @@ func testAccCheckAppmeshRouteExists(name string, v *appmesh.RouteData) resource.
}
}

func testAccAppmeshRouteConfig_httpRoute(meshName, vrName, vn1Name, vn2Name, rName string) string {
func testAccAppmeshRouteConfigBase(meshName, vrName, vn1Name, vn2Name string) string {
return fmt.Sprintf(`
resource "aws_appmesh_mesh" "foo" {
name = %[1]q
name = %[1]q
}

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

spec {
listener {
port_mapping {
port = 8080
protocol = "http"
}
}
}
name = %[2]q
mesh_name = "${aws_appmesh_mesh.foo.id}"

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

resource "aws_appmesh_virtual_node" "foo" {
name = %[3]q
mesh_name = "${aws_appmesh_mesh.foo.id}"
name = %[3]q
mesh_name = "${aws_appmesh_mesh.foo.id}"

spec {}
spec {}
}

resource "aws_appmesh_virtual_node" "bar" {
name = %[4]q
mesh_name = "${aws_appmesh_mesh.foo.id}"
name = %[4]q
mesh_name = "${aws_appmesh_mesh.foo.id}"

spec {}
spec {}
}
`, meshName, vrName, vn1Name, vn2Name)
}

func testAccAppmeshRouteConfig_httpRoute(meshName, vrName, vn1Name, vn2Name, rName string) string {
return testAccAppmeshRouteConfigBase(meshName, vrName, vn1Name, vn2Name) + fmt.Sprintf(`

resource "aws_appmesh_route" "foo" {
name = %[5]q
name = %[1]q
mesh_name = "${aws_appmesh_mesh.foo.id}"
virtual_router_name = "${aws_appmesh_virtual_router.foo.name}"

Expand All @@ -322,45 +383,14 @@ resource "aws_appmesh_route" "foo" {
}
}
}
`, meshName, vrName, vn1Name, vn2Name, rName)
`, rName)
}

func testAccAppmeshRouteConfig_httpRouteUpdated(meshName, vrName, vn1Name, vn2Name, rName string) string {
return fmt.Sprintf(`
resource "aws_appmesh_mesh" "foo" {
name = %[1]q
}

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

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

resource "aws_appmesh_virtual_node" "foo" {
name = %[3]q
mesh_name = "${aws_appmesh_mesh.foo.id}"

spec {}
}

resource "aws_appmesh_virtual_node" "bar" {
name = %[4]q
mesh_name = "${aws_appmesh_mesh.foo.id}"

spec {}
}
return testAccAppmeshRouteConfigBase(meshName, vrName, vn1Name, vn2Name) + fmt.Sprintf(`

resource "aws_appmesh_route" "foo" {
name = %[5]q
name = %[1]q
mesh_name = "${aws_appmesh_mesh.foo.id}"
virtual_router_name = "${aws_appmesh_virtual_router.foo.name}"

Expand All @@ -384,45 +414,14 @@ resource "aws_appmesh_route" "foo" {
}
}
}
`, meshName, vrName, vn1Name, vn2Name, rName)
`, rName)
}

func testAccAppmeshRouteConfig_tcpRoute(meshName, vrName, vn1Name, vn2Name, rName string) string {
return fmt.Sprintf(`
resource "aws_appmesh_mesh" "foo" {
name = %[1]q
}

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

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

resource "aws_appmesh_virtual_node" "foo" {
name = %[3]q
mesh_name = "${aws_appmesh_mesh.foo.id}"

spec {}
}

resource "aws_appmesh_virtual_node" "bar" {
name = %[4]q
mesh_name = "${aws_appmesh_mesh.foo.id}"

spec {}
}
return testAccAppmeshRouteConfigBase(meshName, vrName, vn1Name, vn2Name) + fmt.Sprintf(`

resource "aws_appmesh_route" "foo" {
name = %[5]q
name = %[1]q
mesh_name = "${aws_appmesh_mesh.foo.id}"
virtual_router_name = "${aws_appmesh_virtual_router.foo.name}"

Expand All @@ -437,63 +436,63 @@ resource "aws_appmesh_route" "foo" {
}
}
}
`, meshName, vrName, vn1Name, vn2Name, rName)
`, rName)
}

func testAccAppmeshRouteConfig_tcpRouteUpdated(meshName, vrName, vn1Name, vn2Name, rName string) string {
return fmt.Sprintf(`
resource "aws_appmesh_mesh" "foo" {
name = %[1]q
}
return testAccAppmeshRouteConfigBase(meshName, vrName, vn1Name, vn2Name) + fmt.Sprintf(`

resource "aws_appmesh_virtual_router" "foo" {
name = %[2]q
mesh_name = "${aws_appmesh_mesh.foo.id}"
resource "aws_appmesh_route" "foo" {
name = %[1]q
mesh_name = "${aws_appmesh_mesh.foo.id}"
virtual_router_name = "${aws_appmesh_virtual_router.foo.name}"

spec {
listener {
port_mapping {
port = 8080
protocol = "tcp"
tcp_route {
action {
weighted_target {
virtual_node = "${aws_appmesh_virtual_node.foo.name}"
weight = 90
}

weighted_target {
virtual_node = "${aws_appmesh_virtual_node.bar.name}"
weight = 10
}
}
}
}
}

resource "aws_appmesh_virtual_node" "foo" {
name = %[3]q
mesh_name = "${aws_appmesh_mesh.foo.id}"

spec {}
`, rName)
}

resource "aws_appmesh_virtual_node" "bar" {
name = %[4]q
mesh_name = "${aws_appmesh_mesh.foo.id}"

spec {}
}
func testAccAppmeshRouteConfigWithTags(meshName, vrName, vn1Name, vn2Name, rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return testAccAppmeshRouteConfigBase(meshName, vrName, vn1Name, vn2Name) + fmt.Sprintf(`

resource "aws_appmesh_route" "foo" {
name = %[5]q
name = %[1]q
mesh_name = "${aws_appmesh_mesh.foo.id}"
virtual_router_name = "${aws_appmesh_virtual_router.foo.name}"

spec {
tcp_route {
http_route {
match {
prefix = "/"
}

action {
weighted_target {
virtual_node = "${aws_appmesh_virtual_node.foo.name}"
weight = 90
}

weighted_target {
virtual_node = "${aws_appmesh_virtual_node.bar.name}"
weight = 10
weight = 100
}
}
}
}

tags = {
%[2]s = %[3]q
%[4]s = %[5]q
}
}
`, meshName, vrName, vn1Name, vn2Name, rName)
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}
1 change: 1 addition & 0 deletions aws/resource_aws_appmesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestAccAWSAppmesh(t *testing.T) {
"Route": {
"httpRoute": testAccAwsAppmeshRoute_httpRoute,
"tcpRoute": testAccAwsAppmeshRoute_tcpRoute,
"tags": testAccAwsAppmeshRoute_tags,
},
"VirtualNode": {
"basic": testAccAwsAppmeshVirtualNode_basic,
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/appmesh_route.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ The following arguments are supported:
* `mesh_name` - (Required) The name of the service mesh in which to create the route.
* `virtual_router_name` - (Required) The name of the virtual router in which to create the route.
* `spec` - (Required) The route specification to apply.
* `tags` - (Optional) A mapping of tags to assign to the resource.

The `spec` object supports the following:

Expand Down