Skip to content

Commit b18ed02

Browse files
authored
Merge branch 'main' into 667-fleet-data-tags
2 parents 2a7ab7c + b58a599 commit b18ed02

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Fix a provider panic when `elasticstack_kibana_action_connector` reads a non-existant connector ([#729](https://github.com/elastic/terraform-provider-elasticstack/pull/729))
66
- Add support for `remote_indicies` to `elasticstack_elasticsearch_security_role` & `elasticstack_kibana_security_role` (#723)[https://github.com/elastic/terraform-provider-elasticstack/pull/723]
77
- Added support for global data tags in agent policy creation and update. Upgraded the generated Fleet API to version 8.15.0. (#730)[https://github.com/elastic/terraform-provider-elasticstack/pull/730]
8+
- Fix error handling in `elasticstack_kibana_import_saved_objects` ([#738](https://github.com/elastic/terraform-provider-elasticstack/pull/738))
89

910
## [0.11.6] - 2024-08-20
1011

internal/kibana/import_saved_objects/acc_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ func TestAccResourceImportSavedObjects(t *testing.T) {
3030
resource.TestCheckResourceAttr("elasticstack_kibana_import_saved_objects.settings", "errors.#", "0"),
3131
),
3232
},
33+
{
34+
// Ensure a partially successful import doesn't throw a provider error
35+
Config: testAccResourceImportSavedObjectsMissingRef(),
36+
Check: resource.ComposeTestCheckFunc(
37+
resource.TestCheckResourceAttr("elasticstack_kibana_import_saved_objects.settings", "success", "false"),
38+
resource.TestCheckResourceAttr("elasticstack_kibana_import_saved_objects.settings", "success_count", "1"),
39+
resource.TestCheckResourceAttr("elasticstack_kibana_import_saved_objects.settings", "success_results.#", "1"),
40+
resource.TestCheckResourceAttr("elasticstack_kibana_import_saved_objects.settings", "errors.#", "1"),
41+
),
42+
},
3343
},
3444
})
3545
}
@@ -67,3 +77,20 @@ EOT
6777
}
6878
`
6979
}
80+
81+
func testAccResourceImportSavedObjectsMissingRef() string {
82+
return `
83+
provider "elasticstack" {
84+
elasticsearch {}
85+
kibana {}
86+
}
87+
88+
resource "elasticstack_kibana_import_saved_objects" "settings" {
89+
file_contents = <<-EOT
90+
{"attributes":{"buildNum":42747,"defaultIndex":"metricbeat-*","theme:darkMode":true},"coreMigrationVersion":"7.0.0","id":"7.14.0","managed":false,"references":[],"type":"config","typeMigrationVersion":"7.0.0","updated_at":"2021-08-04T02:04:43.306Z","version":"WzY1MiwyXQ=="}
91+
{"attributes":{"description":"","kibanaSavedObjectMeta":{"searchSourceJSON":"{\"query\":{\"query\":\"\",\"language\":\"kuery\"},\"filter\":[{\"$state\":{\"store\":\"appState\"},\"meta\":{\"alias\":null,\"disabled\":false,\"key\":\"testName\",\"negate\":false,\"params\":{\"query\":\"perf-features\"},\"type\":\"phrase\",\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index\"},\"query\":{\"match_phrase\":{\"testName\":\"perf-features\"}}},{\"$state\":{\"store\":\"appState\"},\"meta\":{\"alias\":null,\"disabled\":false,\"key\":\"status\",\"negate\":false,\"params\":{\"query\":\"failed\"},\"type\":\"phrase\",\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.filter[1].meta.index\"},\"query\":{\"match_phrase\":{\"status\":\"failed\"}}},{\"$state\":{\"store\":\"appState\"},\"meta\":{\"alias\":null,\"disabled\":false,\"key\":\"targetEnvironment\",\"negate\":false,\"params\":{\"query\":\"dev\"},\"type\":\"phrase\",\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.filter[2].meta.index\"},\"query\":{\"match_phrase\":{\"targetEnvironment\":\"dev\"}}}],\"indexRefName\":\"kibanaSavedObjectMeta.searchSourceJSON.index\"}"},"title":"healthchecks","uiStateJSON":"{}","version":1,"visState":"{\"title\":\"healthchecks\",\"type\":\"histogram\",\"aggs\":[{\"id\":\"1\",\"enabled\":true,\"type\":\"count\",\"params\":{},\"schema\":\"metric\"},{\"id\":\"2\",\"enabled\":true,\"type\":\"date_histogram\",\"params\":{\"field\":\"@timestamp\",\"timeRange\":{\"from\":\"now/d\",\"to\":\"now/d\"},\"useNormalizedEsInterval\":true,\"scaleMetricValues\":false,\"interval\":\"30m\",\"drop_partials\":false,\"min_doc_count\":1,\"extended_bounds\":{}},\"schema\":\"segment\"},{\"id\":\"3\",\"enabled\":true,\"type\":\"terms\",\"params\":{\"field\":\"testName\",\"orderBy\":\"1\",\"order\":\"desc\",\"size\":5,\"otherBucket\":false,\"otherBucketLabel\":\"Other\",\"missingBucket\":false,\"missingBucketLabel\":\"Missing\"},\"schema\":\"group\"}],\"params\":{\"type\":\"histogram\",\"grid\":{\"categoryLines\":false},\"categoryAxes\":[{\"id\":\"CategoryAxis-1\",\"type\":\"category\",\"position\":\"bottom\",\"show\":true,\"style\":{},\"scale\":{\"type\":\"linear\"},\"labels\":{\"show\":true,\"filter\":true,\"truncate\":100},\"title\":{}}],\"valueAxes\":[{\"id\":\"ValueAxis-1\",\"name\":\"LeftAxis-1\",\"type\":\"value\",\"position\":\"left\",\"show\":true,\"style\":{},\"scale\":{\"type\":\"linear\",\"mode\":\"normal\"},\"labels\":{\"show\":true,\"rotate\":0,\"filter\":false,\"truncate\":100},\"title\":{\"text\":\"Count\"}}],\"seriesParams\":[{\"show\":true,\"type\":\"histogram\",\"mode\":\"normal\",\"data\":{\"label\":\"Count\",\"id\":\"1\"},\"valueAxis\":\"ValueAxis-1\",\"drawLinesBetweenPoints\":true,\"lineWidth\":2,\"showCircles\":true}],\"addTooltip\":true,\"addLegend\":true,\"legendPosition\":\"right\",\"times\":[],\"addTimeMarker\":true,\"labels\":{\"show\":false},\"thresholdLine\":{\"show\":false,\"value\":10,\"width\":1,\"style\":\"full\",\"color\":\"#E7664C\"},\"palette\":{\"type\":\"palette\",\"name\":\"kibana_palette\"},\"isVislibVis\":true,\"detailedTooltip\":true,\"legendSize\":\"auto\"}}"},"coreMigrationVersion":"8.8.0","id":"bacba650-0c8e-11eb-977b-cd2574857abe","managed":false,"references":[{"id":"5ba9aac0-0c7e-11ea-b151-954d48d0eae6","name":"kibanaSavedObjectMeta.searchSourceJSON.index","type":"index-pattern"},{"id":"5ba9aac0-0c7e-11ea-b151-954d48d0eae6","name":"kibanaSavedObjectMeta.searchSourceJSON.filter[0].meta.index","type":"index-pattern"},{"id":"5ba9aac0-0c7e-11ea-b151-954d48d0eae6","name":"kibanaSavedObjectMeta.searchSourceJSON.filter[1].meta.index","type":"index-pattern"},{"id":"5ba9aac0-0c7e-11ea-b151-954d48d0eae6","name":"kibanaSavedObjectMeta.searchSourceJSON.filter[2].meta.index","type":"index-pattern"}],"type":"visualization","typeMigrationVersion":"8.5.0","updated_at":"2020-10-12T13:27:52.373Z","version":"WzE2NjgsMl0="}
92+
{"excludedObjects":[],"excludedObjectsCount":0,"exportedCount":22,"missingRefCount":1,"missingReferences":[{"id":"5ba9aac0-0c7e-11ea-b151-954d48d0eae6","type":"index-pattern"}]}
93+
EOT
94+
overwrite = true
95+
}`
96+
}

internal/kibana/import_saved_objects/create.go

+34-7
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)