|
| 1 | +package tencentcloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 6 | + "log" |
| 7 | + |
| 8 | + postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceTencentCloudPostgresqlInstanceHAConfig() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceTencentCloudPostgresqlInstanceHAConfigCreate, |
| 17 | + Read: resourceTencentCloudPostgresqlInstanceHAConfigRead, |
| 18 | + Update: resourceTencentCloudPostgresqlInstanceHAConfigUpdate, |
| 19 | + Delete: resourceTencentCloudPostgresqlInstanceHAConfigDelete, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + State: schema.ImportStatePassthrough, |
| 22 | + }, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "instance_id": { |
| 25 | + Required: true, |
| 26 | + Type: schema.TypeString, |
| 27 | + Description: "instance id.", |
| 28 | + }, |
| 29 | + "sync_mode": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + ValidateFunc: validateAllowedStringValue(SYNC_MODE), |
| 33 | + Description: "Master slave synchronization method, Semi-sync: Semi synchronous; Async: Asynchronous. Main instance default value: Semi-sync, Read-only instance default value: Async.", |
| 34 | + }, |
| 35 | + "max_standby_latency": { |
| 36 | + Type: schema.TypeInt, |
| 37 | + Required: true, |
| 38 | + ValidateFunc: validateIntegerInRange(1073741824, 322122547200), |
| 39 | + Description: "Maximum latency data volume for highly available backup machines. When the delay data amount of the backup node is less than or equal to this value, and the delay time of the backup node is less than or equal to MaxStandbyLag, it can switch to the main node. Unit: byte; Parameter range: [1073741824, 322122547200].", |
| 40 | + }, |
| 41 | + "max_standby_lag": { |
| 42 | + Type: schema.TypeInt, |
| 43 | + Required: true, |
| 44 | + ValidateFunc: validateIntegerInRange(5, 10), |
| 45 | + Description: "Maximum latency of highly available backup machines. When the delay time of the backup node is less than or equal to this value, and the amount of delay data of the backup node is less than or equal to MaxStandbyLatency, the primary node can be switched. Unit: s; Parameter range: [5, 10].", |
| 46 | + }, |
| 47 | + "max_sync_standby_latency": { |
| 48 | + Type: schema.TypeInt, |
| 49 | + Optional: true, |
| 50 | + Description: "Maximum latency data for synchronous backup. When the amount of data delayed by the backup machine is less than or equal to this value, and the delay time of the backup machine is less than or equal to MaxSyncStandbyLag, then the backup machine adopts synchronous replication; Otherwise, adopt asynchronous replication. This parameter value is valid for instances where SyncMode is set to Semi sync. When a semi synchronous instance prohibits degradation to asynchronous replication, MaxSyncStandbyLatency and MaxSyncStandbyLag are not set. When semi synchronous instances allow degenerate asynchronous replication, PostgreSQL version 9 instances must have MaxSyncStandbyLatency set and MaxSyncStandbyLag not set, while PostgreSQL version 10 and above instances must have MaxSyncStandbyLatency and MaxSyncStandbyLag set.", |
| 51 | + }, |
| 52 | + "max_sync_standby_lag": { |
| 53 | + Type: schema.TypeInt, |
| 54 | + Optional: true, |
| 55 | + Description: "Maximum delay time for synchronous backup. When the delay time of the standby machine is less than or equal to this value, and the amount of delay data of the standby machine is less than or equal to MaxSyncStandbyLatency, then the standby machine adopts synchronous replication; Otherwise, adopt asynchronous replication. This parameter value is valid for instances where SyncMode is set to Semi sync. When a semi synchronous instance prohibits degradation to asynchronous replication, MaxSyncStandbyLatency and MaxSyncStandbyLag are not set. When semi synchronous instances allow degenerate asynchronous replication, PostgreSQL version 9 instances must have MaxSyncStandbyLatency set and MaxSyncStandbyLag not set, while PostgreSQL version 10 and above instances must have MaxSyncStandbyLatency and MaxSyncStandbyLag set.", |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func resourceTencentCloudPostgresqlInstanceHAConfigCreate(d *schema.ResourceData, meta interface{}) error { |
| 62 | + defer logElapsed("resource.tencentcloud_postgresql_instance_ha_config.create")() |
| 63 | + defer inconsistentCheck(d, meta)() |
| 64 | + |
| 65 | + var instanceId string |
| 66 | + |
| 67 | + if v, ok := d.GetOk("instance_id"); ok { |
| 68 | + instanceId = v.(string) |
| 69 | + } |
| 70 | + |
| 71 | + d.SetId(instanceId) |
| 72 | + |
| 73 | + return resourceTencentCloudPostgresqlInstanceHAConfigUpdate(d, meta) |
| 74 | +} |
| 75 | + |
| 76 | +func resourceTencentCloudPostgresqlInstanceHAConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 77 | + defer logElapsed("resource.tencentcloud_postgresql_instance_ha_config.read")() |
| 78 | + defer inconsistentCheck(d, meta)() |
| 79 | + |
| 80 | + var ( |
| 81 | + logId = getLogId(contextNil) |
| 82 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 83 | + service = PostgresqlService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 84 | + instanceId = d.Id() |
| 85 | + ) |
| 86 | + |
| 87 | + haConfig, err := service.DescribePostgresqlInstanceHAConfigById(ctx, instanceId) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + if haConfig == nil { |
| 93 | + d.SetId("") |
| 94 | + log.Printf("[WARN]%s resource `PostgresqlInstanceHAConfig` [%s] not found.\n", logId, d.Id()) |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + _ = d.Set("instance_id", instanceId) |
| 99 | + |
| 100 | + if haConfig.SyncMode != nil { |
| 101 | + _ = d.Set("sync_mode", haConfig.SyncMode) |
| 102 | + } |
| 103 | + |
| 104 | + if haConfig.MaxStandbyLatency != nil { |
| 105 | + _ = d.Set("max_standby_latency", haConfig.MaxStandbyLatency) |
| 106 | + } |
| 107 | + |
| 108 | + if haConfig.MaxStandbyLag != nil { |
| 109 | + _ = d.Set("max_standby_lag", haConfig.MaxStandbyLag) |
| 110 | + } |
| 111 | + |
| 112 | + if haConfig.MaxSyncStandbyLatency != nil { |
| 113 | + _ = d.Set("max_sync_standby_latency", haConfig.MaxSyncStandbyLatency) |
| 114 | + } |
| 115 | + |
| 116 | + if haConfig.MaxSyncStandbyLag != nil { |
| 117 | + _ = d.Set("max_sync_standby_lag", haConfig.MaxSyncStandbyLag) |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func resourceTencentCloudPostgresqlInstanceHAConfigUpdate(d *schema.ResourceData, meta interface{}) error { |
| 124 | + defer logElapsed("resource.tencentcloud_postgresql_instance_ha_config.update")() |
| 125 | + defer inconsistentCheck(d, meta)() |
| 126 | + |
| 127 | + var ( |
| 128 | + logId = getLogId(contextNil) |
| 129 | + request = postgresql.NewModifyDBInstanceHAConfigRequest() |
| 130 | + instanceId = d.Id() |
| 131 | + syncMode = d.Get("sync_mode").(string) |
| 132 | + maxStandbyLatency = d.Get("max_standby_latency").(int) |
| 133 | + maxStandbyLag = d.Get("max_standby_lag").(int) |
| 134 | + ) |
| 135 | + |
| 136 | + request.DBInstanceId = &instanceId |
| 137 | + request.SyncMode = &syncMode |
| 138 | + request.MaxStandbyLatency = helper.IntUint64(maxStandbyLatency) |
| 139 | + request.MaxStandbyLag = helper.IntUint64(maxStandbyLag) |
| 140 | + if v, ok := d.GetOkExists("max_sync_standby_latency"); ok { |
| 141 | + request.MaxSyncStandbyLatency = helper.IntUint64(v.(int)) |
| 142 | + } |
| 143 | + |
| 144 | + if v, ok := d.GetOkExists("max_sync_standby_lag"); ok { |
| 145 | + request.MaxSyncStandbyLag = helper.IntUint64(v.(int)) |
| 146 | + } |
| 147 | + |
| 148 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 149 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UsePostgresqlClient().ModifyDBInstanceHAConfig(request) |
| 150 | + if e != nil { |
| 151 | + return retryError(e) |
| 152 | + } else { |
| 153 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 154 | + } |
| 155 | + |
| 156 | + return nil |
| 157 | + }) |
| 158 | + |
| 159 | + if err != nil { |
| 160 | + log.Printf("[CRITAL]%s operate postgresql ModifyDBInstanceHAConfig failed, reason:%+v", logId, err) |
| 161 | + return err |
| 162 | + } |
| 163 | + |
| 164 | + return resourceTencentCloudPostgresqlInstanceHAConfigRead(d, meta) |
| 165 | +} |
| 166 | + |
| 167 | +func resourceTencentCloudPostgresqlInstanceHAConfigDelete(d *schema.ResourceData, meta interface{}) error { |
| 168 | + defer logElapsed("resource.tencentcloud_postgresql_instance_ha_config.delete")() |
| 169 | + defer inconsistentCheck(d, meta)() |
| 170 | + |
| 171 | + return nil |
| 172 | +} |
0 commit comments