Skip to content
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

[7.x] [kbn/test] add import/export support to KbnClient (#92526) #92935

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@
"fetch-mock": "^7.3.9",
"file-loader": "^4.2.0",
"file-saver": "^1.3.8",
"form-data": "^4.0.0",
"formsy-react": "^1.1.5",
"geckodriver": "^1.21.0",
"glob-watcher": "5.0.3",
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-dev-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export {
KBN_P12_PATH,
KBN_P12_PASSWORD,
} from './certs';
export * from './kbn_client';
export * from './run';
export * from './axios';
export * from './stdio';
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-es-archiver/src/actions/empty_kibana_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/

import { Client } from '@elastic/elasticsearch';
import { ToolingLog, KbnClient } from '@kbn/dev-utils';
import { ToolingLog } from '@kbn/dev-utils';
import { KbnClient } from '@kbn/test';

import { migrateKibanaIndex, createStats, cleanKibanaIndices } from '../lib';

Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-es-archiver/src/actions/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import { resolve } from 'path';
import { createReadStream } from 'fs';
import { Readable } from 'stream';
import { ToolingLog, KbnClient } from '@kbn/dev-utils';
import { ToolingLog } from '@kbn/dev-utils';
import { KbnClient } from '@kbn/test';
import { Client } from '@elastic/elasticsearch';
import { createPromiseFromStreams, concatStreamProviders } from '@kbn/utils';
import { ES_CLIENT_HEADERS } from '../client_headers';
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-es-archiver/src/actions/unload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { resolve } from 'path';
import { createReadStream } from 'fs';
import { Readable, Writable } from 'stream';
import { Client } from '@elastic/elasticsearch';
import { ToolingLog, KbnClient } from '@kbn/dev-utils';
import { ToolingLog } from '@kbn/dev-utils';
import { KbnClient } from '@kbn/test';
import { createPromiseFromStreams } from '@kbn/utils';

import {
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-es-archiver/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import Url from 'url';
import readline from 'readline';
import Fs from 'fs';

import { RunWithCommands, createFlagError, KbnClient, CA_CERT_PATH } from '@kbn/dev-utils';
import { readConfigFile } from '@kbn/test';
import { RunWithCommands, createFlagError, CA_CERT_PATH } from '@kbn/dev-utils';
import { readConfigFile, KbnClient } from '@kbn/test';
import { Client } from '@elastic/elasticsearch';

import { EsArchiver } from './es_archiver';
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-es-archiver/src/es_archiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/

import { Client } from '@elastic/elasticsearch';
import { ToolingLog, KbnClient } from '@kbn/dev-utils';
import { ToolingLog } from '@kbn/dev-utils';
import { KbnClient } from '@kbn/test';

import {
saveAction,
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-es-archiver/src/lib/indices/kibana_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import { inspect } from 'util';

import { Client } from '@elastic/elasticsearch';
import { ToolingLog, KbnClient } from '@kbn/dev-utils';
import { ToolingLog } from '@kbn/dev-utils';
import { KbnClient } from '@kbn/test';
import { Stats } from '../stats';
import { deleteIndex } from './delete_index';
import { ES_CLIENT_HEADERS } from '../../client_headers';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ export const schema = Joi.object()
})
.default(),

// settings for the saved objects svc
kbnArchiver: Joi.object()
.keys({
directory: Joi.string().default(defaultRelativeToConfigPath('fixtures/kbn_archiver')),
})
.default(),

// settings for the kibanaServer.uiSettings module
uiSettings: Joi.object()
.keys({
Expand Down
4 changes: 4 additions & 0 deletions packages/kbn-test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ export { getUrl } from './jest/utils/get_url';
export { runCheckJestConfigsCli } from './jest/run_check_jest_configs_cli';

export { runJest } from './jest/run';

export * from './kbn_archiver_cli';

export * from './kbn_client';
149 changes: 149 additions & 0 deletions packages/kbn-test/src/kbn_archiver_cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import Path from 'path';
import Url from 'url';

import { RunWithCommands, createFlagError, Flags } from '@kbn/dev-utils';
import { KbnClient } from '@kbn/test';

import { readConfigFile } from './functional_test_runner';

function getSinglePositionalArg(flags: Flags) {
const positional = flags._;
if (positional.length < 1) {
throw createFlagError('missing name of export to import');
}

if (positional.length > 1) {
throw createFlagError(`extra positional arguments, expected 1, got [${positional}]`);
}

return positional[0];
}

function parseTypesFlag(flags: Flags) {
if (!flags.type || (typeof flags.type !== 'string' && !Array.isArray(flags.type))) {
throw createFlagError('--type is a required flag');
}

const types = typeof flags.type === 'string' ? [flags.type] : flags.type;
return types.reduce(
(acc: string[], type) => [...acc, ...type.split(',').map((t) => t.trim())],
[]
);
}

export function runKbnArchiverCli() {
new RunWithCommands({
description: 'Import/export saved objects from archives, for testing',
globalFlags: {
string: ['config', 'space', 'kibana-url', 'dir'],
help: `
--space space id to operate on, defaults to the default space
--config optional path to an FTR config file that will be parsed and used for defaults
--kibana-url set the url that kibana can be reached at, uses the "servers.kibana" setting from --config by default
--dir directory that contains exports to be imported, or where exports will be saved, uses the "kbnArchiver.directory"
setting from --config by default
`,
},
async extendContext({ log, flags }) {
let config;
if (flags.config) {
if (typeof flags.config !== 'string') {
throw createFlagError('expected --config to be a string');
}

config = await readConfigFile(log, Path.resolve(flags.config));
}

let kibanaUrl;
if (flags['kibana-url']) {
if (typeof flags['kibana-url'] !== 'string') {
throw createFlagError('expected --kibana-url to be a string');
}

kibanaUrl = flags['kibana-url'];
} else if (config) {
kibanaUrl = Url.format(config.get('servers.kibana'));
}

if (!kibanaUrl) {
throw createFlagError(
'Either a --config file with `servers.kibana` defined, or a --kibana-url must be passed'
);
}

let importExportDir;
if (flags.dir) {
if (typeof flags.dir !== 'string') {
throw createFlagError('expected --dir to be a string');
}

importExportDir = flags.dir;
} else if (config) {
importExportDir = config.get('kbnArchiver.directory');
}

if (!importExportDir) {
throw createFlagError(
'--config does not include a kbnArchiver.directory, specify it or include --dir flag'
);
}

const space = flags.space;
if (!(space === undefined || typeof space === 'string')) {
throw createFlagError('--space must be a string');
}

return {
space,
kbnClient: new KbnClient({
log,
url: kibanaUrl,
importExportDir,
}),
};
},
})
.command({
name: 'save',
usage: 'save <name>',
description: 'export saved objects from Kibana to a file',
flags: {
string: ['type'],
help: `
--type saved object type that should be fetched and stored in the archive, can
be specified multiple times or be a comma-separated list.
`,
},
async run({ kbnClient, flags, space }) {
await kbnClient.importExport.save(getSinglePositionalArg(flags), {
types: parseTypesFlag(flags),
space,
});
},
})
.command({
name: 'load',
usage: 'load <name>',
description: 'import a saved export to Kibana',
async run({ kbnClient, flags, space }) {
await kbnClient.importExport.load(getSinglePositionalArg(flags), { space });
},
})
.command({
name: 'unload',
usage: 'unload <name>',
description: 'delete the saved objects saved in the archive from the Kibana index',
async run({ kbnClient, flags, space }) {
await kbnClient.importExport.unload(getSinglePositionalArg(flags), { space });
},
})
.execute();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@
* Side Public License, v 1.
*/

import { ToolingLog } from '../tooling_log';
import { ToolingLog } from '@kbn/dev-utils';

import { KbnClientRequester, ReqOptions } from './kbn_client_requester';
import { KbnClientStatus } from './kbn_client_status';
import { KbnClientPlugins } from './kbn_client_plugins';
import { KbnClientVersion } from './kbn_client_version';
import { KbnClientSavedObjects } from './kbn_client_saved_objects';
import { KbnClientUiSettings, UiSettingValues } from './kbn_client_ui_settings';
import { KbnClientImportExport } from './kbn_client_import_export';

export interface KbnClientOptions {
url: string;
certificateAuthorities?: Buffer[];
log: ToolingLog;
uiSettingDefaults?: UiSettingValues;
importExportDir?: string;
}

export class KbnClient {
Expand All @@ -27,6 +30,7 @@ export class KbnClient {
readonly version: KbnClientVersion;
readonly savedObjects: KbnClientSavedObjects;
readonly uiSettings: KbnClientUiSettings;
readonly importExport: KbnClientImportExport;

private readonly requester: KbnClientRequester;
private readonly log: ToolingLog;
Expand Down Expand Up @@ -56,6 +60,12 @@ export class KbnClient {
this.version = new KbnClientVersion(this.status);
this.savedObjects = new KbnClientSavedObjects(this.log, this.requester);
this.uiSettings = new KbnClientUiSettings(this.log, this.requester, this.uiSettingDefaults);
this.importExport = new KbnClientImportExport(
this.log,
this.requester,
this.savedObjects,
options.importExportDir
);
}

/**
Expand Down
Loading