github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_ssm_association_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ssm"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSSSMAssociation_basic(t *testing.T) {
    16  	name := acctest.RandString(10)
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSSSMAssociationDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSSSMAssociationBasicConfig(name),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func testAccCheckAWSSSMAssociationExists(n string) resource.TestCheckFunc {
    33  	return func(s *terraform.State) error {
    34  		rs, ok := s.RootModule().Resources[n]
    35  		if !ok {
    36  			return fmt.Errorf("Not found: %s", n)
    37  		}
    38  
    39  		if rs.Primary.ID == "" {
    40  			return fmt.Errorf("No SSM Assosciation ID is set")
    41  		}
    42  
    43  		conn := testAccProvider.Meta().(*AWSClient).ssmconn
    44  
    45  		_, err := conn.DescribeAssociation(&ssm.DescribeAssociationInput{
    46  			Name:       aws.String(rs.Primary.Attributes["name"]),
    47  			InstanceId: aws.String(rs.Primary.Attributes["instance_id"]),
    48  		})
    49  
    50  		if err != nil {
    51  			return fmt.Errorf("Could not descripbe the assosciation - %s", err)
    52  		}
    53  
    54  		return nil
    55  	}
    56  }
    57  
    58  func testAccCheckAWSSSMAssociationDestroy(s *terraform.State) error {
    59  	conn := testAccProvider.Meta().(*AWSClient).ssmconn
    60  
    61  	for _, rs := range s.RootModule().Resources {
    62  		if rs.Type != "aws_ssm_association" {
    63  			continue
    64  		}
    65  
    66  		out, err := conn.DescribeAssociation(&ssm.DescribeAssociationInput{
    67  			Name:       aws.String(rs.Primary.Attributes["name"]),
    68  			InstanceId: aws.String(rs.Primary.Attributes["instance_id"]),
    69  		})
    70  
    71  		if err != nil {
    72  			// InvalidDocument means it's gone, this is good
    73  			if wserr, ok := err.(awserr.Error); ok && wserr.Code() == "InvalidDocument" {
    74  				return nil
    75  			}
    76  			return err
    77  		}
    78  
    79  		if out != nil {
    80  			return fmt.Errorf("Expected AWS SSM Assosciation to be gone, but was still found")
    81  		}
    82  	}
    83  
    84  	return fmt.Errorf("Default error in SSM Assosciation Test")
    85  }
    86  
    87  func testAccAWSSSMAssociationBasicConfig(rName string) string {
    88  	return fmt.Sprintf(`
    89  resource "aws_security_group" "tf_test_foo" {
    90    name = "tf_test_foo-%s"
    91    description = "foo"
    92    ingress {
    93      protocol = "icmp"
    94      from_port = -1
    95      to_port = -1
    96      cidr_blocks = ["0.0.0.0/0"]
    97    }
    98  }
    99  
   100  resource "aws_instance" "foo" {
   101    ami = "ami-4fccb37f"
   102    availability_zone = "us-west-2a"
   103    instance_type = "m1.small"
   104    security_groups = ["${aws_security_group.tf_test_foo.name}"]
   105  }
   106  
   107  resource "aws_ssm_document" "foo_document" {
   108    name    = "test_document_association-%s",
   109    content = <<DOC
   110    {
   111      "schemaVersion": "1.2",
   112      "description": "Check ip configuration of a Linux instance.",
   113      "parameters": {
   114  
   115      },
   116      "runtimeConfig": {
   117        "aws:runShellScript": {
   118          "properties": [
   119            {
   120              "id": "0.aws:runShellScript",
   121              "runCommand": ["ifconfig"]
   122            }
   123          ]
   124        }
   125      }
   126    }
   127  DOC
   128  }
   129  
   130  resource "aws_ssm_association" "foo" {
   131    name        = "test_document_association-%s",
   132    instance_id = "${aws_instance.foo.id}"
   133  }
   134  `, rName, rName, rName)
   135  }