Skip to content

Commit

Permalink
provider/aws: Add support for importing Kinesis Streams (hashicorp#14278
Browse files Browse the repository at this point in the history
)

Fixes: hashicorp#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
```
  • Loading branch information
stack72 authored May 8, 2017
1 parent d322877 commit 1e7a791
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 49 deletions.
20 changes: 15 additions & 5 deletions resource_aws_kinesis_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
109 changes: 65 additions & 44 deletions resource_aws_kinesis_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@ 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"
)

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),
Expand All @@ -35,20 +34,42 @@ func TestAccAWSKinesisStream_basic(t *testing.T) {
})
}

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{
{
Config: testAccKinesisStreamConfig(rInt),
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateId: streamName,
},
},
})
}

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)
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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -232,49 +247,54 @@ 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
retention_period = 100
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
retention_period = 28
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
Expand All @@ -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
Expand All @@ -303,5 +324,5 @@ resource "aws_kinesis_stream" "test_stream" {
shard_level_metrics = [
"IncomingBytes"
]
}`, rInt)
}
`

0 comments on commit 1e7a791

Please sign in to comment.