github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_kinesis_firehose_delivery_stream_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/service/firehose"
    12  	"github.com/hashicorp/terraform/helper/acctest"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  	"github.com/hashicorp/terraform/terraform"
    15  )
    16  
    17  func TestAccAWSKinesisFirehoseDeliveryStream_s3basic(t *testing.T) {
    18  	var stream firehose.DeliveryStreamDescription
    19  	ri := acctest.RandInt()
    20  	config := fmt.Sprintf(testAccKinesisFirehoseDeliveryStreamConfig_s3basic,
    21  		ri, os.Getenv("AWS_ACCOUNT_ID"), ri, ri, ri)
    22  
    23  	resource.Test(t, resource.TestCase{
    24  		PreCheck:     testAccKinesisFirehosePreCheck(t),
    25  		Providers:    testAccProviders,
    26  		CheckDestroy: testAccCheckKinesisFirehoseDeliveryStreamDestroy,
    27  		Steps: []resource.TestStep{
    28  			resource.TestStep{
    29  				Config: config,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckKinesisFirehoseDeliveryStreamExists("aws_kinesis_firehose_delivery_stream.test_stream", &stream),
    32  					testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(&stream, nil, nil, nil),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccAWSKinesisFirehoseDeliveryStream_s3ConfigUpdates(t *testing.T) {
    40  	var stream firehose.DeliveryStreamDescription
    41  
    42  	ri := acctest.RandInt()
    43  	preConfig := fmt.Sprintf(testAccKinesisFirehoseDeliveryStreamConfig_s3basic,
    44  		ri, os.Getenv("AWS_ACCOUNT_ID"), ri, ri, ri)
    45  	postConfig := fmt.Sprintf(testAccKinesisFirehoseDeliveryStreamConfig_s3Updates,
    46  		ri, os.Getenv("AWS_ACCOUNT_ID"), ri, ri, ri)
    47  
    48  	updatedS3DestinationConfig := &firehose.S3DestinationDescription{
    49  		BufferingHints: &firehose.BufferingHints{
    50  			IntervalInSeconds: aws.Int64(400),
    51  			SizeInMBs:         aws.Int64(10),
    52  		},
    53  	}
    54  
    55  	resource.Test(t, resource.TestCase{
    56  		PreCheck:     testAccKinesisFirehosePreCheck(t),
    57  		Providers:    testAccProviders,
    58  		CheckDestroy: testAccCheckKinesisFirehoseDeliveryStreamDestroy,
    59  		Steps: []resource.TestStep{
    60  			resource.TestStep{
    61  				Config: preConfig,
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testAccCheckKinesisFirehoseDeliveryStreamExists("aws_kinesis_firehose_delivery_stream.test_stream", &stream),
    64  					testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(&stream, nil, nil, nil),
    65  				),
    66  			},
    67  
    68  			resource.TestStep{
    69  				Config: postConfig,
    70  				Check: resource.ComposeTestCheckFunc(
    71  					testAccCheckKinesisFirehoseDeliveryStreamExists("aws_kinesis_firehose_delivery_stream.test_stream", &stream),
    72  					testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(&stream, updatedS3DestinationConfig, nil, nil),
    73  				),
    74  			},
    75  		},
    76  	})
    77  }
    78  
    79  func TestAccAWSKinesisFirehoseDeliveryStream_RedshiftConfigUpdates(t *testing.T) {
    80  	var stream firehose.DeliveryStreamDescription
    81  
    82  	ri := acctest.RandInt()
    83  	preConfig := fmt.Sprintf(testAccKinesisFirehoseDeliveryStreamConfig_RedshiftBasic,
    84  		ri, os.Getenv("AWS_ACCOUNT_ID"), ri, ri, ri, ri)
    85  	postConfig := fmt.Sprintf(testAccKinesisFirehoseDeliveryStreamConfig_RedshiftUpdates,
    86  		ri, os.Getenv("AWS_ACCOUNT_ID"), ri, ri, ri, ri)
    87  
    88  	updatedRedshiftConfig := &firehose.RedshiftDestinationDescription{
    89  		CopyCommand: &firehose.CopyCommand{
    90  			CopyOptions: aws.String("GZIP"),
    91  		},
    92  	}
    93  
    94  	resource.Test(t, resource.TestCase{
    95  		PreCheck:     testAccKinesisFirehosePreCheck(t),
    96  		Providers:    testAccProviders,
    97  		CheckDestroy: testAccCheckKinesisFirehoseDeliveryStreamDestroy,
    98  		Steps: []resource.TestStep{
    99  			resource.TestStep{
   100  				Config: preConfig,
   101  				Check: resource.ComposeTestCheckFunc(
   102  					testAccCheckKinesisFirehoseDeliveryStreamExists("aws_kinesis_firehose_delivery_stream.test_stream", &stream),
   103  					testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(&stream, nil, nil, nil),
   104  				),
   105  			},
   106  
   107  			resource.TestStep{
   108  				Config: postConfig,
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testAccCheckKinesisFirehoseDeliveryStreamExists("aws_kinesis_firehose_delivery_stream.test_stream", &stream),
   111  					testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(&stream, nil, updatedRedshiftConfig, nil),
   112  				),
   113  			},
   114  		},
   115  	})
   116  }
   117  
   118  func TestAccAWSKinesisFirehoseDeliveryStream_ElasticsearchConfigUpdates(t *testing.T) {
   119  	var stream firehose.DeliveryStreamDescription
   120  
   121  	ri := acctest.RandInt()
   122  	awsAccountId := os.Getenv("AWS_ACCOUNT_ID")
   123  	preConfig := fmt.Sprintf(testAccKinesisFirehoseDeliveryStreamConfig_ElasticsearchBasic,
   124  		ri, awsAccountId, ri, ri, ri, awsAccountId, awsAccountId, ri, ri)
   125  	postConfig := fmt.Sprintf(testAccKinesisFirehoseDeliveryStreamConfig_ElasticsearchUpdate,
   126  		ri, awsAccountId, ri, ri, ri, awsAccountId, awsAccountId, ri, ri)
   127  
   128  	updatedElasticSearchConfig := &firehose.ElasticsearchDestinationDescription{
   129  		BufferingHints: &firehose.ElasticsearchBufferingHints{
   130  			IntervalInSeconds: aws.Int64(500),
   131  		},
   132  	}
   133  
   134  	resource.Test(t, resource.TestCase{
   135  		PreCheck:     testAccKinesisFirehosePreCheck(t),
   136  		Providers:    testAccProviders,
   137  		CheckDestroy: testAccCheckKinesisFirehoseDeliveryStreamDestroy,
   138  		Steps: []resource.TestStep{
   139  			resource.TestStep{
   140  				Config: preConfig,
   141  				Check: resource.ComposeTestCheckFunc(
   142  					testAccCheckKinesisFirehoseDeliveryStreamExists("aws_kinesis_firehose_delivery_stream.test_stream_es", &stream),
   143  					testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(&stream, nil, nil, nil),
   144  				),
   145  			},
   146  			resource.TestStep{
   147  				Config: postConfig,
   148  				Check: resource.ComposeTestCheckFunc(
   149  					testAccCheckKinesisFirehoseDeliveryStreamExists("aws_kinesis_firehose_delivery_stream.test_stream_es", &stream),
   150  					testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(&stream, nil, nil, updatedElasticSearchConfig),
   151  				),
   152  			},
   153  		},
   154  	})
   155  }
   156  
   157  func testAccCheckKinesisFirehoseDeliveryStreamExists(n string, stream *firehose.DeliveryStreamDescription) resource.TestCheckFunc {
   158  	return func(s *terraform.State) error {
   159  		rs, ok := s.RootModule().Resources[n]
   160  		log.Printf("State: %#v", s.RootModule().Resources)
   161  		if !ok {
   162  			return fmt.Errorf("Not found: %s", n)
   163  		}
   164  
   165  		if rs.Primary.ID == "" {
   166  			return fmt.Errorf("No Kinesis Firehose ID is set")
   167  		}
   168  
   169  		conn := testAccProvider.Meta().(*AWSClient).firehoseconn
   170  		describeOpts := &firehose.DescribeDeliveryStreamInput{
   171  			DeliveryStreamName: aws.String(rs.Primary.Attributes["name"]),
   172  		}
   173  		resp, err := conn.DescribeDeliveryStream(describeOpts)
   174  		if err != nil {
   175  			return err
   176  		}
   177  
   178  		*stream = *resp.DeliveryStreamDescription
   179  
   180  		return nil
   181  	}
   182  }
   183  
   184  func testAccCheckAWSKinesisFirehoseDeliveryStreamAttributes(stream *firehose.DeliveryStreamDescription, s3config interface{}, redshiftConfig interface{}, elasticsearchConfig interface{}) resource.TestCheckFunc {
   185  	return func(s *terraform.State) error {
   186  		if !strings.HasPrefix(*stream.DeliveryStreamName, "terraform-kinesis-firehose") {
   187  			return fmt.Errorf("Bad Stream name: %s", *stream.DeliveryStreamName)
   188  		}
   189  		for _, rs := range s.RootModule().Resources {
   190  			if rs.Type != "aws_kinesis_firehose_delivery_stream" {
   191  				continue
   192  			}
   193  			if *stream.DeliveryStreamARN != rs.Primary.Attributes["arn"] {
   194  				return fmt.Errorf("Bad Delivery Stream ARN\n\t expected: %s\n\tgot: %s\n", rs.Primary.Attributes["arn"], *stream.DeliveryStreamARN)
   195  			}
   196  
   197  			if s3config != nil {
   198  				s := s3config.(*firehose.S3DestinationDescription)
   199  				// Range over the Stream Destinations, looking for the matching S3
   200  				// destination. For simplicity, our test only have a single S3 or
   201  				// Redshift destination, so at this time it's safe to match on the first
   202  				// one
   203  				var match bool
   204  				for _, d := range stream.Destinations {
   205  					if d.S3DestinationDescription != nil {
   206  						if *d.S3DestinationDescription.BufferingHints.SizeInMBs == *s.BufferingHints.SizeInMBs {
   207  							match = true
   208  						}
   209  					}
   210  				}
   211  				if !match {
   212  					return fmt.Errorf("Mismatch s3 buffer size, expected: %s, got: %s", s, stream.Destinations)
   213  				}
   214  			}
   215  
   216  			if redshiftConfig != nil {
   217  				r := redshiftConfig.(*firehose.RedshiftDestinationDescription)
   218  				// Range over the Stream Destinations, looking for the matching Redshift
   219  				// destination
   220  				var match bool
   221  				for _, d := range stream.Destinations {
   222  					if d.RedshiftDestinationDescription != nil {
   223  						if *d.RedshiftDestinationDescription.CopyCommand.CopyOptions == *r.CopyCommand.CopyOptions {
   224  							match = true
   225  						}
   226  					}
   227  				}
   228  				if !match {
   229  					return fmt.Errorf("Mismatch Redshift CopyOptions, expected: %s, got: %s", r, stream.Destinations)
   230  				}
   231  			}
   232  
   233  			if elasticsearchConfig != nil {
   234  				es := elasticsearchConfig.(*firehose.ElasticsearchDestinationDescription)
   235  				// Range over the Stream Destinations, looking for the matching Elasticsearch destination
   236  				var match bool
   237  				for _, d := range stream.Destinations {
   238  					if d.ElasticsearchDestinationDescription != nil {
   239  						match = true
   240  					}
   241  				}
   242  				if !match {
   243  					return fmt.Errorf("Mismatch Elasticsearch Buffering Interval, expected: %s, got: %s", es, stream.Destinations)
   244  				}
   245  			}
   246  		}
   247  		return nil
   248  	}
   249  }
   250  
   251  func testAccCheckKinesisFirehoseDeliveryStreamDestroy(s *terraform.State) error {
   252  	for _, rs := range s.RootModule().Resources {
   253  		if rs.Type != "aws_kinesis_firehose_delivery_stream" {
   254  			continue
   255  		}
   256  		conn := testAccProvider.Meta().(*AWSClient).firehoseconn
   257  		describeOpts := &firehose.DescribeDeliveryStreamInput{
   258  			DeliveryStreamName: aws.String(rs.Primary.Attributes["name"]),
   259  		}
   260  		resp, err := conn.DescribeDeliveryStream(describeOpts)
   261  		if err == nil {
   262  			if resp.DeliveryStreamDescription != nil && *resp.DeliveryStreamDescription.DeliveryStreamStatus != "DELETING" {
   263  				return fmt.Errorf("Error: Delivery Stream still exists")
   264  			}
   265  		}
   266  
   267  		return nil
   268  
   269  	}
   270  
   271  	return nil
   272  }
   273  
   274  func testAccKinesisFirehosePreCheck(t *testing.T) func() {
   275  	return func() {
   276  		testAccPreCheck(t)
   277  		if os.Getenv("AWS_ACCOUNT_ID") == "" {
   278  			t.Fatal("AWS_ACCOUNT_ID must be set")
   279  		}
   280  	}
   281  }
   282  
   283  const testAccKinesisFirehoseDeliveryStreamBaseConfig = `
   284  resource "aws_iam_role" "firehose" {
   285    name = "tf_acctest_firehose_delivery_role_%d"
   286    assume_role_policy = <<EOF
   287  {
   288    "Version": "2012-10-17",
   289    "Statement": [
   290      {
   291        "Sid": "",
   292        "Effect": "Allow",
   293        "Principal": {
   294          "Service": "firehose.amazonaws.com"
   295        },
   296        "Action": "sts:AssumeRole",
   297        "Condition": {
   298          "StringEquals": {
   299            "sts:ExternalId": "%s"
   300          }
   301        }
   302      }
   303    ]
   304  }
   305  EOF
   306  }
   307  
   308  resource "aws_s3_bucket" "bucket" {
   309    bucket = "tf-test-bucket-%d"
   310    acl = "private"
   311  }
   312  
   313  resource "aws_iam_role_policy" "firehose" {
   314    name = "tf_acctest_firehose_delivery_policy_%d"
   315    role = "${aws_iam_role.firehose.id}"
   316    policy = <<EOF
   317  {
   318    "Version": "2012-10-17",
   319    "Statement": [
   320      {
   321        "Sid": "",
   322        "Effect": "Allow",
   323        "Action": [
   324          "s3:AbortMultipartUpload",
   325          "s3:GetBucketLocation",
   326          "s3:GetObject",
   327          "s3:ListBucket",
   328          "s3:ListBucketMultipartUploads",
   329          "s3:PutObject"
   330        ],
   331        "Resource": [
   332          "arn:aws:s3:::${aws_s3_bucket.bucket.id}",
   333          "arn:aws:s3:::${aws_s3_bucket.bucket.id}/*"
   334        ]
   335      }
   336    ]
   337  }
   338  EOF
   339  }
   340  
   341  `
   342  
   343  var testAccKinesisFirehoseDeliveryStreamConfig_s3basic = testAccKinesisFirehoseDeliveryStreamBaseConfig + `
   344  resource "aws_kinesis_firehose_delivery_stream" "test_stream" {
   345    depends_on = ["aws_iam_role_policy.firehose"]
   346    name = "terraform-kinesis-firehose-basictest-%d"
   347    destination = "s3"
   348    s3_configuration {
   349      role_arn = "${aws_iam_role.firehose.arn}"
   350      bucket_arn = "${aws_s3_bucket.bucket.arn}"
   351    }
   352  }`
   353  
   354  var testAccKinesisFirehoseDeliveryStreamConfig_s3Updates = testAccKinesisFirehoseDeliveryStreamBaseConfig + `
   355  resource "aws_kinesis_firehose_delivery_stream" "test_stream" {
   356    depends_on = ["aws_iam_role_policy.firehose"]
   357    name = "terraform-kinesis-firehose-s3test-%d"
   358    destination = "s3"
   359    s3_configuration {
   360      role_arn = "${aws_iam_role.firehose.arn}"
   361      bucket_arn = "${aws_s3_bucket.bucket.arn}"
   362      buffer_size = 10
   363      buffer_interval = 400
   364      compression_format = "GZIP"
   365    }
   366  }`
   367  
   368  var testAccKinesisFirehoseDeliveryStreamBaseRedshiftConfig = testAccKinesisFirehoseDeliveryStreamBaseConfig + `
   369  resource "aws_redshift_cluster" "test_cluster" {
   370    cluster_identifier = "tf-redshift-cluster-%d"
   371    database_name = "test"
   372    master_username = "testuser"
   373    master_password = "T3stPass"
   374    node_type = "dc1.large"
   375    cluster_type = "single-node"
   376  }`
   377  
   378  var testAccKinesisFirehoseDeliveryStreamConfig_RedshiftBasic = testAccKinesisFirehoseDeliveryStreamBaseRedshiftConfig + `
   379  resource "aws_kinesis_firehose_delivery_stream" "test_stream" {
   380    depends_on = ["aws_iam_role_policy.firehose", "aws_redshift_cluster.test_cluster"]
   381    name = "terraform-kinesis-firehose-basicredshifttest-%d"
   382    destination = "redshift"
   383    s3_configuration {
   384      role_arn = "${aws_iam_role.firehose.arn}"
   385      bucket_arn = "${aws_s3_bucket.bucket.arn}"
   386    }
   387    redshift_configuration {
   388      role_arn = "${aws_iam_role.firehose.arn}"
   389      cluster_jdbcurl = "jdbc:redshift://${aws_redshift_cluster.test_cluster.endpoint}/${aws_redshift_cluster.test_cluster.database_name}"
   390      username = "testuser"
   391      password = "T3stPass"
   392      data_table_name = "test-table"
   393    }
   394  }`
   395  
   396  var testAccKinesisFirehoseDeliveryStreamConfig_RedshiftUpdates = testAccKinesisFirehoseDeliveryStreamBaseRedshiftConfig + `
   397  resource "aws_kinesis_firehose_delivery_stream" "test_stream" {
   398    depends_on = ["aws_iam_role_policy.firehose", "aws_redshift_cluster.test_cluster"]
   399    name = "terraform-kinesis-firehose-basicredshifttest-%d"
   400    destination = "redshift"
   401    s3_configuration {
   402      role_arn = "${aws_iam_role.firehose.arn}"
   403      bucket_arn = "${aws_s3_bucket.bucket.arn}"
   404      buffer_size = 10
   405      buffer_interval = 400
   406      compression_format = "GZIP"
   407    }
   408    redshift_configuration {
   409      role_arn = "${aws_iam_role.firehose.arn}"
   410      cluster_jdbcurl = "jdbc:redshift://${aws_redshift_cluster.test_cluster.endpoint}/${aws_redshift_cluster.test_cluster.database_name}"
   411      username = "testuser"
   412      password = "T3stPass"
   413      data_table_name = "test-table"
   414      copy_options = "GZIP"
   415      data_table_columns = "test-col"
   416    }
   417  }`
   418  
   419  var testAccKinesisFirehoseDeliveryStreamBaseElasticsearchConfig = testAccKinesisFirehoseDeliveryStreamBaseConfig + `
   420  resource "aws_elasticsearch_domain" "test_cluster" {
   421    domain_name = "es-test-%d"
   422  
   423    access_policies = <<CONFIG
   424  {
   425    "Version": "2012-10-17",
   426    "Statement": [
   427      {
   428        "Effect": "Allow",
   429        "Principal": {
   430          "AWS": "arn:aws:iam::%s:root"
   431        },
   432        "Action": "es:*",
   433        "Resource": "arn:aws:es:us-east-1:%s:domain/es-test-%d/*"
   434      }
   435    ]
   436  }
   437  CONFIG
   438  }`
   439  
   440  var testAccKinesisFirehoseDeliveryStreamConfig_ElasticsearchBasic = testAccKinesisFirehoseDeliveryStreamBaseElasticsearchConfig + `
   441  resource "aws_kinesis_firehose_delivery_stream" "test_stream_es" {
   442    depends_on = ["aws_iam_role_policy.firehose", "aws_elasticsearch_domain.test_cluster"]
   443    name = "terraform-kinesis-firehose-es-%d"
   444    destination = "elasticsearch"
   445    s3_configuration {
   446      role_arn = "${aws_iam_role.firehose.arn}"
   447      bucket_arn = "${aws_s3_bucket.bucket.arn}"
   448    }
   449    elasticsearch_configuration {
   450      domain_arn = "${aws_elasticsearch_domain.test_cluster.arn}"
   451      role_arn = "${aws_iam_role.firehose.arn}"
   452      index_name = "test"
   453      type_name = "test"
   454    }
   455  }`
   456  
   457  var testAccKinesisFirehoseDeliveryStreamConfig_ElasticsearchUpdate = testAccKinesisFirehoseDeliveryStreamBaseElasticsearchConfig + `
   458  resource "aws_kinesis_firehose_delivery_stream" "test_stream_es" {
   459    depends_on = ["aws_iam_role_policy.firehose", "aws_elasticsearch_domain.test_cluster"]
   460    name = "terraform-kinesis-firehose-es-%d"
   461    destination = "elasticsearch"
   462    s3_configuration {
   463      role_arn = "${aws_iam_role.firehose.arn}"
   464      bucket_arn = "${aws_s3_bucket.bucket.arn}"
   465    }
   466    elasticsearch_configuration {
   467      domain_arn = "${aws_elasticsearch_domain.test_cluster.arn}"
   468      role_arn = "${aws_iam_role.firehose.arn}"
   469      index_name = "test"
   470      type_name = "test"
   471      buffering_interval = 500
   472    }
   473  }`