Hi there,
my team and I are developing a custom Kibana plugin and during the initialization phase we'd like to import some custom Saved Objects into a new space.
Basically, we managed to create the space during the initialization phase, requiring the spaces
x-pack plugin in the kibana.json
file and adding the following lines:
let namespacesResponse = await this.externalPluginsService.getExternalPlugins().spaces?.spacesService?.createSpacesClient(request).getAll();
const namespaces = namespacesResponse?.map((namespace: GetSpaceResult) => namespace.id) as string[];
if (!namespaces.includes('my_custom_space')) await this.externalPluginsService.getExternalPlugins().spaces?.spacesService?.createSpacesClient(request).create(INIT_QUERIES.INIT_EPI_SPACE);
Now we'd like to import what's inside a ndjson file into that new space, basically the equivalent of switching to that space in the Kibana console, go to Management>Saved Object and import a given ndjson file or of making the following cURL:
curl -XPOST "http://localhost:5601/ayo/s/my_custom_space/api/saved_objects/_import" -H "kbn-xsrf: true" --form file=@./server/services/my_custom_savedobjects.ndjson -u elastic
Is there a straightforward way to achieve such a result?
What we had to do was to install a new module (fs-ndjson), use its readFile method to read from a given path, and use the getImporter method as follows:
const importer = await context.core.savedObjects.getImporter(context.core.savedObjects.getClient());
importer.import({
overwrite: true,
createNewCopies: false,
readStream: this.createReadableStreamFromArray(epi_array)
})
Unfortunately, it seems to only work with the Default space. In fact, if we try to add the namespace
option to the import
method (defined as optional in the SavedObjectsImportOptions
interface), it always returns the Spaces currently determines the namespaces error, thrown by the throwErrorIfNamespaceSpecified
of the checkConflicts
function, defined (in the x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.ts file) as follows
/**
* Check what conflicts will result when creating a given array of saved objects. This includes "unresolvable conflicts", which are
* multi-namespace objects that exist in a different namespace; such conflicts cannot be resolved/overwritten.
*
* @param objects
* @param options
*/
public async checkConflicts(
objects: SavedObjectsCheckConflictsObject[] = [],
options: SavedObjectsBaseOptions = {}
) {
throwErrorIfNamespaceSpecified(options);
return await this.client.checkConflicts(objects, {
...options,
namespace: spaceIdToNamespace(this.spaceId),
});
}
Why is there a namespace
option - which perfectly makes sense in order to pick the space you want to import your saved objects to - but you cannot use it without throwing that error? What are we doing wrong?
Thank you in advance!