This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_cabot_check_graphite.go
115 lines (98 loc) · 3.04 KB
/
resource_cabot_check_graphite.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
package main
import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mrsaints/go-cabot/cabot"
"strconv"
"time"
)
func resourceCabotCheckGraphite() *schema.Resource {
s := map[string]*schema.Schema{
"metric": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"check_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"expected_num_hosts": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
"allowed_num_failures": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
}
return &schema.Resource{
Create: resourceCabotCheckGraphiteCreate,
Read: resourceCabotCheckGraphiteRead,
Update: resourceCabotCheckGraphiteUpdate,
Delete: resourceCabotCheckGraphiteDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: CombineWithBaseCheckSchema(s),
}
}
func getGraphiteCheckFromResourceData(d *schema.ResourceData) *cabot.GraphiteCheck {
checkRequest := &cabot.GraphiteCheck{
Metric: d.Get("metric").(string),
CheckType: d.Get("check_type").(string),
Value: d.Get("value").(string),
ExpectedNumHosts: d.Get("expected_num_hosts").(int),
AllowedNumFailures: d.Get("allowed_num_failures").(int),
}
checkRequest.StatusCheck = getStatusCheckFromResourceData(d)
return checkRequest
}
func resourceCabotCheckGraphiteCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
check, err := client.GraphiteChecks.Create(getGraphiteCheckFromResourceData(d))
if err != nil {
return err
}
d.SetId(strconv.Itoa(check.ID))
return resourceCabotCheckGraphiteRead(d, meta)
}
func resourceCabotCheckGraphiteRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
return resource.Retry(DEFAULT_RETRY_TIMEOUT, func() *resource.RetryError {
check, err := client.GraphiteChecks.Get(id)
if err != nil {
time.Sleep(DEFAULT_GRACE_PERIOD)
return resource.RetryableError(err)
}
d.SetId(strconv.Itoa(check.ID))
setResourceDataForStatusCheck(d, check.StatusCheck)
d.Set("metric", check.Metric)
d.Set("check_type", check.CheckType)
d.Set("value", check.Value)
d.Set("expected_num_hosts", check.ExpectedNumHosts)
d.Set("allowed_num_failures", check.AllowedNumFailures)
return nil
})
}
func resourceCabotCheckGraphiteUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
check, err := client.GraphiteChecks.Update((id), getGraphiteCheckFromResourceData(d))
if err != nil {
return err
}
d.SetId(strconv.Itoa(check.ID))
return resourceCabotCheckGraphiteRead(d, meta)
}
func resourceCabotCheckGraphiteDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
return client.GraphiteChecks.Delete(id)
}