Skip to content

Commit 56ab398

Browse files
gitmknanonymous
andauthored
feat: support manage grafana (#1611)
* feat: support manage grafana * feat: add changelog --------- Co-authored-by: anonymous <anonymous@mail.org>
1 parent 8fc5eee commit 56ab398

7 files changed

+310
-0
lines changed

.changelog/1611.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_monitor_tmp_manage_grafana_attachment
3+
```

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ Managed Service for Prometheus(TMP)
479479
tencentcloud_monitor_tmp_cvm_agent
480480
tencentcloud_monitor_tmp_scrape_job
481481
tencentcloud_monitor_tmp_recording_rule
482+
tencentcloud_monitor_tmp_manage_grafana_attachment
482483
483484
TencentCloud Managed Service for Grafana(TCMG)
484485
Resource
@@ -1534,6 +1535,7 @@ func Provider() terraform.ResourceProvider {
15341535
"tencentcloud_monitor_tmp_tke_config": resourceTencentCloudMonitorTmpTkeConfig(),
15351536
"tencentcloud_monitor_tmp_tke_record_rule_yaml": resourceTencentCloudMonitorTmpTkeRecordRuleYaml(),
15361537
"tencentcloud_monitor_tmp_tke_global_notification": resourceTencentCloudMonitorTmpTkeGlobalNotification(),
1538+
"tencentcloud_monitor_tmp_manage_grafana_attachment": resourceTencentCloudMonitorTmpManageGrafanaAttachment(),
15371539
"tencentcloud_monitor_grafana_instance": resourceTencentCloudMonitorGrafanaInstance(),
15381540
"tencentcloud_monitor_grafana_integration": resourceTencentCloudMonitorGrafanaIntegration(),
15391541
"tencentcloud_monitor_grafana_notification_channel": resourceTencentCloudMonitorGrafanaNotificationChannel(),
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
Provides a resource to create a monitor tmp_manage_grafana_attachment
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_monitor_tmp_manage_grafana_attachment" "manage_grafana_attachment" {
8+
grafana_id = "grafana-xxxxxx"
9+
instance_id = "prom-xxxxxxxx"
10+
}
11+
```
12+
13+
Import
14+
15+
monitor tmp_manage_grafana_attachment can be imported using the id, e.g.
16+
17+
```
18+
terraform import tencentcloud_monitor_tmp_manage_grafana_attachment.manage_grafana_attachment prom-xxxxxxxx
19+
```
20+
*/
21+
package tencentcloud
22+
23+
import (
24+
"context"
25+
"log"
26+
27+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
28+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
29+
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
30+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
31+
)
32+
33+
func resourceTencentCloudMonitorTmpManageGrafanaAttachment() *schema.Resource {
34+
return &schema.Resource{
35+
Create: resourceTencentCloudMonitorTmpManageGrafanaAttachmentCreate,
36+
Read: resourceTencentCloudMonitorTmpManageGrafanaAttachmentRead,
37+
Delete: resourceTencentCloudMonitorTmpManageGrafanaAttachmentDelete,
38+
Importer: &schema.ResourceImporter{
39+
State: schema.ImportStatePassthrough,
40+
},
41+
Schema: map[string]*schema.Schema{
42+
"instance_id": {
43+
Required: true,
44+
ForceNew: true,
45+
Type: schema.TypeString,
46+
Description: "Prometheus instance ID.",
47+
},
48+
49+
"grafana_id": {
50+
Required: true,
51+
ForceNew: true,
52+
Type: schema.TypeString,
53+
Description: "Grafana instance ID.",
54+
},
55+
},
56+
}
57+
}
58+
59+
func resourceTencentCloudMonitorTmpManageGrafanaAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
60+
defer logElapsed("resource.tencentcloud_monitor_tmp_manage_grafana_attachment.create")()
61+
defer inconsistentCheck(d, meta)()
62+
63+
logId := getLogId(contextNil)
64+
65+
var (
66+
request = monitor.NewBindPrometheusManagedGrafanaRequest()
67+
instanceId string
68+
)
69+
if v, ok := d.GetOk("instance_id"); ok {
70+
instanceId = v.(string)
71+
request.InstanceId = helper.String(v.(string))
72+
}
73+
74+
if v, ok := d.GetOk("grafana_id"); ok {
75+
request.GrafanaId = helper.String(v.(string))
76+
}
77+
78+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
79+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseMonitorClient().BindPrometheusManagedGrafana(request)
80+
if e != nil {
81+
return retryError(e)
82+
} else {
83+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
84+
}
85+
return nil
86+
})
87+
if err != nil {
88+
log.Printf("[CRITAL]%s create monitor manageGrafanaAttachment failed, reason:%+v", logId, err)
89+
return err
90+
}
91+
92+
d.SetId(instanceId)
93+
94+
return resourceTencentCloudMonitorTmpManageGrafanaAttachmentRead(d, meta)
95+
}
96+
97+
func resourceTencentCloudMonitorTmpManageGrafanaAttachmentRead(d *schema.ResourceData, meta interface{}) error {
98+
defer logElapsed("resource.tencentcloud_monitor_tmp_manage_grafana_attachment.read")()
99+
defer inconsistentCheck(d, meta)()
100+
101+
logId := getLogId(contextNil)
102+
103+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
104+
105+
service := MonitorService{client: meta.(*TencentCloudClient).apiV3Conn}
106+
107+
instanceId := d.Id()
108+
109+
manageGrafanaAttachment, err := service.DescribeMonitorManageGrafanaAttachmentById(ctx, instanceId)
110+
if err != nil {
111+
return err
112+
}
113+
114+
if manageGrafanaAttachment == nil {
115+
d.SetId("")
116+
log.Printf("[WARN]%s resource `MonitorTmpManageGrafanaAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
117+
return nil
118+
}
119+
120+
if manageGrafanaAttachment.InstanceId != nil {
121+
_ = d.Set("instance_id", manageGrafanaAttachment.InstanceId)
122+
}
123+
124+
if manageGrafanaAttachment.GrafanaInstanceId != nil {
125+
_ = d.Set("grafana_id", manageGrafanaAttachment.GrafanaInstanceId)
126+
}
127+
128+
return nil
129+
}
130+
131+
func resourceTencentCloudMonitorTmpManageGrafanaAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
132+
defer logElapsed("resource.tencentcloud_monitor_tmp_manage_grafana_attachment.delete")()
133+
defer inconsistentCheck(d, meta)()
134+
135+
logId := getLogId(contextNil)
136+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
137+
138+
service := MonitorService{client: meta.(*TencentCloudClient).apiV3Conn}
139+
instanceId := d.Id()
140+
141+
if err := service.DeleteMonitorManageGrafanaAttachmentById(ctx, instanceId); err != nil {
142+
return err
143+
}
144+
145+
return nil
146+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
// go test -i; go test -test.run TestAccTencentCloudMonitorTmpManageGrafanaAttachmentResource_basic -v
10+
func TestAccTencentCloudMonitorTmpManageGrafanaAttachmentResource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
testAccPreCheck(t)
15+
},
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccMonitorTmpManageGrafanaAttachment,
20+
Check: resource.ComposeTestCheckFunc(
21+
resource.TestCheckResourceAttrSet("tencentcloud_monitor_tmp_manage_grafana_attachment.manage_grafana_attachment", "id"),
22+
resource.TestCheckResourceAttrSet("tencentcloud_monitor_tmp_manage_grafana_attachment.manage_grafana_attachment", "instance_id"),
23+
resource.TestCheckResourceAttrSet("tencentcloud_monitor_tmp_manage_grafana_attachment.manage_grafana_attachment", "grafana_id"),
24+
),
25+
},
26+
{
27+
ResourceName: "tencentcloud_monitor_tmp_manage_grafana_attachment.manage_grafana_attachment",
28+
ImportState: true,
29+
ImportStateVerify: true,
30+
},
31+
},
32+
})
33+
}
34+
35+
const testManageGrafanaAttachmentVar = `
36+
variable "prometheus_id" {
37+
default = "` + defaultPrometheusId + `"
38+
}
39+
variable "grafana_id" {
40+
default = "` + defaultGrafanaInstanceId + `"
41+
}
42+
`
43+
44+
const testAccMonitorTmpManageGrafanaAttachment = testManageGrafanaAttachmentVar + `
45+
46+
resource "tencentcloud_monitor_tmp_manage_grafana_attachment" "manage_grafana_attachment" {
47+
grafana_id = var.grafana_id
48+
instance_id = var.prometheus_id
49+
}
50+
51+
`

tencentcloud/service_tencentcloud_monitor.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,3 +1511,63 @@ func (me *MonitorService) DescribePrometheusTempSync(ctx context.Context, templa
15111511

15121512
return
15131513
}
1514+
1515+
func (me *MonitorService) DescribeMonitorManageGrafanaAttachmentById(ctx context.Context, instanceId string) (manageGrafanaAttachment *monitor.PrometheusInstancesItem, errRet error) {
1516+
logId := getLogId(ctx)
1517+
1518+
request := monitor.NewDescribePrometheusInstancesRequest()
1519+
request.InstanceIds = []*string{&instanceId}
1520+
1521+
defer func() {
1522+
if errRet != nil {
1523+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
1524+
}
1525+
}()
1526+
1527+
ratelimit.Check(request.GetAction())
1528+
1529+
response, err := me.client.UseMonitorClient().DescribePrometheusInstances(request)
1530+
if err != nil {
1531+
errRet = err
1532+
return
1533+
}
1534+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
1535+
1536+
if len(response.Response.InstanceSet) < 1 {
1537+
return
1538+
}
1539+
1540+
manageGrafanaAttachment = response.Response.InstanceSet[0]
1541+
return
1542+
}
1543+
1544+
func (me *MonitorService) DeleteMonitorManageGrafanaAttachmentById(ctx context.Context, instanceId string) (errRet error) {
1545+
logId := getLogId(ctx)
1546+
1547+
resp, err := me.DescribeMonitorManageGrafanaAttachmentById(ctx, instanceId)
1548+
if err != nil {
1549+
errRet = err
1550+
return
1551+
}
1552+
1553+
request := monitor.NewUnbindPrometheusManagedGrafanaRequest()
1554+
request.InstanceId = &instanceId
1555+
request.GrafanaId = resp.GrafanaInstanceId
1556+
1557+
defer func() {
1558+
if errRet != nil {
1559+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
1560+
}
1561+
}()
1562+
1563+
ratelimit.Check(request.GetAction())
1564+
1565+
response, err := me.client.UseMonitorClient().UnbindPrometheusManagedGrafana(request)
1566+
if err != nil {
1567+
errRet = err
1568+
return
1569+
}
1570+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
1571+
1572+
return
1573+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
subcategory: "Managed Service for Prometheus(TMP)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_monitor_tmp_manage_grafana_attachment"
5+
sidebar_current: "docs-tencentcloud-resource-monitor_tmp_manage_grafana_attachment"
6+
description: |-
7+
Provides a resource to create a monitor tmp_manage_grafana_attachment
8+
---
9+
10+
# tencentcloud_monitor_tmp_manage_grafana_attachment
11+
12+
Provides a resource to create a monitor tmp_manage_grafana_attachment
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_monitor_tmp_manage_grafana_attachment" "manage_grafana_attachment" {
18+
grafana_id = "grafana-xxxxxx"
19+
instance_id = "prom-xxxxxxxx"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `grafana_id` - (Required, String, ForceNew) Grafana instance ID.
28+
* `instance_id` - (Required, String, ForceNew) Prometheus instance ID.
29+
30+
## Attributes Reference
31+
32+
In addition to all arguments above, the following attributes are exported:
33+
34+
* `id` - ID of the resource.
35+
36+
37+
38+
## Import
39+
40+
monitor tmp_manage_grafana_attachment can be imported using the id, e.g.
41+
42+
```
43+
terraform import tencentcloud_monitor_tmp_manage_grafana_attachment.manage_grafana_attachment prom-xxxxxxxx
44+
```
45+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,9 @@
13961396
<li>
13971397
<a href="/docs/providers/tencentcloud/r/monitor_tmp_instance.html">tencentcloud_monitor_tmp_instance</a>
13981398
</li>
1399+
<li>
1400+
<a href="/docs/providers/tencentcloud/r/monitor_tmp_manage_grafana_attachment.html">tencentcloud_monitor_tmp_manage_grafana_attachment</a>
1401+
</li>
13991402
<li>
14001403
<a href="/docs/providers/tencentcloud/r/monitor_tmp_recording_rule.html">tencentcloud_monitor_tmp_recording_rule</a>
14011404
</li>

0 commit comments

Comments
 (0)