|
| 1 | +/* |
| 2 | +Provides a resource to create a monitor tmp_grafana_config |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_monitor_tmp_grafana_config" "tmp_grafana_config" { |
| 8 | + config = jsonencode( |
| 9 | + { |
| 10 | + server = { |
| 11 | + http_port = 8080 |
| 12 | + root_url = "https://cloud-grafana.woa.com/grafana-ffrdnrfa/" |
| 13 | + serve_from_sub_path = true |
| 14 | + } |
| 15 | + } |
| 16 | + ) |
| 17 | + instance_id = "grafana-29phe08q" |
| 18 | +} |
| 19 | +``` |
| 20 | +
|
| 21 | +Import |
| 22 | +
|
| 23 | +monitor tmp_grafana_config can be imported using the id, e.g. |
| 24 | +
|
| 25 | +``` |
| 26 | +terraform import tencentcloud_monitor_tmp_grafana_config.tmp_grafana_config tmp_grafana_config_id |
| 27 | +``` |
| 28 | +*/ |
| 29 | +package tencentcloud |
| 30 | + |
| 31 | +import ( |
| 32 | + "context" |
| 33 | + "log" |
| 34 | + |
| 35 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 36 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 37 | + monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724" |
| 38 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 39 | +) |
| 40 | + |
| 41 | +func resourceTencentCloudMonitorTmpGrafanaConfig() *schema.Resource { |
| 42 | + return &schema.Resource{ |
| 43 | + Create: resourceTencentCloudMonitorTmpGrafanaConfigCreate, |
| 44 | + Read: resourceTencentCloudMonitorTmpGrafanaConfigRead, |
| 45 | + Update: resourceTencentCloudMonitorTmpGrafanaConfigUpdate, |
| 46 | + Delete: resourceTencentCloudMonitorTmpGrafanaConfigDelete, |
| 47 | + Importer: &schema.ResourceImporter{ |
| 48 | + State: schema.ImportStatePassthrough, |
| 49 | + }, |
| 50 | + Schema: map[string]*schema.Schema{ |
| 51 | + "instance_id": { |
| 52 | + Required: true, |
| 53 | + Type: schema.TypeString, |
| 54 | + Description: "Instance id.", |
| 55 | + }, |
| 56 | + |
| 57 | + "config": { |
| 58 | + Required: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "JSON encoded string.", |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func resourceTencentCloudMonitorTmpGrafanaConfigCreate(d *schema.ResourceData, meta interface{}) error { |
| 67 | + defer logElapsed("resource.tencentcloud_monitor_tmp_grafana_config.create")() |
| 68 | + defer inconsistentCheck(d, meta)() |
| 69 | + |
| 70 | + var instanceId string |
| 71 | + if v, ok := d.GetOk("instance_id"); ok { |
| 72 | + instanceId = v.(string) |
| 73 | + } |
| 74 | + |
| 75 | + d.SetId(instanceId) |
| 76 | + |
| 77 | + return resourceTencentCloudMonitorTmpGrafanaConfigUpdate(d, meta) |
| 78 | +} |
| 79 | + |
| 80 | +func resourceTencentCloudMonitorTmpGrafanaConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 81 | + defer logElapsed("resource.tencentcloud_monitor_tmp_grafana_config.read")() |
| 82 | + defer inconsistentCheck(d, meta)() |
| 83 | + |
| 84 | + logId := getLogId(contextNil) |
| 85 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 86 | + |
| 87 | + service := MonitorService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 88 | + |
| 89 | + instanceId := d.Id() |
| 90 | + |
| 91 | + tmpGrafanaConfig, err := service.DescribeMonitorTmpGrafanaConfigById(ctx, instanceId) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + if tmpGrafanaConfig == nil { |
| 97 | + d.SetId("") |
| 98 | + log.Printf("[WARN]%s resource `MonitorTmpGrafanaConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 99 | + return nil |
| 100 | + } |
| 101 | + |
| 102 | + _ = d.Set("instance_id", instanceId) |
| 103 | + |
| 104 | + if tmpGrafanaConfig.Config != nil { |
| 105 | + _ = d.Set("config", tmpGrafanaConfig.Config) |
| 106 | + } |
| 107 | + |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func resourceTencentCloudMonitorTmpGrafanaConfigUpdate(d *schema.ResourceData, meta interface{}) error { |
| 112 | + defer logElapsed("resource.tencentcloud_monitor_tmp_grafana_config.update")() |
| 113 | + defer inconsistentCheck(d, meta)() |
| 114 | + |
| 115 | + logId := getLogId(contextNil) |
| 116 | + |
| 117 | + request := monitor.NewUpdateGrafanaConfigRequest() |
| 118 | + |
| 119 | + instanceId := d.Id() |
| 120 | + request.InstanceId = &instanceId |
| 121 | + |
| 122 | + if v, ok := d.GetOk("config"); ok { |
| 123 | + request.Config = helper.String(v.(string)) |
| 124 | + } |
| 125 | + |
| 126 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 127 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMonitorClient().UpdateGrafanaConfig(request) |
| 128 | + if e != nil { |
| 129 | + return retryError(e) |
| 130 | + } else { |
| 131 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 132 | + } |
| 133 | + return nil |
| 134 | + }) |
| 135 | + if err != nil { |
| 136 | + log.Printf("[CRITAL]%s update monitor tmpGrafanaConfig failed, reason:%+v", logId, err) |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + return resourceTencentCloudMonitorTmpGrafanaConfigRead(d, meta) |
| 141 | +} |
| 142 | + |
| 143 | +func resourceTencentCloudMonitorTmpGrafanaConfigDelete(d *schema.ResourceData, meta interface{}) error { |
| 144 | + defer logElapsed("resource.tencentcloud_monitor_tmp_grafana_config.delete")() |
| 145 | + defer inconsistentCheck(d, meta)() |
| 146 | + |
| 147 | + return nil |
| 148 | +} |
0 commit comments