Skip to content

Commit

Permalink
resource/aws_ami_from_instance: Tag on create (#17968)
Browse files Browse the repository at this point in the history
Output from acceptance testing in AWS Commercial:

```
--- PASS: TestAccAWSAMIFromInstance_disappears (259.64s)
--- PASS: TestAccAWSAMIFromInstance_basic (276.96s)
--- PASS: TestAccAWSAMIFromInstance_tags (309.87s)
```

Output from acceptance testing in AWS GovCloud (US):

```
--- PASS: TestAccAWSAMIFromInstance_disappears (289.51s)
--- PASS: TestAccAWSAMIFromInstance_basic (307.13s)
--- PASS: TestAccAWSAMIFromInstance_tags (356.92s)
```
  • Loading branch information
DrFaust92 authored Mar 25, 2021
1 parent 82cc1be commit 9216b97
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .changelog/17968.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_ami_from_instance: Tag on create.
```
16 changes: 5 additions & 11 deletions aws/resource_aws_ami_from_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -223,10 +222,11 @@ func resourceAwsAmiFromInstanceCreate(d *schema.ResourceData, meta interface{})
client := meta.(*AWSClient).ec2conn

req := &ec2.CreateImageInput{
Description: aws.String(d.Get("description").(string)),
InstanceId: aws.String(d.Get("source_instance_id").(string)),
Name: aws.String(d.Get("name").(string)),
NoReboot: aws.Bool(d.Get("snapshot_without_reboot").(bool)),
Description: aws.String(d.Get("description").(string)),
InstanceId: aws.String(d.Get("source_instance_id").(string)),
Name: aws.String(d.Get("name").(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)
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions aws/resource_aws_ami_from_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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"),
),
},
},
Expand Down Expand Up @@ -80,6 +81,29 @@ 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) },
ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID),
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]
Expand Down

0 comments on commit 9216b97

Please sign in to comment.