|
| 1 | +package postgresql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + postgresqlv20170312 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312" |
| 13 | + |
| 14 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 15 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 16 | +) |
| 17 | + |
| 18 | +func ResourceTencentCloudPostgresqlInstanceNetworkAccess() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + Create: resourceTencentCloudPostgresqlInstanceNetworkAccessCreate, |
| 21 | + Read: resourceTencentCloudPostgresqlInstanceNetworkAccessRead, |
| 22 | + Delete: resourceTencentCloudPostgresqlInstanceNetworkAccessDelete, |
| 23 | + Importer: &schema.ResourceImporter{ |
| 24 | + State: schema.ImportStatePassthrough, |
| 25 | + }, |
| 26 | + Schema: map[string]*schema.Schema{ |
| 27 | + "db_instance_id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + Description: "Instance ID in the format of postgres-6bwgamo3.", |
| 32 | + }, |
| 33 | + |
| 34 | + "vpc_id": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + ForceNew: true, |
| 38 | + Description: "Unified VPC ID.", |
| 39 | + }, |
| 40 | + |
| 41 | + "subnet_id": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Required: true, |
| 44 | + ForceNew: true, |
| 45 | + Description: "Subnet ID.", |
| 46 | + }, |
| 47 | + |
| 48 | + "vip": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + Computed: true, |
| 52 | + ForceNew: true, |
| 53 | + Description: "Target VIP.", |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func resourceTencentCloudPostgresqlInstanceNetworkAccessCreate(d *schema.ResourceData, meta interface{}) error { |
| 60 | + defer tccommon.LogElapsed("resource.tencentcloud_postgresql_instance_network_access.create")() |
| 61 | + defer tccommon.InconsistentCheck(d, meta)() |
| 62 | + |
| 63 | + var ( |
| 64 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 65 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 66 | + request = postgresqlv20170312.NewCreateDBInstanceNetworkAccessRequest() |
| 67 | + response = postgresqlv20170312.NewCreateDBInstanceNetworkAccessResponse() |
| 68 | + dbInsntaceId string |
| 69 | + vpcId string |
| 70 | + subnetId string |
| 71 | + vip string |
| 72 | + ) |
| 73 | + |
| 74 | + if v, ok := d.GetOk("db_instance_id"); ok { |
| 75 | + request.DBInstanceId = helper.String(v.(string)) |
| 76 | + dbInsntaceId = v.(string) |
| 77 | + } |
| 78 | + |
| 79 | + if v, ok := d.GetOk("vpc_id"); ok { |
| 80 | + request.VpcId = helper.String(v.(string)) |
| 81 | + vpcId = v.(string) |
| 82 | + } |
| 83 | + |
| 84 | + if v, ok := d.GetOk("subnet_id"); ok { |
| 85 | + request.SubnetId = helper.String(v.(string)) |
| 86 | + subnetId = v.(string) |
| 87 | + } |
| 88 | + |
| 89 | + request.IsAssignVip = helper.Bool(false) |
| 90 | + |
| 91 | + if v, ok := d.GetOk("vip"); ok { |
| 92 | + request.Vip = helper.String(v.(string)) |
| 93 | + request.IsAssignVip = helper.Bool(true) |
| 94 | + vip = v.(string) |
| 95 | + } |
| 96 | + |
| 97 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 98 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlV20170312Client().CreateDBInstanceNetworkAccessWithContext(ctx, request) |
| 99 | + if e != nil { |
| 100 | + return tccommon.RetryError(e) |
| 101 | + } else { |
| 102 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 103 | + } |
| 104 | + |
| 105 | + if result == nil || result.Response == nil { |
| 106 | + return resource.NonRetryableError(fmt.Errorf("Create postgresql instance network access failed, Response is nil.")) |
| 107 | + } |
| 108 | + |
| 109 | + response = result |
| 110 | + return nil |
| 111 | + }) |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + log.Printf("[CRITAL]%s create postgresql instance network access failed, reason:%+v", logId, err) |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + if response.Response.FlowId == nil { |
| 119 | + return fmt.Errorf("FlowId is nil.") |
| 120 | + } |
| 121 | + |
| 122 | + // wait & get vip |
| 123 | + flowId := *response.Response.FlowId |
| 124 | + flowRequest := postgresqlv20170312.NewDescribeTasksRequest() |
| 125 | + flowRequest.TaskId = helper.Int64Uint64(flowId) |
| 126 | + err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 127 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlV20170312Client().DescribeTasksWithContext(ctx, flowRequest) |
| 128 | + if e != nil { |
| 129 | + return tccommon.RetryError(e) |
| 130 | + } else { |
| 131 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 132 | + } |
| 133 | + |
| 134 | + if result == nil || result.Response == nil || result.Response.TaskSet == nil { |
| 135 | + return resource.NonRetryableError(fmt.Errorf("Describe tasks failed, Response is nil.")) |
| 136 | + } |
| 137 | + |
| 138 | + if len(result.Response.TaskSet) == 0 { |
| 139 | + return resource.RetryableError(fmt.Errorf("wait TaskSet init.")) |
| 140 | + } |
| 141 | + |
| 142 | + if result.Response.TaskSet[0].Status != nil && *result.Response.TaskSet[0].Status == "Success" { |
| 143 | + if result.Response.TaskSet[0].TaskDetail != nil && result.Response.TaskSet[0].TaskDetail.Output != nil { |
| 144 | + outPutObj := make(map[string]interface{}) |
| 145 | + outputStr := *result.Response.TaskSet[0].TaskDetail.Output |
| 146 | + e := json.Unmarshal([]byte(outputStr), &outPutObj) |
| 147 | + if e != nil { |
| 148 | + return resource.NonRetryableError(fmt.Errorf("Json unmarshall output error: %s.", e.Error())) |
| 149 | + } |
| 150 | + |
| 151 | + dBInstanceNetInfo := outPutObj["DBInstanceNetInfo"].(map[string]interface{}) |
| 152 | + vip = dBInstanceNetInfo["Ip"].(string) |
| 153 | + return nil |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return resource.RetryableError(fmt.Errorf("postgresql instance network access is running, status is %s.", *result.Response.TaskSet[0].Status)) |
| 158 | + }) |
| 159 | + |
| 160 | + if err != nil { |
| 161 | + log.Printf("[CRITAL]%s create postgresql instance network access failed, reason:%+v", logId, err) |
| 162 | + return err |
| 163 | + } |
| 164 | + |
| 165 | + d.SetId(strings.Join([]string{dbInsntaceId, vpcId, subnetId, vip}, tccommon.FILED_SP)) |
| 166 | + |
| 167 | + return resourceTencentCloudPostgresqlInstanceNetworkAccessRead(d, meta) |
| 168 | +} |
| 169 | + |
| 170 | +func resourceTencentCloudPostgresqlInstanceNetworkAccessRead(d *schema.ResourceData, meta interface{}) error { |
| 171 | + defer tccommon.LogElapsed("resource.tencentcloud_postgresql_instance_network_access.read")() |
| 172 | + defer tccommon.InconsistentCheck(d, meta)() |
| 173 | + |
| 174 | + var ( |
| 175 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 176 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 177 | + service = PostgresqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 178 | + ) |
| 179 | + |
| 180 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 181 | + if len(idSplit) != 4 { |
| 182 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 183 | + } |
| 184 | + |
| 185 | + dbInsntaceId := idSplit[0] |
| 186 | + vpcId := idSplit[1] |
| 187 | + subnetId := idSplit[2] |
| 188 | + vip := idSplit[3] |
| 189 | + |
| 190 | + _ = d.Set("db_instance_id", dbInsntaceId) |
| 191 | + |
| 192 | + _ = d.Set("vpc_id", vpcId) |
| 193 | + |
| 194 | + _ = d.Set("subnet_id", subnetId) |
| 195 | + |
| 196 | + respData, err := service.DescribePostgresqlInstanceNetworkAccessById(ctx, dbInsntaceId) |
| 197 | + if err != nil { |
| 198 | + return err |
| 199 | + } |
| 200 | + |
| 201 | + if respData == nil { |
| 202 | + d.SetId("") |
| 203 | + log.Printf("[WARN]%s resource `postgresql_instance_network_access` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 204 | + return nil |
| 205 | + } |
| 206 | + |
| 207 | + var checkFlag bool |
| 208 | + if respData.DBInstanceNetInfo != nil && len(respData.DBInstanceNetInfo) > 0 { |
| 209 | + for _, item := range respData.DBInstanceNetInfo { |
| 210 | + if *item.Ip == vip { |
| 211 | + _ = d.Set("vip", item.Ip) |
| 212 | + checkFlag = true |
| 213 | + break |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + if checkFlag == false { |
| 219 | + return fmt.Errorf("Not found vip %s, please check if it has been deleted.", vip) |
| 220 | + } |
| 221 | + |
| 222 | + return nil |
| 223 | +} |
| 224 | + |
| 225 | +func resourceTencentCloudPostgresqlInstanceNetworkAccessDelete(d *schema.ResourceData, meta interface{}) error { |
| 226 | + defer tccommon.LogElapsed("resource.tencentcloud_postgresql_instance_network_access.delete")() |
| 227 | + defer tccommon.InconsistentCheck(d, meta)() |
| 228 | + |
| 229 | + var ( |
| 230 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 231 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 232 | + request = postgresqlv20170312.NewDeleteDBInstanceNetworkAccessRequest() |
| 233 | + response = postgresqlv20170312.NewDeleteDBInstanceNetworkAccessResponse() |
| 234 | + ) |
| 235 | + |
| 236 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 237 | + if len(idSplit) != 4 { |
| 238 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 239 | + } |
| 240 | + |
| 241 | + dbInsntaceId := idSplit[0] |
| 242 | + vpcId := idSplit[1] |
| 243 | + subnetId := idSplit[2] |
| 244 | + vip := idSplit[3] |
| 245 | + |
| 246 | + request.DBInstanceId = helper.String(dbInsntaceId) |
| 247 | + request.VpcId = helper.String(vpcId) |
| 248 | + request.SubnetId = helper.String(subnetId) |
| 249 | + request.Vip = helper.String(vip) |
| 250 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 251 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlV20170312Client().DeleteDBInstanceNetworkAccessWithContext(ctx, request) |
| 252 | + if e != nil { |
| 253 | + return tccommon.RetryError(e) |
| 254 | + } else { |
| 255 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 256 | + } |
| 257 | + |
| 258 | + if result == nil || result.Response == nil { |
| 259 | + return resource.NonRetryableError(fmt.Errorf("Delete postgresql instance network access failed, Response is nil.")) |
| 260 | + } |
| 261 | + |
| 262 | + response = result |
| 263 | + return nil |
| 264 | + }) |
| 265 | + |
| 266 | + if err != nil { |
| 267 | + log.Printf("[CRITAL]%s delete postgresql instance network access failed, reason:%+v", logId, err) |
| 268 | + return err |
| 269 | + } |
| 270 | + |
| 271 | + // wait |
| 272 | + flowId := *response.Response.FlowId |
| 273 | + flowRequest := postgresqlv20170312.NewDescribeTasksRequest() |
| 274 | + flowRequest.TaskId = helper.Int64Uint64(flowId) |
| 275 | + err = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 276 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UsePostgresqlV20170312Client().DescribeTasksWithContext(ctx, flowRequest) |
| 277 | + if e != nil { |
| 278 | + return tccommon.RetryError(e) |
| 279 | + } else { |
| 280 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 281 | + } |
| 282 | + |
| 283 | + if result == nil || result.Response == nil || result.Response.TaskSet == nil { |
| 284 | + return resource.NonRetryableError(fmt.Errorf("Describe tasks failed, Response is nil.")) |
| 285 | + } |
| 286 | + |
| 287 | + if len(result.Response.TaskSet) == 0 { |
| 288 | + return resource.RetryableError(fmt.Errorf("wait TaskSet init.")) |
| 289 | + } |
| 290 | + |
| 291 | + if result.Response.TaskSet[0].Status != nil && *result.Response.TaskSet[0].Status == "Success" { |
| 292 | + return nil |
| 293 | + } |
| 294 | + |
| 295 | + return resource.RetryableError(fmt.Errorf("postgresql instance network access is running, status is %s.", *result.Response.TaskSet[0].Status)) |
| 296 | + }) |
| 297 | + |
| 298 | + if err != nil { |
| 299 | + log.Printf("[CRITAL]%s delete postgresql instance network access failed, reason:%+v", logId, err) |
| 300 | + return err |
| 301 | + } |
| 302 | + |
| 303 | + return nil |
| 304 | +} |
0 commit comments