Skip to content

Commit

Permalink
Add backup_retention_period to docdbelastic resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ayumitamai97 committed Jul 21, 2024
1 parent cc6a0f5 commit 825c8b6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
31 changes: 23 additions & 8 deletions internal/service/docdbelastic/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ func (r *resourceCluster) Schema(ctx context.Context, _ resource.SchemaRequest,
enum.FrameworkValidate[awstypes.Auth](),
},
},
"backup_retention_period": schema.Int64Attribute{
Optional: true,
Computed: true,
Validators: []validator.Int64{
int64validator.Between(1, 35),
},
},
names.AttrEndpoint: schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
Expand Down Expand Up @@ -162,14 +169,15 @@ func (r *resourceCluster) Create(ctx context.Context, request resource.CreateReq
}

input := &docdbelastic.CreateClusterInput{
ClientToken: aws.String(id.UniqueId()),
AdminUserName: flex.StringFromFramework(ctx, plan.AdminUserName),
AdminUserPassword: flex.StringFromFramework(ctx, plan.AdminUserPassword),
AuthType: awstypes.Auth(plan.AuthType.ValueString()),
ClusterName: flex.StringFromFramework(ctx, plan.Name),
ShardCapacity: flex.Int32FromFramework(ctx, plan.ShardCapacity),
ShardCount: flex.Int32FromFramework(ctx, plan.ShardCount),
Tags: getTagsIn(ctx),
ClientToken: aws.String(id.UniqueId()),
AdminUserName: flex.StringFromFramework(ctx, plan.AdminUserName),
AdminUserPassword: flex.StringFromFramework(ctx, plan.AdminUserPassword),
AuthType: awstypes.Auth(plan.AuthType.ValueString()),
BackupRetentionPeriod: flex.Int32FromFramework(ctx, plan.BackupRetentionPeriod),
ClusterName: flex.StringFromFramework(ctx, plan.Name),
ShardCapacity: flex.Int32FromFramework(ctx, plan.ShardCapacity),
ShardCount: flex.Int32FromFramework(ctx, plan.ShardCount),
Tags: getTagsIn(ctx),
}

if !plan.KmsKeyID.IsNull() || !plan.KmsKeyID.IsUnknown() {
Expand Down Expand Up @@ -276,6 +284,10 @@ func (r *resourceCluster) Update(ctx context.Context, request resource.UpdateReq
input.AuthType = awstypes.Auth(plan.AuthType.ValueString())
}

if !plan.BackupRetentionPeriod.Equal(state.BackupRetentionPeriod) {
input.BackupRetentionPeriod = flex.Int32FromFramework(ctx, plan.BackupRetentionPeriod)
}

if !plan.PreferredMaintenanceWindow.Equal(state.PreferredMaintenanceWindow) {
input.PreferredMaintenanceWindow = flex.StringFromFramework(ctx, plan.PreferredMaintenanceWindow)
}
Expand Down Expand Up @@ -381,6 +393,7 @@ type resourceClusterData struct {
AdminUserPassword types.String `tfsdk:"admin_user_password"`
ARN types.String `tfsdk:"arn"`
AuthType types.String `tfsdk:"auth_type"`
BackupRetentionPeriod types.Int64 `tfsdk:"backup_retention_period"`
Endpoint types.String `tfsdk:"endpoint"`
ID types.String `tfsdk:"id"`
KmsKeyID types.String `tfsdk:"kms_key_id"`
Expand Down Expand Up @@ -489,6 +502,7 @@ func findClusterByID(ctx context.Context, conn *docdbelastic.Client, id string)
func (r *resourceClusterData) refreshFromOutput(ctx context.Context, output *awstypes.Cluster) {
r.AdminUserName = flex.StringToFrameworkLegacy(ctx, output.AdminUserName)
r.AuthType = flex.StringValueToFramework(ctx, string(output.AuthType))
r.BackupRetentionPeriod = flex.Int32ToFramework(ctx, output.BackupRetentionPeriod)
r.ARN = flex.StringToFramework(ctx, output.ClusterArn)
r.Endpoint = flex.StringToFramework(ctx, output.ClusterEndpoint)
r.KmsKeyID = flex.StringToFramework(ctx, output.KmsKeyId)
Expand All @@ -504,6 +518,7 @@ func clusterHasChanges(_ context.Context, plan, state resourceClusterData) bool
return !plan.Name.Equal(state.Name) ||
!plan.AdminUserPassword.Equal(state.AdminUserPassword) ||
!plan.AuthType.Equal(state.AuthType) ||
!plan.BackupRetentionPeriod.Equal(state.BackupRetentionPeriod) ||
!plan.PreferredMaintenanceWindow.Equal(state.PreferredMaintenanceWindow) ||
!plan.ShardCapacity.Equal(state.ShardCapacity) ||
!plan.ShardCount.Equal(state.ShardCount) ||
Expand Down
12 changes: 9 additions & 3 deletions internal/service/docdbelastic/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,15 @@ func TestAccDocDBElasticCluster_update(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "shard_count", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "admin_user_name", "testuser"),
resource.TestCheckResourceAttr(resourceName, "admin_user_password", "testpassword"),
resource.TestCheckResourceAttr(resourceName, "backup_retention_period", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "subnet_ids.#", acctest.Ct2),
resource.TestCheckResourceAttr(resourceName, "vpc_security_group_ids.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, names.AttrPreferredMaintenanceWindow, "tue:04:00-tue:04:30"),
resource.TestCheckResourceAttrSet(resourceName, names.AttrARN),
),
},
{
Config: testAccClusterConfig_update(rName, 4),
Config: testAccClusterConfig_update(rName, 4, 2),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &cluster),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName),
Expand All @@ -196,6 +197,7 @@ func TestAccDocDBElasticCluster_update(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "shard_count", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "admin_user_name", "testuser"),
resource.TestCheckResourceAttr(resourceName, "admin_user_password", "testpassword"),
resource.TestCheckResourceAttr(resourceName, "backup_retention_period", acctest.Ct2),
resource.TestCheckResourceAttr(resourceName, "subnet_ids.#", acctest.Ct2),
resource.TestCheckResourceAttr(resourceName, "vpc_security_group_ids.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, names.AttrPreferredMaintenanceWindow, "tue:04:00-tue:04:30"),
Expand Down Expand Up @@ -296,6 +298,8 @@ resource "aws_docdbelastic_cluster" "test" {
admin_user_password = "testpassword"
auth_type = "PLAIN_TEXT"
backup_retention_period = 1
preferred_maintenance_window = "tue:04:00-tue:04:30"
vpc_security_group_ids = [
Expand All @@ -310,7 +314,7 @@ resource "aws_docdbelastic_cluster" "test" {
`, rName))
}

func testAccClusterConfig_update(rName string, shardCapacity int) string {
func testAccClusterConfig_update(rName string, shardCapacity int, backupRetentionPeriod int) string {
return acctest.ConfigCompose(
testAccClusterBaseConfig(rName),
fmt.Sprintf(`
Expand All @@ -323,6 +327,8 @@ resource "aws_docdbelastic_cluster" "test" {
admin_user_password = "testpassword"
auth_type = "PLAIN_TEXT"
backup_retention_period = %[3]d
preferred_maintenance_window = "tue:04:00-tue:04:30"
vpc_security_group_ids = [
Expand All @@ -334,7 +340,7 @@ resource "aws_docdbelastic_cluster" "test" {
aws_subnet.test[1].id
]
}
`, rName, shardCapacity))
`, rName, shardCapacity, backupRetentionPeriod))
}

func testAccClusterConfig_tags1(rName, key1, value1 string) string {
Expand Down

0 comments on commit 825c8b6

Please sign in to comment.