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_instance.go
145 lines (126 loc) · 3.71 KB
/
resource_cabot_instance.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
package main
import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mrsaints/go-cabot/cabot"
"strconv"
"time"
)
func resourceCabotInstance() *schema.Resource {
return &schema.Resource{
Create: resourceCabotInstanceCreate,
Read: resourceCabotInstanceRead,
Update: resourceCabotInstanceUpdate,
Delete: resourceCabotInstanceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"alerts": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Set: HashInt,
},
"alerts_enabled": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"overall_status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"status_checks": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Set: HashInt,
},
"users_to_notify": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Set: HashInt,
},
},
}
}
func getInstanceFromResourceData(d *schema.ResourceData) *cabot.Instance {
instanceRequest := &cabot.Instance{
Address: d.Get("address").(string),
AlertsEnabled: d.Get("alerts_enabled").(bool),
Name: d.Get("name").(string),
}
alerts := d.Get("alerts").(*schema.Set).List()
for _, alert := range alerts {
instanceRequest.Alerts = append(instanceRequest.Alerts, alert.(int))
}
statusChecks := d.Get("status_checks").(*schema.Set).List()
for _, check := range statusChecks {
instanceRequest.StatusChecks = append(instanceRequest.StatusChecks, check.(int))
}
usersToNotify := d.Get("users_to_notify").(*schema.Set).List()
for _, user := range usersToNotify {
instanceRequest.UsersToNotify = append(instanceRequest.UsersToNotify, user.(int))
}
return instanceRequest
}
func resourceCabotInstanceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
instance, err := client.Instances.Create(getInstanceFromResourceData(d))
if err != nil {
return err
}
d.SetId(strconv.Itoa(instance.ID))
return resourceCabotInstanceRead(d, meta)
}
func resourceCabotInstanceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
return resource.Retry(DEFAULT_RETRY_TIMEOUT, func() *resource.RetryError {
instance, err := client.Instances.Get(id)
if err != nil {
time.Sleep(DEFAULT_GRACE_PERIOD)
return resource.RetryableError(err)
}
d.SetId(strconv.Itoa(instance.ID))
d.Set("address", instance.Address)
d.Set("alerts", instance.Alerts)
d.Set("alerts_enabled", instance.AlertsEnabled)
d.Set("name", instance.Name)
d.Set("overall_status", instance.OverallStatus)
d.Set("status_checks", instance.StatusChecks)
d.Set("users_to_notify", instance.UsersToNotify)
return nil
})
}
func resourceCabotInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
instance, err := client.Instances.Update((id), getInstanceFromResourceData(d))
if err != nil {
return err
}
d.SetId(strconv.Itoa(instance.ID))
return resourceCabotInstanceRead(d, meta)
}
func resourceCabotInstanceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
return client.Instances.Delete(id)
}