2015-11-14 18:54:48 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-05-17 17:40:42 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-11-14 18:54:48 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAWSKmsAlias_basic(t *testing.T) {
|
2017-02-09 15:49:34 +01:00
|
|
|
rInt := acctest.RandInt()
|
|
|
|
kmsAliasTimestamp := time.Now().Format(time.RFC1123)
|
2015-11-14 18:54:48 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSKmsAliasDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-02-09 15:49:34 +01:00
|
|
|
{
|
|
|
|
Config: testAccAWSKmsSingleAlias(rInt, kmsAliasTimestamp),
|
2015-11-14 18:54:48 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSKmsAliasExists("aws_kms_alias.single"),
|
|
|
|
),
|
|
|
|
},
|
2017-02-09 15:49:34 +01:00
|
|
|
{
|
|
|
|
Config: testAccAWSKmsSingleAlias_modified(rInt, kmsAliasTimestamp),
|
2015-11-14 18:54:48 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSKmsAliasExists("aws_kms_alias.single"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-11 21:54:23 +01:00
|
|
|
func TestAccAWSKmsAlias_name_prefix(t *testing.T) {
|
2017-02-09 15:49:34 +01:00
|
|
|
rInt := acctest.RandInt()
|
|
|
|
kmsAliasTimestamp := time.Now().Format(time.RFC1123)
|
2016-03-11 21:54:23 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSKmsAliasDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-02-09 15:49:34 +01:00
|
|
|
{
|
|
|
|
Config: testAccAWSKmsSingleAlias(rInt, kmsAliasTimestamp),
|
2016-03-11 21:54:23 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSKmsAliasExists("aws_kms_alias.name_prefix"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSKmsAlias_no_name(t *testing.T) {
|
2017-02-09 15:49:34 +01:00
|
|
|
rInt := acctest.RandInt()
|
|
|
|
kmsAliasTimestamp := time.Now().Format(time.RFC1123)
|
2016-03-11 21:54:23 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSKmsAliasDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-02-09 15:49:34 +01:00
|
|
|
{
|
|
|
|
Config: testAccAWSKmsSingleAlias(rInt, kmsAliasTimestamp),
|
2016-03-11 21:54:23 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSKmsAliasExists("aws_kms_alias.nothing"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:54:48 +01:00
|
|
|
func TestAccAWSKmsAlias_multiple(t *testing.T) {
|
2017-02-09 15:49:34 +01:00
|
|
|
rInt := acctest.RandInt()
|
|
|
|
kmsAliasTimestamp := time.Now().Format(time.RFC1123)
|
2015-11-14 18:54:48 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSKmsAliasDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-02-09 15:49:34 +01:00
|
|
|
{
|
|
|
|
Config: testAccAWSKmsMultipleAliases(rInt, kmsAliasTimestamp),
|
2015-11-14 18:54:48 +01:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSKmsAliasExists("aws_kms_alias.one"),
|
|
|
|
testAccCheckAWSKmsAliasExists("aws_kms_alias.two"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSKmsAliasDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).kmsconn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_kms_alias" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-11-14 21:48:00 +01:00
|
|
|
entry, err := findKmsAliasByName(conn, rs.Primary.ID, nil)
|
2015-11-14 18:54:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-14 21:48:00 +01:00
|
|
|
if entry != nil {
|
|
|
|
return fmt.Errorf("KMS alias still exists:\n%#v", entry)
|
2015-11-14 18:54:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSKmsAliasExists(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
_, ok := s.RootModule().Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 15:49:34 +01:00
|
|
|
func testAccAWSKmsSingleAlias(rInt int, timestamp string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-11-14 18:54:48 +01:00
|
|
|
resource "aws_kms_key" "one" {
|
|
|
|
description = "Terraform acc test One %s"
|
|
|
|
deletion_window_in_days = 7
|
|
|
|
}
|
|
|
|
resource "aws_kms_key" "two" {
|
|
|
|
description = "Terraform acc test Two %s"
|
|
|
|
deletion_window_in_days = 7
|
|
|
|
}
|
|
|
|
|
2016-03-11 21:54:23 +01:00
|
|
|
resource "aws_kms_alias" "name_prefix" {
|
2017-02-09 15:49:34 +01:00
|
|
|
name_prefix = "alias/tf-acc-key-alias-%d"
|
2016-03-11 21:54:23 +01:00
|
|
|
target_key_id = "${aws_kms_key.one.key_id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_kms_alias" "nothing" {
|
|
|
|
target_key_id = "${aws_kms_key.one.key_id}"
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:54:48 +01:00
|
|
|
resource "aws_kms_alias" "single" {
|
2017-02-09 15:49:34 +01:00
|
|
|
name = "alias/tf-acc-key-alias-%d"
|
2015-11-14 18:54:48 +01:00
|
|
|
target_key_id = "${aws_kms_key.one.key_id}"
|
2017-02-09 15:49:34 +01:00
|
|
|
}`, timestamp, timestamp, rInt, rInt)
|
|
|
|
}
|
2015-11-14 18:54:48 +01:00
|
|
|
|
2017-02-09 15:49:34 +01:00
|
|
|
func testAccAWSKmsSingleAlias_modified(rInt int, timestamp string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-11-14 18:54:48 +01:00
|
|
|
resource "aws_kms_key" "one" {
|
|
|
|
description = "Terraform acc test One %s"
|
|
|
|
deletion_window_in_days = 7
|
|
|
|
}
|
|
|
|
resource "aws_kms_key" "two" {
|
|
|
|
description = "Terraform acc test Two %s"
|
|
|
|
deletion_window_in_days = 7
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_kms_alias" "single" {
|
2017-02-09 15:49:34 +01:00
|
|
|
name = "alias/tf-acc-key-alias-%d"
|
2015-11-14 18:54:48 +01:00
|
|
|
target_key_id = "${aws_kms_key.two.key_id}"
|
2017-02-09 15:49:34 +01:00
|
|
|
}`, timestamp, timestamp, rInt)
|
|
|
|
}
|
2015-11-14 18:54:48 +01:00
|
|
|
|
2017-02-09 15:49:34 +01:00
|
|
|
func testAccAWSKmsMultipleAliases(rInt int, timestamp string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-11-14 18:54:48 +01:00
|
|
|
resource "aws_kms_key" "single" {
|
|
|
|
description = "Terraform acc test One %s"
|
|
|
|
deletion_window_in_days = 7
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_kms_alias" "one" {
|
2017-02-09 15:49:34 +01:00
|
|
|
name = "alias/tf-acc-alias-one-%d"
|
2015-11-14 18:54:48 +01:00
|
|
|
target_key_id = "${aws_kms_key.single.key_id}"
|
|
|
|
}
|
|
|
|
resource "aws_kms_alias" "two" {
|
2017-02-09 15:49:34 +01:00
|
|
|
name = "alias/tf-acc-alias-two-%d"
|
2015-11-14 18:54:48 +01:00
|
|
|
target_key_id = "${aws_kms_key.single.key_id}"
|
2017-02-09 15:49:34 +01:00
|
|
|
}`, timestamp, rInt, rInt)
|
|
|
|
}
|