forked from whitepages/terraform-provider-stingray
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresource_service_level_monitor.go
124 lines (98 loc) · 2.79 KB
/
resource_service_level_monitor.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
package main
import (
"fmt"
"github.com/atlassian/go-vtm"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceServiceLevelMonitor() *schema.Resource {
return &schema.Resource{
Create: resourceServiceLevelMonitorCreate,
Read: resourceServiceLevelMonitorRead,
Update: resourceServiceLevelMonitorUpdate,
Delete: resourceServiceLevelMonitorDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"note": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
},
"response_time": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 1000,
},
"serious_threshold": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
"warning_threshold": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 50,
},
},
}
}
func resourceServiceLevelMonitorCreate(d *schema.ResourceData, meta interface{}) error {
err := resourceServiceLevelMonitorSet(d, meta)
if err != nil {
return err
}
return resourceServiceLevelMonitorRead(d, meta)
}
func resourceServiceLevelMonitorRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerConfig).client
r, resp, err := c.GetServiceLevelMonitor(d.Get("name").(string))
if err != nil {
if resp != nil && resp.StatusCode == 404 {
// The resource doesn't exist anymore
d.SetId("")
return nil
}
return fmt.Errorf("Error reading resource: %s", err)
}
d.Set("note", string(*r.Basic.Note))
d.Set("response_time", int(*r.Basic.ResponseTime))
d.Set("serious_threshold", int(*r.Basic.SeriousThreshold))
d.Set("warning_threshold", int(*r.Basic.WarningThreshold))
return nil
}
func resourceServiceLevelMonitorUpdate(d *schema.ResourceData, meta interface{}) error {
err := resourceServiceLevelMonitorSet(d, meta)
if err != nil {
return err
}
return resourceServiceLevelMonitorRead(d, meta)
}
func resourceServiceLevelMonitorDelete(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerConfig).client
r := stingray.NewServiceLevelMonitor(d.Id())
_, err := c.Delete(r)
if err != nil {
return err
}
return nil
}
func resourceServiceLevelMonitorSet(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerConfig).client
r := stingray.NewServiceLevelMonitor(d.Get("name").(string))
setString(&r.Basic.Note, d, "note")
setInt(&r.Basic.ResponseTime, d, "response_time")
setInt(&r.Basic.SeriousThreshold, d, "serious_threshold")
setInt(&r.Basic.WarningThreshold, d, "warning_threshold")
_, err := c.Set(r)
if err != nil {
return err
}
d.SetId(d.Get("name").(string))
return nil
}