github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_ssm_activation_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/service/ssm"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSSSMActivation_basic(t *testing.T) {
    15  	name := acctest.RandString(10)
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSSSMActivationDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSSSMActivationBasicConfig(name),
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAWSSSMActivationExists("aws_ssm_activation.foo"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckAWSSSMActivationExists(n string) resource.TestCheckFunc {
    32  	return func(s *terraform.State) error {
    33  		rs, ok := s.RootModule().Resources[n]
    34  		if !ok {
    35  			return fmt.Errorf("Not found: %s", n)
    36  		}
    37  
    38  		if rs.Primary.ID == "" {
    39  			return fmt.Errorf("No SSM Activation ID is set")
    40  		}
    41  
    42  		conn := testAccProvider.Meta().(*AWSClient).ssmconn
    43  
    44  		_, err := conn.DescribeActivations(&ssm.DescribeActivationsInput{
    45  			Filters: []*ssm.DescribeActivationsFilter{
    46  				{
    47  					FilterKey: aws.String("ActivationIds"),
    48  					FilterValues: []*string{
    49  						aws.String(rs.Primary.ID),
    50  					},
    51  				},
    52  			},
    53  			MaxResults: aws.Int64(1),
    54  		})
    55  
    56  		if err != nil {
    57  			return fmt.Errorf("Could not descripbe the activation - %s", err)
    58  		}
    59  
    60  		return nil
    61  	}
    62  }
    63  
    64  func testAccCheckAWSSSMActivationDestroy(s *terraform.State) error {
    65  	conn := testAccProvider.Meta().(*AWSClient).ssmconn
    66  
    67  	for _, rs := range s.RootModule().Resources {
    68  		if rs.Type != "aws_ssm_activation" {
    69  			continue
    70  		}
    71  
    72  		out, err := conn.DescribeActivations(&ssm.DescribeActivationsInput{
    73  			Filters: []*ssm.DescribeActivationsFilter{
    74  				{
    75  					FilterKey: aws.String("ActivationIds"),
    76  					FilterValues: []*string{
    77  						aws.String(rs.Primary.ID),
    78  					},
    79  				},
    80  			},
    81  			MaxResults: aws.Int64(1),
    82  		})
    83  
    84  		if err != nil {
    85  			return err
    86  		}
    87  
    88  		if len(out.ActivationList) > 0 {
    89  			return fmt.Errorf("Expected AWS SSM Activation to be gone, but was still found")
    90  		}
    91  
    92  		return nil
    93  	}
    94  
    95  	return fmt.Errorf("Default error in SSM Activation Test")
    96  }
    97  
    98  func testAccAWSSSMActivationBasicConfig(rName string) string {
    99  	return fmt.Sprintf(`
   100  resource "aws_iam_role" "test_role" {
   101    name = "test_role-%s"
   102    assume_role_policy = <<EOF
   103  {
   104    "Version": "2012-10-17",
   105    "Statement": [
   106      {
   107        "Effect": "Allow",
   108        "Principal": {
   109          "Service": "ssm.amazonaws.com"
   110        },
   111        "Action": "sts:AssumeRole"
   112      }
   113    ]
   114  }
   115  EOF
   116  }
   117  
   118  resource "aws_iam_role_policy_attachment" "test_attach" {
   119    role = "${aws_iam_role.test_role.name}"
   120    policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM"
   121  }
   122  
   123  resource "aws_ssm_activation" "foo" {
   124    name               = "test_ssm_activation-%s",
   125    description        = "Test"
   126    iam_role           = "${aws_iam_role.test_role.name}"
   127    registration_limit = "5"
   128    depends_on         = ["aws_iam_role_policy_attachment.test_attach"]
   129  }
   130  `, rName, rName)
   131  }