From 5b4562aaeaabb9e5471378573558f3faf9caa637 Mon Sep 17 00:00:00 2001 From: kterada0509 Date: Tue, 2 Jul 2019 23:32:34 +0900 Subject: [PATCH] Add support tags attribute for aws_appmesh_route resource --- aws/resource_aws_appmesh_route.go | 23 ++ aws/resource_aws_appmesh_route_test.go | 241 ++++++++++----------- aws/resource_aws_appmesh_test.go | 1 + website/docs/r/appmesh_route.html.markdown | 1 + 4 files changed, 145 insertions(+), 121 deletions(-) diff --git a/aws/resource_aws_appmesh_route.go b/aws/resource_aws_appmesh_route.go index 2acd03d455f..128f1c10040 100644 --- a/aws/resource_aws_appmesh_route.go +++ b/aws/resource_aws_appmesh_route.go @@ -175,6 +175,8 @@ func resourceAwsAppmeshRoute() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "tags": tagsSchema(), }, } } @@ -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) @@ -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 } @@ -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) } diff --git a/aws/resource_aws_appmesh_route_test.go b/aws/resource_aws_appmesh_route_test.go index 58d41881bd2..d0019d29f6c 100644 --- a/aws/resource_aws_appmesh_route_test.go +++ b/aws/resource_aws_appmesh_route_test.go @@ -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 @@ -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}" @@ -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}" @@ -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}" @@ -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) } diff --git a/aws/resource_aws_appmesh_test.go b/aws/resource_aws_appmesh_test.go index ccc4769570b..ff88b4eb4b0 100644 --- a/aws/resource_aws_appmesh_test.go +++ b/aws/resource_aws_appmesh_test.go @@ -14,6 +14,7 @@ func TestAccAWSAppmesh(t *testing.T) { "Route": { "httpRoute": testAccAwsAppmeshRoute_httpRoute, "tcpRoute": testAccAwsAppmeshRoute_tcpRoute, + "tags": testAccAwsAppmeshRoute_tags, }, "VirtualNode": { "basic": testAccAwsAppmeshVirtualNode_basic, diff --git a/website/docs/r/appmesh_route.html.markdown b/website/docs/r/appmesh_route.html.markdown index 41941a8e7d4..f011866f53b 100644 --- a/website/docs/r/appmesh_route.html.markdown +++ b/website/docs/r/appmesh_route.html.markdown @@ -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: