|
| 1 | +/* |
| 2 | +Provides a resource to create a tmp tke template attachment |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +
|
| 8 | +resource "tencentcloud_monitor_tmp_tke_template_attachment" "temp_attachment" { |
| 9 | + template_id = "temp-xxx" |
| 10 | +
|
| 11 | + targets { |
| 12 | + region = "ap-xxx" |
| 13 | + instance_id = "prom-xxx" |
| 14 | + } |
| 15 | +} |
| 16 | +
|
| 17 | +*/ |
| 18 | +package tencentcloud |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "log" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 28 | + tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525" |
| 29 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 30 | +) |
| 31 | + |
| 32 | +func resourceTencentCloudMonitorTmpTkeTemplateAttachment() *schema.Resource { |
| 33 | + return &schema.Resource{ |
| 34 | + Read: resourceTencentCloudMonitorTmpTkeTemplateAttachmentRead, |
| 35 | + Create: resourceTencentCloudMonitorTmpTkeTemplateAttachmentCreate, |
| 36 | + Delete: resourceTencentCloudMonitorTmpTkeTemplateAttachmentDelete, |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "template_id": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Required: true, |
| 41 | + ForceNew: true, |
| 42 | + Description: "The ID of the template, which is used for the outgoing reference.", |
| 43 | + }, |
| 44 | + |
| 45 | + "targets": { |
| 46 | + Type: schema.TypeList, |
| 47 | + MaxItems: 1, |
| 48 | + Required: true, |
| 49 | + ForceNew: true, |
| 50 | + Description: "Sync target details.", |
| 51 | + Elem: &schema.Resource{ |
| 52 | + Schema: map[string]*schema.Schema{ |
| 53 | + "region": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Required: true, |
| 56 | + Description: "target area.", |
| 57 | + }, |
| 58 | + "instance_id": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Required: true, |
| 61 | + Description: "instance id.", |
| 62 | + }, |
| 63 | + "cluster_id": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Optional: true, |
| 66 | + Description: "ID of the cluster.", |
| 67 | + }, |
| 68 | + "sync_time": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Optional: true, |
| 71 | + Description: "Last sync template time.", |
| 72 | + }, |
| 73 | + "version": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Optional: true, |
| 76 | + Description: "Template version currently in use.", |
| 77 | + }, |
| 78 | + "cluster_type": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Optional: true, |
| 81 | + Description: "Cluster type.", |
| 82 | + }, |
| 83 | + "instance_name": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Optional: true, |
| 86 | + Description: "Name of the prometheus instance.", |
| 87 | + }, |
| 88 | + "cluster_name": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Optional: true, |
| 91 | + Description: "Name the cluster.", |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func resourceTencentCloudMonitorTmpTkeTemplateAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 101 | + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_template_attachment.create")() |
| 102 | + defer inconsistentCheck(d, meta)() |
| 103 | + |
| 104 | + logId := getLogId(contextNil) |
| 105 | + |
| 106 | + request := tke.NewSyncPrometheusTempRequest() |
| 107 | + |
| 108 | + if v, ok := d.GetOk("template_id"); ok { |
| 109 | + request.TemplateId = helper.String(v.(string)) |
| 110 | + } |
| 111 | + |
| 112 | + if dMap, ok := helper.InterfacesHeadMap(d, "targets"); ok { |
| 113 | + var prometheusTarget tke.PrometheusTemplateSyncTarget |
| 114 | + if v, ok := dMap["region"]; ok { |
| 115 | + prometheusTarget.Region = helper.String(v.(string)) |
| 116 | + } |
| 117 | + |
| 118 | + if v, ok := dMap["instance_id"]; ok { |
| 119 | + prometheusTarget.InstanceId = helper.String(v.(string)) |
| 120 | + } |
| 121 | + |
| 122 | + if v, ok := dMap["cluster_id"]; ok { |
| 123 | + prometheusTarget.ClusterId = helper.String(v.(string)) |
| 124 | + } |
| 125 | + |
| 126 | + if v, ok := dMap["sync_time"]; ok { |
| 127 | + prometheusTarget.SyncTime = helper.String(v.(string)) |
| 128 | + } |
| 129 | + |
| 130 | + if v, ok := dMap["version"]; ok { |
| 131 | + prometheusTarget.Version = helper.String(v.(string)) |
| 132 | + } |
| 133 | + |
| 134 | + if v, ok := dMap["cluster_type"]; ok { |
| 135 | + prometheusTarget.ClusterType = helper.String(v.(string)) |
| 136 | + } |
| 137 | + |
| 138 | + if v, ok := dMap["instance_name"]; ok { |
| 139 | + prometheusTarget.InstanceName = helper.String(v.(string)) |
| 140 | + } |
| 141 | + |
| 142 | + if v, ok := dMap["cluster_name"]; ok { |
| 143 | + prometheusTarget.ClusterName = helper.String(v.(string)) |
| 144 | + } |
| 145 | + |
| 146 | + prometheusTargets := make([]*tke.PrometheusTemplateSyncTarget, 0) |
| 147 | + prometheusTargets = append(prometheusTargets, &prometheusTarget) |
| 148 | + request.Targets = prometheusTargets |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 153 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseTkeClient().SyncPrometheusTemp(request) |
| 154 | + if e != nil { |
| 155 | + return retryError(e) |
| 156 | + } else { |
| 157 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 158 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 159 | + } |
| 160 | + return nil |
| 161 | + }) |
| 162 | + |
| 163 | + if err != nil { |
| 164 | + log.Printf("[CRITAL]%s sync tke template failed, reason:%+v", logId, err) |
| 165 | + return err |
| 166 | + } |
| 167 | + |
| 168 | + templateId := *request.TemplateId |
| 169 | + instanceId := *request.Targets[0].InstanceId |
| 170 | + region := *request.Targets[0].Region |
| 171 | + d.SetId(strings.Join([]string{templateId, instanceId, region}, FILED_SP)) |
| 172 | + |
| 173 | + return resourceTencentCloudMonitorTmpTkeTemplateAttachmentRead(d, meta) |
| 174 | +} |
| 175 | + |
| 176 | +func resourceTencentCloudMonitorTmpTkeTemplateAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 177 | + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_template_attachment.read")() |
| 178 | + defer inconsistentCheck(d, meta)() |
| 179 | + |
| 180 | + logId := getLogId(contextNil) |
| 181 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 182 | + |
| 183 | + service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 184 | + |
| 185 | + ids := strings.Split(d.Id(), FILED_SP) |
| 186 | + if len(ids) != 3 { |
| 187 | + return fmt.Errorf("id is broken, id is %s", d.Id()) |
| 188 | + } |
| 189 | + |
| 190 | + templateId := ids[0] |
| 191 | + instanceId := ids[1] |
| 192 | + region := ids[2] |
| 193 | + |
| 194 | + targets, err := service.DescribePrometheusTempSync(ctx, templateId) |
| 195 | + |
| 196 | + if err != nil { |
| 197 | + return err |
| 198 | + } |
| 199 | + |
| 200 | + if targets == nil || len(targets) < 1 { |
| 201 | + d.SetId("") |
| 202 | + return fmt.Errorf("resource `targets` %s does not exist", templateId) |
| 203 | + } |
| 204 | + |
| 205 | + tempTargets := make([]map[string]interface{}, 0) |
| 206 | + for _, v := range targets { |
| 207 | + if *v.InstanceId == instanceId && *v.Region == region { |
| 208 | + tempTargets = append(tempTargets, map[string]interface{}{ |
| 209 | + "region": v.Region, |
| 210 | + "instance_id": v.InstanceId, |
| 211 | + //"cluster_id": v.ClusterId, |
| 212 | + //"sync_time": v.SyncTime, |
| 213 | + //"version": v.Version, |
| 214 | + //"cluster_type": v.ClusterType, |
| 215 | + //"instance_name": v.InstanceName, |
| 216 | + //"cluster_name": v.ClusterName, |
| 217 | + }) |
| 218 | + } |
| 219 | + } |
| 220 | + _ = d.Set("targets", tempTargets) |
| 221 | + |
| 222 | + return nil |
| 223 | +} |
| 224 | + |
| 225 | +func resourceTencentCloudMonitorTmpTkeTemplateAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 226 | + defer logElapsed("resource.tencentcloud_monitor_tmp_tke_template_attachment.delete")() |
| 227 | + defer inconsistentCheck(d, meta)() |
| 228 | + |
| 229 | + logId := getLogId(contextNil) |
| 230 | + request := tke.NewDeletePrometheusTempSyncRequest() |
| 231 | + |
| 232 | + ids := strings.Split(d.Id(), FILED_SP) |
| 233 | + if len(ids) != 3 { |
| 234 | + return fmt.Errorf("id is broken, id is %s", d.Id()) |
| 235 | + } |
| 236 | + |
| 237 | + templateId := ids[0] |
| 238 | + instanceId := ids[1] |
| 239 | + region := ids[2] |
| 240 | + |
| 241 | + request.TemplateId = &templateId |
| 242 | + var targets []*tke.PrometheusTemplateSyncTarget |
| 243 | + target := tke.PrometheusTemplateSyncTarget{ |
| 244 | + Region: ®ion, |
| 245 | + InstanceId: &instanceId, |
| 246 | + } |
| 247 | + targets = append(targets, &target) |
| 248 | + request.Targets = targets |
| 249 | + |
| 250 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 251 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseTkeClient().DeletePrometheusTempSync(request) |
| 252 | + if e != nil { |
| 253 | + return retryError(e) |
| 254 | + } else { |
| 255 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 256 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 257 | + } |
| 258 | + return nil |
| 259 | + }) |
| 260 | + if err != nil { |
| 261 | + return err |
| 262 | + } |
| 263 | + |
| 264 | + return nil |
| 265 | +} |
0 commit comments