|
| 1 | +package grafana |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform/helper/schema" |
| 8 | + |
| 9 | + gapi "github.com/apparentlymart/go-grafana-api" |
| 10 | +) |
| 11 | + |
| 12 | +func ResourceAlertNotification() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Create: CreateAlertNotification, |
| 15 | + Update: UpdateAlertNotification, |
| 16 | + Delete: DeleteAlertNotification, |
| 17 | + Read: ReadAlertNotification, |
| 18 | + |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "id": &schema.Schema{ |
| 21 | + Type: schema.TypeString, |
| 22 | + Computed: true, |
| 23 | + }, |
| 24 | + |
| 25 | + "type": &schema.Schema{ |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + }, |
| 29 | + |
| 30 | + "name": &schema.Schema{ |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + }, |
| 34 | + |
| 35 | + "is_default": &schema.Schema{ |
| 36 | + Type: schema.TypeBool, |
| 37 | + Optional: true, |
| 38 | + Default: false, |
| 39 | + }, |
| 40 | + |
| 41 | + "settings": { |
| 42 | + Type: schema.TypeMap, |
| 43 | + Optional: true, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func CreateAlertNotification(a *schema.ResourceData, meta interface{}) error { |
| 50 | + client := meta.(*gapi.Client) |
| 51 | + |
| 52 | + alertNotification, err := makeAlertNotification(a) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + id, err := client.NewAlertNotification(alertNotification) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + a.SetId(strconv.FormatInt(id, 10)) |
| 63 | + |
| 64 | + return ReadAlertNotification(a, meta) |
| 65 | +} |
| 66 | + |
| 67 | +func UpdateAlertNotification(a *schema.ResourceData, meta interface{}) error { |
| 68 | + client := meta.(*gapi.Client) |
| 69 | + |
| 70 | + alertNotification, err := makeAlertNotification(a) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + return client.UpdateAlertNotification(alertNotification) |
| 76 | +} |
| 77 | + |
| 78 | +func ReadAlertNotification(a *schema.ResourceData, meta interface{}) error { |
| 79 | + client := meta.(*gapi.Client) |
| 80 | + |
| 81 | + idStr := a.Id() |
| 82 | + id, err := strconv.ParseInt(idStr, 10, 64) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("Invalid id: %#v", idStr) |
| 85 | + } |
| 86 | + |
| 87 | + alertNotification, err := client.AlertNotification(id) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + a.Set("id", alertNotification.Id) |
| 93 | + a.Set("is_default", alertNotification.IsDefault) |
| 94 | + a.Set("name", alertNotification.Name) |
| 95 | + a.Set("type", alertNotification.Type) |
| 96 | + a.Set("settings", alertNotification.Settings) |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func DeleteAlertNotification(a *schema.ResourceData, meta interface{}) error { |
| 102 | + client := meta.(*gapi.Client) |
| 103 | + |
| 104 | + idStr := a.Id() |
| 105 | + id, err := strconv.ParseInt(idStr, 10, 64) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("Invalid id: %#v", idStr) |
| 108 | + } |
| 109 | + |
| 110 | + return client.DeleteAlertNotification(id) |
| 111 | +} |
| 112 | + |
| 113 | +func makeAlertNotification(a *schema.ResourceData) (*gapi.AlertNotification, error) { |
| 114 | + idStr := a.Id() |
| 115 | + var id int64 |
| 116 | + var err error |
| 117 | + if idStr != "" { |
| 118 | + id, err = strconv.ParseInt(idStr, 10, 64) |
| 119 | + } |
| 120 | + |
| 121 | + return &gapi.AlertNotification{ |
| 122 | + Id: id, |
| 123 | + Name: a.Get("name").(string), |
| 124 | + Type: a.Get("type").(string), |
| 125 | + IsDefault: a.Get("is_default").(bool), |
| 126 | + Settings: a.Get("settings").(interface{}), |
| 127 | + }, err |
| 128 | +} |
0 commit comments