Skip to content

Conversation

@tehbooom
Copy link
Member

@tehbooom tehbooom commented Sep 9, 2025

Exports saved objects with the following

data "elasticstack_kibana_export_saved_objects" "example" {
  space_id                = "default"
  exclude_export_details  = true
  include_references_deep = true
  objects = jsonencode([
    {
      "type" : "dashboard",
      "id" : "7c5f07ee-7e41-4d50-ae1f-dfe54cc87209"
    }
  ])
}

output "saved_objects" {
  value = data.elasticstack_kibana_export_saved_objects.example.exported_objects
}

Tested on version 9.1.0

This PR will close #688

Copy link
Member

@tobio tobio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to add this one in! We'd want this implemented with the terraform-plugin-framework rather than the old SDKv2, similarly to how import_saved_objects is implemented.

@tehbooom
Copy link
Member Author

tehbooom commented Sep 10, 2025

@tobio running make lint and make docs-generate shows no change for me and is passing locally.

I'm not sure why CI is failing

I see you changed the generation here 0da7e29 should be fixed now. disregard

Copy link
Member

@tobio tobio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of comments, but they all stem back to using a richer input schema. You'll need to regenerate the docs again after the schema change too :)

Otherwise, LGTM

Comment on lines 24 to 27
"objects": schema.StringAttribute{
Description: "JSON-encoded list of objects to export. Each object should have 'type' and 'id' fields.",
Required: true,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"objects": schema.StringAttribute{
Description: "JSON-encoded list of objects to export. Each object should have 'type' and 'id' fields.",
Required: true,
},
"objects": schema.ListNestedAttribute{
Description: "List of objects to export.",
Required: true,
Validators: []validator.List{
listvalidator.SizeAtLeast(1),
},
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
Description: "The type of the saved object.",
Required: true,
},
"id": schema.StringAttribute{
Description: "The ID of the saved object.",
Required: true,
},
},
},
},

We can make this much simpler for users by fully defining the input schema here.

Comment on lines 40 to 67
var objectsList kbapi.PostSavedObjectsExportJSONBodyHasReference1
objectsJSON := config.Objects.ValueString()

var rawObjects []map[string]interface{}
if err := json.Unmarshal([]byte(objectsJSON), &rawObjects); err != nil {
resp.Diagnostics.AddError("Invalid objects JSON", fmt.Sprintf("Error parsing objects JSON: %v", err))
return
}

for _, obj := range rawObjects {
id, ok := obj["id"].(string)
if !ok {
resp.Diagnostics.AddError("Invalid object", "Object missing 'id' field")
return
}
objType, ok := obj["type"].(string)
if !ok {
resp.Diagnostics.AddError("Invalid object", "Object missing 'type' field")
return
}
objectsList = append(objectsList, struct {
Id string `json:"id"`
Type string `json:"type"`
}{
Id: id,
Type: objType,
})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the schema change above

Suggested change
var objectsList kbapi.PostSavedObjectsExportJSONBodyHasReference1
objectsJSON := config.Objects.ValueString()
var rawObjects []map[string]interface{}
if err := json.Unmarshal([]byte(objectsJSON), &rawObjects); err != nil {
resp.Diagnostics.AddError("Invalid objects JSON", fmt.Sprintf("Error parsing objects JSON: %v", err))
return
}
for _, obj := range rawObjects {
id, ok := obj["id"].(string)
if !ok {
resp.Diagnostics.AddError("Invalid object", "Object missing 'id' field")
return
}
objType, ok := obj["type"].(string)
if !ok {
resp.Diagnostics.AddError("Invalid object", "Object missing 'type' field")
return
}
objectsList = append(objectsList, struct {
Id string `json:"id"`
Type string `json:"type"`
}{
Id: id,
Type: objType,
})
}
objectsList := utils.ListTypeToSlice(ctx, config.Objects, path.Root("objects"), &resp.Diagnostics, func(item objectModel, meta utils.ListMeta) struct {
Id string `json:"id"`
Type string `json:"type"`
} {
return struct {
Id string `json:"id"`
Type string `json:"type"`
}{
Id: item.ID.ValueString(),
Type: item.Type.ValueString(),
}
})

ExcludeExportDetails types.Bool `tfsdk:"exclude_export_details"`
IncludeReferencesDeep types.Bool `tfsdk:"include_references_deep"`
ExportedObjects types.String `tfsdk:"exported_objects"`
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the schema change above

Suggested change
}
}
type objectModel struct {
Type types.String `tfsdk:"type"`
ID types.String `tfsdk:"id"`
}

Comment on lines 47 to 52
objects = jsonencode([
{
"type": "action",
"id": elasticstack_kibana_action_connector.test.connector_id
}
])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
objects = jsonencode([
{
"type": "action",
"id": elasticstack_kibana_action_connector.test.connector_id
}
])
objects = [
{
type = "action",
id = elasticstack_kibana_action_connector.test.connector_id
}
]

type dataSourceModel struct {
ID types.String `tfsdk:"id"`
SpaceID types.String `tfsdk:"space_id"`
Objects types.String `tfsdk:"objects"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Objects types.String `tfsdk:"objects"`
Objects types.List `tfsdk:"objects"`

Copy link
Member

@tobio tobio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 thanks for adding this

@tobio tobio merged commit ecc0e89 into elastic:main Oct 2, 2025
50 of 51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Export saved objects

2 participants