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_service resource #9252

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 @@ -28,6 +28,7 @@ func TestAccAWSAppmesh(t *testing.T) {
"VirtualService": {
"virtualNode": testAccAwsAppmeshVirtualService_virtualNode,
"virtualRouter": testAccAwsAppmeshVirtualService_virtualRouter,
"tags": testAccAwsAppmeshVirtualService_tags,
},
}

Expand Down
23 changes: 23 additions & 0 deletions aws/resource_aws_appmesh_virtual_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func resourceAwsAppmeshVirtualService() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchema(),
},
}
}
Expand All @@ -116,6 +118,7 @@ func resourceAwsAppmeshVirtualServiceCreate(d *schema.ResourceData, meta interfa
MeshName: aws.String(d.Get("mesh_name").(string)),
VirtualServiceName: aws.String(d.Get("name").(string)),
Spec: expandAppmeshVirtualServiceSpec(d.Get("spec").([]interface{})),
Tags: tagsFromMapAppmesh(d.Get("tags").(map[string]interface{})),
}

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

err = saveTagsAppmesh(conn, d, aws.StringValue(resp.VirtualService.Metadata.Arn))
if isAWSErr(err, appmesh.ErrCodeNotFoundException, "") {
log.Printf("[WARN] App Mesh virtual service (%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 @@ -181,6 +194,16 @@ func resourceAwsAppmeshVirtualServiceUpdate(d *schema.ResourceData, meta interfa
}
}

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

return resourceAwsAppmeshVirtualServiceRead(d, meta)
}

Expand Down
95 changes: 95 additions & 0 deletions aws/resource_aws_appmesh_virtual_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,61 @@ func testAccAwsAppmeshVirtualService_virtualRouter(t *testing.T) {
})
}

func testAccAwsAppmeshVirtualService_tags(t *testing.T) {
var vs appmesh.VirtualServiceData
resourceName := "aws_appmesh_virtual_service.foo"
meshName := fmt.Sprintf("tf-test-mesh-%d", acctest.RandInt())
vnName1 := fmt.Sprintf("tf-test-node-%d", acctest.RandInt())
vnName2 := fmt.Sprintf("tf-test-node-%d", acctest.RandInt())
vsName := fmt.Sprintf("tf-test-%d.mesh.local", acctest.RandInt())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAppmeshVirtualServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAppmeshVirtualServiceConfig_tags(meshName, vnName1, vnName2, vsName, "aws_appmesh_virtual_node.foo", "foo", "bar", "good", "bad"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppmeshVirtualServiceExists(resourceName, &vs),
resource.TestCheckResourceAttr(
resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(
resourceName, "tags.foo", "bar"),
resource.TestCheckResourceAttr(
resourceName, "tags.good", "bad"),
),
},
{
Config: testAccAppmeshVirtualServiceConfig_tags(meshName, vnName1, vnName2, vsName, "aws_appmesh_virtual_node.foo", "foo2", "bar", "good", "bad2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppmeshVirtualServiceExists(resourceName, &vs),
resource.TestCheckResourceAttr(
resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(
resourceName, "tags.foo2", "bar"),
resource.TestCheckResourceAttr(
resourceName, "tags.good", "bad2"),
),
},
{
Config: testAccAppmeshVirtualServiceConfig_virtualNode(meshName, vnName1, vnName2, vsName, "aws_appmesh_virtual_node.foo"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppmeshVirtualServiceExists(resourceName, &vs),
resource.TestCheckResourceAttr(
resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportStateId: fmt.Sprintf("%s/%s", meshName, vsName),
ImportState: true,
ImportStateVerify: true,
},
},
})
}

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

Expand Down Expand Up @@ -241,3 +296,43 @@ resource "aws_appmesh_virtual_service" "foo" {
}
`, meshName, vrName1, vrName2, vsName, rName)
}

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

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

spec {}
}

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

spec {}
}

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

spec {
provider {
virtual_node {
virtual_node_name = "${%s.name}"
}
}
}

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

The `spec` object supports the following:

Expand Down