Skip to content

Commit 6d04cd6

Browse files
authored
Merge pull request #764 from tencentcloudstack/feat/support_pg_modify_params
[feat]support pg modify params
2 parents b46b92a + 7376b8d commit 6d04cd6

File tree

10 files changed

+1228
-12
lines changed

10 files changed

+1228
-12
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ require (
2929
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.283
3030
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.199
3131
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.291
32-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.264
32+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.306
3333
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.199
3434
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.199
3535
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dayu v1.0.199
@@ -41,7 +41,7 @@ require (
4141
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.199
4242
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.199
4343
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.291
44-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.199
44+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.306
4545
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.290
4646
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.199
4747
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.275

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.291/go.mod h1:
473473
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.194/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
474474
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.264 h1:USf7I8ohzoqCwAcCHz5rz0hD57mmNXZgrLfxFLx1WmA=
475475
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.264/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
476+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.306 h1:wQe/QkYMFu41adzjalR4Un3z7g1g7pmc2aixjqxIyP0=
477+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.306/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
476478
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.199 h1:ajgJogYSIQ5u1PIbiV5nsvr5K0fYpm1/T7Dy+mxEM6U=
477479
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.199/go.mod h1:AqyM/ZZMD7q5mHBqNY9YImbSpEpoEe7E/vrTbUWX+po=
478480
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.199 h1:L0twFkJMOZzLkX08w8S14nX6oanD8YxMQDIaYXVim6A=
@@ -498,6 +500,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.291 h1:EIU
498500
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.291/go.mod h1:aMwrB/fRSIS8IFabcF0hfRC89y76DptUknUjAf1jlHE=
499501
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.199 h1:Opze570l6JbOc+/nYftYmheyCU9omz+emNJpDUBYYFQ=
500502
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.199/go.mod h1:Rh/4NXBd0aqmaRGDYcW4gL2Zi8JShGZiB23zrfVaS90=
503+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.306 h1:R/nD8qAexHLwcKhWOTmr9rzIrvnH1C4sEudpS4qUBWY=
504+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.306/go.mod h1:Rh/4NXBd0aqmaRGDYcW4gL2Zi8JShGZiB23zrfVaS90=
501505
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.290 h1:osYZxh9ApEc8UpvIMwjAUfdl7ytRcWUpcnnqLIpiJ/U=
502506
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/privatedns v1.0.290/go.mod h1:En+pdagcHkAASorHT1l8R6tUtieRNNxaQ7nfyqWPefk=
503507
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.199 h1:lXCng7HQqvubF7uwa7x5COsDZlJEjEJ/RBpaeYGc0+I=

tencentcloud/resource_tc_postgresql_instance.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ package tencentcloud
5757
import (
5858
"context"
5959
"fmt"
60+
"strconv"
6061
"strings"
62+
"time"
6163

6264
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
6365

@@ -176,6 +178,16 @@ func resourceTencentCloudPostgresqlInstance() *schema.Resource {
176178
Optional: true,
177179
Description: "The available tags within this postgresql.",
178180
},
181+
"max_standby_archive_delay": {
182+
Type: schema.TypeInt,
183+
Optional: true,
184+
Description: "max_standby_archive_delay applies when WAL data is being read from WAL archive (and is therefore not current). Units are milliseconds if not specified.",
185+
},
186+
"max_standby_streaming_delay": {
187+
Type: schema.TypeInt,
188+
Optional: true,
189+
Description: "max_standby_streaming_delay applies when WAL data is being received via streaming replication. Units are milliseconds if not specified.",
190+
},
179191
// Computed values
180192
"public_access_host": {
181193
Type: schema.TypeString,
@@ -381,6 +393,30 @@ func resourceTencentCloudPostgresqlInstanceCreate(d *schema.ResourceData, meta i
381393
}
382394
}
383395

396+
// set pg params
397+
paramEntrys := make(map[string]string)
398+
if v, ok := d.GetOkExists("max_standby_archive_delay"); ok {
399+
paramEntrys["max_standby_archive_delay"] = strconv.Itoa(v.(int))
400+
}
401+
if v, ok := d.GetOkExists("max_standby_streaming_delay"); ok {
402+
paramEntrys["max_standby_streaming_delay"] = strconv.Itoa(v.(int))
403+
}
404+
405+
if len(paramEntrys) != 0 {
406+
outErr = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
407+
inErr := postgresqlService.ModifyPgParams(ctx, instanceId, paramEntrys)
408+
if inErr != nil {
409+
return retryError(inErr)
410+
}
411+
return nil
412+
})
413+
if outErr != nil {
414+
return outErr
415+
}
416+
// 10s is required to synchronize the data(`DescribeParamsEvent`).
417+
time.Sleep(10 * time.Second)
418+
}
419+
384420
return resourceTencentCloudPostgresqlInstanceRead(d, meta)
385421
}
386422

@@ -536,6 +572,34 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
536572
}
537573
d.SetPartial("tags")
538574
}
575+
paramEntrys := make(map[string]string)
576+
if d.HasChange("max_standby_archive_delay") {
577+
if v, ok := d.GetOkExists("max_standby_archive_delay"); ok {
578+
paramEntrys["max_standby_archive_delay"] = strconv.Itoa(v.(int))
579+
}
580+
}
581+
if d.HasChange("max_standby_streaming_delay") {
582+
if v, ok := d.GetOkExists("max_standby_streaming_delay"); ok {
583+
paramEntrys["max_standby_streaming_delay"] = strconv.Itoa(v.(int))
584+
}
585+
}
586+
if len(paramEntrys) != 0 {
587+
outErr = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
588+
inErr := postgresqlService.ModifyPgParams(ctx, instanceId, paramEntrys)
589+
if inErr != nil {
590+
return retryError(inErr)
591+
}
592+
return nil
593+
})
594+
if outErr != nil {
595+
return outErr
596+
}
597+
for key := range paramEntrys {
598+
d.SetPartial(key)
599+
}
600+
// 10s is required to synchronize the data(`DescribeParamsEvent`).
601+
time.Sleep(10 * time.Second)
602+
}
539603

540604
d.Partial(false)
541605

@@ -650,6 +714,33 @@ func resourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta int
650714
}
651715
_ = d.Set("tags", tags)
652716

717+
// pg params
718+
var parmas map[string]string
719+
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
720+
parmas, inErr = postgresqlService.DescribePgParams(ctx, d.Id())
721+
if inErr != nil {
722+
ee, ok := inErr.(*sdkErrors.TencentCloudSDKError)
723+
if ok && ee.GetCode() == "ResourceNotFound.InstanceNotFoundError" {
724+
return nil
725+
}
726+
return retryError(inErr)
727+
}
728+
return nil
729+
})
730+
if err != nil {
731+
return err
732+
}
733+
maxStandbyStreamingDelayValue, err := strconv.Atoi(parmas["max_standby_streaming_delay"])
734+
if err != nil {
735+
return err
736+
}
737+
maxStandbyArchiveDelayValue, err := strconv.Atoi(parmas["max_standby_archive_delay"])
738+
if err != nil {
739+
return err
740+
}
741+
_ = d.Set("max_standby_streaming_delay", maxStandbyStreamingDelayValue)
742+
_ = d.Set("max_standby_archive_delay", maxStandbyArchiveDelayValue)
743+
653744
return nil
654745
}
655746

tencentcloud/service_tencentcloud_postgresql.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,76 @@ func (me *PostgresqlService) DescribeRootUser(ctx context.Context, instanceId st
454454

455455
return accounts, errRet
456456
}
457+
458+
func (me *PostgresqlService) ModifyPgParams(ctx context.Context, instanceId string, paramEntrys map[string]string) (err error) {
459+
logId := getLogId(ctx)
460+
request := postgresql.NewModifyDBInstanceParametersRequest()
461+
request.DBInstanceId = &instanceId
462+
request.ParamList = make([]*postgresql.ParamEntry, 0)
463+
464+
for key, value := range paramEntrys {
465+
request.ParamList = append(request.ParamList, &postgresql.ParamEntry{
466+
Name: helper.String(key),
467+
ExpectedValue: helper.String(value),
468+
})
469+
}
470+
_, err = me.client.UsePostgresqlClient().ModifyDBInstanceParameters(request)
471+
472+
if err != nil {
473+
log.Printf("[CRITAL]%s modify pgInstance parameter failed, reason: %v", logId, err)
474+
return err
475+
}
476+
return nil
477+
}
478+
479+
func (me *PostgresqlService) DescribePgParams(ctx context.Context, instanceId string) (params map[string]string, err error) {
480+
logId := getLogId(ctx)
481+
stateCheckRequest := postgresql.NewDescribeParamsEventRequest()
482+
stateCheckRequest.DBInstanceId = &instanceId
483+
484+
// wait for param effective
485+
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
486+
stateCheckResponse, inErr := me.client.UsePostgresqlClient().DescribeParamsEvent(stateCheckRequest)
487+
if inErr != nil {
488+
ee, ok := inErr.(*sdkErrors.TencentCloudSDKError)
489+
if ok && ee.GetCode() == "ResourceNotFound.InstanceNotFoundError" {
490+
return nil
491+
}
492+
return retryError(inErr)
493+
}
494+
495+
for _, eventItem := range stateCheckResponse.Response.EventItems {
496+
eventDetail := eventItem.EventDetail
497+
if eventDetail == nil {
498+
continue
499+
}
500+
for _, eventDetailItem := range eventDetail {
501+
if *(eventDetailItem.State) != "success" {
502+
return resource.RetryableError(
503+
fmt.Errorf("params is updating!"))
504+
}
505+
}
506+
}
507+
return nil
508+
})
509+
if err != nil {
510+
return
511+
}
512+
513+
request := postgresql.NewDescribeDBInstanceParametersRequest()
514+
request.DBInstanceId = &instanceId
515+
516+
response, err := me.client.UsePostgresqlClient().DescribeDBInstanceParameters(request)
517+
518+
if err != nil {
519+
log.Printf("[CRITAL]%s fetch pg instance parameter failed, reason: %v", logId, err)
520+
return
521+
}
522+
detail := response.Response.Detail
523+
params = make(map[string]string)
524+
for _, item := range detail {
525+
params[*item.Name] = *item.CurrentValue
526+
}
527+
528+
return
529+
}

vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http/request.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)