package aws import ( "fmt" "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "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) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSSSMAssociationDestroy, Steps: []resource.TestStep{ { Config: testAccAWSSSMAssociationBasicConfig(name), Check: resource.ComposeTestCheckFunc( testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), ), }, }, }) } func TestAccAWSSSMAssociation_withTargets(t *testing.T) { name := acctest.RandString(10) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSSSMAssociationDestroy, Steps: []resource.TestStep{ { Config: testAccAWSSSMAssociationBasicConfigWithTargets(name), Check: resource.ComposeTestCheckFunc( testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), ), }, }, }) } func testAccCheckAWSSSMAssociationExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } if rs.Primary.ID == "" { return fmt.Errorf("No SSM Assosciation ID is set") } conn := testAccProvider.Meta().(*AWSClient).ssmconn _, err := conn.DescribeAssociation(&ssm.DescribeAssociationInput{ AssociationId: aws.String(rs.Primary.Attributes["association_id"]), }) if err != nil { if wserr, ok := err.(awserr.Error); ok && wserr.Code() == "AssociationDoesNotExist" { return nil } return err } return nil } } func testAccCheckAWSSSMAssociationDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).ssmconn for _, rs := range s.RootModule().Resources { if rs.Type != "aws_ssm_association" { continue } out, err := conn.DescribeAssociation(&ssm.DescribeAssociationInput{ AssociationId: aws.String(rs.Primary.Attributes["association_id"]), }) if err != nil { if wserr, ok := err.(awserr.Error); ok && wserr.Code() == "AssociationDoesNotExist" { return nil } return err } if out != nil { return fmt.Errorf("Expected AWS SSM Association to be gone, but was still found") } } return fmt.Errorf("Default error in SSM Association Test") } func testAccAWSSSMAssociationBasicConfigWithTargets(rName string) string { return fmt.Sprintf(` resource "aws_ssm_document" "foo_document" { name = "test_document_association-%s", document_type = "Command" content = <