From 812c542cff88e40c74f9d34660b2c07c2fecddea Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sat, 6 Mar 2021 14:49:03 +0200 Subject: [PATCH 1/3] tag on create --- aws/resource_aws_ami_from_instance.go | 16 +++++---------- aws/resource_aws_ami_from_instance_test.go | 23 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/aws/resource_aws_ami_from_instance.go b/aws/resource_aws_ami_from_instance.go index c996483e867..348c2835145 100644 --- a/aws/resource_aws_ami_from_instance.go +++ b/aws/resource_aws_ami_from_instance.go @@ -8,7 +8,6 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/terraform-providers/terraform-provider-aws/aws/internal/hashcode" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) func resourceAwsAmiFromInstance() *schema.Resource { @@ -223,10 +222,11 @@ func resourceAwsAmiFromInstanceCreate(d *schema.ResourceData, meta interface{}) client := meta.(*AWSClient).ec2conn req := &ec2.CreateImageInput{ - Name: aws.String(d.Get("name").(string)), - Description: aws.String(d.Get("description").(string)), - InstanceId: aws.String(d.Get("source_instance_id").(string)), - NoReboot: aws.Bool(d.Get("snapshot_without_reboot").(bool)), + Name: aws.String(d.Get("name").(string)), + Description: aws.String(d.Get("description").(string)), + InstanceId: aws.String(d.Get("source_instance_id").(string)), + NoReboot: aws.Bool(d.Get("snapshot_without_reboot").(bool)), + TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeImage), } res, err := client.CreateImage(req) @@ -237,12 +237,6 @@ func resourceAwsAmiFromInstanceCreate(d *schema.ResourceData, meta interface{}) d.SetId(aws.StringValue(res.ImageId)) d.Set("manage_ebs_snapshots", true) - if v := d.Get("tags").(map[string]interface{}); len(v) > 0 { - if err := keyvaluetags.Ec2CreateTags(client, d.Id(), v); err != nil { - return fmt.Errorf("error adding tags: %s", err) - } - } - _, err = resourceAwsAmiWaitForAvailable(d.Timeout(schema.TimeoutCreate), d.Id(), client) if err != nil { return err diff --git a/aws/resource_aws_ami_from_instance_test.go b/aws/resource_aws_ami_from_instance_test.go index d2e7ef44391..51de853d35a 100644 --- a/aws/resource_aws_ami_from_instance_test.go +++ b/aws/resource_aws_ami_from_instance_test.go @@ -33,6 +33,7 @@ func TestAccAWSAMIFromInstance_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "image_type", "machine"), resource.TestCheckResourceAttr(resourceName, "hypervisor", "xen"), testAccCheckResourceAttrAccountID(resourceName, "owner_id"), + resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), ), }, }, @@ -78,6 +79,28 @@ func TestAccAWSAMIFromInstance_tags(t *testing.T) { }) } +func TestAccAWSAMIFromInstance_disappears(t *testing.T) { + var image ec2.Image + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_ami_from_instance.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAMIFromInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAMIFromInstanceConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAMIFromInstanceExists(resourceName, &image), + testAccCheckResourceDisappears(testAccProvider, resourceAwsAmiFromInstance(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccCheckAWSAMIFromInstanceExists(resourceName string, image *ec2.Image) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[resourceName] From 2f10e8960878eca88efc3ca184829da94226e128 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sat, 6 Mar 2021 14:52:15 +0200 Subject: [PATCH 2/3] changelog --- .changelog/17968.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/17968.txt diff --git a/.changelog/17968.txt b/.changelog/17968.txt new file mode 100644 index 00000000000..daf69ea76e6 --- /dev/null +++ b/.changelog/17968.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_ami_from_instance: Tag on create. +``` \ No newline at end of file From cb13863ca40094569deb91ad3a81846036490a3b Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Thu, 25 Mar 2021 15:18:24 -0400 Subject: [PATCH 3/3] Update aws/resource_aws_ami_from_instance_test.go --- aws/resource_aws_ami_from_instance_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/resource_aws_ami_from_instance_test.go b/aws/resource_aws_ami_from_instance_test.go index ea14855ce06..98cc9c5a9ed 100644 --- a/aws/resource_aws_ami_from_instance_test.go +++ b/aws/resource_aws_ami_from_instance_test.go @@ -88,6 +88,7 @@ func TestAccAWSAMIFromInstance_disappears(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), Providers: testAccProviders, CheckDestroy: testAccCheckAWSAMIFromInstanceDestroy, Steps: []resource.TestStep{