|
| 1 | +package ns1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + ns1 "gopkg.in/ns1/ns1-go.v2/rest" |
| 8 | + "gopkg.in/ns1/ns1-go.v2/rest/model/alerting" |
| 9 | +) |
| 10 | + |
| 11 | +func alertResource() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Schema: map[string]*schema.Schema{ |
| 14 | + // Required |
| 15 | + "name": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Required: true, |
| 18 | + }, |
| 19 | + "type": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + // ValidateFunc: validatePath, |
| 23 | + // DiffSuppressFunc: caseSensitivityDiffSuppress, |
| 24 | + }, |
| 25 | + "subtype": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + // ValidateFunc: validateURL, |
| 29 | + // DiffSuppressFunc: caseSensitivityDiffSuppress, |
| 30 | + }, |
| 31 | + // Read-only |
| 32 | + "id": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + }, |
| 36 | + "created_at": { |
| 37 | + Type: schema.TypeInt, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "updated_at": { |
| 41 | + Type: schema.TypeInt, |
| 42 | + Computed: true, |
| 43 | + }, |
| 44 | + "created_by": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "updated_by": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + // Optional |
| 53 | + "notification_lists": { |
| 54 | + Type: schema.TypeSet, |
| 55 | + Optional: true, |
| 56 | + Elem: &schema.Schema{ |
| 57 | + Type: schema.TypeString, |
| 58 | + }, |
| 59 | + }, |
| 60 | + "zone_names": { |
| 61 | + Type: schema.TypeSet, |
| 62 | + Optional: true, |
| 63 | + Elem: &schema.Schema{ |
| 64 | + Type: schema.TypeString, |
| 65 | + }, |
| 66 | + ConflictsWith: []string{"record_ids"}, |
| 67 | + }, |
| 68 | + "record_ids": { |
| 69 | + Type: schema.TypeSet, |
| 70 | + Optional: true, |
| 71 | + Elem: &schema.Schema{ |
| 72 | + Type: schema.TypeString, |
| 73 | + }, |
| 74 | + ConflictsWith: []string{"zone_names"}, |
| 75 | + }, |
| 76 | + }, |
| 77 | + Create: AlertConfigCreate, |
| 78 | + Read: AlertConfigRead, |
| 79 | + Update: AlertConfigUpdate, |
| 80 | + Delete: AlertConfigDelete, |
| 81 | + Importer: &schema.ResourceImporter{}, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func alertToResourceData(d *schema.ResourceData, alert *alerting.Alert) error { |
| 86 | + d.SetId(*alert.ID) |
| 87 | + d.Set("name", alert.Name) |
| 88 | + d.Set("type", alert.Type) |
| 89 | + d.Set("subtype", alert.Subtype) |
| 90 | + d.Set("created_at", alert.CreatedAt) |
| 91 | + d.Set("updated_at", alert.UpdatedAt) |
| 92 | + d.Set("created_by", alert.CreatedBy) |
| 93 | + d.Set("updated_by", alert.UpdatedBy) |
| 94 | + d.Set("notification_lists", alert.NotifierListIds) |
| 95 | + d.Set("zone_names", alert.ZoneNames) |
| 96 | + d.Set("record_ids", alert.RecordIds) |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +func strPtr(str string) *string { |
| 101 | + return &str |
| 102 | +} |
| 103 | + |
| 104 | +func int64Ptr(n int) *int64 { |
| 105 | + n64 := int64(n) |
| 106 | + return &n64 |
| 107 | +} |
| 108 | + |
| 109 | +func resourceDataToAlert(d *schema.ResourceData) (*alerting.Alert, error) { |
| 110 | + alert := alerting.Alert{ |
| 111 | + ID: strPtr(d.Id()), |
| 112 | + } |
| 113 | + if v, ok := d.GetOk("name"); ok { |
| 114 | + alert.Name = strPtr(v.(string)) |
| 115 | + } |
| 116 | + if v, ok := d.GetOk("type"); ok { |
| 117 | + alert.Type = strPtr(v.(string)) |
| 118 | + } |
| 119 | + if v, ok := d.GetOk("subtype"); ok { |
| 120 | + alert.Subtype = strPtr(v.(string)) |
| 121 | + } |
| 122 | + if v, ok := d.GetOk("created_at"); ok { |
| 123 | + alert.CreatedAt = int64Ptr(v.(int)) |
| 124 | + } |
| 125 | + if v, ok := d.GetOk("updated_at"); ok { |
| 126 | + alert.UpdatedAt = int64Ptr(v.(int)) |
| 127 | + } |
| 128 | + if v, ok := d.GetOk("created_by"); ok { |
| 129 | + alert.CreatedBy = strPtr(v.(string)) |
| 130 | + } |
| 131 | + if v, ok := d.GetOk("updated_by"); ok { |
| 132 | + alert.UpdatedBy = strPtr(v.(string)) |
| 133 | + } |
| 134 | + if v, ok := d.GetOk("notification_lists"); ok { |
| 135 | + listIds := v.(*schema.Set) |
| 136 | + alert.NotifierListIds = make([]string, 0, listIds.Len()) |
| 137 | + for _, id := range listIds.List() { |
| 138 | + alert.NotifierListIds = append(alert.NotifierListIds, id.(string)) |
| 139 | + } |
| 140 | + } else { |
| 141 | + alert.NotifierListIds = []string{} |
| 142 | + } |
| 143 | + if v, ok := d.GetOk("zone_names"); ok { |
| 144 | + zoneNames := v.(*schema.Set) |
| 145 | + alert.ZoneNames = make([]string, 0, zoneNames.Len()) |
| 146 | + for _, zone := range zoneNames.List() { |
| 147 | + alert.ZoneNames = append(alert.ZoneNames, zone.(string)) |
| 148 | + } |
| 149 | + } else { |
| 150 | + alert.ZoneNames = []string{} |
| 151 | + } |
| 152 | + if v, ok := d.GetOk("record_ids"); ok { |
| 153 | + recordIds := v.(*schema.Set) |
| 154 | + alert.RecordIds = make([]string, 0, recordIds.Len()) |
| 155 | + for _, id := range recordIds.List() { |
| 156 | + alert.RecordIds = append(alert.RecordIds, id.(string)) |
| 157 | + } |
| 158 | + } else { |
| 159 | + alert.RecordIds = []string{} |
| 160 | + } |
| 161 | + return &alert, nil |
| 162 | +} |
| 163 | + |
| 164 | +func AlertConfigCreate(d *schema.ResourceData, meta interface{}) error { |
| 165 | + client := meta.(*ns1.Client) |
| 166 | + |
| 167 | + var alert *alerting.Alert = nil |
| 168 | + alert, err := resourceDataToAlert(d) |
| 169 | + if err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + |
| 173 | + if resp, err := client.Alerts.Create(alert); err != nil { |
| 174 | + return ConvertToNs1Error(resp, err) |
| 175 | + } |
| 176 | + |
| 177 | + return alertToResourceData(d, alert) |
| 178 | +} |
| 179 | + |
| 180 | +func AlertConfigRead(d *schema.ResourceData, meta interface{}) error { |
| 181 | + client := meta.(*ns1.Client) |
| 182 | + |
| 183 | + alert, resp, err := client.Alerts.Get(d.Id()) |
| 184 | + if err != nil { |
| 185 | + if err == ns1.ErrAlertMissing { |
| 186 | + log.Printf("[DEBUG] NS1 alert (%s) not found", d.Id()) |
| 187 | + d.SetId("") |
| 188 | + return nil |
| 189 | + } |
| 190 | + |
| 191 | + return ConvertToNs1Error(resp, err) |
| 192 | + } |
| 193 | + |
| 194 | + return alertToResourceData(d, alert) |
| 195 | +} |
| 196 | + |
| 197 | +func AlertConfigUpdate(d *schema.ResourceData, meta interface{}) error { |
| 198 | + client := meta.(*ns1.Client) |
| 199 | + |
| 200 | + alert, err := resourceDataToAlert(d) |
| 201 | + if err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + |
| 205 | + if resp, err := client.Alerts.Update(alert); err != nil { |
| 206 | + return ConvertToNs1Error(resp, err) |
| 207 | + } |
| 208 | + |
| 209 | + return alertToResourceData(d, alert) |
| 210 | +} |
| 211 | + |
| 212 | +func AlertConfigDelete(d *schema.ResourceData, meta interface{}) error { |
| 213 | + client := meta.(*ns1.Client) |
| 214 | + |
| 215 | + resp, err := client.Alerts.Delete(d.Id()) |
| 216 | + d.SetId("") |
| 217 | + return ConvertToNs1Error(resp, err) |
| 218 | +} |
0 commit comments