From 5f831021061910146b75b46c4045f226aca3e60b Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Fri, 15 Jan 2021 11:14:58 -0500 Subject: [PATCH 1/5] resource/instance: Fix invalid address set --- aws/resource_aws_instance_test.go | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 4fc3687923c..84e10fdaebf 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -1220,6 +1220,33 @@ func TestAccAWSInstance_blockDeviceTags_ebsAndRoot(t *testing.T) { }) } +func TestAccAWSInstance_blockDeviceTags_noDevices(t *testing.T) { + // https://github.com/hashicorp/terraform-provider-aws/issues/17125 + var v ec2.Instance + resourceName := "aws_instance.test" + rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandString(12)) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccInstanceConfigBlockDeviceTagsNoTags(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckInstanceExists(resourceName, &v), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"ephemeral_block_device"}, + }, + }, + }) +} + func TestAccAWSInstance_instanceProfileChange(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" @@ -4207,6 +4234,23 @@ resource "aws_instance" "test" { `) } +func testAccInstanceConfigBlockDeviceTagsNoTags(rName string) string { + // https://github.com/hashicorp/terraform-provider-aws/issues/17125 + return composeConfig( + testAccLatestAmazonLinuxHvmEbsAmiConfig(), + testAccAvailableEc2InstanceTypeForRegion("t1.micro", "m1.small", "t3.micro", "t2.micro"), + fmt.Sprintf(` +resource "aws_instance" "test" { + ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id + instance_type = data.aws_ec2_instance_type_offering.available.instance_type + + tags = { + Name = %[1]q + } +} +`, rName)) +} + func testAccInstanceConfigBlockDeviceTagsEBSTagsConflict() string { return composeConfig(testAccLatestAmazonLinuxHvmEbsAmiConfig(), ` resource "aws_instance" "test" { From 3d22c961bc2ce420354eb5d4a4e8aa12fbaeba39 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Fri, 15 Jan 2021 12:55:12 -0500 Subject: [PATCH 2/5] data source/instance: Add block device tags --- aws/data_source_aws_instance.go | 4 ++ aws/data_source_aws_instance_test.go | 56 ++++++++++++++++++++++++++++ aws/resource_aws_instance.go | 2 +- aws/resource_aws_instance_test.go | 44 ---------------------- 4 files changed, 61 insertions(+), 45 deletions(-) diff --git a/aws/data_source_aws_instance.go b/aws/data_source_aws_instance.go index ad5f55c6d53..171de746cef 100644 --- a/aws/data_source_aws_instance.go +++ b/aws/data_source_aws_instance.go @@ -208,6 +208,8 @@ func dataSourceAwsInstance() *schema.Resource { Computed: true, }, + "tags": tagsSchema(), + "throughput": { Type: schema.TypeInt, Computed: true, @@ -268,6 +270,8 @@ func dataSourceAwsInstance() *schema.Resource { Computed: true, }, + "tags": tagsSchema(), + "throughput": { Type: schema.TypeInt, Computed: true, diff --git a/aws/data_source_aws_instance_test.go b/aws/data_source_aws_instance_test.go index ae488ca058f..ae5831ebbce 100644 --- a/aws/data_source_aws_instance_test.go +++ b/aws/data_source_aws_instance_test.go @@ -533,6 +533,25 @@ func TestAccAWSInstanceDataSource_enclaveOptions(t *testing.T) { }) } +func TestAccAWSInstanceDataSource_blockDeviceTags(t *testing.T) { + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_instance.test" + datasourceName := "data.aws_instance.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccInstanceDataSourceConfig_blockDeviceTags(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"), + ), + }, + }, + }) +} + // Lookup based on InstanceID var testAccInstanceDataSourceConfig = testAccLatestAmazonLinuxHvmEbsAmiConfig() + ` resource "aws_instance" "test" { @@ -1016,3 +1035,40 @@ data "aws_instance" "test" { } `, rName)) } + +func testAccInstanceDataSourceConfig_blockDeviceTags(rName string) string { + return composeConfig( + testAccLatestAmazonLinuxHvmEbsAmiConfig(), + testAccAvailableEc2InstanceTypeForRegion("t3.micro", "t2.micro"), + fmt.Sprintf(` +resource "aws_instance" "test" { + ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id + instance_type = data.aws_ec2_instance_type_offering.available.instance_type + + tags = { + Name = %[1]q + } + + ebs_block_device { + device_name = "/dev/xvdc" + volume_size = 10 + + tags = { + Name = %[1]q + Factum = "SapereAude" + } + } + + root_block_device { + tags = { + Name = %[1]q + Factum = "VincitQuiSeVincit" + } + } +} + +data "aws_instance" "test" { + instance_id = aws_instance.test.id +} +`, rName)) +} diff --git a/aws/resource_aws_instance.go b/aws/resource_aws_instance.go index 925861a4489..bc5a81d0711 100644 --- a/aws/resource_aws_instance.go +++ b/aws/resource_aws_instance.go @@ -1790,7 +1790,7 @@ func readBlockDevicesFromInstance(d *schema.ResourceData, instance *ec2.Instance if instanceBd.DeviceName != nil { bd["device_name"] = aws.StringValue(instanceBd.DeviceName) } - if _, ok := d.GetOk("volume_tags"); !ok && vol.Tags != nil { + if v, ok := d.GetOk("volume_tags"); (!ok || v == nil) && vol.Tags != nil { bd["tags"] = keyvaluetags.Ec2KeyValueTags(vol.Tags).IgnoreAws().Map() } diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 84e10fdaebf..4fc3687923c 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -1220,33 +1220,6 @@ func TestAccAWSInstance_blockDeviceTags_ebsAndRoot(t *testing.T) { }) } -func TestAccAWSInstance_blockDeviceTags_noDevices(t *testing.T) { - // https://github.com/hashicorp/terraform-provider-aws/issues/17125 - var v ec2.Instance - resourceName := "aws_instance.test" - rName := fmt.Sprintf("tf-testacc-instance-%s", acctest.RandString(12)) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckInstanceDestroy, - Steps: []resource.TestStep{ - { - Config: testAccInstanceConfigBlockDeviceTagsNoTags(rName), - Check: resource.ComposeTestCheckFunc( - testAccCheckInstanceExists(resourceName, &v), - ), - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"ephemeral_block_device"}, - }, - }, - }) -} - func TestAccAWSInstance_instanceProfileChange(t *testing.T) { var v ec2.Instance resourceName := "aws_instance.test" @@ -4234,23 +4207,6 @@ resource "aws_instance" "test" { `) } -func testAccInstanceConfigBlockDeviceTagsNoTags(rName string) string { - // https://github.com/hashicorp/terraform-provider-aws/issues/17125 - return composeConfig( - testAccLatestAmazonLinuxHvmEbsAmiConfig(), - testAccAvailableEc2InstanceTypeForRegion("t1.micro", "m1.small", "t3.micro", "t2.micro"), - fmt.Sprintf(` -resource "aws_instance" "test" { - ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id - instance_type = data.aws_ec2_instance_type_offering.available.instance_type - - tags = { - Name = %[1]q - } -} -`, rName)) -} - func testAccInstanceConfigBlockDeviceTagsEBSTagsConflict() string { return composeConfig(testAccLatestAmazonLinuxHvmEbsAmiConfig(), ` resource "aws_instance" "test" { From 7e8995108ab85d5a0c1fbb1947b05396cb1c60d1 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Fri, 15 Jan 2021 13:04:27 -0500 Subject: [PATCH 3/5] resource/instance: Revert volume_tags logic --- aws/resource_aws_instance.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_instance.go b/aws/resource_aws_instance.go index bc5a81d0711..925861a4489 100644 --- a/aws/resource_aws_instance.go +++ b/aws/resource_aws_instance.go @@ -1790,7 +1790,7 @@ func readBlockDevicesFromInstance(d *schema.ResourceData, instance *ec2.Instance if instanceBd.DeviceName != nil { bd["device_name"] = aws.StringValue(instanceBd.DeviceName) } - if v, ok := d.GetOk("volume_tags"); (!ok || v == nil) && vol.Tags != nil { + if _, ok := d.GetOk("volume_tags"); !ok && vol.Tags != nil { bd["tags"] = keyvaluetags.Ec2KeyValueTags(vol.Tags).IgnoreAws().Map() } From 6202907fa47eb1e5624f4711023eed61497da735 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Fri, 15 Jan 2021 13:11:39 -0500 Subject: [PATCH 4/5] data source/instance: Fix tags to be computed --- aws/data_source_aws_instance.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/data_source_aws_instance.go b/aws/data_source_aws_instance.go index 171de746cef..3eb17e5c359 100644 --- a/aws/data_source_aws_instance.go +++ b/aws/data_source_aws_instance.go @@ -208,7 +208,7 @@ func dataSourceAwsInstance() *schema.Resource { Computed: true, }, - "tags": tagsSchema(), + "tags": tagsSchemaComputed(), "throughput": { Type: schema.TypeInt, @@ -270,7 +270,7 @@ func dataSourceAwsInstance() *schema.Resource { Computed: true, }, - "tags": tagsSchema(), + "tags": tagsSchemaComputed(), "throughput": { Type: schema.TypeInt, From 14c19280a95e6cadf6aa6d5dfbff79bed9a87ca4 Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Fri, 15 Jan 2021 13:29:07 -0500 Subject: [PATCH 5/5] resource/instance: Adjust volume tags logic --- aws/resource_aws_instance.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_instance.go b/aws/resource_aws_instance.go index 925861a4489..9cd45cf3818 100644 --- a/aws/resource_aws_instance.go +++ b/aws/resource_aws_instance.go @@ -1790,7 +1790,7 @@ func readBlockDevicesFromInstance(d *schema.ResourceData, instance *ec2.Instance if instanceBd.DeviceName != nil { bd["device_name"] = aws.StringValue(instanceBd.DeviceName) } - if _, ok := d.GetOk("volume_tags"); !ok && vol.Tags != nil { + if v, ok := d.GetOk("volume_tags"); (!ok || v == nil || len(v.(map[string]interface{})) == 0) && vol.Tags != nil { bd["tags"] = keyvaluetags.Ec2KeyValueTags(vol.Tags).IgnoreAws().Map() }