-
Notifications
You must be signed in to change notification settings - Fork 222
/
zzz_deprecated_resource_team_preferences.go
121 lines (97 loc) · 3.67 KB
/
zzz_deprecated_resource_team_preferences.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package grafana
import (
"context"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
)
func ResourceTeamPreferences() *schema.Resource {
return &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/administration/organization-preferences/)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/team/)
!> Deprecated: Use the ` + "`preferences`" + ` attribute of the ` + "`grafana_team`" + ` resource instead.
`,
CreateContext: UpdateTeamPreferences,
ReadContext: ReadTeamPreferences,
UpdateContext: UpdateTeamPreferences,
DeleteContext: DeleteTeamPreferences,
DeprecationMessage: "Use the `preferences` attribute of the `grafana_team` resource",
Schema: map[string]*schema.Schema{
"team_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "The numeric team ID.",
},
"theme": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"light", "dark", ""}, false),
Description: "The theme for the specified team. Available themes are `light`, `dark`, or an empty string for the default theme.",
},
"home_dashboard_id": {
Type: schema.TypeInt,
Optional: true,
Description: "The numeric ID of the dashboard to display when a team member logs in.",
},
"timezone": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"utc", "browser", ""}, false),
Description: "The timezone for the specified team. Available values are `utc`, `browser`, or an empty string for the default.",
},
},
}
}
func UpdateTeamPreferences(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaAPI
teamID := int64(d.Get("team_id").(int))
theme := d.Get("theme").(string)
homeDashboardID := int64(d.Get("home_dashboard_id").(int))
timezone := d.Get("timezone").(string)
preferences := gapi.Preferences{
Theme: theme,
HomeDashboardID: homeDashboardID,
Timezone: timezone,
}
err := client.UpdateTeamPreferences(teamID, preferences)
if err != nil {
return diag.FromErr(err)
}
return ReadTeamPreferences(ctx, d, meta)
}
func ReadTeamPreferences(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaAPI
teamID := int64(d.Get("team_id").(int))
preferences, err := client.TeamPreferences(teamID)
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.FormatInt(teamID, 10))
d.Set("theme", preferences.Theme)
d.Set("home_dashboard_id", preferences.HomeDashboardID)
d.Set("timezone", preferences.Timezone)
return nil
}
func DeleteTeamPreferences(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// there is no delete call for team preferences. instead we will just remove
// the specified preferences and go back to the default values. note: if the
// call fails because the team no longer exists - we'll just ignore the error
client := meta.(*common.Client).GrafanaAPI
teamID := int64(d.Get("team_id").(int))
defaultPreferences := gapi.Preferences{}
err := client.UpdateTeamPreferences(teamID, defaultPreferences)
if err != nil {
if strings.HasPrefix(err.Error(), "status: 404") {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
return nil
}