Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/Upgrade weekStart preference for Teams and Organizations #1237

Merged
merged 6 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/data-sources/organization_preferences.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ data "grafana_organization_preferences" "test" {}
- `id` (String) The ID of this resource.
- `theme` (String) The Organization theme. Available values are `light`, `dark`, `system`, or an empty string for the default.
- `timezone` (String) The Organization timezone. Available values are `utc`, `browser`, or an empty string for the default.
- `week_start` (String) The Organization week start.
- `week_start` (String) The Organization week start day. Available values are `sunday`, `monday`, `saturday`, or an empty string for the default.
1 change: 1 addition & 0 deletions docs/data-sources/team.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Read-Only:
- `home_dashboard_uid` (String)
- `theme` (String)
- `timezone` (String)
- `week_start` (String)


<a id="nestedatt--team_sync"></a>
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/organization_preferences.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ description: |-
resource "grafana_organization_preferences" "test" {
theme = "light"
timezone = "utc"
week_start = "Tuesday"
week_start = "sunday"
}
```

Expand All @@ -31,7 +31,7 @@ resource "grafana_organization_preferences" "test" {
- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
- `theme` (String) The Organization theme. Available values are `light`, `dark`, `system`, or an empty string for the default.
- `timezone` (String) The Organization timezone. Available values are `utc`, `browser`, or an empty string for the default.
- `week_start` (String) The Organization week start.
- `week_start` (String) The Organization week start day. Available values are `sunday`, `monday`, `saturday`, or an empty string for the default. Defaults to ``.

### Read-Only

Expand Down
1 change: 1 addition & 0 deletions docs/resources/team.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Optional:
- `home_dashboard_uid` (String) The UID of the dashboard to display when a team member logs in. Defaults to ``.
- `theme` (String) The default theme for this team. Available themes are `light`, `dark`, `system`, or an empty string for the default theme. Defaults to ``.
- `timezone` (String) The default timezone for this team. Available values are `utc`, `browser`, or an empty string for the default. Defaults to ``.
- `week_start` (String) The default week start day for this team. Available values are `sunday`, `monday`, `saturday`, or an empty string for the default. Defaults to ``.


<a id="nestedblock--team_sync"></a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "grafana_organization_preferences" "test" {
theme = "light"
timezone = "utc"
week_start = "Tuesday"
week_start = "sunday"
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ func ResourceOrganizationPreferences() *schema.Resource {
Description: "The Organization timezone. Available values are `utc`, `browser`, or an empty string for the default.",
ValidateFunc: validation.StringInSlice([]string{"utc", "browser", ""}, false),
},
// TODO: add validation?
"week_start": {
Type: schema.TypeString,
Optional: true,
Description: "The Organization week start.",
Type: schema.TypeString,
Optional: true,
Description: "The Organization week start day. Available values are `sunday`, `monday`, `saturday`, or an empty string for the default.",
ValidateFunc: validation.StringInSlice([]string{"sunday", "monday", "saturday", ""}, false),
Cleymax marked this conversation as resolved.
Show resolved Hide resolved
Default: "",
},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ func testAccResourceOrganizationPreferences(t *testing.T, withUID bool) {
prefs := models.Preferences{
Theme: "light",
Timezone: "utc",
WeekStart: "Monday",
WeekStart: "monday",
}
updatedPrefs := models.Preferences{
Theme: "dark",
Timezone: "utc",
WeekStart: "Tuesday",
Cleymax marked this conversation as resolved.
Show resolved Hide resolved
WeekStart: "sunday",
}
finalPrefs := models.Preferences{
Theme: "",
Timezone: "browser",
WeekStart: "Monday",
WeekStart: "saturday",
}
emptyPrefs := models.Preferences{
Theme: "",
Expand Down
13 changes: 11 additions & 2 deletions internal/resources/grafana/resource_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ Team Sync can be provisioned using [grafana_team_external_group resource](https:
Description: "The default timezone for this team. Available values are `utc`, `browser`, or an empty string for the default.",
Default: "",
},
"week_start": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"sunday", "monday", "saturday", ""}, false),
Description: "The default week start day for this team. Available values are `sunday`, `monday`, `saturday`, or an empty string for the default.",
Default: "",
},
},
},
},
Expand Down Expand Up @@ -220,12 +227,13 @@ func readTeamFromID(client *goapi.GrafanaHTTPAPI, teamID int64, d *schema.Resour
})
}

if preferences.Theme+preferences.Timezone+preferences.HomeDashboardUID != "" {
if preferences.Theme+preferences.Timezone+preferences.HomeDashboardUID+preferences.WeekStart != "" {
d.Set("preferences", []map[string]interface{}{
{
"theme": preferences.Theme,
"home_dashboard_uid": preferences.HomeDashboardUID,
"timezone": preferences.Timezone,
"week_start": preferences.WeekStart,
},
})
}
Expand Down Expand Up @@ -272,11 +280,12 @@ func DeleteTeam(ctx context.Context, d *schema.ResourceData, meta interface{}) d
}

func updateTeamPreferences(client *goapi.GrafanaHTTPAPI, teamID int64, d *schema.ResourceData) diag.Diagnostics {
if d.IsNewResource() || d.HasChanges("preferences.0.theme", "preferences.0.home_dashboard_uid", "preferences.0.timezone") {
if d.IsNewResource() || d.HasChanges("preferences.0.theme", "preferences.0.home_dashboard_uid", "preferences.0.timezone", "preferences.0.week_start") {
body := models.UpdatePrefsCmd{
Theme: d.Get("preferences.0.theme").(string),
HomeDashboardUID: d.Get("preferences.0.home_dashboard_uid").(string),
Timezone: d.Get("preferences.0.timezone").(string),
WeekStart: d.Get("preferences.0.week_start").(string),
}
_, err := client.Teams.UpdateTeamPreferences(strconv.FormatInt(teamID, 10), &body)
return diag.FromErr(err)
Expand Down
3 changes: 3 additions & 0 deletions internal/resources/grafana/resource_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func TestAccTeam_preferences(t *testing.T) {
resource.TestCheckNoResourceAttr("grafana_team.test", "preferences.0.home_dashboard_uid"),
resource.TestCheckNoResourceAttr("grafana_team.test", "preferences.0.theme"),
resource.TestCheckNoResourceAttr("grafana_team.test", "preferences.0.timezone"),
resource.TestCheckNoResourceAttr("grafana_team.test", "preferences.0.week_start"),
),
},
{
Expand All @@ -89,6 +90,7 @@ func TestAccTeam_preferences(t *testing.T) {
resource.TestMatchResourceAttr("grafana_team.test", "preferences.0.home_dashboard_uid", common.UIDRegexp),
resource.TestCheckResourceAttr("grafana_team.test", "preferences.0.theme", "dark"),
resource.TestCheckResourceAttr("grafana_team.test", "preferences.0.timezone", "utc"),
resource.TestCheckResourceAttr("grafana_team.test", "preferences.0.week_start", "monday"),
),
},
{
Expand Down Expand Up @@ -323,6 +325,7 @@ func testAccTeamDefinition(name string, teamMembers []string, withPreferences bo
theme = "dark"
timezone = "utc"
home_dashboard_uid = grafana_dashboard.test.uid
week_start = "monday"
}
`
}
Expand Down
Loading