|
| 1 | +/* |
| 2 | +Provides a resource to create a CLB customized config. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_clb_customized_config" "foo" { |
| 8 | + config_content = "client_max_body_size 224M;" |
| 9 | + config_name = "helloWorld" |
| 10 | + load_balancer_ids = [ |
| 11 | + "${tencentcloud_clb_instance.internal_clb.id}", |
| 12 | + "${tencentcloud_clb_instance.internal_clb2.id}", |
| 13 | + ] |
| 14 | +} |
| 15 | +``` |
| 16 | +Import |
| 17 | +
|
| 18 | +CLB customized config can be imported using the id, e.g. |
| 19 | +
|
| 20 | +``` |
| 21 | +$ terraform import tencentcloud_clb_customized_config.foo pz-diowqstq |
| 22 | +``` |
| 23 | +*/ |
| 24 | +package tencentcloud |
| 25 | + |
| 26 | +import ( |
| 27 | + "context" |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 30 | + "github.com/pkg/errors" |
| 31 | + clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317" |
| 32 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 33 | + "log" |
| 34 | +) |
| 35 | + |
| 36 | +func resourceTencentCloudClbCustomizedConfig() *schema.Resource { |
| 37 | + return &schema.Resource{ |
| 38 | + Create: resourceTencentCloudClbCustomizedConfigCreate, |
| 39 | + Read: resourceTencentCloudClbCustomizedConfigRead, |
| 40 | + Update: resourceTencentCloudClbCustomizedConfigUpdate, |
| 41 | + Delete: resourceTencentCloudClbCustomizedConfigDelete, |
| 42 | + Importer: &schema.ResourceImporter{ |
| 43 | + State: schema.ImportStatePassthrough, |
| 44 | + }, |
| 45 | + |
| 46 | + Schema: map[string]*schema.Schema{ |
| 47 | + "config_name": { |
| 48 | + Type: schema.TypeString, |
| 49 | + Required: true, |
| 50 | + Description: "Name of Customized Config.", |
| 51 | + }, |
| 52 | + "config_content": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Required: true, |
| 55 | + ValidateFunc: validateStringLengthInRange(1, 60), |
| 56 | + Description: "Content of Customized Config.", |
| 57 | + }, |
| 58 | + "load_balancer_ids": { |
| 59 | + Type: schema.TypeSet, |
| 60 | + Optional: true, |
| 61 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 62 | + Description: "List of LoadBalancer Ids.", |
| 63 | + }, |
| 64 | + //computed |
| 65 | + "create_time": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "Create time of Customized Config.", |
| 69 | + }, |
| 70 | + "update_time": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "Update time of Customized Config.", |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func resourceTencentCloudClbCustomizedConfigCreate(d *schema.ResourceData, meta interface{}) error { |
| 80 | + defer logElapsed("resource.tencentcloud_clb_customized_config.create")() |
| 81 | + logId := getLogId(contextNil) |
| 82 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 83 | + |
| 84 | + configName := d.Get("config_name").(string) |
| 85 | + configContent := d.Get("config_content").(string) |
| 86 | + |
| 87 | + request := clb.NewSetCustomizedConfigForLoadBalancerRequest() |
| 88 | + request.OperationType = helper.String("ADD") |
| 89 | + request.ConfigName = helper.String(configName) |
| 90 | + request.ConfigContent = helper.String(configContent) |
| 91 | + |
| 92 | + var response *clb.SetCustomizedConfigForLoadBalancerResponse |
| 93 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 94 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseClbClient().SetCustomizedConfigForLoadBalancer(request) |
| 95 | + if e != nil { |
| 96 | + return retryError(e) |
| 97 | + } else { |
| 98 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 99 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 100 | + requestId := *result.Response.RequestId |
| 101 | + retryErr := waitForTaskFinish(requestId, meta.(*TencentCloudClient).apiV3Conn.UseClbClient()) |
| 102 | + if retryErr != nil { |
| 103 | + return retryError(errors.WithStack(retryErr)) |
| 104 | + } |
| 105 | + } |
| 106 | + response = result |
| 107 | + return nil |
| 108 | + }) |
| 109 | + if err != nil { |
| 110 | + log.Printf("[CRITAL]%s Create CLB Customized Config Failed, reason:%+v", logId, err) |
| 111 | + return err |
| 112 | + } |
| 113 | + d.SetId(*response.Response.ConfigId) |
| 114 | + |
| 115 | + if v, ok := d.GetOk("load_balancer_ids"); ok { |
| 116 | + loadBalancerIds := v.(*schema.Set).List() |
| 117 | + clbService := ClbService{ |
| 118 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 119 | + } |
| 120 | + err := clbService.BindOrUnBindCustomizedConfigWithLbId(ctx, |
| 121 | + "BIND", *response.Response.ConfigId, loadBalancerIds) |
| 122 | + if err != nil { |
| 123 | + log.Printf("[CRITAL]%s Binding LB Customized Config Failed, reason:%+v", logId, err) |
| 124 | + return err |
| 125 | + } |
| 126 | + } |
| 127 | + return resourceTencentCloudClbCustomizedConfigRead(d, meta) |
| 128 | +} |
| 129 | + |
| 130 | +func resourceTencentCloudClbCustomizedConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 131 | + defer logElapsed("resource.tencentcloud_clb_customized_config.read")() |
| 132 | + defer inconsistentCheck(d, meta)() |
| 133 | + |
| 134 | + logId := getLogId(contextNil) |
| 135 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 136 | + |
| 137 | + configId := d.Id() |
| 138 | + clbService := ClbService{ |
| 139 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 140 | + } |
| 141 | + var config *clb.ConfigListItem |
| 142 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 143 | + result, e := clbService.DescribeLbCustomizedConfigById(ctx, configId) |
| 144 | + if e != nil { |
| 145 | + return retryError(e) |
| 146 | + } |
| 147 | + config = result |
| 148 | + return nil |
| 149 | + }) |
| 150 | + if err != nil { |
| 151 | + log.Printf("[CRITAL]%s read CLB customized config failed, reason:%+v", logId, err) |
| 152 | + return err |
| 153 | + } |
| 154 | + if config == nil { |
| 155 | + d.SetId("") |
| 156 | + return nil |
| 157 | + } |
| 158 | + |
| 159 | + _ = d.Set("config_name", config.ConfigName) |
| 160 | + _ = d.Set("config_content", config.ConfigContent) |
| 161 | + _ = d.Set("create_time", config.CreateTimestamp) |
| 162 | + _ = d.Set("update_time", config.UpdateTimestamp) |
| 163 | + |
| 164 | + request := clb.NewDescribeCustomizedConfigAssociateListRequest() |
| 165 | + request.UconfigId = &configId |
| 166 | + var response *clb.DescribeCustomizedConfigAssociateListResponse |
| 167 | + assErr := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 168 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseClbClient().DescribeCustomizedConfigAssociateList(request) |
| 169 | + if e != nil { |
| 170 | + return retryError(e) |
| 171 | + } else { |
| 172 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 173 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 174 | + } |
| 175 | + response = result |
| 176 | + return nil |
| 177 | + }) |
| 178 | + if assErr != nil { |
| 179 | + log.Printf("[CRITAL]%s Describe CLB Customized Config Associate List Failed, reason:%+v", logId, assErr) |
| 180 | + return err |
| 181 | + } |
| 182 | + _ = d.Set("load_balancer_ids", extractBindClbList(response.Response.BindList)) |
| 183 | + |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +func resourceTencentCloudClbCustomizedConfigUpdate(d *schema.ResourceData, meta interface{}) error { |
| 188 | + defer logElapsed("resource.tencentcloud_clb_customized_config.update")() |
| 189 | + |
| 190 | + logId := getLogId(contextNil) |
| 191 | + |
| 192 | + d.Partial(true) |
| 193 | + |
| 194 | + configId := d.Id() |
| 195 | + request := clb.NewSetCustomizedConfigForLoadBalancerRequest() |
| 196 | + request.UconfigId = &configId |
| 197 | + request.OperationType = helper.String("UPDATE") |
| 198 | + |
| 199 | + if d.HasChange("config_name") { |
| 200 | + configName := d.Get("config_name").(string) |
| 201 | + request.ConfigName = &configName |
| 202 | + } |
| 203 | + |
| 204 | + if d.HasChange("config_content") { |
| 205 | + configContent := d.Get("config_content").(string) |
| 206 | + request.ConfigContent = &configContent |
| 207 | + } |
| 208 | + |
| 209 | + var response *clb.SetCustomizedConfigForLoadBalancerResponse |
| 210 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 211 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseClbClient().SetCustomizedConfigForLoadBalancer(request) |
| 212 | + if e != nil { |
| 213 | + return retryError(e) |
| 214 | + } else { |
| 215 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 216 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 217 | + requestId := *result.Response.RequestId |
| 218 | + retryErr := waitForTaskFinish(requestId, meta.(*TencentCloudClient).apiV3Conn.UseClbClient()) |
| 219 | + if retryErr != nil { |
| 220 | + return retryError(errors.WithStack(retryErr)) |
| 221 | + } |
| 222 | + } |
| 223 | + response = result |
| 224 | + return nil |
| 225 | + }) |
| 226 | + if err != nil { |
| 227 | + log.Printf("[CRITAL]%s Update CLB Customized Config Failed, reason:%+v", logId, err) |
| 228 | + return err |
| 229 | + } |
| 230 | + |
| 231 | + if d.HasChange("load_balancer_ids") { |
| 232 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 233 | + clbService := ClbService{ |
| 234 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 235 | + } |
| 236 | + old, new := d.GetChange("load_balancer_ids") |
| 237 | + olds := old.(*schema.Set) |
| 238 | + news := new.(*schema.Set) |
| 239 | + add := news.Difference(olds).List() |
| 240 | + remove := olds.Difference(news).List() |
| 241 | + if len(remove) > 0 { |
| 242 | + err := clbService.BindOrUnBindCustomizedConfigWithLbId(ctx, |
| 243 | + "UNBIND", configId, remove) |
| 244 | + if err != nil { |
| 245 | + log.Printf("[CRITAL]%s UnBinding LB Customized Config Failed, reason:%+v", logId, err) |
| 246 | + return err |
| 247 | + } |
| 248 | + } |
| 249 | + if len(add) > 0 { |
| 250 | + err := clbService.BindOrUnBindCustomizedConfigWithLbId(ctx, |
| 251 | + "BIND", configId, add) |
| 252 | + if err != nil { |
| 253 | + log.Printf("[CRITAL]%s Binding LB Customized Config Failed, reason:%+v", logId, err) |
| 254 | + return err |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | + return nil |
| 259 | +} |
| 260 | + |
| 261 | +func resourceTencentCloudClbCustomizedConfigDelete(d *schema.ResourceData, meta interface{}) error { |
| 262 | + defer logElapsed("resource.tencentcloud_clb_customized_config.delete")() |
| 263 | + |
| 264 | + logId := getLogId(contextNil) |
| 265 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 266 | + |
| 267 | + configId := d.Id() |
| 268 | + clbService := ClbService{ |
| 269 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 270 | + } |
| 271 | + |
| 272 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 273 | + e := clbService.DeleteLbCustomizedConfigById(ctx, configId) |
| 274 | + if e != nil { |
| 275 | + return retryError(e) |
| 276 | + } |
| 277 | + return nil |
| 278 | + }) |
| 279 | + if err != nil { |
| 280 | + log.Printf("[CRITAL]%s delete CLB customized config failed, reason:%+v", logId, err) |
| 281 | + return err |
| 282 | + } |
| 283 | + |
| 284 | + return nil |
| 285 | +} |
0 commit comments