Skip to content

Commit 8a14545

Browse files
toppercodesampagent
andcommitted
Normalize default dashboard owner during import
Amp-Thread-ID: https://ampcode.com/threads/T-019cb38a-d07d-75bf-9176-bc810a11c485 Co-authored-by: Amp <amp@ampcode.com>
1 parent b9aed91 commit 8a14545

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

axiom/resource_dashboard.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var dashboardServerManagedFields = map[string]struct{}{
2828
"updatedBy": {},
2929
}
3030

31+
const dashboardDefaultOwner = "x-axiom-everyone"
32+
3133
var (
3234
_ resource.Resource = &DashboardResource{}
3335
_ resource.ResourceWithImportState = &DashboardResource{}
@@ -447,6 +449,8 @@ func normalizeDashboardRaw(raw json.RawMessage, configured types.String) (string
447449
normalizeConfiguredOwnerCase(parsed, configuredMap)
448450
}
449451

452+
removeDefaultOwnerIfUnconfigured(parsed, configuredMap, hasConfiguredMap)
453+
450454
for field := range dashboardServerManagedFields {
451455
delete(parsed, field)
452456
}
@@ -533,6 +537,21 @@ func normalizeConfiguredOwnerCase(parsed, configured map[string]any) {
533537
}
534538
}
535539

540+
func removeDefaultOwnerIfUnconfigured(parsed, configured map[string]any, hasConfiguredMap bool) {
541+
owner, ok := stringValueFromMap(parsed, "owner")
542+
if !ok || !strings.EqualFold(owner, dashboardDefaultOwner) {
543+
return
544+
}
545+
546+
if hasConfiguredMap {
547+
if _, configuredHasOwner := configured["owner"]; configuredHasOwner {
548+
return
549+
}
550+
}
551+
552+
delete(parsed, "owner")
553+
}
554+
536555
func addDashboardUpdateError(resp *resource.UpdateResponse, err error, uid string, localVersion int64) {
537556
addDashboardWriteErrorDiagnostics(&resp.Diagnostics, err, uid, localVersion)
538557
}

axiom/resource_dashboard_integration_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ func TestAccAxiomDashboardResource_WithProvidedUID(t *testing.T) {
6363
ImportState: true,
6464
ImportStateVerify: true,
6565
ImportStateId: uid,
66-
ImportStateVerifyIgnore: []string{
67-
"dashboard",
68-
},
69-
ImportStateCheck: testAccCheckImportedDashboardState(uid, updatedName, nil),
66+
ImportStateCheck: testAccCheckImportedDashboardState(uid, updatedName, nil),
7067
},
7168
},
7269
})
@@ -103,7 +100,7 @@ func TestAccAxiomDashboardResource_ServerGeneratedUID(t *testing.T) {
103100
),
104101
},
105102
{
106-
Config: testAccAxiomDashboardConfigWithoutUID(updatedName, true),
103+
Config: testAccAxiomDashboardConfigWithoutUID(updatedName, false),
107104
Check: resource.ComposeTestCheckFunc(
108105
testAccCheckAxiomResourcesExist(client, resourceName),
109106
resource.TestCheckResourceAttrWith(resourceName, "uid", func(v string) error {
@@ -133,10 +130,6 @@ func TestAccAxiomDashboardResource_ServerGeneratedUID(t *testing.T) {
133130
return generatedUID, nil
134131
},
135132
ImportStateVerify: true,
136-
ImportStateVerifyIgnore: []string{
137-
"dashboard",
138-
"overwrite",
139-
},
140133
ImportStateCheck: testAccCheckImportedDashboardState(generatedUID, updatedName, func(v string) error {
141134
if v != "false" {
142135
return fmt.Errorf("expected imported overwrite to default to false, got %q", v)

axiom/resource_dashboard_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ func TestNormalizeDashboardRaw_RemovesEmptyOverridesWhenConfigUnavailable(t *tes
123123
}
124124
}
125125

126+
func TestNormalizeDashboardRaw_RemovesDefaultOwnerWhenConfigUnavailable(t *testing.T) {
127+
got, err := normalizeDashboardRaw(
128+
json.RawMessage(`{"name":"dashboard","owner":"X-AXIOM-EVERYONE"}`),
129+
types.StringNull(),
130+
)
131+
if err != nil {
132+
t.Fatalf("expected no error, got %v", err)
133+
}
134+
if strings.Contains(got, `"owner"`) {
135+
t.Fatalf("expected default owner to be removed when not configured, got %s", got)
136+
}
137+
}
138+
126139
func TestNormalizeDashboardRaw_PreservesConfiguredOwnerCasing(t *testing.T) {
127140
got, err := normalizeDashboardRaw(
128141
json.RawMessage(`{"name":"dashboard","owner":"x-axiom-everyone"}`),

0 commit comments

Comments
 (0)