-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93fa712
commit c62c396
Showing
3 changed files
with
368 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/timestreamwrite" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" | ||
) | ||
|
||
func resourceAwsTimestreamWriteDatabase() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsTimestreamWriteDatabaseCreate, | ||
Read: resourceAwsTimestreamWriteDatabaseRead, | ||
Update: resourceAwsTimestreamWriteDatabaseUpdate, | ||
Delete: resourceAwsTimestreamWriteDatabaseDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"database_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"kms_key_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
}, | ||
|
||
"tags": tagsSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsTimestreamWriteDatabaseCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).timestreamwriteconn | ||
|
||
input := ×treamwrite.CreateDatabaseInput{ | ||
DatabaseName: aws.String(d.Get("database_name").(string)), | ||
} | ||
if v, ok := d.GetOk("kms_key_id"); ok { | ||
input.KmsKeyId = aws.String(v.(string)) | ||
} | ||
|
||
if attr, ok := d.GetOk("tags"); ok { | ||
input.Tags = keyvaluetags.New(attr.(map[string]interface{})).IgnoreAws().TimestreamwriteTags() | ||
} | ||
|
||
_, err := conn.CreateDatabase(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(d.Get("database_name").(string)) | ||
|
||
return resourceAwsTimestreamWriteDatabaseRead(d, meta) | ||
} | ||
|
||
func resourceAwsTimestreamWriteDatabaseRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).timestreamwriteconn | ||
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig | ||
|
||
resp, err := conn.DescribeDatabase(×treamwrite.DescribeDatabaseInput{ | ||
DatabaseName: aws.String(d.Id()), | ||
}) | ||
if err != nil { | ||
if isAWSErr(err, timestreamwrite.ErrCodeResourceNotFoundException, "") { | ||
log.Printf("[WARN] Timestream Database %q not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
d.Set("database_name", resp.Database.DatabaseName) | ||
d.Set("kms_key_id", resp.Database.KmsKeyId) | ||
d.Set("arn", resp.Database.Arn) | ||
|
||
arn := aws.StringValue(resp.Database.Arn) | ||
|
||
tags, err := keyvaluetags.TimestreamwriteListTags(conn, arn) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error listing tags for Timestream Database (%s): %s", arn, err) | ||
} | ||
|
||
if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { | ||
return fmt.Errorf("error setting tags: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsTimestreamWriteDatabaseUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).timestreamwriteconn | ||
|
||
if d.HasChange("kms_key_id") { | ||
input := ×treamwrite.UpdateDatabaseInput{ | ||
DatabaseName: aws.String(d.Id()), | ||
KmsKeyId: aws.String(d.Get("kms_key_id").(string)), | ||
} | ||
|
||
_, err := conn.UpdateDatabase(input) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if d.HasChange("tags") { | ||
o, n := d.GetChange("tags") | ||
|
||
if err := keyvaluetags.TimestreamwriteUpdateTags(conn, d.Get("arn").(string), o, n); err != nil { | ||
return fmt.Errorf("error updating Timesteram Database (%s) tags: %s", d.Get("arn").(string), err) | ||
} | ||
} | ||
|
||
return resourceAwsTimestreamWriteDatabaseRead(d, meta) | ||
} | ||
|
||
func resourceAwsTimestreamWriteDatabaseDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).timestreamwriteconn | ||
|
||
input := ×treamwrite.DeleteDatabaseInput{ | ||
DatabaseName: aws.String(d.Id()), | ||
} | ||
|
||
_, err := conn.DeleteDatabase(input) | ||
if err != nil { | ||
if isAWSErr(err, timestreamwrite.ErrCodeResourceNotFoundException, "") { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/timestreamwrite" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccAWSTimestreamWriteDatabase_basic(t *testing.T) { | ||
resourceName := "aws_timestreamwrite_database.test_database" | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSTimestreamWriteDatabaseDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSTimestreamWriteDatabaseConfigNoTags(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSTimestreamWriteDatabaseExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "database_name", rName), | ||
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSTimestreamWriteDatabase_kmsKey(t *testing.T) { | ||
resourceName := "aws_timestreamwrite_database.test_database" | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
kmsResourceName := "aws_kms_key.foo" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSTimestreamWriteDatabaseDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSTimestreamWriteDatabaseConfigKmsKey(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSTimestreamWriteDatabaseExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "database_name", rName), | ||
resource.TestCheckResourceAttrPair(resourceName, "kms_key_id", kmsResourceName, "arn"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSTimestreamWriteDatabase_Tags(t *testing.T) { | ||
resourceName := "aws_timestreamwrite_database.test_database" | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSTimestreamWriteDatabaseDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSTimestreamWriteDatabaseConfigTags1(rName, "key1", "value1"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSTimestreamWriteDatabaseExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"), | ||
), | ||
}, | ||
{ | ||
Config: testAccAWSTimestreamWriteDatabaseConfigTags2(rName, "key1", "value1updated", "key2", "value2"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSTimestreamWriteDatabaseExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), | ||
), | ||
}, | ||
{ | ||
Config: testAccAWSTimestreamWriteDatabaseConfigTags1(rName, "key2", "value2"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSTimestreamWriteDatabaseExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), | ||
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSTimestreamWriteDatabaseDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).timestreamwriteconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_timestreamwrite_database" { | ||
continue | ||
} | ||
|
||
_, err := conn.DescribeDatabase(×treamwrite.DescribeDatabaseInput{ | ||
DatabaseName: aws.String(rs.Primary.ID), | ||
}) | ||
|
||
if isAWSErr(err, timestreamwrite.ErrCodeResourceNotFoundException, "") { | ||
continue | ||
} | ||
|
||
if err == nil { | ||
return fmt.Errorf("Timestream Database (%s) still exists", rs.Primary.ID) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckAWSTimestreamWriteDatabaseExists(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No Timestream Database ID is set") | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).timestreamwriteconn | ||
|
||
_, err := conn.DescribeDatabase(×treamwrite.DescribeDatabaseInput{ | ||
DatabaseName: aws.String(rs.Primary.ID), | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccAWSTimestreamWriteDatabaseConfigNoTags(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_timestreamwrite_database" "test_database" { | ||
database_name = %[1]q | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccAWSTimestreamWriteDatabaseConfigTags1(rName, tagKey1, tagValue1 string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_timestreamwrite_database" "test_database" { | ||
database_name = %[1]q | ||
tags = { | ||
%[2]q = %[3]q | ||
} | ||
} | ||
`, rName, tagKey1, tagValue1) | ||
} | ||
|
||
func testAccAWSTimestreamWriteDatabaseConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_timestreamwrite_database" "test_database" { | ||
database_name = %[1]q | ||
tags = { | ||
%[2]q = %[3]q | ||
%[4]q = %[5]q | ||
} | ||
} | ||
`, rName, tagKey1, tagValue1, tagKey2, tagValue2) | ||
} | ||
|
||
func testAccAWSTimestreamWriteDatabaseConfigKmsKey(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_kms_key" "foo" { | ||
description = "Terraform acc test" | ||
policy = <<POLICY | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "*" | ||
}, | ||
"Action": "kms:*", | ||
"Resource": "*" | ||
} | ||
] | ||
} | ||
POLICY | ||
} | ||
resource "aws_timestreamwrite_database" "test_database" { | ||
database_name = %[1]q | ||
kms_key_id = aws_kms_key.foo.arn | ||
} | ||
`, rName) | ||
} |