|
| 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 | +} |
0 commit comments