|
| 1 | +/* |
| 2 | +Provides a resource to create a apm instance |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_apm_instance" "instance" { |
| 8 | + name = "terraform-test" |
| 9 | + description = "for terraform test" |
| 10 | + trace_duration = 15 |
| 11 | + span_daily_counters = 20 |
| 12 | + tags = { |
| 13 | + "createdBy" = "terraform" |
| 14 | + } |
| 15 | +} |
| 16 | +``` |
| 17 | +
|
| 18 | +Import |
| 19 | +
|
| 20 | +apm instance can be imported using the id, e.g. |
| 21 | +
|
| 22 | +``` |
| 23 | +terraform import tencentcloud_apm_instance.instance instance_id |
| 24 | +``` |
| 25 | +*/ |
| 26 | +package tencentcloud |
| 27 | + |
| 28 | +import ( |
| 29 | + "context" |
| 30 | + "fmt" |
| 31 | + "log" |
| 32 | + |
| 33 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 34 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 35 | + apm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/apm/v20210622" |
| 36 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 37 | +) |
| 38 | + |
| 39 | +func resourceTencentCloudApmInstance() *schema.Resource { |
| 40 | + return &schema.Resource{ |
| 41 | + Create: resourceTencentCloudApmInstanceCreate, |
| 42 | + Read: resourceTencentCloudApmInstanceRead, |
| 43 | + Update: resourceTencentCloudApmInstanceUpdate, |
| 44 | + Delete: resourceTencentCloudApmInstanceDelete, |
| 45 | + Importer: &schema.ResourceImporter{ |
| 46 | + State: schema.ImportStatePassthrough, |
| 47 | + }, |
| 48 | + Schema: map[string]*schema.Schema{ |
| 49 | + "name": { |
| 50 | + Required: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "Name Of Instance.", |
| 53 | + }, |
| 54 | + |
| 55 | + "description": { |
| 56 | + Optional: true, |
| 57 | + Type: schema.TypeString, |
| 58 | + Description: "Description Of Instance.", |
| 59 | + }, |
| 60 | + |
| 61 | + "trace_duration": { |
| 62 | + Optional: true, |
| 63 | + Type: schema.TypeInt, |
| 64 | + Description: "Duration Of Trace Data.", |
| 65 | + }, |
| 66 | + |
| 67 | + "span_daily_counters": { |
| 68 | + Optional: true, |
| 69 | + Type: schema.TypeInt, |
| 70 | + Description: "Quota Of Instance Reporting.", |
| 71 | + }, |
| 72 | + |
| 73 | + "tags": { |
| 74 | + Type: schema.TypeMap, |
| 75 | + Optional: true, |
| 76 | + Description: "Tag description list.", |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func resourceTencentCloudApmInstanceCreate(d *schema.ResourceData, meta interface{}) error { |
| 83 | + defer logElapsed("resource.tencentcloud_apm_instance.create")() |
| 84 | + defer inconsistentCheck(d, meta)() |
| 85 | + |
| 86 | + logId := getLogId(contextNil) |
| 87 | + |
| 88 | + var ( |
| 89 | + request = apm.NewCreateApmInstanceRequest() |
| 90 | + response = apm.NewCreateApmInstanceResponse() |
| 91 | + instanceId string |
| 92 | + ) |
| 93 | + if v, ok := d.GetOk("name"); ok { |
| 94 | + request.Name = helper.String(v.(string)) |
| 95 | + } |
| 96 | + |
| 97 | + if v, ok := d.GetOk("description"); ok { |
| 98 | + request.Description = helper.String(v.(string)) |
| 99 | + } |
| 100 | + |
| 101 | + if v, ok := d.GetOkExists("trace_duration"); ok { |
| 102 | + request.TraceDuration = helper.IntInt64(v.(int)) |
| 103 | + } |
| 104 | + |
| 105 | + if v, ok := d.GetOkExists("span_daily_counters"); ok { |
| 106 | + request.SpanDailyCounters = helper.IntUint64(v.(int)) |
| 107 | + } |
| 108 | + |
| 109 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 110 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseApmClient().CreateApmInstance(request) |
| 111 | + if e != nil { |
| 112 | + return retryError(e) |
| 113 | + } else { |
| 114 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 115 | + } |
| 116 | + response = result |
| 117 | + return nil |
| 118 | + }) |
| 119 | + if err != nil { |
| 120 | + log.Printf("[CRITAL]%s create apm instance failed, reason:%+v", logId, err) |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + instanceId = *response.Response.InstanceId |
| 125 | + d.SetId(instanceId) |
| 126 | + |
| 127 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 128 | + if tags := helper.GetTags(d, "tags"); len(tags) > 0 { |
| 129 | + tagService := TagService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 130 | + region := meta.(*TencentCloudClient).apiV3Conn.Region |
| 131 | + resourceName := fmt.Sprintf("qcs::apm:%s:uin/:apm-instance/%s", region, d.Id()) |
| 132 | + if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return resourceTencentCloudApmInstanceRead(d, meta) |
| 138 | +} |
| 139 | + |
| 140 | +func resourceTencentCloudApmInstanceRead(d *schema.ResourceData, meta interface{}) error { |
| 141 | + defer logElapsed("resource.tencentcloud_apm_instance.read")() |
| 142 | + defer inconsistentCheck(d, meta)() |
| 143 | + |
| 144 | + logId := getLogId(contextNil) |
| 145 | + |
| 146 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 147 | + |
| 148 | + service := ApmService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 149 | + |
| 150 | + instanceId := d.Id() |
| 151 | + |
| 152 | + instance, err := service.DescribeApmInstanceById(ctx, instanceId) |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + if instance == nil { |
| 158 | + d.SetId("") |
| 159 | + log.Printf("[WARN]%s resource `ApmInstance` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 160 | + return nil |
| 161 | + } |
| 162 | + |
| 163 | + if instance.Name != nil { |
| 164 | + _ = d.Set("name", instance.Name) |
| 165 | + } |
| 166 | + |
| 167 | + if instance.Description != nil { |
| 168 | + _ = d.Set("description", instance.Description) |
| 169 | + } |
| 170 | + |
| 171 | + if instance.TraceDuration != nil { |
| 172 | + _ = d.Set("trace_duration", instance.TraceDuration) |
| 173 | + } |
| 174 | + |
| 175 | + if instance.SpanDailyCounters != nil { |
| 176 | + _ = d.Set("span_daily_counters", instance.SpanDailyCounters) |
| 177 | + } |
| 178 | + |
| 179 | + tcClient := meta.(*TencentCloudClient).apiV3Conn |
| 180 | + tagService := &TagService{client: tcClient} |
| 181 | + tags, err := tagService.DescribeResourceTags(ctx, "apm", "apm-instance", tcClient.Region, d.Id()) |
| 182 | + if err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + _ = d.Set("tags", tags) |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
| 189 | + |
| 190 | +func resourceTencentCloudApmInstanceUpdate(d *schema.ResourceData, meta interface{}) error { |
| 191 | + defer logElapsed("resource.tencentcloud_apm_instance.update")() |
| 192 | + defer inconsistentCheck(d, meta)() |
| 193 | + |
| 194 | + logId := getLogId(contextNil) |
| 195 | + |
| 196 | + request := apm.NewModifyApmInstanceRequest() |
| 197 | + |
| 198 | + needChange := false |
| 199 | + |
| 200 | + instanceId := d.Id() |
| 201 | + |
| 202 | + request.InstanceId = &instanceId |
| 203 | + |
| 204 | + mutableArgs := []string{"name", "description", "trace_duration", "span_daily_counters"} |
| 205 | + |
| 206 | + for _, v := range mutableArgs { |
| 207 | + if d.HasChange(v) { |
| 208 | + needChange = true |
| 209 | + break |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + if needChange { |
| 214 | + |
| 215 | + if v, ok := d.GetOk("name"); ok { |
| 216 | + request.Name = helper.String(v.(string)) |
| 217 | + } |
| 218 | + |
| 219 | + if v, ok := d.GetOk("description"); ok { |
| 220 | + request.Description = helper.String(v.(string)) |
| 221 | + } |
| 222 | + |
| 223 | + if v, ok := d.GetOkExists("trace_duration"); ok { |
| 224 | + request.TraceDuration = helper.IntInt64(v.(int)) |
| 225 | + } |
| 226 | + |
| 227 | + if v, ok := d.GetOkExists("span_daily_counters"); ok { |
| 228 | + request.SpanDailyCounters = helper.IntUint64(v.(int)) |
| 229 | + } |
| 230 | + |
| 231 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 232 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseApmClient().ModifyApmInstance(request) |
| 233 | + if e != nil { |
| 234 | + return retryError(e) |
| 235 | + } else { |
| 236 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 237 | + } |
| 238 | + return nil |
| 239 | + }) |
| 240 | + if err != nil { |
| 241 | + log.Printf("[CRITAL]%s update apm instance failed, reason:%+v", logId, err) |
| 242 | + return err |
| 243 | + } |
| 244 | + |
| 245 | + } |
| 246 | + |
| 247 | + if d.HasChange("tags") { |
| 248 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 249 | + tcClient := meta.(*TencentCloudClient).apiV3Conn |
| 250 | + tagService := &TagService{client: tcClient} |
| 251 | + oldTags, newTags := d.GetChange("tags") |
| 252 | + replaceTags, deleteTags := diffTags(oldTags.(map[string]interface{}), newTags.(map[string]interface{})) |
| 253 | + resourceName := BuildTagResourceName("apm", "apm-instance", tcClient.Region, d.Id()) |
| 254 | + if err := tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags); err != nil { |
| 255 | + return err |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + return resourceTencentCloudApmInstanceRead(d, meta) |
| 260 | +} |
| 261 | + |
| 262 | +func resourceTencentCloudApmInstanceDelete(d *schema.ResourceData, meta interface{}) error { |
| 263 | + defer logElapsed("resource.tencentcloud_apm_instance.delete")() |
| 264 | + defer inconsistentCheck(d, meta)() |
| 265 | + |
| 266 | + logId := getLogId(contextNil) |
| 267 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 268 | + |
| 269 | + service := ApmService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 270 | + instanceId := d.Id() |
| 271 | + |
| 272 | + if err := service.DeleteApmInstanceById(ctx, instanceId); err != nil { |
| 273 | + return err |
| 274 | + } |
| 275 | + |
| 276 | + return nil |
| 277 | +} |
0 commit comments