Skip to content

Commit e755577

Browse files
tongyimingmikatong
and
mikatong
authored
feat(mongodb): [116424984] support maintenance (#2765)
* support maintenance * add changelog * update * update --------- Co-authored-by: mikatong <[email protected]>
1 parent 64f8528 commit e755577

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

.changelog/2765.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_mongodb_instance: support `maintenance_start` and `maintenance_end`
3+
```

tencentcloud/services/mongodb/resource_tc_mongodb_instance.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ func ResourceTencentCloudMongodbInstance() *schema.Resource {
123123
RequiredWith: []string{"availability_zone_list"},
124124
Description: "The availability zone to which the Hidden node belongs. This parameter must be configured to deploy instances across availability zones.",
125125
},
126+
"maintenance_start": {
127+
Type: schema.TypeString,
128+
Optional: true,
129+
Computed: true,
130+
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.",
131+
},
132+
"maintenance_end": {
133+
Type: schema.TypeString,
134+
Optional: true,
135+
Computed: true,
136+
Description: "Maintenance window end time.\n" +
137+
" - 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" +
138+
" - The end time must be based on the start time backwards.",
139+
},
126140
}
127141
basic := TencentMongodbBasicInfo()
128142
conflictList := []string{"mongos_cpu", "mongos_memory", "mongos_node_num"}
@@ -350,6 +364,16 @@ func resourceTencentCloudMongodbInstanceCreate(d *schema.ResourceData, meta inte
350364
if !has {
351365
return fmt.Errorf("[CRITAL]%s creating mongodb instance failed, instance doesn't exist", logId)
352366
}
367+
368+
maintenanceStart, okMaintenanceStart := d.GetOk("maintenance_start")
369+
maintenanceEnd, okMaintenanceEnd := d.GetOk("maintenance_end")
370+
if okMaintenanceStart && okMaintenanceEnd {
371+
err := mongodbService.SetInstanceMaintenance(ctx, instanceId, maintenanceStart.(string), maintenanceEnd.(string))
372+
if err != nil {
373+
return err
374+
}
375+
}
376+
// mongodbService.SetInstanceMaintenance()
353377
//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.
354378
//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.
355379
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
@@ -442,6 +466,12 @@ func resourceTencentCloudMongodbInstanceRead(d *schema.ResourceData, meta interf
442466
_ = d.Set("vport", instance.Vport)
443467
_ = d.Set("create_time", instance.CreateTime)
444468
_ = d.Set("node_num", *instance.SecondaryNum+1)
469+
if instance.MaintenanceStart != nil && len(*instance.MaintenanceStart) == 8 {
470+
_ = d.Set("maintenance_start", (*instance.MaintenanceStart)[:5])
471+
}
472+
if instance.MaintenanceEnd != nil && len(*instance.MaintenanceEnd) == 8 {
473+
_ = d.Set("maintenance_end", (*instance.MaintenanceEnd)[:5])
474+
}
445475

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

663+
if d.HasChange("maintenance_start") || d.HasChange("maintenance_end") {
664+
maintenanceStart, okMaintenanceStart := d.GetOk("maintenance_start")
665+
maintenanceEnd, okMaintenanceEnd := d.GetOk("maintenance_end")
666+
if okMaintenanceStart && okMaintenanceEnd {
667+
err := mongodbService.SetInstanceMaintenance(ctx, instanceId, maintenanceStart.(string), maintenanceEnd.(string))
668+
if err != nil {
669+
return err
670+
}
671+
}
672+
}
673+
633674
d.Partial(false)
634675

635676
return resourceTencentCloudMongodbInstanceRead(d, meta)

tencentcloud/services/mongodb/resource_tc_mongodb_instance_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ func TestAccTencentCloudMongodbInstanceResource_PostPaid(t *testing.T) {
130130
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "security_groups.0", "sg-05f7wnhn"),
131131
),
132132
},
133+
{
134+
Config: testAccMongodbInstance_updateMaintenance,
135+
Check: resource.ComposeTestCheckFunc(
136+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "maintenance_start", "05:00"),
137+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "maintenance_end", "06:00"),
138+
),
139+
},
133140
},
134141
})
135142
}
@@ -188,6 +195,13 @@ func TestAccTencentCloudMongodbInstanceResource_Prepaid(t *testing.T) {
188195
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "tags.prepaid", "prepaid"),
189196
),
190197
},
198+
{
199+
Config: testAccMongodbInstancePrepaid_updateMaintenance,
200+
Check: resource.ComposeTestCheckFunc(
201+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "maintenance_start", "05:00"),
202+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "maintenance_end", "06:00"),
203+
),
204+
},
191205
{
192206
ResourceName: "tencentcloud_mongodb_instance.mongodb_prepaid",
193207
ImportState: true,
@@ -388,6 +402,49 @@ resource "tencentcloud_mongodb_instance" "mongodb" {
388402
}
389403
`
390404

405+
const testAccMongodbInstance_updateMaintenance = tcacctest.DefaultMongoDBSpec + `
406+
resource "tencentcloud_vpc" "vpc" {
407+
name = "mongodb-instance-vpc"
408+
cidr_block = "10.0.0.0/16"
409+
}
410+
411+
resource "tencentcloud_subnet" "subnet" {
412+
vpc_id = tencentcloud_vpc.vpc.id
413+
name = "mongodb-instance-subnet"
414+
cidr_block = "10.0.0.0/16"
415+
availability_zone = "ap-guangzhou-3"
416+
}
417+
418+
resource "tencentcloud_mongodb_instance" "mongodb" {
419+
instance_name = "tf-mongodb-update"
420+
memory = local.memory * 2
421+
volume = local.volume * 2
422+
engine_version = local.engine_version
423+
machine_type = local.machine_type
424+
security_groups = ["sg-05f7wnhn"]
425+
available_zone = "ap-guangzhou-3"
426+
project_id = 0
427+
password = "test1234update"
428+
vpc_id = tencentcloud_vpc.vpc.id
429+
subnet_id = tencentcloud_subnet.subnet.id
430+
431+
node_num = 5
432+
add_node_list {
433+
role = "SECONDARY"
434+
zone = "ap-guangzhou-3"
435+
}
436+
add_node_list {
437+
role = "SECONDARY"
438+
zone = "ap-guangzhou-3"
439+
}
440+
tags = {
441+
abc = "abc"
442+
}
443+
maintenance_start = "05:00"
444+
maintenance_end = "06:00"
445+
}
446+
`
447+
391448
const testAccMongodbInstancePrepaid = tcacctest.DefaultMongoDBSpec + `
392449
resource "tencentcloud_vpc" "vpc" {
393450
name = "mongodb-instance-prepaid-vpc"
@@ -458,6 +515,43 @@ resource "tencentcloud_mongodb_instance" "mongodb_prepaid" {
458515
}
459516
`
460517

518+
const testAccMongodbInstancePrepaid_updateMaintenance = tcacctest.DefaultMongoDBSpec + `
519+
resource "tencentcloud_vpc" "vpc" {
520+
name = "mongodb-instance-prepaid-vpc"
521+
cidr_block = "10.0.0.0/16"
522+
}
523+
524+
resource "tencentcloud_subnet" "subnet" {
525+
vpc_id = tencentcloud_vpc.vpc.id
526+
name = "mongodb-instance-prepaid-subnet"
527+
cidr_block = "10.0.0.0/16"
528+
availability_zone = "ap-guangzhou-3"
529+
}
530+
531+
resource "tencentcloud_mongodb_instance" "mongodb_prepaid" {
532+
instance_name = "tf-mongodb-test-prepaid-update"
533+
memory = local.memory
534+
volume = local.volume
535+
engine_version = local.engine_version
536+
machine_type = local.machine_type
537+
security_groups = [local.security_group_id]
538+
available_zone = "ap-guangzhou-3"
539+
project_id = 0
540+
password = "test1234update"
541+
charge_type = "PREPAID"
542+
prepaid_period = 1
543+
auto_renew_flag = 1
544+
vpc_id = tencentcloud_vpc.vpc.id
545+
subnet_id = tencentcloud_subnet.subnet.id
546+
547+
tags = {
548+
prepaid = "prepaid"
549+
}
550+
maintenance_start = "05:00"
551+
maintenance_end = "06:00"
552+
}
553+
`
554+
461555
const testAccMongodbInstance_multiZone = tcacctest.DefaultMongoDBSpec + `
462556
resource "tencentcloud_vpc" "vpc" {
463557
name = "mongodb-multi-zone-vpc"

tencentcloud/services/mongodb/service_tencentcloud_mongodb.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,3 +903,23 @@ func (me *MongodbService) DescribeDBInstanceDeal(ctx context.Context, dealId str
903903

904904
return
905905
}
906+
907+
func (me *MongodbService) SetInstanceMaintenance(ctx context.Context, instanceId, maintenanceStart, maintenanceEnd string) error {
908+
logId := tccommon.GetLogId(ctx)
909+
910+
request := mongodb.NewSetInstanceMaintenanceRequest()
911+
request.InstanceId = helper.String(instanceId)
912+
request.MaintenanceStart = helper.String(maintenanceStart)
913+
request.MaintenanceEnd = helper.String(maintenanceEnd)
914+
915+
ratelimit.Check(request.GetAction())
916+
917+
response, err := me.client.UseMongodbClient().SetInstanceMaintenance(request)
918+
if err != nil {
919+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), err.Error())
920+
return err
921+
}
922+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
923+
924+
return nil
925+
}

website/docs/r/mongodb_instance.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ The following arguments are supported:
4747
- Basic network cannot be selected.
4848
* `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.
4949
* `hidden_zone` - (Optional, String) The availability zone to which the Hidden node belongs. This parameter must be configured to deploy instances across availability zones.
50+
* `maintenance_end` - (Optional, String) Maintenance window end time.
51+
- 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.
52+
- The end time must be based on the start time backwards.
53+
* `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.
5054
* `node_num` - (Optional, Int) The number of nodes in each replica set. Default value: 3.
5155
* `password` - (Optional, String) Password of this Mongodb account.
5256
* `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`.

0 commit comments

Comments
 (0)