Skip to content

Commit 54906f7

Browse files
authored
Add cloudamqp_alarms datasource (#335)
New datasource (cloudamqp_alarms) to fetch all (or filtered down for a specifc type) alarms.
1 parent 15d33c1 commit 54906f7

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package cloudamqp
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/cloudamqp/terraform-provider-cloudamqp/api"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceAlarms() *schema.Resource {
13+
return &schema.Resource{
14+
ReadContext: dataSourceAlarmsRead,
15+
Importer: &schema.ResourceImporter{
16+
StateContext: schema.ImportStatePassthroughContext,
17+
},
18+
Schema: map[string]*schema.Schema{
19+
"instance_id": {
20+
Type: schema.TypeInt,
21+
Required: true,
22+
Description: "Instance identifier",
23+
},
24+
"type": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Description: "Type of the alarm",
28+
ValidateDiagFunc: validateAlarmType(),
29+
},
30+
"alarms": {
31+
Type: schema.TypeList,
32+
Computed: true,
33+
Description: "List of alarms",
34+
Elem: &schema.Resource{
35+
Schema: map[string]*schema.Schema{
36+
"alarm_id": {
37+
Type: schema.TypeInt,
38+
Optional: true,
39+
Description: "Alarm identifier",
40+
},
41+
"type": {
42+
Type: schema.TypeString,
43+
Optional: true,
44+
Description: "Type of the alarm",
45+
ValidateDiagFunc: validateAlarmType(),
46+
},
47+
"enabled": {
48+
Type: schema.TypeBool,
49+
Computed: true,
50+
Description: "Enable or disable an alarm",
51+
},
52+
"reminder_interval": {
53+
Type: schema.TypeInt,
54+
Computed: true,
55+
Description: "The reminder interval (in seconds) to resend the alarm if not resolved. Set to 0 for no reminders",
56+
},
57+
"value_threshold": {
58+
Type: schema.TypeInt,
59+
Computed: true,
60+
Description: "What value to trigger the alarm for",
61+
},
62+
"value_calculation": {
63+
Type: schema.TypeString,
64+
Optional: true,
65+
Description: "Disk value threshold calculation. Fixed or percentage of disk space remaining",
66+
},
67+
"time_threshold": {
68+
Type: schema.TypeInt,
69+
Computed: true,
70+
Description: "For how long (in seconds) the value_threshold should be active before trigger alarm",
71+
},
72+
"vhost_regex": {
73+
Type: schema.TypeString,
74+
Computed: true,
75+
Description: "Regex for which vhost the queues are in",
76+
},
77+
"queue_regex": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
Description: "Regex for which queues to check",
81+
},
82+
"message_type": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
Description: "Message types (total, unacked, ready) of the queue to trigger the alarm",
86+
},
87+
"recipients": {
88+
Type: schema.TypeList,
89+
Computed: true,
90+
Elem: &schema.Schema{
91+
Type: schema.TypeInt,
92+
},
93+
Description: "Identifiers for recipients to be notified.",
94+
},
95+
},
96+
},
97+
},
98+
},
99+
}
100+
}
101+
102+
func dataSourceAlarmsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
103+
var (
104+
data []map[string]any
105+
instanceID = d.Get("instance_id").(int)
106+
err error
107+
)
108+
109+
api := meta.(*api.API)
110+
data, err = api.ListAlarms(ctx, instanceID)
111+
112+
if err != nil {
113+
return diag.FromErr(err)
114+
}
115+
116+
if d.Get("type") != "" {
117+
d.SetId(fmt.Sprintf("%v.%v.alarms", instanceID, d.Get("type")))
118+
filteredAlarms := make([]map[string]any, 0)
119+
for _, alarm := range data {
120+
if alarm["type"] == d.Get("type") {
121+
filteredAlarms = append(filteredAlarms, alarm)
122+
}
123+
}
124+
data = filteredAlarms
125+
} else {
126+
d.SetId(fmt.Sprintf("%v.alarms", instanceID))
127+
}
128+
129+
alarms := make([]map[string]any, len(data))
130+
for k, v := range data {
131+
alarms[k] = readAlarm(v)
132+
}
133+
134+
if err = d.Set("alarms", alarms); err != nil {
135+
return diag.Errorf("error setting alarms for resource %s: %s", d.Id(), err)
136+
}
137+
138+
return diag.Diagnostics{}
139+
}
140+
141+
func readAlarm(data map[string]any) map[string]any {
142+
alarm := make(map[string]any)
143+
for k, v := range data {
144+
if k == "id" {
145+
alarm["alarm_id"] = int(v.(float64))
146+
} else if validateAlarmSchemaAttribute(k) {
147+
alarm[k] = v
148+
}
149+
}
150+
return alarm
151+
}

cloudamqp/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func Provider(v string, client *http.Client) *schema.Provider {
4040
"cloudamqp_account_vpcs": dataSourceAccountVpcs(),
4141
"cloudamqp_account": dataSourceAccount(),
4242
"cloudamqp_alarm": dataSourceAlarm(),
43+
"cloudamqp_alarms": dataSourceAlarms(),
4344
"cloudamqp_credentials": dataSourceCredentials(),
4445
"cloudamqp_instance": dataSourceInstance(),
4546
"cloudamqp_nodes": dataSourceNodes(),

docs/data-sources/alarms.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
layout: "cloudamqp"
3+
page_title: "CloudAMQP: data source cloudamqp_alarms"
4+
description: |-
5+
Get a list of pre-defined or created alarms.
6+
---
7+
8+
# cloudamqp_alarms
9+
10+
Use this data source to retrieve a list of default or created alarms.
11+
12+
## Example Usage
13+
14+
```hcl
15+
data "cloudamqp_alarms" "queue_alarms" {
16+
instance_id = cloudamqp_instance.instance.id
17+
type = "queue"
18+
}
19+
```
20+
21+
## Argument Reference
22+
23+
* `instance_id` - (Required) The CloudAMQP instance identifier.
24+
* `type` - (Optional) The alarm type to filter for. Supported
25+
[alarm types](#alarm-types).
26+
27+
## Attributes Reference
28+
29+
All attributes reference are computed
30+
31+
*`alarms` - List of alarms (see [below for nested schema](#nestedatt--alarms))
32+
33+
<a id="nestedatt--alarms"></a>
34+
35+
### Nested Schema for `alarms`
36+
37+
* `alarm_id` - The alarm identifier.
38+
* `type` - The type of the alarm.
39+
* `enabled` - Enable/disable status of the alarm.
40+
* `value_threshold` - The value threshold that triggers the alarm.
41+
* `reminder_interval` - The reminder interval (in seconds) to resend the alarm if not resolved.
42+
Set to 0 for no reminders.
43+
* `time_threshold` - The time interval (in seconds) the `value_threshold` should be active
44+
before trigger an alarm.
45+
* `queue_regex` - Regular expression for which queue to check.
46+
* `vhost_regex` - Regular expression for which vhost to check
47+
* `recipients` - Identifier for recipient to be notified.
48+
* `message_type` - Message type `(total, unacked, ready)` used by queue alarm type.
49+
50+
Specific attribute for `disk` alarm
51+
52+
* `value_calculation` - Disk value threshold calculation, `(fixed, percentage)` of disk space
53+
remaining.
54+
55+
## Dependency
56+
57+
This data source depends on CloudAMQP instance identifier, `cloudamqp_instance.instance.id`.
58+
59+
## Alarm Types
60+
61+
`cpu, memory, disk, queue, connection, flow, consumer, netsplit, server_unreachable, notice`

0 commit comments

Comments
 (0)