/
data_source_cloudamqp_alarm.go
133 lines (123 loc) · 3.55 KB
/
data_source_cloudamqp_alarm.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
package cloudamqp
import (
"fmt"
"strconv"
"github.com/84codes/go-api/api"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceAlarm() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlarmRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeInt,
Required: true,
Description: "Instance identifier",
},
"alarm_id": {
Type: schema.TypeInt,
Optional: true,
Description: "Alarm identifier",
},
"type": {
Type: schema.TypeString,
Optional: true,
Description: "Type of the alarm",
ValidateFunc: validateAlarmType(),
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Enable or disable an alarm",
},
"reminder_interval": {
Type: schema.TypeInt,
Computed: true,
Description: "The reminder interval (in seconds) to resend the alarm if not resolved. Set to 0 for no reminders",
},
"value_threshold": {
Type: schema.TypeInt,
Computed: true,
Description: "What value to trigger the alarm for",
},
"value_calculation": {
Type: schema.TypeString,
Optional: true,
Description: "Disk value threshold calculation. Fixed or percentage of disk space remaining",
},
"time_threshold": {
Type: schema.TypeInt,
Computed: true,
Description: "For how long (in seconds) the value_threshold should be active before trigger alarm",
},
"vhost_regex": {
Type: schema.TypeString,
Computed: true,
Description: "Regex for which vhost the queues are in",
},
"queue_regex": {
Type: schema.TypeString,
Computed: true,
Description: "Regex for which queues to check",
},
"message_type": {
Type: schema.TypeString,
Computed: true,
Description: "Message types (total, unacked, ready) of the queue to trigger the alarm",
},
"recipients": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Description: "Identifiers for recipients to be notified.",
},
},
}
}
func dataSourceAlarmRead(d *schema.ResourceData, meta interface{}) error {
var data map[string]interface{}
var err error
// Multiple purpose read. To be used when using data source either by declaring alarm id or type.
if d.Get("alarm_id") != 0 {
data, err = dataSourceAlarmIDRead(d.Get("instance_id").(int), d.Get("alarm_id").(int), meta)
} else if d.Get("type") != "" {
data, err = dataSourceAlarmTypeRead(d.Get("instance_id").(int), d.Get("type").(string), meta)
}
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%v", data["id"]))
d.Set("alarm_id", data["id"])
for k, v := range data {
if validateAlarmSchemaAttribute(k) {
if err = d.Set(k, v); err != nil {
return fmt.Errorf("error setting %s for resource %s: %s", k, d.Id(), err)
}
}
}
return nil
}
func dataSourceAlarmIDRead(instanceID int, alarmID int, meta interface{}) (map[string]interface{}, error) {
api := meta.(*api.API)
id := strconv.Itoa(alarmID)
alarm, err := api.ReadAlarm(instanceID, id)
return alarm, err
}
func dataSourceAlarmTypeRead(instanceID int, alarmType string, meta interface{}) (map[string]interface{}, error) {
api := meta.(*api.API)
alarms, err := api.ReadAlarms(instanceID)
if err != nil {
return nil, err
}
for _, alarm := range alarms {
if alarm["type"] == alarmType {
return alarm, nil
}
}
return nil, nil
}