From 221a88610b3dd394dc952b7074f8fd61e808f113 Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Mon, 8 May 2017 14:11:23 +0300 Subject: [PATCH] provider/aws: Add support for importing Kinesis Streams (#14278) Fixes: #14260 ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSKinesisStream_import' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/05/08 10:32:47 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSKinesisStream_import -timeout 120m === RUN TestAccAWSKinesisStream_importBasic --- PASS: TestAccAWSKinesisStream_importBasic (101.93s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 101.978s ``` --- .../aws/resource_aws_kinesis_stream.go | 20 ++- .../aws/resource_aws_kinesis_stream_test.go | 115 +++++++++++------- .../aws/r/kinesis_stream.html.markdown | 9 ++ 3 files changed, 92 insertions(+), 52 deletions(-) diff --git a/builtin/providers/aws/resource_aws_kinesis_stream.go b/builtin/providers/aws/resource_aws_kinesis_stream.go index 2a59d3449..c620ee3a1 100644 --- a/builtin/providers/aws/resource_aws_kinesis_stream.go +++ b/builtin/providers/aws/resource_aws_kinesis_stream.go @@ -18,21 +18,24 @@ func resourceAwsKinesisStream() *schema.Resource { Read: resourceAwsKinesisStreamRead, Update: resourceAwsKinesisStreamUpdate, Delete: resourceAwsKinesisStreamDelete, + Importer: &schema.ResourceImporter{ + State: resourceAwsKinesisStreamImport, + }, Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "shard_count": &schema.Schema{ + "shard_count": { Type: schema.TypeInt, Required: true, ForceNew: true, }, - "retention_period": &schema.Schema{ + "retention_period": { Type: schema.TypeInt, Optional: true, Default: 24, @@ -46,14 +49,14 @@ func resourceAwsKinesisStream() *schema.Resource { }, }, - "shard_level_metrics": &schema.Schema{ + "shard_level_metrics": { Type: schema.TypeSet, Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, Set: schema.HashString, }, - "arn": &schema.Schema{ + "arn": { Type: schema.TypeString, Optional: true, Computed: true, @@ -63,6 +66,12 @@ func resourceAwsKinesisStream() *schema.Resource { } } +func resourceAwsKinesisStreamImport( + d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + d.Set("name", d.Id()) + return []*schema.ResourceData{d}, nil +} + func resourceAwsKinesisStreamCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).kinesisconn sn := d.Get("name").(string) @@ -140,6 +149,7 @@ func resourceAwsKinesisStreamRead(d *schema.ResourceData, meta interface{}) erro return err } + d.SetId(state.arn) d.Set("arn", state.arn) d.Set("shard_count", len(state.openShards)) d.Set("retention_period", state.retentionPeriod) diff --git a/builtin/providers/aws/resource_aws_kinesis_stream_test.go b/builtin/providers/aws/resource_aws_kinesis_stream_test.go index 8debb2336..e8afbdc6c 100644 --- a/builtin/providers/aws/resource_aws_kinesis_stream_test.go +++ b/builtin/providers/aws/resource_aws_kinesis_stream_test.go @@ -2,14 +2,13 @@ package aws import ( "fmt" - "math/rand" "strconv" "strings" "testing" - "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/kinesis" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) @@ -17,15 +16,15 @@ import ( func TestAccAWSKinesisStream_basic(t *testing.T) { var stream kinesis.StreamDescription - config := fmt.Sprintf(testAccKinesisStreamConfig, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) + rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckKinesisStreamDestroy, Steps: []resource.TestStep{ - resource.TestStep{ - Config: config, + { + Config: testAccKinesisStreamConfig(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckAWSKinesisStreamAttributes(&stream), @@ -35,20 +34,42 @@ func TestAccAWSKinesisStream_basic(t *testing.T) { }) } -func TestAccAWSKinesisStream_shardCount(t *testing.T) { - var stream kinesis.StreamDescription - - ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() - config := fmt.Sprintf(testAccKinesisStreamConfig, ri) - updateConfig := fmt.Sprintf(testAccKinesisStreamConfigUpdateShardCount, ri) +func TestAccAWSKinesisStream_importBasic(t *testing.T) { + rInt := acctest.RandInt() + resourceName := "aws_kinesis_stream.test_stream" + streamName := fmt.Sprintf("terraform-kinesis-test-%d", rInt) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckKinesisStreamDestroy, Steps: []resource.TestStep{ - resource.TestStep{ - Config: config, + { + Config: testAccKinesisStreamConfig(rInt), + }, + + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateId: streamName, + }, + }, + }) +} + +func TestAccAWSKinesisStream_shardCount(t *testing.T) { + var stream kinesis.StreamDescription + + rInt := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckKinesisStreamDestroy, + Steps: []resource.TestStep{ + { + Config: testAccKinesisStreamConfig(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckAWSKinesisStreamAttributes(&stream), @@ -57,8 +78,8 @@ func TestAccAWSKinesisStream_shardCount(t *testing.T) { ), }, - resource.TestStep{ - Config: updateConfig, + { + Config: testAccKinesisStreamConfigUpdateShardCount(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckAWSKinesisStreamAttributes(&stream), @@ -73,18 +94,15 @@ func TestAccAWSKinesisStream_shardCount(t *testing.T) { func TestAccAWSKinesisStream_retentionPeriod(t *testing.T) { var stream kinesis.StreamDescription - ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() - config := fmt.Sprintf(testAccKinesisStreamConfig, ri) - updateConfig := fmt.Sprintf(testAccKinesisStreamConfigUpdateRetentionPeriod, ri) - decreaseConfig := fmt.Sprintf(testAccKinesisStreamConfigDecreaseRetentionPeriod, ri) + rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckKinesisStreamDestroy, Steps: []resource.TestStep{ - resource.TestStep{ - Config: config, + { + Config: testAccKinesisStreamConfig(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckAWSKinesisStreamAttributes(&stream), @@ -93,8 +111,8 @@ func TestAccAWSKinesisStream_retentionPeriod(t *testing.T) { ), }, - resource.TestStep{ - Config: updateConfig, + { + Config: testAccKinesisStreamConfigUpdateRetentionPeriod(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckAWSKinesisStreamAttributes(&stream), @@ -103,8 +121,8 @@ func TestAccAWSKinesisStream_retentionPeriod(t *testing.T) { ), }, - resource.TestStep{ - Config: decreaseConfig, + { + Config: testAccKinesisStreamConfigDecreaseRetentionPeriod(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckAWSKinesisStreamAttributes(&stream), @@ -119,18 +137,15 @@ func TestAccAWSKinesisStream_retentionPeriod(t *testing.T) { func TestAccAWSKinesisStream_shardLevelMetrics(t *testing.T) { var stream kinesis.StreamDescription - ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() - config := fmt.Sprintf(testAccKinesisStreamConfig, ri) - allConfig := fmt.Sprintf(testAccKinesisStreamConfigAllShardLevelMetrics, ri) - singleConfig := fmt.Sprintf(testAccKinesisStreamConfigSingleShardLevelMetric, ri) + rInt := acctest.RandInt() resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckKinesisStreamDestroy, Steps: []resource.TestStep{ - resource.TestStep{ - Config: config, + { + Config: testAccKinesisStreamConfig(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckAWSKinesisStreamAttributes(&stream), @@ -139,8 +154,8 @@ func TestAccAWSKinesisStream_shardLevelMetrics(t *testing.T) { ), }, - resource.TestStep{ - Config: allConfig, + { + Config: testAccKinesisStreamConfigAllShardLevelMetrics(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckAWSKinesisStreamAttributes(&stream), @@ -149,8 +164,8 @@ func TestAccAWSKinesisStream_shardLevelMetrics(t *testing.T) { ), }, - resource.TestStep{ - Config: singleConfig, + { + Config: testAccKinesisStreamConfigSingleShardLevelMetric(rInt), Check: resource.ComposeTestCheckFunc( testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckAWSKinesisStreamAttributes(&stream), @@ -232,27 +247,30 @@ func testAccCheckKinesisStreamDestroy(s *terraform.State) error { return nil } -var testAccKinesisStreamConfig = ` +func testAccKinesisStreamConfig(rInt int) string { + return fmt.Sprintf(` resource "aws_kinesis_stream" "test_stream" { name = "terraform-kinesis-test-%d" shard_count = 2 tags { Name = "tf-test" } +}`, rInt) } -` -var testAccKinesisStreamConfigUpdateShardCount = ` +func testAccKinesisStreamConfigUpdateShardCount(rInt int) string { + return fmt.Sprintf(` resource "aws_kinesis_stream" "test_stream" { name = "terraform-kinesis-test-%d" shard_count = 4 tags { Name = "tf-test" } +}`, rInt) } -` -var testAccKinesisStreamConfigUpdateRetentionPeriod = ` +func testAccKinesisStreamConfigUpdateRetentionPeriod(rInt int) string { + return fmt.Sprintf(` resource "aws_kinesis_stream" "test_stream" { name = "terraform-kinesis-test-%d" shard_count = 2 @@ -260,10 +278,11 @@ resource "aws_kinesis_stream" "test_stream" { tags { Name = "tf-test" } +}`, rInt) } -` -var testAccKinesisStreamConfigDecreaseRetentionPeriod = ` +func testAccKinesisStreamConfigDecreaseRetentionPeriod(rInt int) string { + return fmt.Sprintf(` resource "aws_kinesis_stream" "test_stream" { name = "terraform-kinesis-test-%d" shard_count = 2 @@ -271,10 +290,11 @@ resource "aws_kinesis_stream" "test_stream" { tags { Name = "tf-test" } +}`, rInt) } -` -var testAccKinesisStreamConfigAllShardLevelMetrics = ` +func testAccKinesisStreamConfigAllShardLevelMetrics(rInt int) string { + return fmt.Sprintf(` resource "aws_kinesis_stream" "test_stream" { name = "terraform-kinesis-test-%d" shard_count = 2 @@ -290,10 +310,11 @@ resource "aws_kinesis_stream" "test_stream" { "ReadProvisionedThroughputExceeded", "IteratorAgeMilliseconds" ] +}`, rInt) } -` -var testAccKinesisStreamConfigSingleShardLevelMetric = ` +func testAccKinesisStreamConfigSingleShardLevelMetric(rInt int) string { + return fmt.Sprintf(` resource "aws_kinesis_stream" "test_stream" { name = "terraform-kinesis-test-%d" shard_count = 2 @@ -303,5 +324,5 @@ resource "aws_kinesis_stream" "test_stream" { shard_level_metrics = [ "IncomingBytes" ] +}`, rInt) } -` diff --git a/website/source/docs/providers/aws/r/kinesis_stream.html.markdown b/website/source/docs/providers/aws/r/kinesis_stream.html.markdown index 90f0d81a0..51f92a7b1 100644 --- a/website/source/docs/providers/aws/r/kinesis_stream.html.markdown +++ b/website/source/docs/providers/aws/r/kinesis_stream.html.markdown @@ -53,6 +53,15 @@ when creating a Kinesis stream. See [Amazon Kinesis Streams][2] for more. * `arn` - The Amazon Resource Name (ARN) specifying the Stream +## Import + +Kinesis Streams can be imported using the `name`, e.g. + +``` +$ terraform import aws_kinesis_stream.test_stream terraform-kinesis-test +``` + [1]: https://aws.amazon.com/documentation/kinesis/ [2]: https://docs.aws.amazon.com/kinesis/latest/dev/amazon-kinesis-streams.html [3]: https://docs.aws.amazon.com/streams/latest/dev/monitoring-with-cloudwatch.html +