]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - statuscake/resource_statuscakessl.go
f07d6ebb1b766bc57a1ddf4d1deaec49f365ba82
[github/fretlink/terraform-provider-statuscake.git] / statuscake / resource_statuscakessl.go
1 package statuscake
2
3 import (
4 "fmt"
5 "strconv"
6
7 "log"
8
9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
11 )
12
13 func resourceStatusCakeSsl() *schema.Resource {
14 return &schema.Resource{
15 Create: CreateSsl,
16 Update: UpdateSsl,
17 Delete: DeleteSsl,
18 Read: ReadSsl,
19 Importer: &schema.ResourceImporter{
20 State: schema.ImportStatePassthrough,
21 },
22
23 Schema: map[string]*schema.Schema{
24 "ssl_id": {
25 Type: schema.TypeString,
26 Computed: true,
27 },
28
29 "domain": {
30 Type: schema.TypeString,
31 Required: true,
32 },
33
34 "contact_groups": {
35 Type: schema.TypeList,
36 Elem: &schema.Schema{Type: schema.TypeString},
37 Computed: true,
38 },
39
40 "contact_groups_c": {
41 Type: schema.TypeString,
42 Required: true,
43 },
44
45 "checkrate": {
46 Type: schema.TypeInt,
47 Required: true,
48 },
49
50 "alert_at": {
51 Type: schema.TypeString,
52 Required: true,
53 },
54
55 "alert_reminder": {
56 Type: schema.TypeBool,
57 Required: true,
58 },
59
60 "alert_expiry": {
61 Type: schema.TypeBool,
62 Required: true,
63 },
64
65 "alert_broken": {
66 Type: schema.TypeBool,
67 Required: true,
68 },
69
70 "alert_mixed": {
71 Type: schema.TypeBool,
72 Required: true,
73 },
74
75 "paused": {
76 Type: schema.TypeBool,
77 Computed: true,
78 },
79
80 "issuer_cn": {
81 Type: schema.TypeString,
82 Computed: true,
83 },
84
85 "cert_score": {
86 Type: schema.TypeString,
87 Computed: true,
88 },
89
90 "cipher_score": {
91 Type: schema.TypeString,
92 Computed: true,
93 },
94
95 "cert_status": {
96 Type: schema.TypeString,
97 Computed: true,
98 },
99
100 "cipher": {
101 Type: schema.TypeString,
102 Computed: true,
103 },
104
105 "valid_from_utc": {
106 Type: schema.TypeString,
107 Computed: true,
108 },
109
110 "valid_until_utc": {
111 Type: schema.TypeString,
112 Computed: true,
113 },
114
115 "mixed_content": {
116 Type: schema.TypeList,
117 Elem: &schema.Schema{
118 Type: schema.TypeMap,
119 Elem: &schema.Schema{Type: schema.TypeString},
120 },
121 Computed: true,
122 },
123
124 "flags": {
125 Type: schema.TypeMap,
126 Elem: &schema.Schema{Type: schema.TypeBool},
127 Computed: true,
128 },
129
130 "last_reminder": {
131 Type: schema.TypeInt,
132 Computed: true,
133 },
134
135 "last_updated_utc": {
136 Type: schema.TypeString,
137 Computed: true,
138 },
139 },
140 }
141 }
142
143 func CreateSsl(d *schema.ResourceData, meta interface{}) error {
144 client := meta.(*statuscake.Client)
145
146 newSsl := &statuscake.PartialSsl{
147 Domain: d.Get("domain").(string),
148 Checkrate: strconv.Itoa(d.Get("checkrate").(int)),
149 ContactGroupsC: d.Get("contact_groups_c").(string),
150 AlertReminder: d.Get("alert_reminder").(bool),
151 AlertExpiry: d.Get("alert_expiry").(bool),
152 AlertBroken: d.Get("alert_broken").(bool),
153 AlertMixed: d.Get("alert_mixed").(bool),
154 AlertAt: d.Get("alert_at").(string),
155 }
156
157 log.Printf("[DEBUG] Creating new StatusCake Ssl: %s", d.Get("domain").(string))
158
159 response, err := statuscake.NewSsls(client).Create(newSsl)
160 if err != nil {
161 return fmt.Errorf("Error creating StatusCake Ssl: %s", err.Error())
162 }
163
164 d.Set("ssl_id", response.ID)
165 d.Set("contact_groups", response.ContactGroups)
166 d.Set("paused", response.Paused)
167 d.Set("issuer_cn", response.IssuerCn)
168 d.Set("cert_score", response.CertScore)
169 d.Set("cipher_score", response.CipherScore)
170 d.Set("cert_status", response.CertStatus)
171 d.Set("cipher", response.Cipher)
172 d.Set("valid_from_utc", response.ValidFromUtc)
173 d.Set("valid_until_utc", response.ValidUntilUtc)
174 d.Set("mixed_content", response.MixedContent)
175 d.Set("flags", response.Flags)
176 d.Set("last_reminder", response.LastReminder)
177 d.Set("last_updated_utc", response.LastUpdatedUtc)
178 d.SetId(response.ID)
179
180 return ReadSsl(d, meta)
181 }
182
183 func UpdateSsl(d *schema.ResourceData, meta interface{}) error {
184 client := meta.(*statuscake.Client)
185
186 params := getStatusCakeSslInput(d)
187
188 log.Printf("[DEBUG] StatusCake Ssl Update for %s", d.Id())
189 _, err := statuscake.NewSsls(client).Update(params)
190 if err != nil {
191 return fmt.Errorf("Error Updating StatusCake Ssl: %s", err.Error())
192 }
193 return nil
194 }
195
196 func DeleteSsl(d *schema.ResourceData, meta interface{}) error {
197 client := meta.(*statuscake.Client)
198
199 log.Printf("[DEBUG] Deleting StatusCake Ssl: %s", d.Id())
200 err := statuscake.NewSsls(client).Delete(d.Id())
201
202 return err
203 }
204
205 func ReadSsl(d *schema.ResourceData, meta interface{}) error {
206 client := meta.(*statuscake.Client)
207
208 response, err := statuscake.NewSsls(client).Detail(d.Id())
209 if err != nil {
210 return fmt.Errorf("Error Getting StatusCake Ssl Details for %s: Error: %s", d.Id(), err)
211 }
212 d.Set("domain", response.Domain)
213 d.Set("checkrate", response.Checkrate)
214 d.Set("contact_groups_c", response.ContactGroupsC)
215 d.Set("alert_reminder", response.AlertReminder)
216 d.Set("alert_expiry", response.AlertExpiry)
217 d.Set("alert_broken", response.AlertBroken)
218 d.Set("alert_mixed", response.AlertMixed)
219 d.Set("alert_at", response.AlertAt)
220 d.Set("ssl_id", response.ID)
221 d.Set("contact_groups", response.ContactGroups)
222 d.Set("paused", response.Paused)
223 d.Set("issuer_cn", response.IssuerCn)
224 d.Set("cert_score", response.CertScore)
225 d.Set("cipher_score", response.CipherScore)
226 d.Set("cert_status", response.CertStatus)
227 d.Set("cipher", response.Cipher)
228 d.Set("valid_from_utc", response.ValidFromUtc)
229 d.Set("valid_until_utc", response.ValidUntilUtc)
230 d.Set("mixed_content", response.MixedContent)
231 d.Set("flags", response.Flags)
232 d.Set("last_reminder", response.LastReminder)
233 d.Set("last_updated_utc", response.LastUpdatedUtc)
234 d.SetId(response.ID)
235
236 return nil
237 }
238
239 func getStatusCakeSslInput(d *schema.ResourceData) *statuscake.PartialSsl {
240 sslId, parseErr := strconv.Atoi(d.Id())
241 if parseErr != nil {
242 log.Printf("[DEBUG] Error Parsing StatusCake Id: %s", d.Id())
243 }
244 ssl := &statuscake.PartialSsl{
245 ID: sslId,
246 }
247
248 if v, ok := d.GetOk("domain"); ok {
249 ssl.Domain = v.(string)
250 }
251
252 if v, ok := d.GetOk("checkrate"); ok {
253 ssl.Checkrate = strconv.Itoa(v.(int))
254 }
255
256 if v, ok := d.GetOk("contact_groups_c"); ok {
257 ssl.ContactGroupsC = v.(string)
258 }
259
260 if v, ok := d.GetOk("alert_reminder"); ok {
261 ssl.AlertReminder = v.(bool)
262 }
263
264 if v, ok := d.GetOk("alert_expiry"); ok {
265 ssl.AlertExpiry = v.(bool)
266 }
267
268 if v, ok := d.GetOk("alert_broken"); ok {
269 ssl.AlertBroken = v.(bool)
270 }
271
272 if v, ok := d.GetOk("alert_mixed"); ok {
273 ssl.AlertMixed = v.(bool)
274 }
275
276 if v, ok := d.GetOk("alert_at"); ok {
277 ssl.AlertAt = v.(string)
278 }
279
280 return ssl
281 }