Skip to content

Commit

Permalink
Merge pull request #38452 from ayumitamai97/docdbelastic-add-backup-r…
Browse files Browse the repository at this point in the history
…etention-period

docdbelastic: Support backup_retention_period
  • Loading branch information
johnsonaj authored Sep 5, 2024
2 parents 362643d + 9352f72 commit 0934246
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/38452.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_docdbelastic_cluster: Add `backup_retention_period` and `preferred_backup_window` attributes
```
1 change: 1 addition & 0 deletions .ci/.golangci2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ linters-settings:
- schema.DefaultTimeout
- validation.*
# Terraform Plugin Framework
- int32validator.*
- int64validator.*
- listvalidator.*
- setvalidator.*
Expand Down
31 changes: 31 additions & 0 deletions internal/service/docdbelastic/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"github.com/aws/aws-sdk-go-v2/service/docdbelastic"
awstypes "github.com/aws/aws-sdk-go-v2/service/docdbelastic/types"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int32planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
Expand Down Expand Up @@ -81,6 +83,16 @@ func (r *resourceCluster) Schema(ctx context.Context, _ resource.SchemaRequest,
stringplanmodifier.RequiresReplace(),
},
},
"backup_retention_period": schema.Int32Attribute{
Optional: true,
Computed: true,
Validators: []validator.Int32{
int32validator.Between(1, 35),
},
PlanModifiers: []planmodifier.Int32{
int32planmodifier.UseStateForUnknown(),
},
},
names.AttrEndpoint: schema.StringAttribute{
Computed: true,
PlanModifiers: []planmodifier.String{
Expand All @@ -102,6 +114,13 @@ func (r *resourceCluster) Schema(ctx context.Context, _ resource.SchemaRequest,
stringplanmodifier.RequiresReplace(),
},
},
"preferred_backup_window": schema.StringAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
names.AttrPreferredMaintenanceWindow: schema.StringAttribute{
CustomType: fwtypes.OnceAWeekWindowType,
Optional: true,
Expand Down Expand Up @@ -279,6 +298,14 @@ func (r *resourceCluster) Update(ctx context.Context, request resource.UpdateReq
input.AuthType = plan.AuthType.ValueEnum()
}

if !plan.BackupRetentionPeriod.Equal(state.BackupRetentionPeriod) {
input.BackupRetentionPeriod = plan.BackupRetentionPeriod.ValueInt32Pointer()
}

if !plan.PreferredBackupWindow.Equal(state.PreferredBackupWindow) {
input.PreferredBackupWindow = plan.PreferredBackupWindow.ValueStringPointer()
}

if !plan.PreferredMaintenanceWindow.Equal(state.PreferredMaintenanceWindow) {
input.PreferredMaintenanceWindow = plan.PreferredMaintenanceWindow.ValueStringPointer()
}
Expand Down Expand Up @@ -385,10 +412,12 @@ type resourceClusterData struct {
AdminUserPassword types.String `tfsdk:"admin_user_password"`
ARN types.String `tfsdk:"arn"`
AuthType fwtypes.StringEnum[awstypes.Auth] `tfsdk:"auth_type"`
BackupRetentionPeriod types.Int32 `tfsdk:"backup_retention_period"`
Endpoint types.String `tfsdk:"endpoint"`
ID types.String `tfsdk:"id"`
KmsKeyID types.String `tfsdk:"kms_key_id"`
Name types.String `tfsdk:"name"`
PreferredBackupWindow types.String `tfsdk:"preferred_backup_window"`
PreferredMaintenanceWindow fwtypes.OnceAWeekWindow `tfsdk:"preferred_maintenance_window"`
ShardCapacity types.Int64 `tfsdk:"shard_capacity"`
ShardCount types.Int64 `tfsdk:"shard_count"`
Expand Down Expand Up @@ -494,6 +523,8 @@ 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.PreferredBackupWindow.Equal(state.PreferredBackupWindow) ||
!plan.PreferredMaintenanceWindow.Equal(state.PreferredMaintenanceWindow) ||
!plan.ShardCapacity.Equal(state.ShardCapacity) ||
!plan.ShardCount.Equal(state.ShardCount) ||
Expand Down
25 changes: 22 additions & 3 deletions internal/service/docdbelastic/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestAccDocDBElasticCluster_update(t *testing.T) {
),
},
{
Config: testAccClusterConfig_update(rName, 4),
Config: testAccClusterConfig_update(rName, 4, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &cluster),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName),
Expand All @@ -196,6 +196,24 @@ 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, 3),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &cluster),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName),
resource.TestCheckResourceAttrSet(resourceName, names.AttrEndpoint),
resource.TestCheckResourceAttr(resourceName, "shard_capacity", acctest.Ct4),
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.Ct3),
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 @@ -310,7 +328,7 @@ resource "aws_docdbelastic_cluster" "test" {
`, rName))
}

func testAccClusterConfig_update(rName string, shardCapacity int) string {
func testAccClusterConfig_update(rName string, shardCapacity, backupRetentionPeriod int) string {
return acctest.ConfigCompose(
testAccClusterBaseConfig(rName),
fmt.Sprintf(`
Expand All @@ -324,6 +342,7 @@ resource "aws_docdbelastic_cluster" "test" {
auth_type = "PLAIN_TEXT"
preferred_maintenance_window = "Tue:04:00-Tue:04:30"
backup_retention_period = %[3]d
vpc_security_group_ids = [
aws_security_group.test.id
Expand All @@ -334,7 +353,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
2 changes: 2 additions & 0 deletions website/docs/r/docdbelastic_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ The following arguments are required:

The following arguments are optional:

* `backup_retention_period` - (Optional) The number of days for which automatic snapshots are retained. It should be in between 1 and 35. If not specified, the default value of 1 is set.
* `kms_key_id` - (Optional) ARN of a KMS key that is used to encrypt the Elastic DocumentDB cluster. If not specified, the default encryption key that KMS creates for your account is used.
* `preferred_backup_window` - (Optional) The daily time range during which automated backups are created if automated backups are enabled, as determined by the `backup_retention_period`.
* `preferred_maintenance_window` - (Optional) Weekly time range during which system maintenance can occur in UTC. Format: `ddd:hh24:mi-ddd:hh24:mi`. If not specified, AWS will choose a random 30-minute window on a random day of the week.
* `subnet_ids` - (Optional) IDs of subnets in which the Elastic DocumentDB Cluster operates.
* `tags` - (Optional) A map of tags to assign to the collection. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.
Expand Down

0 comments on commit 0934246

Please sign in to comment.