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_service.go
160 lines (139 loc) · 4.02 KB
/
resource_cabot_service.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
package main
import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mrsaints/go-cabot/cabot"
"strconv"
"time"
)
func resourceCabotService() *schema.Resource {
return &schema.Resource{
Create: resourceCabotServiceCreate,
Read: resourceCabotServiceRead,
Update: resourceCabotServiceUpdate,
Delete: resourceCabotServiceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"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,
},
"instances": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Set: HashInt,
},
"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,
},
"url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"users_to_notify": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Set: HashInt,
},
},
}
}
func getServiceFromResourceData(d *schema.ResourceData) *cabot.Service {
serviceRequest := &cabot.Service{
AlertsEnabled: d.Get("alerts_enabled").(bool),
Name: d.Get("name").(string),
URL: d.Get("url").(string),
}
alerts := d.Get("alerts").(*schema.Set).List()
for _, alert := range alerts {
serviceRequest.Alerts = append(serviceRequest.Alerts, alert.(int))
}
instances := d.Get("instances").(*schema.Set).List()
for _, instance := range instances {
serviceRequest.Instances = append(serviceRequest.Instances, instance.(int))
}
statusChecks := d.Get("status_checks").(*schema.Set).List()
for _, check := range statusChecks {
serviceRequest.StatusChecks = append(serviceRequest.StatusChecks, check.(int))
}
usersToNotify := d.Get("users_to_notify").(*schema.Set).List()
for _, user := range usersToNotify {
serviceRequest.UsersToNotify = append(serviceRequest.UsersToNotify, user.(int))
}
return serviceRequest
}
func resourceCabotServiceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
service, err := client.Services.Create(getServiceFromResourceData(d))
if err != nil {
return err
}
d.SetId(strconv.Itoa(service.ID))
return resourceCabotServiceRead(d, meta)
}
func resourceCabotServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
return resource.Retry(DEFAULT_RETRY_TIMEOUT, func() *resource.RetryError {
service, err := client.Services.Get(id)
if err != nil {
time.Sleep(DEFAULT_GRACE_PERIOD)
return resource.RetryableError(err)
}
d.SetId(strconv.Itoa(service.ID))
d.Set("alerts", service.Alerts)
d.Set("alerts_enabled", service.AlertsEnabled)
d.Set("instances", service.Instances)
d.Set("name", service.Name)
d.Set("overall_status", service.OverallStatus)
d.Set("status_checks", service.StatusChecks)
d.Set("url", service.URL)
d.Set("users_to_notify", service.UsersToNotify)
return nil
})
}
func resourceCabotServiceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
service, err := client.Services.Update((id), getServiceFromResourceData(d))
if err != nil {
return err
}
d.SetId(strconv.Itoa(service.ID))
return resourceCabotServiceRead(d, meta)
}
func resourceCabotServiceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
return client.Services.Delete(id)
}