-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resource/timestreamwrite_table: new resource #19354
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6cb1c40
add table resource
anGie44 ac93a51
Update CHANGELOG for #19354
anGie44 93594c1
fix terrafmt error
anGie44 0358fbb
Update website/docs/r/timestreamwrite_table.html.markdown
anGie44 4e86a0a
Merge remote-tracking branch 'origin/main' into f-timestreamwrite-table
anGie44 71c8878
add INFO line in sweeper before deleting
anGie44 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
aws_timestreamwrite_table | ||
``` |
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
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,285 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/timestreamwrite" | ||
"github.com/hashicorp/aws-sdk-go-base/tfawserr" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" | ||
) | ||
|
||
func resourceAwsTimestreamWriteTable() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateWithoutTimeout: resourceAwsTimestreamWriteTableCreate, | ||
ReadWithoutTimeout: resourceAwsTimestreamWriteTableRead, | ||
UpdateWithoutTimeout: resourceAwsTimestreamWriteTableUpdate, | ||
DeleteWithoutTimeout: resourceAwsTimestreamWriteTableDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"database_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.All( | ||
validation.StringLenBetween(3, 64), | ||
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`), "must only include alphanumeric, underscore, period, or hyphen characters"), | ||
), | ||
}, | ||
|
||
"retention_properties": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"magnetic_store_retention_period_in_days": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ValidateFunc: validation.IntBetween(1, 73000), | ||
}, | ||
|
||
"memory_store_retention_period_in_hours": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ValidateFunc: validation.IntBetween(1, 8766), | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"table_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.All( | ||
validation.StringLenBetween(3, 64), | ||
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`), "must only include alphanumeric, underscore, period, or hyphen characters"), | ||
), | ||
}, | ||
|
||
"tags": tagsSchema(), | ||
|
||
"tags_all": tagsSchemaComputed(), | ||
}, | ||
|
||
CustomizeDiff: SetTagsDiff, | ||
} | ||
} | ||
|
||
func resourceAwsTimestreamWriteTableCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*AWSClient).timestreamwriteconn | ||
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig | ||
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{}))) | ||
|
||
tableName := d.Get("table_name").(string) | ||
input := ×treamwrite.CreateTableInput{ | ||
DatabaseName: aws.String(d.Get("database_name").(string)), | ||
TableName: aws.String(tableName), | ||
} | ||
|
||
if v, ok := d.GetOk("retention_properties"); ok && len(v.([]interface{})) > 0 && v.([]interface{}) != nil { | ||
input.RetentionProperties = expandTimestreamWriteRetentionProperties(v.([]interface{})) | ||
} | ||
|
||
if len(tags) > 0 { | ||
input.Tags = tags.IgnoreAws().TimestreamwriteTags() | ||
} | ||
|
||
output, err := conn.CreateTableWithContext(ctx, input) | ||
|
||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error creating Timestream Table (%s): %w", tableName, err)) | ||
} | ||
|
||
if output == nil || output.Table == nil { | ||
return diag.FromErr(fmt.Errorf("error creating Timestream Table (%s): empty output", tableName)) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s:%s", aws.StringValue(output.Table.TableName), aws.StringValue(output.Table.DatabaseName))) | ||
|
||
return resourceAwsTimestreamWriteTableRead(ctx, d, meta) | ||
} | ||
|
||
func resourceAwsTimestreamWriteTableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*AWSClient).timestreamwriteconn | ||
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig | ||
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig | ||
|
||
tableName, databaseName, err := resourceAwsTimestreamWriteTableParseId(d.Id()) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
input := ×treamwrite.DescribeTableInput{ | ||
DatabaseName: aws.String(databaseName), | ||
TableName: aws.String(tableName), | ||
} | ||
|
||
output, err := conn.DescribeTableWithContext(ctx, input) | ||
|
||
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, timestreamwrite.ErrCodeResourceNotFoundException) { | ||
log.Printf("[WARN] Timestream Table %s not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if output == nil || output.Table == nil { | ||
return diag.FromErr(fmt.Errorf("error reading Timestream Table (%s): empty output", d.Id())) | ||
} | ||
|
||
table := output.Table | ||
arn := aws.StringValue(table.Arn) | ||
|
||
d.Set("arn", arn) | ||
d.Set("database_name", table.DatabaseName) | ||
|
||
if err := d.Set("retention_properties", flattenTimestreamWriteRetentionProperties(table.RetentionProperties)); err != nil { | ||
return diag.FromErr(fmt.Errorf("error setting retention_properties: %w", err)) | ||
} | ||
|
||
d.Set("table_name", table.TableName) | ||
|
||
tags, err := keyvaluetags.TimestreamwriteListTags(conn, arn) | ||
|
||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error listing tags for Timestream Table (%s): %w", arn, err)) | ||
} | ||
|
||
tags = tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig) | ||
|
||
//lintignore:AWSR002 | ||
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil { | ||
return diag.FromErr(fmt.Errorf("error setting tags: %w", err)) | ||
} | ||
|
||
if err := d.Set("tags_all", tags.Map()); err != nil { | ||
return diag.FromErr(fmt.Errorf("error setting tags_all: %w", err)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsTimestreamWriteTableUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*AWSClient).timestreamwriteconn | ||
|
||
if d.HasChange("retention_properties") { | ||
tableName, databaseName, err := resourceAwsTimestreamWriteTableParseId(d.Id()) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
input := ×treamwrite.UpdateTableInput{ | ||
DatabaseName: aws.String(databaseName), | ||
RetentionProperties: expandTimestreamWriteRetentionProperties(d.Get("retention_properties").([]interface{})), | ||
TableName: aws.String(tableName), | ||
} | ||
|
||
_, err = conn.UpdateTableWithContext(ctx, input) | ||
|
||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error updating Timestream Table (%s): %w", d.Id(), err)) | ||
} | ||
} | ||
|
||
if d.HasChange("tags_all") { | ||
o, n := d.GetChange("tags_all") | ||
|
||
if err := keyvaluetags.TimestreamwriteUpdateTags(conn, d.Get("arn").(string), o, n); err != nil { | ||
return diag.FromErr(fmt.Errorf("error updating Timestream Table (%s) tags: %w", d.Get("arn").(string), err)) | ||
} | ||
} | ||
|
||
return resourceAwsTimestreamWriteTableRead(ctx, d, meta) | ||
} | ||
|
||
func resourceAwsTimestreamWriteTableDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*AWSClient).timestreamwriteconn | ||
|
||
tableName, databaseName, err := resourceAwsTimestreamWriteTableParseId(d.Id()) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
input := ×treamwrite.DeleteTableInput{ | ||
DatabaseName: aws.String(databaseName), | ||
TableName: aws.String(tableName), | ||
} | ||
|
||
_, err = conn.DeleteTableWithContext(ctx, input) | ||
|
||
if tfawserr.ErrCodeEquals(err, timestreamwrite.ErrCodeResourceNotFoundException) { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("error deleting Timestream Table (%s): %w", d.Id(), err)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func expandTimestreamWriteRetentionProperties(l []interface{}) *timestreamwrite.RetentionProperties { | ||
if len(l) == 0 || l[0] == nil { | ||
return nil | ||
} | ||
|
||
tfMap, ok := l[0].(map[string]interface{}) | ||
|
||
if !ok { | ||
return nil | ||
} | ||
|
||
rp := ×treamwrite.RetentionProperties{} | ||
|
||
if v, ok := tfMap["magnetic_store_retention_period_in_days"].(int); ok { | ||
rp.MagneticStoreRetentionPeriodInDays = aws.Int64(int64(v)) | ||
} | ||
|
||
if v, ok := tfMap["memory_store_retention_period_in_hours"].(int); ok { | ||
rp.MemoryStoreRetentionPeriodInHours = aws.Int64(int64(v)) | ||
} | ||
|
||
return rp | ||
} | ||
|
||
func flattenTimestreamWriteRetentionProperties(rp *timestreamwrite.RetentionProperties) []interface{} { | ||
if rp == nil { | ||
return []interface{}{} | ||
} | ||
|
||
m := map[string]interface{}{ | ||
"magnetic_store_retention_period_in_days": aws.Int64Value(rp.MagneticStoreRetentionPeriodInDays), | ||
"memory_store_retention_period_in_hours": aws.Int64Value(rp.MemoryStoreRetentionPeriodInHours), | ||
} | ||
|
||
return []interface{}{m} | ||
} | ||
|
||
func resourceAwsTimestreamWriteTableParseId(id string) (string, string, error) { | ||
idParts := strings.SplitN(id, ":", 2) | ||
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { | ||
return "", "", fmt.Errorf("unexpected format of ID (%s), expected table_name:database_name", id) | ||
} | ||
return idParts[0], idParts[1], nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add line
so that the sweeper reports the deletion.