forked from grafana/terraform-provider-grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_dashboard.go
172 lines (132 loc) · 3.69 KB
/
resource_dashboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package grafana
import (
"encoding/json"
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
gapi "github.com/nytm/go-grafana-api"
)
func ResourceDashboard() *schema.Resource {
return &schema.Resource{
Create: CreateDashboard,
Read: ReadDashboard,
Update: UpdateDashboard,
Delete: DeleteDashboard,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"slug": {
Type: schema.TypeString,
Computed: true,
},
"folder": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
"config_json": {
Type: schema.TypeString,
Required: true,
StateFunc: NormalizeDashboardConfigJSON,
ValidateFunc: ValidateDashboardConfigJSON,
},
},
}
}
func CreateDashboard(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
dashboard := gapi.Dashboard{}
dashboard.Model = prepareDashboardModel(d.Get("config_json").(string))
dashboard.Folder = int64(d.Get("folder").(int))
resp, err := client.NewDashboard(dashboard)
if err != nil {
return err
}
d.SetId(resp.Slug)
return ReadDashboard(d, meta)
}
func ReadDashboard(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
slug := d.Id()
dashboard, err := client.Dashboard(slug)
if err != nil {
if err.Error() == "404 Not Found" {
log.Printf("[WARN] removing dashboard %s from state because it no longer exists in grafana", slug)
d.SetId("")
return nil
}
return err
}
configJSONBytes, err := json.Marshal(dashboard.Model)
if err != nil {
return err
}
configJSON := NormalizeDashboardConfigJSON(string(configJSONBytes))
d.SetId(dashboard.Meta.Slug)
d.Set("slug", dashboard.Meta.Slug)
d.Set("config_json", configJSON)
d.Set("folder", dashboard.Folder)
return nil
}
func UpdateDashboard(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
dashboard := gapi.Dashboard{}
dashboard.Model = prepareDashboardModel(d.Get("config_json").(string))
dashboard.Folder = int64(d.Get("folder").(int))
dashboard.Overwrite = true
resp, err := client.NewDashboard(dashboard)
if err != nil {
return err
}
d.SetId(resp.Slug)
return ReadDashboard(d, meta)
}
func DeleteDashboard(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
slug := d.Id()
return client.DeleteDashboard(slug)
}
func prepareDashboardModel(configJSON string) map[string]interface{} {
configMap := map[string]interface{}{}
err := json.Unmarshal([]byte(configJSON), &configMap)
if err != nil {
// The validate function should've taken care of this.
panic(fmt.Errorf("Invalid JSON got into prepare func"))
}
delete(configMap, "id")
// Only exists in 5.0+
delete(configMap, "uid")
configMap["version"] = 0
return configMap
}
func ValidateDashboardConfigJSON(configI interface{}, k string) ([]string, []error) {
configJSON := configI.(string)
configMap := map[string]interface{}{}
err := json.Unmarshal([]byte(configJSON), &configMap)
if err != nil {
return nil, []error{err}
}
return nil, nil
}
func NormalizeDashboardConfigJSON(configI interface{}) string {
configJSON := configI.(string)
configMap := map[string]interface{}{}
err := json.Unmarshal([]byte(configJSON), &configMap)
if err != nil {
// The validate function should've taken care of this.
return ""
}
// Some properties are managed by this provider and are thus not
// significant when included in the JSON.
delete(configMap, "id")
delete(configMap, "version")
// Only exists in 5.0+
delete(configMap, "uid")
ret, err := json.Marshal(configMap)
if err != nil {
// Should never happen.
return configJSON
}
return string(ret)
}