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_icmp.go
83 lines (66 loc) · 2.14 KB
/
resource_cabot_check_icmp.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
package main
import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mrsaints/go-cabot/cabot"
"strconv"
"time"
)
func resourceCabotCheckICMP() *schema.Resource {
return &schema.Resource{
Create: resourceCabotCheckICMPCreate,
Read: resourceCabotCheckICMPRead,
Update: resourceCabotCheckICMPUpdate,
Delete: resourceCabotCheckICMPDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: baseCheckSchema,
}
}
func getICMPCheckFromResourceData(d *schema.ResourceData) *cabot.ICMPCheck {
checkRequest := &cabot.ICMPCheck{}
checkRequest.StatusCheck = getStatusCheckFromResourceData(d)
return checkRequest
}
func resourceCabotCheckICMPCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
check, err := client.ICMPChecks.Create(getICMPCheckFromResourceData(d))
if err != nil {
return err
}
d.SetId(strconv.Itoa(check.ID))
return resourceCabotCheckICMPRead(d, meta)
}
func resourceCabotCheckICMPRead(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.ICMPChecks.Get(id)
if err != nil {
time.Sleep(DEFAULT_GRACE_PERIOD)
return resource.RetryableError(err)
}
d.SetId(strconv.Itoa(check.ID))
setResourceDataForStatusCheck(d, check.StatusCheck)
return nil
})
}
func resourceCabotCheckICMPUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
check, err := client.ICMPChecks.Update((id), getICMPCheckFromResourceData(d))
if err != nil {
return err
}
d.SetId(strconv.Itoa(check.ID))
return resourceCabotCheckICMPRead(d, meta)
}
func resourceCabotCheckICMPDelete(d *schema.ResourceData, meta interface{}) error {
// TODO: handle deletion when a linked instance is deleted
// That is, the ICMP check will be removed automatically resulting in a
// 404 not found error
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
return client.ICMPChecks.Delete(id)
}