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
```
This commit is contained in:
Paul Stack 2017-05-08 14:11:23 +03:00 committed by GitHub
parent 3cec809297
commit 221a88610b
3 changed files with 92 additions and 52 deletions

View File

@ -18,21 +18,24 @@ func resourceAwsKinesisStream() *schema.Resource {
Read: resourceAwsKinesisStreamRead, Read: resourceAwsKinesisStreamRead,
Update: resourceAwsKinesisStreamUpdate, Update: resourceAwsKinesisStreamUpdate,
Delete: resourceAwsKinesisStreamDelete, Delete: resourceAwsKinesisStreamDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsKinesisStreamImport,
},
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
}, },
"shard_count": &schema.Schema{ "shard_count": {
Type: schema.TypeInt, Type: schema.TypeInt,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
}, },
"retention_period": &schema.Schema{ "retention_period": {
Type: schema.TypeInt, Type: schema.TypeInt,
Optional: true, Optional: true,
Default: 24, Default: 24,
@ -46,14 +49,14 @@ func resourceAwsKinesisStream() *schema.Resource {
}, },
}, },
"shard_level_metrics": &schema.Schema{ "shard_level_metrics": {
Type: schema.TypeSet, Type: schema.TypeSet,
Optional: true, Optional: true,
Elem: &schema.Schema{Type: schema.TypeString}, Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString, Set: schema.HashString,
}, },
"arn": &schema.Schema{ "arn": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Computed: 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 { func resourceAwsKinesisStreamCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).kinesisconn conn := meta.(*AWSClient).kinesisconn
sn := d.Get("name").(string) sn := d.Get("name").(string)
@ -140,6 +149,7 @@ func resourceAwsKinesisStreamRead(d *schema.ResourceData, meta interface{}) erro
return err return err
} }
d.SetId(state.arn)
d.Set("arn", state.arn) d.Set("arn", state.arn)
d.Set("shard_count", len(state.openShards)) d.Set("shard_count", len(state.openShards))
d.Set("retention_period", state.retentionPeriod) d.Set("retention_period", state.retentionPeriod)

View File

@ -2,14 +2,13 @@ package aws
import ( import (
"fmt" "fmt"
"math/rand"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kinesis" "github.com/aws/aws-sdk-go/service/kinesis"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -17,15 +16,15 @@ import (
func TestAccAWSKinesisStream_basic(t *testing.T) { func TestAccAWSKinesisStream_basic(t *testing.T) {
var stream kinesis.StreamDescription var stream kinesis.StreamDescription
config := fmt.Sprintf(testAccKinesisStreamConfig, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckKinesisStreamDestroy, CheckDestroy: testAccCheckKinesisStreamDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: config, Config: testAccKinesisStreamConfig(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream),
testAccCheckAWSKinesisStreamAttributes(&stream), testAccCheckAWSKinesisStreamAttributes(&stream),
@ -35,20 +34,42 @@ func TestAccAWSKinesisStream_basic(t *testing.T) {
}) })
} }
func TestAccAWSKinesisStream_shardCount(t *testing.T) { func TestAccAWSKinesisStream_importBasic(t *testing.T) {
var stream kinesis.StreamDescription rInt := acctest.RandInt()
resourceName := "aws_kinesis_stream.test_stream"
ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() streamName := fmt.Sprintf("terraform-kinesis-test-%d", rInt)
config := fmt.Sprintf(testAccKinesisStreamConfig, ri)
updateConfig := fmt.Sprintf(testAccKinesisStreamConfigUpdateShardCount, ri)
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckKinesisStreamDestroy, CheckDestroy: testAccCheckKinesisStreamDestroy,
Steps: []resource.TestStep{ 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( Check: resource.ComposeTestCheckFunc(
testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream),
testAccCheckAWSKinesisStreamAttributes(&stream), testAccCheckAWSKinesisStreamAttributes(&stream),
@ -57,8 +78,8 @@ func TestAccAWSKinesisStream_shardCount(t *testing.T) {
), ),
}, },
resource.TestStep{ {
Config: updateConfig, Config: testAccKinesisStreamConfigUpdateShardCount(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream),
testAccCheckAWSKinesisStreamAttributes(&stream), testAccCheckAWSKinesisStreamAttributes(&stream),
@ -73,18 +94,15 @@ func TestAccAWSKinesisStream_shardCount(t *testing.T) {
func TestAccAWSKinesisStream_retentionPeriod(t *testing.T) { func TestAccAWSKinesisStream_retentionPeriod(t *testing.T) {
var stream kinesis.StreamDescription var stream kinesis.StreamDescription
ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() rInt := acctest.RandInt()
config := fmt.Sprintf(testAccKinesisStreamConfig, ri)
updateConfig := fmt.Sprintf(testAccKinesisStreamConfigUpdateRetentionPeriod, ri)
decreaseConfig := fmt.Sprintf(testAccKinesisStreamConfigDecreaseRetentionPeriod, ri)
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckKinesisStreamDestroy, CheckDestroy: testAccCheckKinesisStreamDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: config, Config: testAccKinesisStreamConfig(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream),
testAccCheckAWSKinesisStreamAttributes(&stream), testAccCheckAWSKinesisStreamAttributes(&stream),
@ -93,8 +111,8 @@ func TestAccAWSKinesisStream_retentionPeriod(t *testing.T) {
), ),
}, },
resource.TestStep{ {
Config: updateConfig, Config: testAccKinesisStreamConfigUpdateRetentionPeriod(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream),
testAccCheckAWSKinesisStreamAttributes(&stream), testAccCheckAWSKinesisStreamAttributes(&stream),
@ -103,8 +121,8 @@ func TestAccAWSKinesisStream_retentionPeriod(t *testing.T) {
), ),
}, },
resource.TestStep{ {
Config: decreaseConfig, Config: testAccKinesisStreamConfigDecreaseRetentionPeriod(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream),
testAccCheckAWSKinesisStreamAttributes(&stream), testAccCheckAWSKinesisStreamAttributes(&stream),
@ -119,18 +137,15 @@ func TestAccAWSKinesisStream_retentionPeriod(t *testing.T) {
func TestAccAWSKinesisStream_shardLevelMetrics(t *testing.T) { func TestAccAWSKinesisStream_shardLevelMetrics(t *testing.T) {
var stream kinesis.StreamDescription var stream kinesis.StreamDescription
ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() rInt := acctest.RandInt()
config := fmt.Sprintf(testAccKinesisStreamConfig, ri)
allConfig := fmt.Sprintf(testAccKinesisStreamConfigAllShardLevelMetrics, ri)
singleConfig := fmt.Sprintf(testAccKinesisStreamConfigSingleShardLevelMetric, ri)
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckKinesisStreamDestroy, CheckDestroy: testAccCheckKinesisStreamDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: config, Config: testAccKinesisStreamConfig(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream),
testAccCheckAWSKinesisStreamAttributes(&stream), testAccCheckAWSKinesisStreamAttributes(&stream),
@ -139,8 +154,8 @@ func TestAccAWSKinesisStream_shardLevelMetrics(t *testing.T) {
), ),
}, },
resource.TestStep{ {
Config: allConfig, Config: testAccKinesisStreamConfigAllShardLevelMetrics(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream),
testAccCheckAWSKinesisStreamAttributes(&stream), testAccCheckAWSKinesisStreamAttributes(&stream),
@ -149,8 +164,8 @@ func TestAccAWSKinesisStream_shardLevelMetrics(t *testing.T) {
), ),
}, },
resource.TestStep{ {
Config: singleConfig, Config: testAccKinesisStreamConfigSingleShardLevelMetric(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream), testAccCheckKinesisStreamExists("aws_kinesis_stream.test_stream", &stream),
testAccCheckAWSKinesisStreamAttributes(&stream), testAccCheckAWSKinesisStreamAttributes(&stream),
@ -232,27 +247,30 @@ func testAccCheckKinesisStreamDestroy(s *terraform.State) error {
return nil return nil
} }
var testAccKinesisStreamConfig = ` func testAccKinesisStreamConfig(rInt int) string {
return fmt.Sprintf(`
resource "aws_kinesis_stream" "test_stream" { resource "aws_kinesis_stream" "test_stream" {
name = "terraform-kinesis-test-%d" name = "terraform-kinesis-test-%d"
shard_count = 2 shard_count = 2
tags { tags {
Name = "tf-test" Name = "tf-test"
} }
}`, rInt)
} }
`
var testAccKinesisStreamConfigUpdateShardCount = ` func testAccKinesisStreamConfigUpdateShardCount(rInt int) string {
return fmt.Sprintf(`
resource "aws_kinesis_stream" "test_stream" { resource "aws_kinesis_stream" "test_stream" {
name = "terraform-kinesis-test-%d" name = "terraform-kinesis-test-%d"
shard_count = 4 shard_count = 4
tags { tags {
Name = "tf-test" Name = "tf-test"
} }
}`, rInt)
} }
`
var testAccKinesisStreamConfigUpdateRetentionPeriod = ` func testAccKinesisStreamConfigUpdateRetentionPeriod(rInt int) string {
return fmt.Sprintf(`
resource "aws_kinesis_stream" "test_stream" { resource "aws_kinesis_stream" "test_stream" {
name = "terraform-kinesis-test-%d" name = "terraform-kinesis-test-%d"
shard_count = 2 shard_count = 2
@ -260,10 +278,11 @@ resource "aws_kinesis_stream" "test_stream" {
tags { tags {
Name = "tf-test" Name = "tf-test"
} }
}`, rInt)
} }
`
var testAccKinesisStreamConfigDecreaseRetentionPeriod = ` func testAccKinesisStreamConfigDecreaseRetentionPeriod(rInt int) string {
return fmt.Sprintf(`
resource "aws_kinesis_stream" "test_stream" { resource "aws_kinesis_stream" "test_stream" {
name = "terraform-kinesis-test-%d" name = "terraform-kinesis-test-%d"
shard_count = 2 shard_count = 2
@ -271,10 +290,11 @@ resource "aws_kinesis_stream" "test_stream" {
tags { tags {
Name = "tf-test" Name = "tf-test"
} }
}`, rInt)
} }
`
var testAccKinesisStreamConfigAllShardLevelMetrics = ` func testAccKinesisStreamConfigAllShardLevelMetrics(rInt int) string {
return fmt.Sprintf(`
resource "aws_kinesis_stream" "test_stream" { resource "aws_kinesis_stream" "test_stream" {
name = "terraform-kinesis-test-%d" name = "terraform-kinesis-test-%d"
shard_count = 2 shard_count = 2
@ -290,10 +310,11 @@ resource "aws_kinesis_stream" "test_stream" {
"ReadProvisionedThroughputExceeded", "ReadProvisionedThroughputExceeded",
"IteratorAgeMilliseconds" "IteratorAgeMilliseconds"
] ]
}`, rInt)
} }
`
var testAccKinesisStreamConfigSingleShardLevelMetric = ` func testAccKinesisStreamConfigSingleShardLevelMetric(rInt int) string {
return fmt.Sprintf(`
resource "aws_kinesis_stream" "test_stream" { resource "aws_kinesis_stream" "test_stream" {
name = "terraform-kinesis-test-%d" name = "terraform-kinesis-test-%d"
shard_count = 2 shard_count = 2
@ -303,5 +324,5 @@ resource "aws_kinesis_stream" "test_stream" {
shard_level_metrics = [ shard_level_metrics = [
"IncomingBytes" "IncomingBytes"
] ]
}`, rInt)
} }
`

View File

@ -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 * `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/ [1]: https://aws.amazon.com/documentation/kinesis/
[2]: https://docs.aws.amazon.com/kinesis/latest/dev/amazon-kinesis-streams.html [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 [3]: https://docs.aws.amazon.com/streams/latest/dev/monitoring-with-cloudwatch.html