Skip to content

Commit 1ae05a4

Browse files
2 parents 12ac5cc + 0d5832f commit 1ae05a4

File tree

8 files changed

+231
-38
lines changed

8 files changed

+231
-38
lines changed

grafana/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func Provider() terraform.ResourceProvider {
3838
"grafana_folder_permission": ResourceFolderPermission(),
3939
"grafana_organization": ResourceOrganization(),
4040
"grafana_team": ResourceTeam(),
41+
"grafana_team_preferences": ResourceTeamPreferences(),
4142
"grafana_user": ResourceUser(),
4243
},
4344

grafana/resource_dashboard.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func ResourceDashboard() *schema.Resource {
2727
Computed: true,
2828
},
2929

30+
"dashboard_id": {
31+
Type: schema.TypeInt,
32+
Computed: true,
33+
},
34+
3035
"folder": {
3136
Type: schema.TypeInt,
3237
Optional: true,
@@ -86,6 +91,7 @@ func ReadDashboard(d *schema.ResourceData, meta interface{}) error {
8691
configJSON := NormalizeDashboardConfigJSON(string(configJSONBytes))
8792

8893
d.SetId(dashboard.Meta.Slug)
94+
d.Set("dashboard_id", dashboard.Id)
8995
d.Set("slug", dashboard.Meta.Slug)
9096
d.Set("config_json", configJSON)
9197
d.Set("folder", dashboard.Folder)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package grafana
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform/helper/resource"
7+
"github.com/hashicorp/terraform/terraform"
8+
)
9+
10+
func TestAccTeamPreferences_basic(t *testing.T) {
11+
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
CheckDestroy: testAccTeamPreferencesCheckDestroy(),
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccTeamPreferencesConfig_Basic,
19+
Check: resource.ComposeAggregateTestCheckFunc(
20+
resource.TestCheckResourceAttr("grafana_team_preferences.testTeamPreferences", "theme", "dark"),
21+
resource.TestCheckResourceAttr("grafana_team_preferences.testTeamPreferences", "timezone", "utc"),
22+
),
23+
},
24+
{
25+
Config: testAccTeamPreferencesConfig_Update,
26+
Check: resource.ComposeAggregateTestCheckFunc(
27+
resource.TestCheckResourceAttr("grafana_team_preferences.testTeamPreferences", "theme", "light"),
28+
resource.TestCheckResourceAttr("grafana_team_preferences.testTeamPreferences", "timezone", "browser"),
29+
),
30+
},
31+
},
32+
})
33+
}
34+
35+
func testAccTeamPreferencesCheckDestroy() resource.TestCheckFunc {
36+
return func(s *terraform.State) error {
37+
//you can't really destroy team preferences so nothing to check for
38+
return nil
39+
}
40+
}
41+
42+
const testAccTeamPreferencesConfig_Basic = `
43+
resource "grafana_team" "testTeam" {
44+
name = "terraform-test-team-preferences"
45+
}
46+
47+
resource "grafana_dashboard" "test" {
48+
dashboard_id = 13
49+
config_json = <<EOT
50+
{
51+
"title": "Terraform Team Preferences Acceptance Test",
52+
"id": 13,
53+
"version": "43"
54+
}
55+
EOT
56+
}
57+
58+
resource "grafana_team_preferences" "testTeamPreferences" {
59+
team_id = grafana_team.testTeam.id
60+
theme = "dark"
61+
home_dashboard_id = grafana_dashboard.test.dashboard_id
62+
timezone = "utc"
63+
}
64+
`
65+
const testAccTeamPreferencesConfig_Update = `
66+
resource "grafana_team_preferences" "testTeamPreferences" {
67+
team_id = grafana_team.testTeam.id
68+
theme = "light"
69+
home_dashboard_id = grafana_dashboard.test.dashboard_id
70+
timezone = "browser"
71+
}
72+
`

vendor/github.com/grafana/grafana-api-golang-client/.drone.jsonnet

Lines changed: 0 additions & 38 deletions
This file was deleted.

website/docs/r/dashboard.html.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ In addition to all arguments above, the following attributes are exported:
4747
* `slug` - A URL "slug" for this dashboard, generated by Grafana by removing
4848
certain characters from the dashboard name given as part of the `config_json`
4949
argument. This can be used to generate the URL for a dashboard.
50+
* `dashboard_id` - The numeric ID of the dashboard computed by Grafana.
5051

5152
## Import
5253

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
layout: "grafana"
3+
page_title: "Grafana: grafana_team_preferences"
4+
sidebar_current: "docs-grafana-resource-team-preferences"
5+
description: |-
6+
The grafana_team_preferences resource allows Team Preferences to be maintained.
7+
---
8+
9+
# grafana\_team\_preferences
10+
11+
The team preferences resource allows for team preferences to be set once a team
12+
has been created.
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "grafana_dashboard" "metrics" {
18+
config_json = "${file("grafana-dashboard.json")}"
19+
}
20+
21+
resource "grafana_team" "team" {
22+
name = "Team Name"
23+
}
24+
25+
resource "grafana_team_preferences" "teamPreferences" {
26+
team_id = grafana_team.team.id
27+
theme = "dark"
28+
timezone = "browser"
29+
home_dashboard_id = grafana_dashboard.metrics.dashboard_id
30+
}
31+
```
32+
33+
## Argument Reference
34+
35+
The following arguments are supported:
36+
37+
* `team_id` - (Required) The numeric team ID.
38+
* `theme` - (Optional) The theme for the specified team. Available themes are `light`, `dark`, or an empty string for the default theme.
39+
* `timezone` - (Optional) The timezone for the specified team. Available values are `utc`, `browser`, or an empty string for the default.
40+
* `home_dashboard_id` - (Optional) The numeric ID of the dashboard to display when a team member logs in.
41+
42+
## Import
43+
44+
Team preferences cannot be imported.

website/grafana.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
<li<%= sidebar_current("docs-grafana-resource-team") %>>
3535
<a href="/docs/providers/grafana/r/team.html">grafana_team</a>
3636
</li>
37+
<li<%= sidebar_current("docs-grafana-resource-team-preferences") %>>
38+
<a href="/docs/providers/grafana/r/team_preferences.html">grafana_team_preferences</a>
39+
</li>
3740
</ul>
3841
</li>
3942
</ul>

0 commit comments

Comments
 (0)