Skip to content

Commit

Permalink
grafana_organization_preferences: Managed by SA token (#1476)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
julienduchesne authored Apr 10, 2024
1 parent 549e3e2 commit e58c3df
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
18 changes: 4 additions & 14 deletions internal/resources/grafana/resource_organization_preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,8 @@ func CreateOrganizationPreferences(ctx context.Context, d *schema.ResourceData,
}

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

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

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

if _, err := client.OrgPreferences.UpdateOrgPreferences(&models.UpdatePrefsCmd{}); err != nil {
return diag.FromErr(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,45 @@ func TestAccResourceOrganizationPreferences_WithDashboardUID(t *testing.T) {
testAccResourceOrganizationPreferences(t, true)
}

// Tests that the org preferences can be managed with a service account (managing org prefs for its own org)
func TestAccResourceOrganizationPreferences_OrgScoped(t *testing.T) {
testutils.CheckOSSTestsEnabled(t, ">=9.0.0")
orgID := orgScopedTest(t)

resource.Test(t, resource.TestCase{
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
resource "grafana_dashboard" "test" {
config_json = jsonencode({
title = "test-org-prefs"
uid = "test-org-prefs"
})
}
resource "grafana_organization_preferences" "test" {
theme = "dark"
timezone = "browser"
week_start = "saturday"
home_dashboard_uid = grafana_dashboard.test.uid
}`,
Check: resource.ComposeTestCheckFunc(
testAccCheckOrganizationPreferences(&models.OrgDetailsDTO{ID: orgID}, models.Preferences{
Theme: "dark",
Timezone: "browser",
WeekStart: "saturday",
}),
resource.TestCheckResourceAttr("grafana_organization_preferences.test", "theme", "dark"),
resource.TestCheckResourceAttr("grafana_organization_preferences.test", "timezone", "browser"),
resource.TestCheckResourceAttr("grafana_organization_preferences.test", "week_start", "saturday"),
resource.TestCheckResourceAttr("grafana_organization_preferences.test", "home_dashboard_uid", "test-org-prefs"),
),
},
},
})
}

func testAccResourceOrganizationPreferences(t *testing.T, withUID bool) {
t.Helper()

Expand Down

0 comments on commit e58c3df

Please sign in to comment.