-
Notifications
You must be signed in to change notification settings - Fork 122
Add export saved objects data source #1293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
tobio
left a comment
There was a problem hiding this 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.
tobio
left a comment
There was a problem hiding this 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
| "objects": schema.StringAttribute{ | ||
| Description: "JSON-encoded list of objects to export. Each object should have 'type' and 'id' fields.", | ||
| Required: true, | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| "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.
| 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, | ||
| }) | ||
| } |
There was a problem hiding this comment.
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
| 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"` | ||
| } |
There was a problem hiding this comment.
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
| } | |
| } | |
| type objectModel struct { | |
| Type types.String `tfsdk:"type"` | |
| ID types.String `tfsdk:"id"` | |
| } |
| objects = jsonencode([ | ||
| { | ||
| "type": "action", | ||
| "id": elasticstack_kibana_action_connector.test.connector_id | ||
| } | ||
| ]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Objects types.String `tfsdk:"objects"` | |
| Objects types.List `tfsdk:"objects"` |
tobio
left a comment
There was a problem hiding this 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
Exports saved objects with the following
Tested on version 9.1.0
This PR will close #688