|
| 1 | +package clb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + clbv20180317 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317" |
| 12 | + |
| 13 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 14 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 15 | +) |
| 16 | + |
| 17 | +func ResourceTencentCloudClbClsLogAttachment() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceTencentCloudClbClsLogAttachmentCreate, |
| 20 | + Read: resourceTencentCloudClbClsLogAttachmentRead, |
| 21 | + Delete: resourceTencentCloudClbClsLogAttachmentDelete, |
| 22 | + Importer: &schema.ResourceImporter{ |
| 23 | + State: schema.ImportStatePassthrough, |
| 24 | + }, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "load_balancer_id": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + Description: "CLB instance ID.", |
| 31 | + }, |
| 32 | + |
| 33 | + "log_set_id": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Required: true, |
| 36 | + ForceNew: true, |
| 37 | + Description: "Logset ID of the Cloud Log Service (CLS).<li>When adding or updating a log topic, call the [DescribeLogsets](https://intl.cloud.tencent.com/document/product/614/58624?from_cn_redirect=1) API to obtain the logset ID.</li><li>When deleting a log topic, set this parameter to null.</li>.", |
| 38 | + }, |
| 39 | + |
| 40 | + "log_topic_id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + ForceNew: true, |
| 44 | + Description: "Log topic ID of the CLS.<li>When adding or updating a log topic, call the [DescribeTopics](https://intl.cloud.tencent.com/document/product/614/56454?from_cn_redirect=1) API to obtain the log topic ID.</li><li>When deleting a log topic, set this parameter to null.</li>.", |
| 45 | + }, |
| 46 | + |
| 47 | + // "log_type": { |
| 48 | + // Type: schema.TypeString, |
| 49 | + // Optional: true, |
| 50 | + // ForceNew: true, |
| 51 | + // Computed: true, |
| 52 | + // Description: "Log type:\n<li>`ACCESS`: access logs</li>\n<li>`HEALTH`: health check logs</li>\nDefault: `ACCESS`.", |
| 53 | + // }, |
| 54 | + }, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func resourceTencentCloudClbClsLogAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 59 | + defer tccommon.LogElapsed("resource.tencentcloud_clb_cls_log_attachment.create")() |
| 60 | + defer tccommon.InconsistentCheck(d, meta)() |
| 61 | + |
| 62 | + var ( |
| 63 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 64 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 65 | + request = clbv20180317.NewSetLoadBalancerClsLogRequest() |
| 66 | + loadBalancerId string |
| 67 | + logSetId string |
| 68 | + logTopicId string |
| 69 | + ) |
| 70 | + |
| 71 | + if v, ok := d.GetOk("load_balancer_id"); ok { |
| 72 | + request.LoadBalancerId = helper.String(v.(string)) |
| 73 | + loadBalancerId = v.(string) |
| 74 | + } |
| 75 | + |
| 76 | + if v, ok := d.GetOk("log_set_id"); ok { |
| 77 | + request.LogSetId = helper.String(v.(string)) |
| 78 | + logSetId = v.(string) |
| 79 | + } |
| 80 | + |
| 81 | + if v, ok := d.GetOk("log_topic_id"); ok { |
| 82 | + request.LogTopicId = helper.String(v.(string)) |
| 83 | + logTopicId = v.(string) |
| 84 | + } |
| 85 | + |
| 86 | + // if v, ok := d.GetOk("log_type"); ok { |
| 87 | + // request.LogType = helper.String(v.(string)) |
| 88 | + // } |
| 89 | + |
| 90 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 91 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().SetLoadBalancerClsLogWithContext(ctx, request) |
| 92 | + if e != nil { |
| 93 | + return tccommon.RetryError(e) |
| 94 | + } else { |
| 95 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | + }) |
| 100 | + |
| 101 | + if reqErr != nil { |
| 102 | + log.Printf("[CRITAL]%s create clb cls log attachment failed, reason:%+v", logId, reqErr) |
| 103 | + return reqErr |
| 104 | + } |
| 105 | + |
| 106 | + d.SetId(strings.Join([]string{loadBalancerId, logSetId, logTopicId}, tccommon.FILED_SP)) |
| 107 | + return resourceTencentCloudClbClsLogAttachmentRead(d, meta) |
| 108 | +} |
| 109 | + |
| 110 | +func resourceTencentCloudClbClsLogAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 111 | + defer tccommon.LogElapsed("resource.tencentcloud_clb_cls_log_attachment.read")() |
| 112 | + defer tccommon.InconsistentCheck(d, meta)() |
| 113 | + |
| 114 | + var ( |
| 115 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 116 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 117 | + service = ClbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 118 | + ) |
| 119 | + |
| 120 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 121 | + if len(idSplit) != 3 { |
| 122 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 123 | + } |
| 124 | + |
| 125 | + loadBalancerId := idSplit[0] |
| 126 | + logSetId := idSplit[1] |
| 127 | + logTopicId := idSplit[2] |
| 128 | + |
| 129 | + respData, err := service.DescribeClbClsLogAttachmentById(ctx, loadBalancerId, logSetId, logTopicId) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + if respData == nil { |
| 135 | + log.Printf("[WARN]%s resource `tencentcloud_clb_cls_log_attachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 136 | + d.SetId("") |
| 137 | + return nil |
| 138 | + } |
| 139 | + |
| 140 | + if respData.LoadBalancerId != nil { |
| 141 | + _ = d.Set("load_balancer_id", respData.LoadBalancerId) |
| 142 | + } |
| 143 | + |
| 144 | + if respData.LogSetId != nil && respData.LogTopicId != nil { |
| 145 | + if *respData.LogSetId == logSetId && *respData.LogTopicId == logTopicId { |
| 146 | + _ = d.Set("log_set_id", respData.LogSetId) |
| 147 | + _ = d.Set("log_topic_id", respData.LogTopicId) |
| 148 | + // _ = d.Set("log_type", "ACCESS") |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // if respData.HealthLogSetId != nil && respData.HealthLogTopicId != nil { |
| 153 | + // if *respData.HealthLogSetId == logSetId && *respData.HealthLogTopicId == logTopicId { |
| 154 | + // _ = d.Set("log_set_id", respData.HealthLogSetId) |
| 155 | + // _ = d.Set("log_topic_id", respData.HealthLogTopicId) |
| 156 | + // _ = d.Set("log_type", "HEALTH") |
| 157 | + // } |
| 158 | + // } |
| 159 | + |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +func resourceTencentCloudClbClsLogAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 164 | + defer tccommon.LogElapsed("resource.tencentcloud_clb_cls_log_attachment.delete")() |
| 165 | + defer tccommon.InconsistentCheck(d, meta)() |
| 166 | + |
| 167 | + var ( |
| 168 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 169 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 170 | + request = clbv20180317.NewSetLoadBalancerClsLogRequest() |
| 171 | + ) |
| 172 | + |
| 173 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 174 | + if len(idSplit) != 3 { |
| 175 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 176 | + } |
| 177 | + |
| 178 | + loadBalancerId := idSplit[0] |
| 179 | + |
| 180 | + request.LoadBalancerId = &loadBalancerId |
| 181 | + request.LogSetId = helper.String("") |
| 182 | + request.LogTopicId = helper.String("") |
| 183 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 184 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().SetLoadBalancerClsLogWithContext(ctx, request) |
| 185 | + if e != nil { |
| 186 | + return tccommon.RetryError(e) |
| 187 | + } else { |
| 188 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 189 | + } |
| 190 | + |
| 191 | + return nil |
| 192 | + }) |
| 193 | + |
| 194 | + if reqErr != nil { |
| 195 | + log.Printf("[CRITAL]%s delete clb cls log attachment failed, reason:%+v", logId, reqErr) |
| 196 | + return reqErr |
| 197 | + } |
| 198 | + |
| 199 | + return nil |
| 200 | +} |
0 commit comments