Skip to content

Commit e9cf446

Browse files
authored
Merge branch 'master' into master
2 parents 271aaf6 + 42597b2 commit e9cf446

File tree

10 files changed

+247
-46
lines changed

10 files changed

+247
-46
lines changed

grafana/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func Provider() terraform.ResourceProvider {
4444
"grafana_folder_permission": ResourceFolderPermission(),
4545
"grafana_organization": ResourceOrganization(),
4646
"grafana_team": ResourceTeam(),
47+
"grafana_team_preferences": ResourceTeamPreferences(),
4748
"grafana_user": ResourceUser(),
4849
},
4950

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,
@@ -89,6 +94,7 @@ func ReadDashboard(d *schema.ResourceData, meta interface{}) error {
8994
d.Set("slug", dashboard.Meta.Slug)
9095
d.Set("config_json", configJSON)
9196
d.Set("folder", dashboard.Folder)
97+
d.Set("dashboard_id", int64(dashboard.Model["id"].(float64)))
9298

9399
return nil
94100
}
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: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
config_json = <<EOT
49+
{
50+
"title": "Terraform Team Preferences Acceptance Test",
51+
"id": 13,
52+
"version": "43"
53+
}
54+
EOT
55+
}
56+
57+
resource "grafana_team_preferences" "testTeamPreferences" {
58+
team_id = grafana_team.testTeam.id
59+
theme = "dark"
60+
home_dashboard_id = grafana_dashboard.test.dashboard_id
61+
timezone = "utc"
62+
}
63+
`
64+
const testAccTeamPreferencesConfig_Update = `
65+
resource "grafana_team" "testTeam" {
66+
name = "terraform-test-team-preferences"
67+
}
68+
69+
resource "grafana_dashboard" "test" {
70+
config_json = <<EOT
71+
{
72+
"title": "Terraform Team Preferences Acceptance Test",
73+
"id": 13,
74+
"version": "43"
75+
}
76+
EOT
77+
}
78+
resource "grafana_team_preferences" "testTeamPreferences" {
79+
team_id = grafana_team.testTeam.id
80+
theme = "light"
81+
home_dashboard_id = grafana_dashboard.test.dashboard_id
82+
timezone = "browser"
83+
}
84+
`

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

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

vendor/github.com/grafana/grafana-api-golang-client/team.go

Lines changed: 2 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ github.com/google/go-cmp/cmp/internal/function
7474
github.com/google/go-cmp/cmp/internal/value
7575
# github.com/googleapis/gax-go/v2 v2.0.3
7676
github.com/googleapis/gax-go/v2
77-
# github.com/grafana/grafana-api-golang-client v0.0.0-20201019145005-e01a63d40166
77+
# github.com/grafana/grafana-api-golang-client v0.0.0-20201026050958-d171908f4835
7878
## explicit
7979
github.com/grafana/grafana-api-golang-client
8080
# github.com/hashicorp/errwrap v1.0.0

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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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. Available preferences are a light or dark theme, the default
13+
timezone to be used, and the dashboard to be displayed upon login.
14+
15+
## Example Usage
16+
17+
```hcl
18+
resource "grafana_dashboard" "metrics" {
19+
config_json = file("grafana-dashboard.json")
20+
}
21+
22+
resource "grafana_team" "team" {
23+
name = "Team Name"
24+
}
25+
26+
resource "grafana_team_preferences" "team_preferences" {
27+
team_id = grafana_team.team.id
28+
theme = "dark"
29+
timezone = "browser"
30+
home_dashboard_id = grafana_dashboard.metrics.dashboard_id
31+
}
32+
```
33+
34+
## Argument Reference
35+
36+
The following arguments are supported:
37+
38+
* `team_id` - (Required) The numeric team ID.
39+
* `theme` - (Optional) The theme for the specified team. Available themes are `light`, `dark`, or an empty string for the default theme.
40+
* `timezone` - (Optional) The timezone for the specified team. Available values are `utc`, `browser`, or an empty string for the default.
41+
* `home_dashboard_id` - (Optional) The numeric ID of the dashboard to display when a team member logs in.
42+
43+
## Import
44+
45+
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)