Skip to content

feat(mongodb): [116424984] support maintenance #2765

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

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2765.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_mongodb_instance: support `maintenance_start` and `maintenance_end`
```
41 changes: 41 additions & 0 deletions tencentcloud/services/mongodb/resource_tc_mongodb_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ func ResourceTencentCloudMongodbInstance() *schema.Resource {
RequiredWith: []string{"availability_zone_list"},
Description: "The availability zone to which the Hidden node belongs. This parameter must be configured to deploy instances across availability zones.",
},
"maintenance_start": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Maintenance window start time. The value range is any full point or half point from `00:00-23:00`, such as 00:00 or 00:30.",
},
"maintenance_end": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Maintenance window end time.\n" +
" - The value range is any full point or half point from `00:00-23:00`, and the maintenance time duration is at least 30 minutes and at most 3 hours.\n" +
" - The end time must be based on the start time backwards.",
},
}
basic := TencentMongodbBasicInfo()
conflictList := []string{"mongos_cpu", "mongos_memory", "mongos_node_num"}
Expand Down Expand Up @@ -350,6 +364,16 @@ func resourceTencentCloudMongodbInstanceCreate(d *schema.ResourceData, meta inte
if !has {
return fmt.Errorf("[CRITAL]%s creating mongodb instance failed, instance doesn't exist", logId)
}

maintenanceStart, okMaintenanceStart := d.GetOk("maintenance_start")
maintenanceEnd, okMaintenanceEnd := d.GetOk("maintenance_end")
if okMaintenanceStart && okMaintenanceEnd {
err := mongodbService.SetInstanceMaintenance(ctx, instanceId, maintenanceStart.(string), maintenanceEnd.(string))
if err != nil {
return err
}
}
// mongodbService.SetInstanceMaintenance()
//internal version: replace begin begin, please do not modify this annotation and refrain from inserting any code between the beginning and end lines of the annotation.
//internal version: replace begin end, please do not modify this annotation and refrain from inserting any code between the beginning and end lines of the annotation.
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
Expand Down Expand Up @@ -442,6 +466,12 @@ func resourceTencentCloudMongodbInstanceRead(d *schema.ResourceData, meta interf
_ = d.Set("vport", instance.Vport)
_ = d.Set("create_time", instance.CreateTime)
_ = d.Set("node_num", *instance.SecondaryNum+1)
if instance.MaintenanceStart != nil && len(*instance.MaintenanceStart) == 8 {
_ = d.Set("maintenance_start", (*instance.MaintenanceStart)[:5])
}
if instance.MaintenanceEnd != nil && len(*instance.MaintenanceEnd) == 8 {
_ = d.Set("maintenance_end", (*instance.MaintenanceEnd)[:5])
}

replicateSets, err := mongodbService.DescribeDBInstanceNodeProperty(ctx, instanceId)
if err != nil {
Expand Down Expand Up @@ -630,6 +660,17 @@ func resourceTencentCloudMongodbInstanceUpdate(d *schema.ResourceData, meta inte
}
}

if d.HasChange("maintenance_start") || d.HasChange("maintenance_end") {
maintenanceStart, okMaintenanceStart := d.GetOk("maintenance_start")
maintenanceEnd, okMaintenanceEnd := d.GetOk("maintenance_end")
if okMaintenanceStart && okMaintenanceEnd {
err := mongodbService.SetInstanceMaintenance(ctx, instanceId, maintenanceStart.(string), maintenanceEnd.(string))
if err != nil {
return err
}
}
}

d.Partial(false)

return resourceTencentCloudMongodbInstanceRead(d, meta)
Expand Down
94 changes: 94 additions & 0 deletions tencentcloud/services/mongodb/resource_tc_mongodb_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ func TestAccTencentCloudMongodbInstanceResource_PostPaid(t *testing.T) {
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "security_groups.0", "sg-05f7wnhn"),
),
},
{
Config: testAccMongodbInstance_updateMaintenance,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "maintenance_start", "05:00"),
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "maintenance_end", "06:00"),
),
},
},
})
}
Expand Down Expand Up @@ -188,6 +195,13 @@ func TestAccTencentCloudMongodbInstanceResource_Prepaid(t *testing.T) {
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "tags.prepaid", "prepaid"),
),
},
{
Config: testAccMongodbInstancePrepaid_updateMaintenance,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "maintenance_start", "05:00"),
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "maintenance_end", "06:00"),
),
},
{
ResourceName: "tencentcloud_mongodb_instance.mongodb_prepaid",
ImportState: true,
Expand Down Expand Up @@ -388,6 +402,49 @@ resource "tencentcloud_mongodb_instance" "mongodb" {
}
`

const testAccMongodbInstance_updateMaintenance = tcacctest.DefaultMongoDBSpec + `
resource "tencentcloud_vpc" "vpc" {
name = "mongodb-instance-vpc"
cidr_block = "10.0.0.0/16"
}

resource "tencentcloud_subnet" "subnet" {
vpc_id = tencentcloud_vpc.vpc.id
name = "mongodb-instance-subnet"
cidr_block = "10.0.0.0/16"
availability_zone = "ap-guangzhou-3"
}

resource "tencentcloud_mongodb_instance" "mongodb" {
instance_name = "tf-mongodb-update"
memory = local.memory * 2
volume = local.volume * 2
engine_version = local.engine_version
machine_type = local.machine_type
security_groups = ["sg-05f7wnhn"]
available_zone = "ap-guangzhou-3"
project_id = 0
password = "test1234update"
vpc_id = tencentcloud_vpc.vpc.id
subnet_id = tencentcloud_subnet.subnet.id

node_num = 5
add_node_list {
role = "SECONDARY"
zone = "ap-guangzhou-3"
}
add_node_list {
role = "SECONDARY"
zone = "ap-guangzhou-3"
}
tags = {
abc = "abc"
}
maintenance_start = "05:00"
maintenance_end = "06:00"
}
`

const testAccMongodbInstancePrepaid = tcacctest.DefaultMongoDBSpec + `
resource "tencentcloud_vpc" "vpc" {
name = "mongodb-instance-prepaid-vpc"
Expand Down Expand Up @@ -458,6 +515,43 @@ resource "tencentcloud_mongodb_instance" "mongodb_prepaid" {
}
`

const testAccMongodbInstancePrepaid_updateMaintenance = tcacctest.DefaultMongoDBSpec + `
resource "tencentcloud_vpc" "vpc" {
name = "mongodb-instance-prepaid-vpc"
cidr_block = "10.0.0.0/16"
}

resource "tencentcloud_subnet" "subnet" {
vpc_id = tencentcloud_vpc.vpc.id
name = "mongodb-instance-prepaid-subnet"
cidr_block = "10.0.0.0/16"
availability_zone = "ap-guangzhou-3"
}

resource "tencentcloud_mongodb_instance" "mongodb_prepaid" {
instance_name = "tf-mongodb-test-prepaid-update"
memory = local.memory
volume = local.volume
engine_version = local.engine_version
machine_type = local.machine_type
security_groups = [local.security_group_id]
available_zone = "ap-guangzhou-3"
project_id = 0
password = "test1234update"
charge_type = "PREPAID"
prepaid_period = 1
auto_renew_flag = 1
vpc_id = tencentcloud_vpc.vpc.id
subnet_id = tencentcloud_subnet.subnet.id

tags = {
prepaid = "prepaid"
}
maintenance_start = "05:00"
maintenance_end = "06:00"
}
`

const testAccMongodbInstance_multiZone = tcacctest.DefaultMongoDBSpec + `
resource "tencentcloud_vpc" "vpc" {
name = "mongodb-multi-zone-vpc"
Expand Down
20 changes: 20 additions & 0 deletions tencentcloud/services/mongodb/service_tencentcloud_mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,3 +903,23 @@ func (me *MongodbService) DescribeDBInstanceDeal(ctx context.Context, dealId str

return
}

func (me *MongodbService) SetInstanceMaintenance(ctx context.Context, instanceId, maintenanceStart, maintenanceEnd string) error {
logId := tccommon.GetLogId(ctx)

request := mongodb.NewSetInstanceMaintenanceRequest()
request.InstanceId = helper.String(instanceId)
request.MaintenanceStart = helper.String(maintenanceStart)
request.MaintenanceEnd = helper.String(maintenanceEnd)

ratelimit.Check(request.GetAction())

response, err := me.client.UseMongodbClient().SetInstanceMaintenance(request)
if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), err.Error())
return err
}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())

return nil
}
4 changes: 4 additions & 0 deletions website/docs/r/mongodb_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ The following arguments are supported:
- Basic network cannot be selected.
* `charge_type` - (Optional, String, ForceNew) The charge type of instance. Valid values are `PREPAID` and `POSTPAID_BY_HOUR`. Default value is `POSTPAID_BY_HOUR`. Note: TencentCloud International only supports `POSTPAID_BY_HOUR`. Caution that update operation on this field will delete old instances and create new one with new charge type.
* `hidden_zone` - (Optional, String) The availability zone to which the Hidden node belongs. This parameter must be configured to deploy instances across availability zones.
* `maintenance_end` - (Optional, String) Maintenance window end time.
- The value range is any full point or half point from `00:00-23:00`, and the maintenance time duration is at least 30 minutes and at most 3 hours.
- The end time must be based on the start time backwards.
* `maintenance_start` - (Optional, String) Maintenance window start time. The value range is any full point or half point from `00:00-23:00`, such as 00:00 or 00:30.
* `node_num` - (Optional, Int) The number of nodes in each replica set. Default value: 3.
* `password` - (Optional, String) Password of this Mongodb account.
* `prepaid_period` - (Optional, Int) The tenancy (time unit is month) of the prepaid instance. Valid values are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 24, 36. NOTE: it only works when charge_type is set to `PREPAID`.
Expand Down
Loading