|
| 1 | +/* |
| 2 | +Provides a resource to create a tcm cluster_attachment |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_tcm_cluster_attachment" "cluster_attachment" { |
| 8 | + mesh_id = "mesh-b9q6vf9l" |
| 9 | + cluster_list { |
| 10 | + cluster_id = "cls-rc5uy6dy" |
| 11 | + region = "ap-guangzhou" |
| 12 | + role = "REMOTE" |
| 13 | + vpc_id = "vpc-a1jycmbx" |
| 14 | + subnet_id = "subnet-lkyb3ayc" |
| 15 | + type = "TKE" |
| 16 | + } |
| 17 | +} |
| 18 | +
|
| 19 | +``` |
| 20 | +Import |
| 21 | +
|
| 22 | +tcm cluster_attachment can be imported using the mesh_id#cluster_id, e.g. |
| 23 | +``` |
| 24 | +$ terraform import tencentcloud_tcm_cluster_attachment.cluster_attachment mesh-b9q6vf9l#cls-rc5uy6dy |
| 25 | +``` |
| 26 | +*/ |
| 27 | +package tencentcloud |
| 28 | + |
| 29 | +import ( |
| 30 | + "context" |
| 31 | + "fmt" |
| 32 | + "log" |
| 33 | + "strings" |
| 34 | + |
| 35 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 36 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 37 | + tcm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcm/v20210413" |
| 38 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 39 | +) |
| 40 | + |
| 41 | +func resourceTencentCloudTcmClusterAttachment() *schema.Resource { |
| 42 | + return &schema.Resource{ |
| 43 | + Read: resourceTencentCloudTcmClusterAttachmentRead, |
| 44 | + Create: resourceTencentCloudTcmClusterAttachmentCreate, |
| 45 | + Delete: resourceTencentCloudTcmClusterAttachmentDelete, |
| 46 | + Importer: &schema.ResourceImporter{ |
| 47 | + State: schema.ImportStatePassthrough, |
| 48 | + }, |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "mesh_id": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Required: true, |
| 53 | + ForceNew: true, |
| 54 | + Description: "Mesh ID.", |
| 55 | + }, |
| 56 | + |
| 57 | + "cluster_list": { |
| 58 | + Type: schema.TypeList, |
| 59 | + Optional: true, |
| 60 | + ForceNew: true, |
| 61 | + Description: "Cluster list.", |
| 62 | + Elem: &schema.Resource{ |
| 63 | + Schema: map[string]*schema.Schema{ |
| 64 | + "cluster_id": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Required: true, |
| 67 | + Description: "TKE Cluster id.", |
| 68 | + }, |
| 69 | + "region": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Required: true, |
| 72 | + Description: "TKE cluster region.", |
| 73 | + }, |
| 74 | + "role": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Required: true, |
| 77 | + Description: "Cluster role in mesh, REMOTE or MASTER.", |
| 78 | + }, |
| 79 | + "vpc_id": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Required: true, |
| 82 | + Description: "Cluster's VpcId.", |
| 83 | + }, |
| 84 | + "subnet_id": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Optional: true, |
| 87 | + Description: "Subnet id, only needed if it's standalone mesh.", |
| 88 | + }, |
| 89 | + "type": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Required: true, |
| 92 | + Description: "Cluster type.", |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func resourceTencentCloudTcmClusterAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 102 | + defer logElapsed("resource.tencentcloud_tcm_cluster_attachment.create")() |
| 103 | + defer inconsistentCheck(d, meta)() |
| 104 | + |
| 105 | + var ( |
| 106 | + logId = getLogId(contextNil) |
| 107 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 108 | + request = tcm.NewLinkClusterListRequest() |
| 109 | + meshId string |
| 110 | + clusterId string |
| 111 | + ) |
| 112 | + |
| 113 | + if v, ok := d.GetOk("mesh_id"); ok { |
| 114 | + meshId = v.(string) |
| 115 | + request.MeshId = helper.String(v.(string)) |
| 116 | + } |
| 117 | + |
| 118 | + if v, ok := d.GetOk("cluster_list"); ok { |
| 119 | + for _, item := range v.([]interface{}) { |
| 120 | + dMap := item.(map[string]interface{}) |
| 121 | + cluster := tcm.Cluster{} |
| 122 | + if v, ok := dMap["cluster_id"]; ok { |
| 123 | + clusterId = v.(string) |
| 124 | + cluster.ClusterId = helper.String(v.(string)) |
| 125 | + } |
| 126 | + if v, ok := dMap["region"]; ok { |
| 127 | + cluster.Region = helper.String(v.(string)) |
| 128 | + } |
| 129 | + if v, ok := dMap["role"]; ok { |
| 130 | + cluster.Role = helper.String(v.(string)) |
| 131 | + } |
| 132 | + if v, ok := dMap["vpc_id"]; ok { |
| 133 | + cluster.VpcId = helper.String(v.(string)) |
| 134 | + } |
| 135 | + if v, ok := dMap["subnet_id"]; ok { |
| 136 | + cluster.SubnetId = helper.String(v.(string)) |
| 137 | + } |
| 138 | + if v, ok := dMap["type"]; ok { |
| 139 | + cluster.Type = helper.String(v.(string)) |
| 140 | + } |
| 141 | + |
| 142 | + request.ClusterList = append(request.ClusterList, &cluster) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 147 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseTcmClient().LinkClusterList(request) |
| 148 | + if e != nil { |
| 149 | + return retryError(e) |
| 150 | + } else { |
| 151 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 152 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 153 | + } |
| 154 | + return nil |
| 155 | + }) |
| 156 | + |
| 157 | + if err != nil { |
| 158 | + log.Printf("[CRITAL]%s create tcm clusterAttachment failed, reason:%+v", logId, err) |
| 159 | + return err |
| 160 | + } |
| 161 | + |
| 162 | + service := TcmService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 163 | + err = resource.Retry(3*readRetryTimeout, func() *resource.RetryError { |
| 164 | + mesh, errRet := service.DescribeTcmMesh(ctx, meshId) |
| 165 | + if errRet != nil { |
| 166 | + return retryError(errRet, InternalError) |
| 167 | + } |
| 168 | + clusterList := mesh.Mesh.ClusterList |
| 169 | + if len(clusterList) < 1 { |
| 170 | + return resource.RetryableError(fmt.Errorf("link is being created, retry...")) |
| 171 | + } |
| 172 | + var linkState string |
| 173 | + for _, v := range clusterList { |
| 174 | + if *v.ClusterId != clusterId { |
| 175 | + continue |
| 176 | + } |
| 177 | + linkState = *v.Status.LinkState |
| 178 | + if linkState == "LINKED" { |
| 179 | + return nil |
| 180 | + } |
| 181 | + if linkState == "LINK_FAILED" { |
| 182 | + return resource.NonRetryableError(fmt.Errorf("link status is %v, operate failed.", linkState)) |
| 183 | + } |
| 184 | + } |
| 185 | + return resource.RetryableError(fmt.Errorf("link status is %v, retry...", linkState)) |
| 186 | + }) |
| 187 | + if err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + |
| 191 | + d.SetId(strings.Join([]string{meshId, clusterId}, FILED_SP)) |
| 192 | + return resourceTencentCloudTcmClusterAttachmentRead(d, meta) |
| 193 | +} |
| 194 | + |
| 195 | +func resourceTencentCloudTcmClusterAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 196 | + defer logElapsed("resource.tencentcloud_tcm_cluster_attachment.read")() |
| 197 | + defer inconsistentCheck(d, meta)() |
| 198 | + |
| 199 | + logId := getLogId(contextNil) |
| 200 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 201 | + |
| 202 | + service := TcmService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 203 | + |
| 204 | + ids := strings.Split(d.Id(), FILED_SP) |
| 205 | + if len(ids) != 2 { |
| 206 | + return fmt.Errorf("id is broken, id is %s", d.Id()) |
| 207 | + } |
| 208 | + |
| 209 | + meshId := ids[0] |
| 210 | + clusterId := ids[1] |
| 211 | + |
| 212 | + mesh, err := service.DescribeTcmMesh(ctx, meshId) |
| 213 | + |
| 214 | + if err != nil { |
| 215 | + return err |
| 216 | + } |
| 217 | + |
| 218 | + if mesh == nil || mesh.Mesh == nil || len(mesh.Mesh.ClusterList) < 1 { |
| 219 | + d.SetId("") |
| 220 | + return fmt.Errorf("resource `clusterAttachment` %s does not exist", meshId) |
| 221 | + } |
| 222 | + |
| 223 | + _ = d.Set("mesh_id", meshId) |
| 224 | + |
| 225 | + if len(mesh.Mesh.ClusterList) > 0 { |
| 226 | + clusterAttachment := mesh.Mesh |
| 227 | + clusterListList := []interface{}{} |
| 228 | + for _, clusterList := range clusterAttachment.ClusterList { |
| 229 | + if *clusterList.ClusterId != clusterId { |
| 230 | + continue |
| 231 | + } |
| 232 | + clusterListMap := map[string]interface{}{} |
| 233 | + if clusterList.ClusterId != nil { |
| 234 | + clusterListMap["cluster_id"] = clusterList.ClusterId |
| 235 | + } |
| 236 | + if clusterList.Region != nil { |
| 237 | + clusterListMap["region"] = clusterList.Region |
| 238 | + } |
| 239 | + if clusterList.Role != nil { |
| 240 | + clusterListMap["role"] = clusterList.Role |
| 241 | + } |
| 242 | + if clusterList.VpcId != nil { |
| 243 | + clusterListMap["vpc_id"] = clusterList.VpcId |
| 244 | + } |
| 245 | + if clusterList.SubnetId != nil { |
| 246 | + clusterListMap["subnet_id"] = clusterList.SubnetId |
| 247 | + } |
| 248 | + if clusterList.Type != nil { |
| 249 | + clusterListMap["type"] = clusterList.Type |
| 250 | + } |
| 251 | + |
| 252 | + clusterListList = append(clusterListList, clusterListMap) |
| 253 | + } |
| 254 | + _ = d.Set("cluster_list", clusterListList) |
| 255 | + } |
| 256 | + |
| 257 | + return nil |
| 258 | +} |
| 259 | + |
| 260 | +func resourceTencentCloudTcmClusterAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 261 | + defer logElapsed("resource.tencentcloud_tcm_cluster_attachment.delete")() |
| 262 | + defer inconsistentCheck(d, meta)() |
| 263 | + |
| 264 | + logId := getLogId(contextNil) |
| 265 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 266 | + |
| 267 | + service := TcmService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 268 | + |
| 269 | + ids := strings.Split(d.Id(), FILED_SP) |
| 270 | + if len(ids) != 2 { |
| 271 | + return fmt.Errorf("id is broken, id is %s", d.Id()) |
| 272 | + } |
| 273 | + |
| 274 | + meshId := ids[0] |
| 275 | + clusterId := ids[1] |
| 276 | + |
| 277 | + if err := service.DeleteTcmClusterAttachmentById(ctx, meshId, clusterId); err != nil { |
| 278 | + return err |
| 279 | + } |
| 280 | + |
| 281 | + err := resource.Retry(3*readRetryTimeout, func() *resource.RetryError { |
| 282 | + mesh, errRet := service.DescribeTcmMesh(ctx, meshId) |
| 283 | + if errRet != nil { |
| 284 | + return retryError(errRet, InternalError) |
| 285 | + } |
| 286 | + clusterList := mesh.Mesh.ClusterList |
| 287 | + if len(clusterList) < 1 { |
| 288 | + return nil |
| 289 | + } |
| 290 | + var linkState string |
| 291 | + for _, v := range clusterList { |
| 292 | + if *v.ClusterId != clusterId { |
| 293 | + continue |
| 294 | + } |
| 295 | + linkState = *v.Status.LinkState |
| 296 | + if linkState == "UNLINK_FAILED" { |
| 297 | + return resource.NonRetryableError(fmt.Errorf("link status is %v, operate failed.", linkState)) |
| 298 | + } |
| 299 | + } |
| 300 | + return resource.RetryableError(fmt.Errorf("link status is %v, retry...", linkState)) |
| 301 | + }) |
| 302 | + if err != nil { |
| 303 | + return err |
| 304 | + } |
| 305 | + |
| 306 | + return nil |
| 307 | +} |
0 commit comments