|
| 1 | +/* |
| 2 | +Use this resource to create postgresql readonly attachment. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_postgresql_readonly_attachment" "attach" { |
| 8 | + db_instance_id = tencentcloud_postgresql_readonly_instance.foo.id |
| 9 | + read_only_group_id = tencentcloud_postgresql_readonly_group.group.id |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "fmt" |
| 18 | + "log" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 23 | + postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312" |
| 24 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 25 | +) |
| 26 | + |
| 27 | +func resourceTencentCloudPostgresqlReadonlyAttachment() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + Create: resourceTencentCloudPostgresqlReadOnlyAttachmentCreate, |
| 30 | + Read: resourceTencentCloudPostgresqlReadOnlyAttachmentRead, |
| 31 | + //Update: resourceTencentCloudPostgresqlReadOnlyAttachmentUpdate, |
| 32 | + Delete: resourceTencentCLoudPostgresqlReadOnlyAttachmentDelete, |
| 33 | + //Importer: &schema.ResourceImporter{ |
| 34 | + // State: schema.ImportStatePassthrough, |
| 35 | + //}, |
| 36 | + |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "db_instance_id": { |
| 39 | + Type: schema.TypeString, |
| 40 | + ForceNew: true, |
| 41 | + Required: true, |
| 42 | + Description: "Read only instance ID.", |
| 43 | + }, |
| 44 | + "read_only_group_id": { |
| 45 | + Type: schema.TypeString, |
| 46 | + ForceNew: true, |
| 47 | + Required: true, |
| 48 | + Description: "Read only group ID.", |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func resourceTencentCloudPostgresqlReadOnlyAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 55 | + defer logElapsed("resource.tencentcloud_postgresql_readonly_attachment.create")() |
| 56 | + |
| 57 | + logId := getLogId(contextNil) |
| 58 | + |
| 59 | + var ( |
| 60 | + request = postgresql.NewAddDBInstanceToReadOnlyGroupRequest() |
| 61 | + dbInstanceId string |
| 62 | + groupId string |
| 63 | + ) |
| 64 | + if v, ok := d.GetOk("db_instance_id"); ok { |
| 65 | + dbInstanceId = v.(string) |
| 66 | + request.DBInstanceId = helper.String(dbInstanceId) |
| 67 | + |
| 68 | + } |
| 69 | + if v, ok := d.GetOk("read_only_group_id"); ok { |
| 70 | + groupId = v.(string) |
| 71 | + request.ReadOnlyGroupId = helper.String(groupId) |
| 72 | + } |
| 73 | + |
| 74 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 75 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UsePostgresqlClient().AddDBInstanceToReadOnlyGroup(request) |
| 76 | + if e != nil { |
| 77 | + return retryError(e) |
| 78 | + } else { |
| 79 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 80 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 81 | + } |
| 82 | + return nil |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + instanceId := helper.IdFormat(dbInstanceId, groupId) |
| 88 | + d.SetId(instanceId) |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func resourceTencentCloudPostgresqlReadOnlyAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 94 | + defer logElapsed("resource.tencentcloud_postgresql_readonly_attachment.read")() |
| 95 | + |
| 96 | + logId := getLogId(contextNil) |
| 97 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 98 | + |
| 99 | + postgresqlService := PostgresqlService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 100 | + _, err := postgresqlService.DescribePostgresqlReadOnlyGroupById(ctx, d.Id()) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func resourceTencentCLoudPostgresqlReadOnlyAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 109 | + defer logElapsed("resource.tencentcloud_postgresql_readonly_attachment.delete")() |
| 110 | + |
| 111 | + logId := getLogId(contextNil) |
| 112 | + request := postgresql.NewRemoveDBInstanceFromReadOnlyGroupRequest() |
| 113 | + |
| 114 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 115 | + if len(idSplit) != 2 { |
| 116 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 117 | + } |
| 118 | + dbInstanceId := idSplit[0] |
| 119 | + groupId := idSplit[1] |
| 120 | + request.ReadOnlyGroupId = helper.String(groupId) |
| 121 | + request.DBInstanceId = helper.String(dbInstanceId) |
| 122 | + |
| 123 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 124 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UsePostgresqlClient().RemoveDBInstanceFromReadOnlyGroup(request) |
| 125 | + if e != nil { |
| 126 | + return retryError(e) |
| 127 | + } else { |
| 128 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 129 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 130 | + } |
| 131 | + return nil |
| 132 | + }) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + return nil |
| 137 | +} |
0 commit comments