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

resource/ssm_association: remove resource from state in case of external deletion #3776

Merged
merged 2 commits into from
Mar 15, 2018
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
13 changes: 8 additions & 5 deletions aws/resource_aws_ssm_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -132,7 +131,7 @@ func resourceAwsSsmAssociationCreate(d *schema.ResourceData, meta interface{}) e

resp, err := ssmconn.CreateAssociation(assosciationInput)
if err != nil {
return errwrap.Wrapf("[ERROR] Error creating SSM association: {{err}}", err)
return fmt.Errorf("[ERROR] Error creating SSM association: %s", err)
}

if resp.AssociationDescription == nil {
Expand All @@ -157,7 +156,11 @@ func resourceAwsSsmAssociationRead(d *schema.ResourceData, meta interface{}) err
resp, err := ssmconn.DescribeAssociation(params)

if err != nil {
return errwrap.Wrapf("[ERROR] Error reading SSM association: {{err}}", err)
if isAWSErr(err, ssm.ErrCodeAssociationDoesNotExist, "") {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error reading SSM association: %s", err)
}
if resp.AssociationDescription == nil {
return fmt.Errorf("[ERROR] AssociationDescription was nil")
Expand Down Expand Up @@ -218,7 +221,7 @@ func resourceAwsSsmAssocationUpdate(d *schema.ResourceData, meta interface{}) er

_, err := ssmconn.UpdateAssociation(associationInput)
if err != nil {
return errwrap.Wrapf("[ERROR] Error updating SSM association: {{err}}", err)
return fmt.Errorf("[ERROR] Error updating SSM association: %s", err)
}

return resourceAwsSsmAssociationRead(d, meta)
Expand All @@ -236,7 +239,7 @@ func resourceAwsSsmAssociationDelete(d *schema.ResourceData, meta interface{}) e
_, err := ssmconn.DeleteAssociation(params)

if err != nil {
return errwrap.Wrapf("[ERROR] Error deleting SSM association: {{err}}", err)
return fmt.Errorf("[ERROR] Error deleting SSM association: %s", err)
}

return nil
Expand Down
101 changes: 80 additions & 21 deletions aws/resource_aws_ssm_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,45 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSSSMAssociation_basic(t *testing.T) {
name := acctest.RandString(10)
name := fmt.Sprintf("tf-acc-ssm-association-%s", acctest.RandString(10))

deleteSsmAssociaton := func() {
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
ssmconn := testAccProvider.Meta().(*AWSClient).ssmconn

ins, err := ec2conn.DescribeInstances(&ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:Name"),
Values: []*string{aws.String(name)},
},
},
})
if err != nil {
t.Fatalf("Error getting instance with tag:Name %s: %s", name, err)
}
if len(ins.Reservations) == 0 || len(ins.Reservations[0].Instances) == 0 {
t.Fatalf("No instance exists with tag:Name %s", name)
}
instanceId := ins.Reservations[0].Instances[0].InstanceId

_, err = ssmconn.DeleteAssociation(&ssm.DeleteAssociationInput{
Name: aws.String(name),
InstanceId: instanceId,
})
if err != nil {
t.Fatalf("Error deleting ssm association %s/%s: %s", name, aws.StringValue(instanceId), err)
}
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -25,6 +56,13 @@ func TestAccAWSSSMAssociation_basic(t *testing.T) {
testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"),
),
},
{
PreConfig: deleteSsmAssociaton,
Config: testAccAWSSSMAssociationBasicConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"),
),
},
},
})
}
Expand Down Expand Up @@ -412,9 +450,37 @@ resource "aws_ssm_association" "foo" {

func testAccAWSSSMAssociationBasicConfig(rName string) string {
return fmt.Sprintf(`
variable "name" { default = "%s" }

data "aws_availability_zones" "available" {}

data "aws_ami" "amzn" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = ["amzn2-ami-hvm-*-gp2"]
}
}

resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags {
Name = "${var.name}"
}
}

resource "aws_subnet" "first" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.0.0/24"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
}

resource "aws_security_group" "tf_test_foo" {
name = "tf_test_foo-%s"
name = "${var.name}"
description = "foo"
vpc_id = "${aws_vpc.main.id}"
ingress {
protocol = "icmp"
from_port = -1
Expand All @@ -424,14 +490,18 @@ resource "aws_security_group" "tf_test_foo" {
}

resource "aws_instance" "foo" {
ami = "ami-4fccb37f"
availability_zone = "us-west-2a"
instance_type = "m1.small"
security_groups = ["${aws_security_group.tf_test_foo.name}"]
ami = "${data.aws_ami.amzn.image_id}"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
instance_type = "t2.micro"
vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"]
subnet_id = "${aws_subnet.first.id}"
tags {
Name = "${var.name}"
}
}

resource "aws_ssm_document" "foo_document" {
name = "test_document_association-%s",
name = "${var.name}",
document_type = "Command"
content = <<DOC
{
Expand All @@ -455,25 +525,14 @@ DOC
}

resource "aws_ssm_association" "foo" {
name = "test_document_association-%s",
name = "${var.name}",
instance_id = "${aws_instance.foo.id}"
}
`, rName, rName, rName)
`, rName)
}

func testAccAWSSSMAssociationBasicConfigWithDocumentVersion(rName string) string {
return fmt.Sprintf(`
resource "aws_security_group" "tf_test_foo" {
name = "tf_test_foo-%s"
description = "foo"
ingress {
protocol = "icmp"
from_port = -1
to_port = -1
cidr_blocks = ["0.0.0.0/0"]
}
}

resource "aws_ssm_document" "foo_document" {
name = "test_document_association-%s",
document_type = "Command"
Expand Down Expand Up @@ -506,7 +565,7 @@ resource "aws_ssm_association" "foo" {
values = ["acceptanceTest"]
}
}
`, rName, rName, rName)
`, rName, rName)
}

func testAccAWSSSMAssociationBasicConfigWithScheduleExpression(rName string) string {
Expand Down