Skip to content

Commit 2a162a5

Browse files
Annotations: Support org_id (#865)
* Annotations: Support `org_id` Also, `dashboard_uid` rather than `dashboard_id`. IDs are being deprecated across the board * Split up dashboard UID test
1 parent 5668d84 commit 2a162a5

File tree

5 files changed

+188
-48
lines changed

5 files changed

+188
-48
lines changed

docs/resources/annotation.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ resource "grafana_annotation" "test" {
2828

2929
### Optional
3030

31-
- `dashboard_id` (Number) The ID of the dashboard on which to create the annotation.
31+
- `dashboard_id` (Number, Deprecated) The ID of the dashboard on which to create the annotation. Deprecated: Use dashboard_uid instead.
32+
- `dashboard_uid` (String) The ID of the dashboard on which to create the annotation.
33+
- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
3234
- `panel_id` (Number) The ID of the dashboard panel on which to create the annotation.
3335
- `tags` (Set of String) The tags to associate with the annotation.
3436
- `time` (String) The RFC 3339-formatted time string indicating the annotation's time.

internal/resources/grafana/resource_annotation.go

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func ResourceAnnotation() *schema.Resource {
3030
},
3131

3232
Schema: map[string]*schema.Schema{
33+
"org_id": orgIDAttribute(),
3334
"text": {
3435
Type: schema.TypeString,
3536
Required: true,
@@ -53,10 +54,28 @@ func ResourceAnnotation() *schema.Resource {
5354
},
5455

5556
"dashboard_id": {
56-
Type: schema.TypeInt,
57-
Optional: true,
58-
ForceNew: true,
59-
Description: "The ID of the dashboard on which to create the annotation.",
57+
Type: schema.TypeInt,
58+
Optional: true,
59+
ForceNew: true,
60+
Deprecated: "Use dashboard_uid instead.",
61+
Description: "The ID of the dashboard on which to create the annotation. Deprecated: Use dashboard_uid instead.",
62+
ConflictsWith: []string{"dashboard_uid"},
63+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
64+
_, ok := d.GetOk("dashboard_uid")
65+
return ok
66+
},
67+
},
68+
69+
"dashboard_uid": {
70+
Type: schema.TypeString,
71+
Optional: true,
72+
ForceNew: true,
73+
Description: "The ID of the dashboard on which to create the annotation.",
74+
ConflictsWith: []string{"dashboard_id"},
75+
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
76+
_, ok := d.GetOk("dashboard_id")
77+
return ok
78+
},
6079
},
6180

6281
"panel_id": {
@@ -79,7 +98,7 @@ func ResourceAnnotation() *schema.Resource {
7998
}
8099

81100
func CreateAnnotation(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
82-
client := meta.(*common.Client).GrafanaAPI
101+
client, orgID := ClientFromNewOrgResource(meta, d)
83102

84103
annotation, err := makeAnnotation(ctx, d)
85104
if err != nil {
@@ -91,37 +110,31 @@ func CreateAnnotation(ctx context.Context, d *schema.ResourceData, meta interfac
91110
return diag.FromErr(err)
92111
}
93112

94-
d.SetId(strconv.FormatInt(id, 10))
113+
d.SetId(MakeOrgResourceID(orgID, id))
95114

96115
return ReadAnnotation(ctx, d, meta)
97116
}
98117

99118
func UpdateAnnotation(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
100-
client := meta.(*common.Client).GrafanaAPI
119+
client, _, idStr := ClientFromExistingOrgResource(meta, d.Id())
101120

102121
annotation, err := makeAnnotation(ctx, d)
103122
if err != nil {
104123
return diag.FromErr(err)
105124
}
106125

107-
idStr := d.Id()
108126
id, err := strconv.ParseInt(idStr, 10, 64)
109127
if err != nil {
110128
return diag.Errorf("invalid Grafana annotation ID: %#v", idStr)
111129
}
112130

113131
_, err = client.UpdateAnnotation(id, annotation)
114-
if err != nil {
115-
return diag.FromErr(err)
116-
}
117-
118-
return diag.Diagnostics{}
132+
return diag.FromErr(err)
119133
}
120134

121135
func ReadAnnotation(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
122-
client := meta.(*common.Client).GrafanaAPI
136+
client, orgID, idStr := ClientFromExistingOrgResource(meta, d.Id())
123137

124-
idStr := d.Id()
125138
id, err := strconv.ParseInt(idStr, 10, 64)
126139
if err != nil {
127140
return diag.Errorf("invalid Grafana annotation ID: %#v", idStr)
@@ -132,6 +145,10 @@ func ReadAnnotation(ctx context.Context, d *schema.ResourceData, meta interface{
132145
"panelId": []string{strconv.FormatInt(int64(d.Get("panel_id").(int)), 10)},
133146
"limit": []string{"100"},
134147
}
148+
if v, ok := d.GetOk("dashboard_uid"); ok {
149+
params.Set("dashboardUid", v.(string))
150+
params.Del("dashboardId")
151+
}
135152
annotations, err := client.Annotations(params)
136153
if err != nil {
137154
return diag.FromErr(err)
@@ -154,19 +171,19 @@ func ReadAnnotation(ctx context.Context, d *schema.ResourceData, meta interface{
154171

155172
d.Set("text", annotation.Text)
156173
d.Set("dashboard_id", annotation.DashboardID)
174+
d.Set("dashboard_uid", annotation.DashboardUID)
157175
d.Set("panel_id", annotation.PanelID)
158176
d.Set("tags", annotation.Tags)
159177
d.Set("time", t.Format(time.RFC3339))
160178
d.Set("time_end", tEnd.Format(time.RFC3339))
161-
d.SetId(strconv.FormatInt(annotation.ID, 10))
179+
d.Set("org_id", strconv.FormatInt(orgID, 10))
162180

163181
return nil
164182
}
165183

166184
func DeleteAnnotation(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
167-
client := meta.(*common.Client).GrafanaAPI
185+
client, _, idStr := ClientFromExistingOrgResource(meta, d.Id())
168186

169-
idStr := d.Id()
170187
id, err := strconv.ParseInt(idStr, 10, 64)
171188
if err != nil {
172189
return diag.Errorf("invalid Grafana annotation ID: %#v", idStr)
@@ -184,15 +201,17 @@ func makeAnnotation(_ context.Context, d *schema.ResourceData) (*gapi.Annotation
184201
var id int64
185202
var err error
186203
if idStr != "" {
204+
_, idStr = SplitOrgResourceID(idStr)
187205
id, err = strconv.ParseInt(idStr, 10, 64)
188206
}
189207

190208
a := &gapi.Annotation{
191-
ID: id,
192-
Text: d.Get("text").(string),
193-
PanelID: int64(d.Get("panel_id").(int)),
194-
DashboardID: int64(d.Get("dashboard_id").(int)),
195-
Tags: common.SetToStringSlice(d.Get("tags").(*schema.Set)),
209+
ID: id,
210+
Text: d.Get("text").(string),
211+
PanelID: int64(d.Get("panel_id").(int)),
212+
DashboardID: int64(d.Get("dashboard_id").(int)),
213+
DashboardUID: d.Get("dashboard_uid").(string),
214+
Tags: common.SetToStringSlice(d.Get("tags").(*schema.Set)),
196215
}
197216

198217
start := d.Get("time").(string)

0 commit comments

Comments
 (0)