From c953a2fc412bd3451e4fd59e834df81675d0ef97 Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Thu, 27 Apr 2017 07:50:06 +1200 Subject: [PATCH] provider/aws: Set aws_instance volume_tags to be Computed (#14007) Fixes: #14003 When an EBS volume was created and tags were specified on that resource and NOT the aws_instance it was attached to, the tags would be removed on subsequent Terraform runs. We need to set volume_tags to be Computed to allow for changes to EBS volumes not created as part of the instance but that are attached to the instance ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSInstance_volumeTagsComputed' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/04/27 07:33:36 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSInstance_volumeTagsComputed -timeout 120m === RUN TestAccAWSInstance_volumeTagsComputed --- PASS: TestAccAWSInstance_volumeTagsComputed (151.37s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 151.411s ``` --- .../providers/aws/resource_aws_instance.go | 2 +- .../aws/resource_aws_instance_test.go | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index 383beb090..b648b3b35 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -234,7 +234,7 @@ func resourceAwsInstance() *schema.Resource { "tags": tagsSchema(), - "volume_tags": tagsSchema(), + "volume_tags": tagsSchemaComputed(), "block_device": { Type: schema.TypeMap, diff --git a/builtin/providers/aws/resource_aws_instance_test.go b/builtin/providers/aws/resource_aws_instance_test.go index 7c70f1cbd..01dd6d99c 100644 --- a/builtin/providers/aws/resource_aws_instance_test.go +++ b/builtin/providers/aws/resource_aws_instance_test.go @@ -678,6 +678,25 @@ func TestAccAWSInstance_volumeTags(t *testing.T) { }) } +func TestAccAWSInstance_volumeTagsComputed(t *testing.T) { + var v ec2.Instance + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckInstanceConfigWithAttachedVolume, + Check: resource.ComposeTestCheckFunc( + testAccCheckInstanceExists("aws_instance.foo", &v), + ), + ExpectNonEmptyPlan: false, + }, + }, + }) +} + func TestAccAWSInstance_instanceProfileChange(t *testing.T) { var v ec2.Instance rName := acctest.RandString(5) @@ -1382,6 +1401,69 @@ resource "aws_instance" "foo" { } ` +const testAccCheckInstanceConfigWithAttachedVolume = ` +data "aws_ami" "debian_jessie_latest" { + most_recent = true + + filter { + name = "name" + values = ["debian-jessie-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + filter { + name = "architecture" + values = ["x86_64"] + } + + filter { + name = "root-device-type" + values = ["ebs"] + } + + owners = ["379101102735"] # Debian +} + +resource "aws_instance" "foo" { + ami = "${data.aws_ami.debian_jessie_latest.id}" + associate_public_ip_address = true + count = 1 + instance_type = "t2.medium" + + root_block_device { + volume_size = "10" + volume_type = "standard" + delete_on_termination = true + } + + tags { + Name = "test-terraform" + } +} + +resource "aws_ebs_volume" "test" { + depends_on = ["aws_instance.foo"] + availability_zone = "${aws_instance.foo.availability_zone}" + type = "gp2" + size = "10" + + tags { + Name = "test-terraform" + } +} + +resource "aws_volume_attachment" "test" { + depends_on = ["aws_ebs_volume.test"] + device_name = "/dev/xvdg" + volume_id = "${aws_ebs_volume.test.id}" + instance_id = "${aws_instance.foo.id}" +} +` + const testAccCheckInstanceConfigNoVolumeTags = ` resource "aws_instance" "foo" { ami = "ami-55a7ea65"