|
| 1 | +/* |
| 2 | +Provides a resource to create a mysql deploy_group |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_mysql_deploy_group" "deploy_group" { |
| 8 | + deploy_group_name = "terrform-deploy" |
| 9 | + description = "deploy test" |
| 10 | + limit_num = 1 |
| 11 | + dev_class = ["TS85"] |
| 12 | +} |
| 13 | +``` |
| 14 | +
|
| 15 | +Import |
| 16 | +
|
| 17 | +mysql deploy_group can be imported using the id, e.g. |
| 18 | +
|
| 19 | +``` |
| 20 | +terraform import tencentcloud_mysql_deploy_group.deploy_group deploy_group_id |
| 21 | +``` |
| 22 | +*/ |
| 23 | +package tencentcloud |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "fmt" |
| 28 | + "log" |
| 29 | + |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 32 | + mysql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320" |
| 33 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 34 | +) |
| 35 | + |
| 36 | +func resourceTencentCloudMysqlDeployGroup() *schema.Resource { |
| 37 | + return &schema.Resource{ |
| 38 | + Create: resourceTencentCloudMysqlDeployGroupCreate, |
| 39 | + Read: resourceTencentCloudMysqlDeployGroupRead, |
| 40 | + Update: resourceTencentCloudMysqlDeployGroupUpdate, |
| 41 | + Delete: resourceTencentCloudMysqlDeployGroupDelete, |
| 42 | + Importer: &schema.ResourceImporter{ |
| 43 | + State: schema.ImportStatePassthrough, |
| 44 | + }, |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "deploy_group_name": { |
| 47 | + Required: true, |
| 48 | + Type: schema.TypeString, |
| 49 | + Description: "The name of deploy group. the maximum length cannot exceed 60 characters.", |
| 50 | + }, |
| 51 | + |
| 52 | + "description": { |
| 53 | + Optional: true, |
| 54 | + Type: schema.TypeString, |
| 55 | + Description: "The description of deploy group. the maximum length cannot exceed 200 characters.", |
| 56 | + }, |
| 57 | + |
| 58 | + "limit_num": { |
| 59 | + Optional: true, |
| 60 | + Type: schema.TypeInt, |
| 61 | + Description: "The limit on the number of instances on the same physical machine in deploy group affinity policy 1.", |
| 62 | + }, |
| 63 | + |
| 64 | + "dev_class": { |
| 65 | + Optional: true, |
| 66 | + Type: schema.TypeSet, |
| 67 | + Elem: &schema.Schema{ |
| 68 | + Type: schema.TypeString, |
| 69 | + }, |
| 70 | + Description: "The device class of deploy group. optional value is SH12+SH02, TS85, etc.", |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func resourceTencentCloudMysqlDeployGroupCreate(d *schema.ResourceData, meta interface{}) error { |
| 77 | + defer logElapsed("resource.tencentcloud_mysql_deploy_group.create")() |
| 78 | + defer inconsistentCheck(d, meta)() |
| 79 | + |
| 80 | + logId := getLogId(contextNil) |
| 81 | + |
| 82 | + var ( |
| 83 | + request = mysql.NewCreateDeployGroupRequest() |
| 84 | + response = mysql.NewCreateDeployGroupResponse() |
| 85 | + deployGroupId string |
| 86 | + ) |
| 87 | + if v, ok := d.GetOk("deploy_group_name"); ok { |
| 88 | + request.DeployGroupName = helper.String(v.(string)) |
| 89 | + } |
| 90 | + |
| 91 | + if v, ok := d.GetOk("description"); ok { |
| 92 | + request.Description = helper.String(v.(string)) |
| 93 | + } |
| 94 | + |
| 95 | + if v, _ := d.GetOk("limit_num"); v != nil { |
| 96 | + request.LimitNum = helper.IntInt64(v.(int)) |
| 97 | + } |
| 98 | + |
| 99 | + if v, ok := d.GetOk("dev_class"); ok { |
| 100 | + devClassSet := v.(*schema.Set).List() |
| 101 | + for i := range devClassSet { |
| 102 | + devClass := devClassSet[i].(string) |
| 103 | + request.DevClass = append(request.DevClass, &devClass) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 108 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMysqlClient().CreateDeployGroup(request) |
| 109 | + if e != nil { |
| 110 | + return retryError(e) |
| 111 | + } else { |
| 112 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 113 | + } |
| 114 | + response = result |
| 115 | + return nil |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + log.Printf("[CRITAL]%s create mysql deployGroup failed, reason:%+v", logId, err) |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + deployGroupId = *response.Response.DeployGroupId |
| 123 | + d.SetId(deployGroupId) |
| 124 | + |
| 125 | + return resourceTencentCloudMysqlDeployGroupRead(d, meta) |
| 126 | +} |
| 127 | + |
| 128 | +func resourceTencentCloudMysqlDeployGroupRead(d *schema.ResourceData, meta interface{}) error { |
| 129 | + defer logElapsed("resource.tencentcloud_mysql_deploy_group.read")() |
| 130 | + defer inconsistentCheck(d, meta)() |
| 131 | + |
| 132 | + logId := getLogId(contextNil) |
| 133 | + |
| 134 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 135 | + |
| 136 | + service := MysqlService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 137 | + |
| 138 | + deployGroupId := d.Id() |
| 139 | + |
| 140 | + deployGroup, err := service.DescribeMysqlDeployGroupById(ctx, deployGroupId) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + if deployGroup == nil { |
| 146 | + d.SetId("") |
| 147 | + log.Printf("[WARN]%s resource `tencentcloud_mysql_deploy_group` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 148 | + return nil |
| 149 | + |
| 150 | + } |
| 151 | + if deployGroup.DeployGroupName != nil { |
| 152 | + _ = d.Set("deploy_group_name", deployGroup.DeployGroupName) |
| 153 | + } |
| 154 | + |
| 155 | + if deployGroup.Description != nil { |
| 156 | + _ = d.Set("description", deployGroup.Description) |
| 157 | + } |
| 158 | + |
| 159 | + if deployGroup.LimitNum != nil { |
| 160 | + _ = d.Set("limit_num", deployGroup.LimitNum) |
| 161 | + } |
| 162 | + |
| 163 | + if deployGroup.DevClass != nil { |
| 164 | + _ = d.Set("dev_class", []*string{deployGroup.DevClass}) |
| 165 | + } |
| 166 | + |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func resourceTencentCloudMysqlDeployGroupUpdate(d *schema.ResourceData, meta interface{}) error { |
| 171 | + defer logElapsed("resource.tencentcloud_mysql_deploy_group.update")() |
| 172 | + defer inconsistentCheck(d, meta)() |
| 173 | + |
| 174 | + logId := getLogId(contextNil) |
| 175 | + |
| 176 | + request := mysql.NewModifyNameOrDescByDpIdRequest() |
| 177 | + |
| 178 | + deployGroupId := d.Id() |
| 179 | + |
| 180 | + request.DeployGroupId = &deployGroupId |
| 181 | + |
| 182 | + immutableArgs := []string{"limit_num", "dev_class"} |
| 183 | + |
| 184 | + for _, v := range immutableArgs { |
| 185 | + if d.HasChange(v) { |
| 186 | + return fmt.Errorf("argument `%s` cannot be changed", v) |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if d.HasChange("deploy_group_name") { |
| 191 | + if v, ok := d.GetOk("deploy_group_name"); ok { |
| 192 | + request.DeployGroupName = helper.String(v.(string)) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + if d.HasChange("description") { |
| 197 | + if v, ok := d.GetOk("description"); ok { |
| 198 | + request.Description = helper.String(v.(string)) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 203 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMysqlClient().ModifyNameOrDescByDpId(request) |
| 204 | + if e != nil { |
| 205 | + return retryError(e) |
| 206 | + } else { |
| 207 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 208 | + } |
| 209 | + return nil |
| 210 | + }) |
| 211 | + if err != nil { |
| 212 | + log.Printf("[CRITAL]%s update mysql deployGroup failed, reason:%+v", logId, err) |
| 213 | + return err |
| 214 | + } |
| 215 | + |
| 216 | + return resourceTencentCloudMysqlDeployGroupRead(d, meta) |
| 217 | +} |
| 218 | + |
| 219 | +func resourceTencentCloudMysqlDeployGroupDelete(d *schema.ResourceData, meta interface{}) error { |
| 220 | + defer logElapsed("resource.tencentcloud_mysql_deploy_group.delete")() |
| 221 | + defer inconsistentCheck(d, meta)() |
| 222 | + |
| 223 | + logId := getLogId(contextNil) |
| 224 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 225 | + |
| 226 | + service := MysqlService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 227 | + deployGroupId := d.Id() |
| 228 | + |
| 229 | + if err := service.DeleteMysqlDeployGroupById(ctx, deployGroupId); err != nil { |
| 230 | + return err |
| 231 | + } |
| 232 | + |
| 233 | + return nil |
| 234 | +} |
0 commit comments