forked from grafana/terraform-provider-grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_folder.go
132 lines (104 loc) · 2.72 KB
/
resource_folder.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
package grafana
import (
"encoding/json"
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
gapi "github.com/nytm/go-grafana-api"
)
func ResourceFolder() *schema.Resource {
return &schema.Resource{
Create: CreateFolder,
Delete: DeleteFolder,
Read: ReadFolder,
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
},
"uid": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"title": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func CreateFolder(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
model := d.Get("title").(string)
resp, err := client.NewFolder(model)
if err != nil {
return err
}
id := strconv.FormatInt(resp.Id, 10)
d.SetId(id)
d.Set("uid", resp.Uid)
d.Set("title", resp.Title)
return ReadFolder(d, meta)
}
func ReadFolder(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return err
}
folder, err := client.Folder(id)
if err != nil {
if err.Error() == "404 Not Found" {
log.Printf("[WARN] removing folder %d from state because it no longer exists in grafana", id)
d.SetId("")
return nil
}
return err
}
d.SetId(strconv.FormatInt(folder.Id, 10))
d.Set("title", folder.Title)
return nil
}
func DeleteFolder(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)
return client.DeleteFolder(d.Get("uid").(string))
}
func prepareFolderModel(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"))
}
return configMap
}
func ValidateFolderConfigJSON(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 NormalizeFolderConfigJSON(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")
ret, err := json.Marshal(configMap)
if err != nil {
// Should never happen.
return configJSON
}
return string(ret)
}