This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
forked from grafana/terraform-provider-grafana
-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_dashboard.go
138 lines (108 loc) · 3.01 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
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,
Delete: DeleteDashboard,
Read: ReadDashboard,
Schema: map[string]*schema.Schema{
"slug": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"config_json": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: NormalizeDashboardConfigJSON,
ValidateFunc: ValidateDashboardConfigJSON,
},
},
}
}
func CreateDashboard(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
model := prepareDashboardModel(d.Get("config_json").(string))
resp, err := client.SaveDashboard(model, false)
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)
return nil
}
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)
}