|
| 1 | +package grafana |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + gapi "github.com/grafana/grafana-api-golang-client" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 13 | +) |
| 14 | + |
| 15 | +func ResourcePlaylist() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + CreateContext: CreatePlaylist, |
| 18 | + ReadContext: ReadPlaylist, |
| 19 | + UpdateContext: UpdatePlaylist, |
| 20 | + DeleteContext: DeletePlaylist, |
| 21 | + Importer: &schema.ResourceImporter{ |
| 22 | + StateContext: schema.ImportStatePassthroughContext, |
| 23 | + }, |
| 24 | + |
| 25 | + Description: ` |
| 26 | + * [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/playlist/) |
| 27 | + * [HTTP API](https://grafana.com/docs/grafana/latest/http_api/playlist/) |
| 28 | + `, |
| 29 | + |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "name": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + ForceNew: true, |
| 35 | + Description: "The name of the playlist.", |
| 36 | + }, |
| 37 | + "interval": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Required: true, |
| 40 | + }, |
| 41 | + "item": { |
| 42 | + Type: schema.TypeSet, |
| 43 | + Required: true, |
| 44 | + Elem: &schema.Resource{ |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "id": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + }, |
| 50 | + "order": { |
| 51 | + Type: schema.TypeInt, |
| 52 | + Required: true, |
| 53 | + }, |
| 54 | + "title": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Required: true, |
| 57 | + }, |
| 58 | + "type": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Optional: true, |
| 61 | + }, |
| 62 | + "value": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Optional: true, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + "org_id": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func CreatePlaylist(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 78 | + client := meta.(*client).gapi |
| 79 | + |
| 80 | + playlist := gapi.Playlist{ |
| 81 | + Name: d.Get("name").(string), |
| 82 | + Interval: d.Get("interval").(string), |
| 83 | + Items: expandPlaylistItems(d.Get("item").(*schema.Set).List()), |
| 84 | + } |
| 85 | + |
| 86 | + id, err := client.NewPlaylist(playlist) |
| 87 | + |
| 88 | + if err != nil { |
| 89 | + return diag.FromErr(fmt.Errorf("error creating Playlist: %w", err)) |
| 90 | + } |
| 91 | + |
| 92 | + d.SetId(strconv.Itoa(id)) |
| 93 | + |
| 94 | + return ReadPlaylist(ctx, d, meta) |
| 95 | +} |
| 96 | + |
| 97 | +func ReadPlaylist(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 98 | + client := meta.(*client).gapi |
| 99 | + |
| 100 | + id, err := strconv.Atoi(d.Id()) |
| 101 | + if err != nil { |
| 102 | + return diag.FromErr(fmt.Errorf("error reading Playlist (%s): %w", d.Id(), err)) |
| 103 | + } |
| 104 | + |
| 105 | + resp, err := client.Playlist(id) |
| 106 | + |
| 107 | + if err != nil { |
| 108 | + if strings.HasPrefix(err.Error(), "status: 404") { |
| 109 | + log.Printf("[WARN] removing playlist %d from state because it no longer exists in grafana", id) |
| 110 | + d.SetId("") |
| 111 | + return nil |
| 112 | + } |
| 113 | + return diag.FromErr(fmt.Errorf("error reading Playlist (%s): %w", d.Id(), err)) |
| 114 | + } |
| 115 | + |
| 116 | + d.Set("name", resp.Name) |
| 117 | + d.Set("interval", resp.Interval) |
| 118 | + if err := d.Set("item", flattenPlaylistItems(resp.Items)); err != nil { |
| 119 | + return diag.FromErr(fmt.Errorf("error setting item: %v", err)) |
| 120 | + } |
| 121 | + |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func UpdatePlaylist(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 126 | + client := meta.(*client).gapi |
| 127 | + |
| 128 | + playlist := gapi.Playlist{ |
| 129 | + Name: d.Get("name").(string), |
| 130 | + Interval: d.Get("interval").(string), |
| 131 | + Items: expandPlaylistItems(d.Get("item").(*schema.Set).List()), |
| 132 | + } |
| 133 | + |
| 134 | + err := client.UpdatePlaylist(playlist) |
| 135 | + if err != nil { |
| 136 | + return diag.FromErr(fmt.Errorf("error updating Playlist (%s): %w", d.Id(), err)) |
| 137 | + } |
| 138 | + |
| 139 | + return ReadPlaylist(ctx, d, meta) |
| 140 | +} |
| 141 | + |
| 142 | +func DeletePlaylist(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 143 | + client := meta.(*client).gapi |
| 144 | + |
| 145 | + id, err := strconv.Atoi(d.Id()) |
| 146 | + |
| 147 | + if err != nil { |
| 148 | + return diag.FromErr(fmt.Errorf("error deleting Playlist (%s): %w", d.Id(), err)) |
| 149 | + } |
| 150 | + |
| 151 | + if err := client.DeletePlaylist(id); err != nil { |
| 152 | + if strings.HasPrefix(err.Error(), "status: 404") { |
| 153 | + return nil |
| 154 | + } |
| 155 | + return diag.FromErr(fmt.Errorf("error deleting Playlist (%s): %w", d.Id(), err)) |
| 156 | + } |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +func expandPlaylistItems(items []interface{}) []gapi.PlaylistItem { |
| 162 | + playlistItems := make([]gapi.PlaylistItem, 0) |
| 163 | + for _, item := range items { |
| 164 | + itemMap := item.(map[string]interface{}) |
| 165 | + p := gapi.PlaylistItem{ |
| 166 | + Order: itemMap["order"].(int), |
| 167 | + Title: itemMap["title"].(string), |
| 168 | + } |
| 169 | + if v, ok := itemMap["type"].(string); ok { |
| 170 | + p.Type = v |
| 171 | + } |
| 172 | + if v, ok := itemMap["value"].(string); ok { |
| 173 | + p.Value = v |
| 174 | + } |
| 175 | + playlistItems = append(playlistItems, p) |
| 176 | + } |
| 177 | + return playlistItems |
| 178 | +} |
| 179 | + |
| 180 | +func flattenPlaylistItems(items []gapi.PlaylistItem) []interface{} { |
| 181 | + playlistItems := make([]interface{}, 0) |
| 182 | + for _, item := range items { |
| 183 | + p := map[string]interface{}{ |
| 184 | + "type": item.Type, |
| 185 | + "value": item.Value, |
| 186 | + "order": item.Order, |
| 187 | + "title": item.Title, |
| 188 | + } |
| 189 | + playlistItems = append(playlistItems, p) |
| 190 | + } |
| 191 | + return playlistItems |
| 192 | +} |
0 commit comments