Skip to content

Commit e58c3df

Browse files
grafana_organization_preferences: Managed by SA token (#1476)
Issue: #1475 Service accounts should be able to manage the org prefs for their own org Currently, it crashes with `global scope resources cannot be managed with an API key. Use basic auth instead`
1 parent 549e3e2 commit e58c3df

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

internal/resources/grafana/resource_organization_preferences.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,8 @@ func CreateOrganizationPreferences(ctx context.Context, d *schema.ResourceData,
9999
}
100100

101101
func ReadOrganizationPreferences(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
102-
client, err := OAPIGlobalClient(meta)
103-
if err != nil {
104-
return diag.FromErr(err)
105-
}
106-
if id, _ := strconv.ParseInt(d.Id(), 10, 64); id > 0 {
107-
client = client.WithOrgID(id)
108-
}
102+
id := d.Id() + ":" // Ensure the ID is in the <orgID>:<resourceID> format. A bit hacky but won't survive the migration to plugin framework
103+
client, _, _ := OAPIClientFromExistingOrgResource(meta, id)
109104

110105
resp, err := client.OrgPreferences.GetOrgPreferences()
111106
if err, shouldReturn := common.CheckReadError("organization preferences", d, err); shouldReturn {
@@ -128,13 +123,8 @@ func UpdateOrganizationPreferences(ctx context.Context, d *schema.ResourceData,
128123
}
129124

130125
func DeleteOrganizationPreferences(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
131-
client, err := OAPIGlobalClient(meta)
132-
if err != nil {
133-
return diag.FromErr(err)
134-
}
135-
if id, _ := strconv.ParseInt(d.Id(), 10, 64); id > 0 {
136-
client = client.WithOrgID(id)
137-
}
126+
id := d.Id() + ":" // Ensure the ID is in the <orgID>:<resourceID> format. A bit hacky but won't survive the migration to plugin framework
127+
client, _, _ := OAPIClientFromExistingOrgResource(meta, id)
138128

139129
if _, err := client.OrgPreferences.UpdateOrgPreferences(&models.UpdatePrefsCmd{}); err != nil {
140130
return diag.FromErr(err)

internal/resources/grafana/resource_organization_preferences_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,45 @@ func TestAccResourceOrganizationPreferences_WithDashboardUID(t *testing.T) {
2424
testAccResourceOrganizationPreferences(t, true)
2525
}
2626

27+
// Tests that the org preferences can be managed with a service account (managing org prefs for its own org)
28+
func TestAccResourceOrganizationPreferences_OrgScoped(t *testing.T) {
29+
testutils.CheckOSSTestsEnabled(t, ">=9.0.0")
30+
orgID := orgScopedTest(t)
31+
32+
resource.Test(t, resource.TestCase{
33+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
34+
Steps: []resource.TestStep{
35+
{
36+
Config: `
37+
resource "grafana_dashboard" "test" {
38+
config_json = jsonencode({
39+
title = "test-org-prefs"
40+
uid = "test-org-prefs"
41+
})
42+
}
43+
44+
resource "grafana_organization_preferences" "test" {
45+
theme = "dark"
46+
timezone = "browser"
47+
week_start = "saturday"
48+
home_dashboard_uid = grafana_dashboard.test.uid
49+
}`,
50+
Check: resource.ComposeTestCheckFunc(
51+
testAccCheckOrganizationPreferences(&models.OrgDetailsDTO{ID: orgID}, models.Preferences{
52+
Theme: "dark",
53+
Timezone: "browser",
54+
WeekStart: "saturday",
55+
}),
56+
resource.TestCheckResourceAttr("grafana_organization_preferences.test", "theme", "dark"),
57+
resource.TestCheckResourceAttr("grafana_organization_preferences.test", "timezone", "browser"),
58+
resource.TestCheckResourceAttr("grafana_organization_preferences.test", "week_start", "saturday"),
59+
resource.TestCheckResourceAttr("grafana_organization_preferences.test", "home_dashboard_uid", "test-org-prefs"),
60+
),
61+
},
62+
},
63+
})
64+
}
65+
2766
func testAccResourceOrganizationPreferences(t *testing.T, withUID bool) {
2867
t.Helper()
2968

0 commit comments

Comments
 (0)