Skip to content

Commit 689e66a

Browse files
authored
refactor: allow multiple key=value for not found diagnostics (#1436)
Some resource are queried using more than one criteria. This allows to reuse the not found diagnostic helper and provide the users all the search criteria that resulted in a not found.
1 parent 165e55f commit 689e66a

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

internal/util/hcloudutil/error_framework.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,26 @@ func APIErrorIsNotFound(err error) bool {
7878
return false
7979
}
8080

81-
func NotFoundDiagnostic(resourceName string, key string, value any) diag.Diagnostic {
82-
return diag.NewErrorDiagnostic(
83-
"Resource not found",
84-
fmt.Sprintf("Resource (%s) was not found: %s=%s", resourceName, key, fmt.Sprint(value)),
85-
)
81+
func NotFoundDiagnostic(resourceName string, values ...any) diag.Diagnostic {
82+
b := &strings.Builder{}
83+
84+
fmt.Fprintf(b, "Resource (%s) was not found", resourceName)
85+
86+
if len(values) > 0 {
87+
fmt.Fprint(b, ":")
88+
// len(values) == 1: value
89+
// len(values) == 2: key=value
90+
// len(values) == 3: value key=value
91+
// len(values) == 4: key=value key=value
92+
offset := 0
93+
if len(values)%2 != 0 {
94+
offset = 1
95+
fmt.Fprintf(b, " %v", values[0])
96+
}
97+
for i := offset; i < len(values); i += 2 {
98+
fmt.Fprintf(b, " %s=%v", values[i], values[i+1])
99+
}
100+
}
101+
102+
return diag.NewErrorDiagnostic("Resource not found", b.String())
86103
}

internal/util/hcloudutil/error_framework_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@ func TestNotFoundDiagnostics(t *testing.T) {
152152
actual: NotFoundDiagnostic("ssh_key", "name", "my-ssh-key"),
153153
expected: diag.NewErrorDiagnostic("Resource not found", "Resource (ssh_key) was not found: name=my-ssh-key"),
154154
},
155+
{
156+
name: "ssh key with 1 arg",
157+
actual: NotFoundDiagnostic("ssh_key", "arg1"),
158+
expected: diag.NewErrorDiagnostic("Resource not found", "Resource (ssh_key) was not found: arg1"),
159+
},
160+
{
161+
name: "ssh key with 2 args",
162+
actual: NotFoundDiagnostic("ssh_key", "arg1", "arg2"),
163+
expected: diag.NewErrorDiagnostic("Resource not found", "Resource (ssh_key) was not found: arg1=arg2"),
164+
},
165+
{
166+
name: "ssh key with 3 args",
167+
actual: NotFoundDiagnostic("ssh_key", "arg1", "arg2", "arg3"),
168+
expected: diag.NewErrorDiagnostic("Resource not found", "Resource (ssh_key) was not found: arg1 arg2=arg3"),
169+
},
170+
{
171+
name: "ssh key with 4 args",
172+
actual: NotFoundDiagnostic("ssh_key", "arg1", "arg2", "arg3", "arg4"),
173+
expected: diag.NewErrorDiagnostic("Resource not found", "Resource (ssh_key) was not found: arg1=arg2 arg3=arg4"),
174+
},
155175
} {
156176
t.Run(testCase.name, func(t *testing.T) {
157177
assert.Equal(t, testCase.expected, testCase.actual)

0 commit comments

Comments
 (0)