|
| 1 | +package clb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/pkg/errors" |
| 9 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 10 | + |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 15 | + clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317" |
| 16 | +) |
| 17 | + |
| 18 | +func ResourceTencentCloudClbCustomizedConfigAttachment() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + Create: resourceTencentCloudClbCustomizedConfigAttachmentCreate, |
| 21 | + Read: resourceTencentCloudClbCustomizedConfigAttachmentRead, |
| 22 | + Update: resourceTencentCloudClbCustomizedConfigAttachmentUpdate, |
| 23 | + Delete: resourceTencentCloudClbCustomizedConfigAttachmentDelete, |
| 24 | + Importer: &schema.ResourceImporter{ |
| 25 | + State: schema.ImportStatePassthrough, |
| 26 | + }, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "config_id": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + ForceNew: true, |
| 32 | + Description: "ID of Customized Config.", |
| 33 | + }, |
| 34 | + "bind_list": { |
| 35 | + Type: schema.TypeSet, |
| 36 | + Required: true, |
| 37 | + Description: "Associated server or location.", |
| 38 | + Elem: &schema.Resource{ |
| 39 | + Schema: map[string]*schema.Schema{ |
| 40 | + "load_balancer_id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + Description: "Clb ID.", |
| 44 | + }, |
| 45 | + "listener_id": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + Description: "Listener ID.", |
| 49 | + }, |
| 50 | + "domain": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Required: true, |
| 53 | + Description: "Domain.", |
| 54 | + }, |
| 55 | + "location_id": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Optional: true, |
| 58 | + Description: "Location ID.", |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func resourceTencentCloudClbCustomizedConfigAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 68 | + defer tccommon.LogElapsed("resource.tencentcloud_clb_customized_config_attachment.create")() |
| 69 | + |
| 70 | + var ( |
| 71 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 72 | + request = clb.NewAssociateCustomizedConfigRequest() |
| 73 | + configId string |
| 74 | + ) |
| 75 | + |
| 76 | + if v, ok := d.GetOk("config_id"); ok { |
| 77 | + request.UconfigId = helper.String(v.(string)) |
| 78 | + configId = v.(string) |
| 79 | + } |
| 80 | + |
| 81 | + if v, ok := d.GetOk("bind_list"); ok { |
| 82 | + for _, item := range v.(*schema.Set).List() { |
| 83 | + bindItem := clb.BindItem{} |
| 84 | + dMap := item.(map[string]interface{}) |
| 85 | + if v, ok := dMap["load_balancer_id"]; ok { |
| 86 | + bindItem.LoadBalancerId = helper.String(v.(string)) |
| 87 | + } |
| 88 | + |
| 89 | + if v, ok := dMap["listener_id"]; ok { |
| 90 | + bindItem.ListenerId = helper.String(v.(string)) |
| 91 | + } |
| 92 | + |
| 93 | + if v, ok := dMap["domain"]; ok { |
| 94 | + bindItem.Domain = helper.String(v.(string)) |
| 95 | + } |
| 96 | + |
| 97 | + if v, ok := dMap["location_id"]; ok { |
| 98 | + bindItem.LocationId = helper.String(v.(string)) |
| 99 | + } |
| 100 | + |
| 101 | + request.BindList = append(request.BindList, &bindItem) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 106 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().AssociateCustomizedConfig(request) |
| 107 | + if e != nil { |
| 108 | + return tccommon.RetryError(e) |
| 109 | + } else { |
| 110 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 111 | + if result == nil || result.Response == nil || result.Response.RequestId == nil { |
| 112 | + return resource.NonRetryableError(fmt.Errorf("Associate CLB Customized Config Failed, Response is nil.")) |
| 113 | + } |
| 114 | + |
| 115 | + requestId := *result.Response.RequestId |
| 116 | + retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient()) |
| 117 | + if retryErr != nil { |
| 118 | + return tccommon.RetryError(errors.WithStack(retryErr)) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return nil |
| 123 | + }) |
| 124 | + |
| 125 | + if err != nil { |
| 126 | + log.Printf("[CRITAL]%s Associate CLB Customized Config Failed, reason:%+v", logId, err) |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + d.SetId(configId) |
| 131 | + |
| 132 | + return resourceTencentCloudClbCustomizedConfigAttachmentRead(d, meta) |
| 133 | +} |
| 134 | + |
| 135 | +func resourceTencentCloudClbCustomizedConfigAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 136 | + defer tccommon.LogElapsed("resource.tencentcloud_clb_customized_config_attachment.read")() |
| 137 | + |
| 138 | + var ( |
| 139 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 140 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 141 | + clbService = ClbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 142 | + configId = d.Id() |
| 143 | + ) |
| 144 | + |
| 145 | + bindList, err := clbService.DescribeDescribeCustomizedConfigAssociateListById(ctx, configId) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + if bindList == nil || len(bindList) == 0 { |
| 151 | + d.SetId("") |
| 152 | + return fmt.Errorf("resource `tencentcloud_clb_customized_config_attachment` %s does not exist", configId) |
| 153 | + } |
| 154 | + |
| 155 | + _ = d.Set("config_id", configId) |
| 156 | + |
| 157 | + tmpList := make([]map[string]interface{}, 0, len(bindList)) |
| 158 | + for _, item := range bindList { |
| 159 | + dMap := make(map[string]interface{}) |
| 160 | + if item.LoadBalancerId != nil { |
| 161 | + dMap["load_balancer_id"] = *item.LoadBalancerId |
| 162 | + } |
| 163 | + |
| 164 | + if item.ListenerId != nil { |
| 165 | + dMap["listener_id"] = *item.ListenerId |
| 166 | + } |
| 167 | + |
| 168 | + if item.Domain != nil { |
| 169 | + dMap["domain"] = *item.Domain |
| 170 | + } |
| 171 | + |
| 172 | + if item.LocationId != nil { |
| 173 | + dMap["location_id"] = *item.LocationId |
| 174 | + } |
| 175 | + |
| 176 | + tmpList = append(tmpList, dMap) |
| 177 | + } |
| 178 | + |
| 179 | + _ = d.Set("bind_list", tmpList) |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +func resourceTencentCloudClbCustomizedConfigAttachmentUpdate(d *schema.ResourceData, meta interface{}) error { |
| 185 | + defer tccommon.LogElapsed("resource.tencentcloud_clb_customized_config_attachment.delete")() |
| 186 | + |
| 187 | + var ( |
| 188 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 189 | + id = d.Id() |
| 190 | + ) |
| 191 | + |
| 192 | + if d.HasChange("bind_list") { |
| 193 | + oldInterface, newInterface := d.GetChange("bind_list") |
| 194 | + olds := oldInterface.(*schema.Set) |
| 195 | + news := newInterface.(*schema.Set) |
| 196 | + remove := olds.Difference(news).List() |
| 197 | + add := news.Difference(olds).List() |
| 198 | + if len(remove) > 0 { |
| 199 | + request := clb.NewDisassociateCustomizedConfigRequest() |
| 200 | + for _, item := range remove { |
| 201 | + bindItem := clb.BindItem{} |
| 202 | + dMap := item.(map[string]interface{}) |
| 203 | + if v, ok := dMap["load_balancer_id"]; ok { |
| 204 | + bindItem.LoadBalancerId = helper.String(v.(string)) |
| 205 | + } |
| 206 | + |
| 207 | + if v, ok := dMap["listener_id"]; ok { |
| 208 | + bindItem.ListenerId = helper.String(v.(string)) |
| 209 | + } |
| 210 | + |
| 211 | + if v, ok := dMap["domain"]; ok { |
| 212 | + bindItem.Domain = helper.String(v.(string)) |
| 213 | + } |
| 214 | + |
| 215 | + if v, ok := dMap["location_id"]; ok { |
| 216 | + bindItem.LocationId = helper.String(v.(string)) |
| 217 | + } |
| 218 | + |
| 219 | + request.BindList = append(request.BindList, &bindItem) |
| 220 | + } |
| 221 | + |
| 222 | + request.UconfigId = helper.String(id) |
| 223 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 224 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().DisassociateCustomizedConfig(request) |
| 225 | + if e != nil { |
| 226 | + return tccommon.RetryError(e) |
| 227 | + } else { |
| 228 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 229 | + if result == nil || result.Response == nil || result.Response.RequestId == nil { |
| 230 | + return resource.NonRetryableError(fmt.Errorf("Disassociate CLB Customized Config Failed, Response is nil.")) |
| 231 | + } |
| 232 | + |
| 233 | + requestId := *result.Response.RequestId |
| 234 | + retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient()) |
| 235 | + if retryErr != nil { |
| 236 | + return tccommon.RetryError(errors.WithStack(retryErr)) |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + return nil |
| 241 | + }) |
| 242 | + |
| 243 | + if err != nil { |
| 244 | + log.Printf("[CRITAL]%s Disassociate CLB Customized Config Failed, reason:%+v", logId, err) |
| 245 | + return err |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + if len(add) > 0 { |
| 250 | + request := clb.NewAssociateCustomizedConfigRequest() |
| 251 | + request.UconfigId = helper.String(id) |
| 252 | + for _, item := range add { |
| 253 | + bindItem := clb.BindItem{} |
| 254 | + dMap := item.(map[string]interface{}) |
| 255 | + if v, ok := dMap["load_balancer_id"]; ok { |
| 256 | + bindItem.LoadBalancerId = helper.String(v.(string)) |
| 257 | + } |
| 258 | + |
| 259 | + if v, ok := dMap["listener_id"]; ok { |
| 260 | + bindItem.ListenerId = helper.String(v.(string)) |
| 261 | + } |
| 262 | + |
| 263 | + if v, ok := dMap["domain"]; ok { |
| 264 | + bindItem.Domain = helper.String(v.(string)) |
| 265 | + } |
| 266 | + |
| 267 | + if v, ok := dMap["location_id"]; ok { |
| 268 | + bindItem.LocationId = helper.String(v.(string)) |
| 269 | + } |
| 270 | + |
| 271 | + request.BindList = append(request.BindList, &bindItem) |
| 272 | + } |
| 273 | + |
| 274 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 275 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().AssociateCustomizedConfig(request) |
| 276 | + if e != nil { |
| 277 | + return tccommon.RetryError(e) |
| 278 | + } else { |
| 279 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 280 | + if result == nil || result.Response == nil || result.Response.RequestId == nil { |
| 281 | + return resource.NonRetryableError(fmt.Errorf("Associate CLB Customized Config Failed, Response is nil.")) |
| 282 | + } |
| 283 | + |
| 284 | + requestId := *result.Response.RequestId |
| 285 | + retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient()) |
| 286 | + if retryErr != nil { |
| 287 | + return tccommon.RetryError(errors.WithStack(retryErr)) |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + return nil |
| 292 | + }) |
| 293 | + |
| 294 | + if err != nil { |
| 295 | + log.Printf("[CRITAL]%s Associate CLB Customized Config Failed, reason:%+v", logId, err) |
| 296 | + return err |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + return resourceTencentCloudClbCustomizedConfigAttachmentRead(d, meta) |
| 302 | +} |
| 303 | + |
| 304 | +func resourceTencentCloudClbCustomizedConfigAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 305 | + defer tccommon.LogElapsed("resource.tencentcloud_clb_customized_config_attachment.delete")() |
| 306 | + |
| 307 | + var ( |
| 308 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 309 | + request = clb.NewDisassociateCustomizedConfigRequest() |
| 310 | + id = d.Id() |
| 311 | + ) |
| 312 | + |
| 313 | + request.UconfigId = helper.String(id) |
| 314 | + if v, ok := d.GetOk("bind_list"); ok { |
| 315 | + for _, item := range v.(*schema.Set).List() { |
| 316 | + bindItem := clb.BindItem{} |
| 317 | + dMap := item.(map[string]interface{}) |
| 318 | + if v, ok := dMap["load_balancer_id"]; ok { |
| 319 | + bindItem.LoadBalancerId = helper.String(v.(string)) |
| 320 | + } |
| 321 | + |
| 322 | + if v, ok := dMap["listener_id"]; ok { |
| 323 | + bindItem.ListenerId = helper.String(v.(string)) |
| 324 | + } |
| 325 | + |
| 326 | + if v, ok := dMap["domain"]; ok { |
| 327 | + bindItem.Domain = helper.String(v.(string)) |
| 328 | + } |
| 329 | + |
| 330 | + if v, ok := dMap["location_id"]; ok { |
| 331 | + bindItem.LocationId = helper.String(v.(string)) |
| 332 | + } |
| 333 | + |
| 334 | + request.BindList = append(request.BindList, &bindItem) |
| 335 | + } |
| 336 | + } |
| 337 | + |
| 338 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 339 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().DisassociateCustomizedConfig(request) |
| 340 | + if e != nil { |
| 341 | + return tccommon.RetryError(e) |
| 342 | + } else { |
| 343 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 344 | + if result == nil || result.Response == nil || result.Response.RequestId == nil { |
| 345 | + return resource.NonRetryableError(fmt.Errorf("Disassociate CLB Customized Config Failed, Response is nil.")) |
| 346 | + } |
| 347 | + |
| 348 | + requestId := *result.Response.RequestId |
| 349 | + retryErr := waitForTaskFinish(requestId, meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient()) |
| 350 | + if retryErr != nil { |
| 351 | + return tccommon.RetryError(errors.WithStack(retryErr)) |
| 352 | + } |
| 353 | + } |
| 354 | + |
| 355 | + return nil |
| 356 | + }) |
| 357 | + |
| 358 | + if err != nil { |
| 359 | + log.Printf("[CRITAL]%s Disassociate CLB Customized Config Failed, reason:%+v", logId, err) |
| 360 | + return err |
| 361 | + } |
| 362 | + |
| 363 | + return nil |
| 364 | +} |
0 commit comments