Skip to content

Commit ac9b739

Browse files
committed
Fixup error handling during saved object imports
1 parent ce3d47c commit ac9b739

File tree

1 file changed

+34
-7
lines changed
  • internal/kibana/import_saved_objects

1 file changed

+34
-7
lines changed

internal/kibana/import_saved_objects/create.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package import_saved_objects
22

33
import (
44
"context"
5+
"fmt"
6+
"strings"
57

68
"github.com/google/uuid"
79
"github.com/hashicorp/terraform-plugin-framework/diag"
@@ -71,7 +73,23 @@ func (r *Resource) importObjects(ctx context.Context, plan tfsdk.Plan, state *tf
7173
}
7274

7375
if !respModel.Success && !model.IgnoreImportErrors.ValueBool() {
74-
diags.AddError("not all objects were imported successfully", "see errors attribute for more details")
76+
var detail strings.Builder
77+
for i, err := range respModel.Errors {
78+
detail.WriteString(fmt.Sprintf("import error [%d]: %s\n", i, err))
79+
}
80+
detail.WriteString("see the `errors` attribute for the full resposne")
81+
82+
if respModel.SuccessCount > 0 {
83+
diags.AddWarning(
84+
"not all objects were imported successfully",
85+
detail.String(),
86+
)
87+
} else {
88+
diags.AddError(
89+
"no objects imported successfully",
90+
detail.String(),
91+
)
92+
}
7593
}
7694
}
7795

@@ -90,15 +108,24 @@ type importSuccess struct {
90108
}
91109

92110
type importError struct {
93-
ID string `json:"id"`
94-
Type string `json:"type"`
95-
Title string `json:"title"`
96-
Error importErrorType `json:"error"`
97-
Meta importMeta `json:"meta"`
111+
ID string `tfsdk:"id" json:"id"`
112+
Type string `tfsdk:"type" json:"type"`
113+
Title string `tfsdk:"title" json:"title"`
114+
Error importErrorType `tfsdk:"error" json:"error"`
115+
Meta importMeta `tfsdk:"meta" json:"meta"`
116+
}
117+
118+
func (ie importError) String() string {
119+
title := ie.Title
120+
if title == "" {
121+
title = ie.Meta.Title
122+
}
123+
124+
return fmt.Sprintf("[%s] error on [%s] with ID [%s] and title [%s]", ie.Error.Type, ie.Type, ie.ID, title)
98125
}
99126

100127
type importErrorType struct {
101-
Type string `json:"type"`
128+
Type string `tfsdk:"type" json:"type"`
102129
}
103130

104131
type importMeta struct {

0 commit comments

Comments
 (0)