|
| 1 | +package grafana |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform/helper/schema" |
| 8 | + "github.com/hashicorp/terraform/helper/validation" |
| 9 | + |
| 10 | + gapi "github.com/grafana/grafana-api-golang-client" |
| 11 | +) |
| 12 | + |
| 13 | +func ResourceTeamPreferences() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Create: UpdateTeamPreferences, |
| 16 | + Read: ReadTeamPreferences, |
| 17 | + Update: UpdateTeamPreferences, |
| 18 | + Delete: DeleteTeamPreferences, |
| 19 | + |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "team_id": { |
| 22 | + Type: schema.TypeInt, |
| 23 | + Required: true, |
| 24 | + ForceNew: true, |
| 25 | + }, |
| 26 | + "theme": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Optional: true, |
| 29 | + ValidateFunc: validation.StringInSlice([]string{"light", "dark", ""}, false), |
| 30 | + }, |
| 31 | + "home_dashboard_id": { |
| 32 | + Type: schema.TypeInt, |
| 33 | + Optional: true, |
| 34 | + }, |
| 35 | + "timezone": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + ValidateFunc: validation.StringInSlice([]string{"utc", "browser", ""}, false), |
| 39 | + }, |
| 40 | + }, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func UpdateTeamPreferences(d *schema.ResourceData, meta interface{}) error { |
| 45 | + client := meta.(*gapi.Client) |
| 46 | + |
| 47 | + teamID := int64(d.Get("team_id").(int)) |
| 48 | + theme := d.Get("theme").(string) |
| 49 | + homeDashboardId := int64(d.Get("home_dashboard_id").(int)) |
| 50 | + timezone := d.Get("timezone").(string) |
| 51 | + |
| 52 | + preferences := gapi.Preferences{ |
| 53 | + Theme: theme, |
| 54 | + HomeDashboardId: homeDashboardId, |
| 55 | + Timezone: timezone, |
| 56 | + } |
| 57 | + |
| 58 | + err := client.UpdateTeamPreferences(teamID, preferences) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + return ReadTeamPreferences(d, meta) |
| 64 | +} |
| 65 | + |
| 66 | +func ReadTeamPreferences(d *schema.ResourceData, meta interface{}) error { |
| 67 | + client := meta.(*gapi.Client) |
| 68 | + |
| 69 | + teamID := int64(d.Get("team_id").(int)) |
| 70 | + |
| 71 | + preferences, err := client.TeamPreferences(teamID) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + d.SetId(strconv.FormatInt(teamID, 10)) |
| 77 | + d.Set("theme", preferences.Theme) |
| 78 | + d.Set("home_dashboard_id", preferences.HomeDashboardId) |
| 79 | + d.Set("timezone", preferences.Timezone) |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func DeleteTeamPreferences(d *schema.ResourceData, meta interface{}) error { |
| 85 | + //there is no delete call for team preferences. instead we will just remove |
| 86 | + //the specified preferences and go back to the default values. note: if the |
| 87 | + //call fails because the team no longer exists - we'll just ignore the error |
| 88 | + |
| 89 | + client := meta.(*gapi.Client) |
| 90 | + |
| 91 | + teamID := int64(d.Get("team_id").(int)) |
| 92 | + defaultPreferences := gapi.Preferences{} |
| 93 | + |
| 94 | + err := client.UpdateTeamPreferences(teamID, &defaultPreferences) |
| 95 | + if err != nil { |
| 96 | + if strings.HasPrefix(err.Error(), "status: 404") { |
| 97 | + d.SetId("") |
| 98 | + return nil |
| 99 | + } |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
0 commit comments