github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_config_delivery_channel_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/configservice"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func testAccConfigDeliveryChannel_basic(t *testing.T) {
    16  	var dc configservice.DeliveryChannel
    17  	rInt := acctest.RandInt()
    18  	expectedName := fmt.Sprintf("tf-acc-test-awsconfig-%d", rInt)
    19  	expectedBucketName := fmt.Sprintf("tf-acc-test-awsconfig-%d", rInt)
    20  
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:     func() { testAccPreCheck(t) },
    23  		Providers:    testAccProviders,
    24  		CheckDestroy: testAccCheckConfigDeliveryChannelDestroy,
    25  		Steps: []resource.TestStep{
    26  			{
    27  				Config: testAccConfigDeliveryChannelConfig_basic(rInt),
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckConfigDeliveryChannelExists("aws_config_delivery_channel.foo", &dc),
    30  					testAccCheckConfigDeliveryChannelName("aws_config_delivery_channel.foo", expectedName, &dc),
    31  					resource.TestCheckResourceAttr("aws_config_delivery_channel.foo", "name", expectedName),
    32  					resource.TestCheckResourceAttr("aws_config_delivery_channel.foo", "s3_bucket_name", expectedBucketName),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccConfigDeliveryChannel_allParams(t *testing.T) {
    40  	var dc configservice.DeliveryChannel
    41  	rInt := acctest.RandInt()
    42  	expectedName := fmt.Sprintf("tf-acc-test-awsconfig-%d", rInt)
    43  	expectedBucketName := fmt.Sprintf("tf-acc-test-awsconfig-%d", rInt)
    44  	expectedSnsTopicArn := regexp.MustCompile(fmt.Sprintf("arn:aws:sns:[a-z0-9-]+:[0-9]{12}:tf-acc-test-%d", rInt))
    45  
    46  	resource.Test(t, resource.TestCase{
    47  		PreCheck:     func() { testAccPreCheck(t) },
    48  		Providers:    testAccProviders,
    49  		CheckDestroy: testAccCheckConfigDeliveryChannelDestroy,
    50  		Steps: []resource.TestStep{
    51  			{
    52  				Config: testAccConfigDeliveryChannelConfig_allParams(rInt),
    53  				Check: resource.ComposeTestCheckFunc(
    54  					testAccCheckConfigDeliveryChannelExists("aws_config_delivery_channel.foo", &dc),
    55  					testAccCheckConfigDeliveryChannelName("aws_config_delivery_channel.foo", expectedName, &dc),
    56  					resource.TestCheckResourceAttr("aws_config_delivery_channel.foo", "name", expectedName),
    57  					resource.TestCheckResourceAttr("aws_config_delivery_channel.foo", "s3_bucket_name", expectedBucketName),
    58  					resource.TestCheckResourceAttr("aws_config_delivery_channel.foo", "s3_key_prefix", "one/two/three"),
    59  					resource.TestMatchResourceAttr("aws_config_delivery_channel.foo", "sns_topic_arn", expectedSnsTopicArn),
    60  					resource.TestCheckResourceAttr("aws_config_delivery_channel.foo", "snapshot_delivery_properties.0.delivery_frequency", "Six_Hours"),
    61  				),
    62  			},
    63  		},
    64  	})
    65  }
    66  
    67  func testAccConfigDeliveryChannel_importBasic(t *testing.T) {
    68  	resourceName := "aws_config_delivery_channel.foo"
    69  	rInt := acctest.RandInt()
    70  
    71  	resource.Test(t, resource.TestCase{
    72  		PreCheck:     func() { testAccPreCheck(t) },
    73  		Providers:    testAccProviders,
    74  		CheckDestroy: testAccCheckConfigDeliveryChannelDestroy,
    75  		Steps: []resource.TestStep{
    76  			resource.TestStep{
    77  				Config: testAccConfigDeliveryChannelConfig_basic(rInt),
    78  			},
    79  
    80  			resource.TestStep{
    81  				ResourceName:      resourceName,
    82  				ImportState:       true,
    83  				ImportStateVerify: true,
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  func testAccCheckConfigDeliveryChannelName(n, desired string, obj *configservice.DeliveryChannel) resource.TestCheckFunc {
    90  	return func(s *terraform.State) error {
    91  		_, ok := s.RootModule().Resources[n]
    92  		if !ok {
    93  			return fmt.Errorf("Not found: %s", n)
    94  		}
    95  		if *obj.Name != desired {
    96  			return fmt.Errorf("Expected name: %q, given: %q", desired, *obj.Name)
    97  		}
    98  		return nil
    99  	}
   100  }
   101  
   102  func testAccCheckConfigDeliveryChannelExists(n string, obj *configservice.DeliveryChannel) resource.TestCheckFunc {
   103  	return func(s *terraform.State) error {
   104  		rs, ok := s.RootModule().Resources[n]
   105  		if !ok {
   106  			return fmt.Errorf("Not Found: %s", n)
   107  		}
   108  
   109  		if rs.Primary.ID == "" {
   110  			return fmt.Errorf("No delivery channel ID is set")
   111  		}
   112  
   113  		conn := testAccProvider.Meta().(*AWSClient).configconn
   114  		out, err := conn.DescribeDeliveryChannels(&configservice.DescribeDeliveryChannelsInput{
   115  			DeliveryChannelNames: []*string{aws.String(rs.Primary.Attributes["name"])},
   116  		})
   117  		if err != nil {
   118  			return fmt.Errorf("Failed to describe delivery channel: %s", err)
   119  		}
   120  		if len(out.DeliveryChannels) < 1 {
   121  			return fmt.Errorf("No delivery channel found when describing %q", rs.Primary.Attributes["name"])
   122  		}
   123  
   124  		dc := out.DeliveryChannels[0]
   125  		*obj = *dc
   126  
   127  		return nil
   128  	}
   129  }
   130  
   131  func testAccCheckConfigDeliveryChannelDestroy(s *terraform.State) error {
   132  	conn := testAccProvider.Meta().(*AWSClient).configconn
   133  
   134  	for _, rs := range s.RootModule().Resources {
   135  		if rs.Type != "aws_config_delivery_channel" {
   136  			continue
   137  		}
   138  
   139  		resp, err := conn.DescribeDeliveryChannels(&configservice.DescribeDeliveryChannelsInput{
   140  			DeliveryChannelNames: []*string{aws.String(rs.Primary.Attributes["name"])},
   141  		})
   142  
   143  		if err == nil {
   144  			if len(resp.DeliveryChannels) != 0 &&
   145  				*resp.DeliveryChannels[0].Name == rs.Primary.Attributes["name"] {
   146  				return fmt.Errorf("Delivery Channel still exists: %s", rs.Primary.Attributes["name"])
   147  			}
   148  		}
   149  	}
   150  
   151  	return nil
   152  }
   153  
   154  func testAccConfigDeliveryChannelConfig_basic(randInt int) string {
   155  	return fmt.Sprintf(`
   156  resource "aws_config_configuration_recorder" "foo" {
   157    name = "tf-acc-test-%d"
   158    role_arn = "${aws_iam_role.r.arn}"
   159  }
   160  
   161  resource "aws_iam_role" "r" {
   162      name = "tf-acc-test-awsconfig-%d"
   163      assume_role_policy = <<POLICY
   164  {
   165    "Version": "2012-10-17",
   166    "Statement": [
   167      {
   168        "Action": "sts:AssumeRole",
   169        "Principal": {
   170          "Service": "config.amazonaws.com"
   171        },
   172        "Effect": "Allow",
   173        "Sid": ""
   174      }
   175    ]
   176  }
   177  POLICY
   178  }
   179  
   180  resource "aws_iam_role_policy" "p" {
   181      name = "tf-acc-test-awsconfig-%d"
   182      role = "${aws_iam_role.r.id}"
   183      policy = <<EOF
   184  {
   185    "Version": "2012-10-17",
   186    "Statement": [
   187      {
   188        "Action": [
   189          "s3:*"
   190        ],
   191        "Effect": "Allow",
   192        "Resource": [
   193          "${aws_s3_bucket.b.arn}",
   194          "${aws_s3_bucket.b.arn}/*"
   195        ]
   196      }
   197    ]
   198  }
   199  EOF
   200  }
   201  
   202  resource "aws_s3_bucket" "b" {
   203    bucket = "tf-acc-test-awsconfig-%d"
   204    force_destroy = true
   205  }
   206  
   207  resource "aws_config_delivery_channel" "foo" {
   208    name = "tf-acc-test-awsconfig-%d"
   209    s3_bucket_name = "${aws_s3_bucket.b.bucket}"
   210  }`, randInt, randInt, randInt, randInt, randInt)
   211  }
   212  
   213  func testAccConfigDeliveryChannelConfig_allParams(randInt int) string {
   214  	return fmt.Sprintf(`
   215  resource "aws_config_configuration_recorder" "foo" {
   216    name = "tf-acc-test-%d"
   217    role_arn = "${aws_iam_role.r.arn}"
   218  }
   219  
   220  resource "aws_iam_role" "r" {
   221      name = "tf-acc-test-awsconfig-%d"
   222      assume_role_policy = <<POLICY
   223  {
   224    "Version": "2012-10-17",
   225    "Statement": [
   226      {
   227        "Action": "sts:AssumeRole",
   228        "Principal": {
   229          "Service": "config.amazonaws.com"
   230        },
   231        "Effect": "Allow",
   232        "Sid": ""
   233      }
   234    ]
   235  }
   236  POLICY
   237  }
   238  
   239  resource "aws_iam_role_policy" "p" {
   240      name = "tf-acc-test-awsconfig-%d"
   241      role = "${aws_iam_role.r.id}"
   242      policy = <<EOF
   243  {
   244    "Version": "2012-10-17",
   245    "Statement": [
   246      {
   247        "Action": [
   248          "s3:*"
   249        ],
   250        "Effect": "Allow",
   251        "Resource": [
   252          "${aws_s3_bucket.b.arn}",
   253          "${aws_s3_bucket.b.arn}/*"
   254        ]
   255      }
   256    ]
   257  }
   258  EOF
   259  }
   260  
   261  resource "aws_s3_bucket" "b" {
   262    bucket = "tf-acc-test-awsconfig-%d"
   263    force_destroy = true
   264  }
   265  
   266  resource "aws_sns_topic" "t" {
   267    name = "tf-acc-test-%d"
   268  }
   269  
   270  resource "aws_config_delivery_channel" "foo" {
   271    name = "tf-acc-test-awsconfig-%d"
   272    s3_bucket_name = "${aws_s3_bucket.b.bucket}"
   273    s3_key_prefix = "one/two/three"
   274    sns_topic_arn = "${aws_sns_topic.t.arn}"
   275    snapshot_delivery_properties {
   276    	delivery_frequency = "Six_Hours"
   277    }
   278  }`, randInt, randInt, randInt, randInt, randInt, randInt)
   279  }