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

[EPM] Implement getConfig for dataset #53261

Merged
merged 9 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions x-pack/legacy/plugins/epm/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export enum InstallationStatus {
}

export type ServiceName = 'kibana' | 'elasticsearch';
export type AssetType = KibanaAssetType | ElasticsearchAssetType;
export type AssetType = KibanaAssetType | ElasticsearchAssetType | AgentAssetType;

export enum KibanaAssetType {
dashboard = 'dashboard',
Expand All @@ -31,6 +31,10 @@ export enum ElasticsearchAssetType {
ilmPolicy = 'ilm-policy',
}

export enum AgentAssetType {
input = 'input',
}

// from /package/{name}
// type Package struct at https://github.com/elastic/package-registry/blob/master/util/package.go
// https://github.com/elastic/package-registry/blob/master/docs/api/package.json
Expand Down Expand Up @@ -120,12 +124,17 @@ export interface Dataset {
name: string;
release: string;
ingeset_pipeline: string;
vars: object[];
vars: VarsEntry[];
type: string;
// This is for convenience and not in the output from the registry. When creating a dataset, this info should be added.
package: string;
}

export interface VarsEntry {
name: string;
default: string;
}

// some properties are optional in Registry responses but required in EPM
// internal until we need them
interface PackageAdditions {
Expand Down
32 changes: 32 additions & 0 deletions x-pack/legacy/plugins/epm/server/datasources/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { installTemplates } from '../lib/elasticsearch/template/install';
import { getPackageInfo, PackageNotInstalledError } from '../packages';
import * as Registry from '../registry';
import { Request } from '../types';
import { createInput } from '../lib/agent/agent';

export async function createDatasource(options: {
savedObjectsClient: SavedObjectsClientContract;
Expand All @@ -38,6 +39,14 @@ export async function createDatasource(options: {
await baseSetup(callCluster);
const pkg = await Registry.fetchInfo(pkgkey);

// Collect the config for each dataset
const configs: string[] = [];
if (pkg.datasets) {
Copy link
Contributor

Choose a reason for hiding this comment

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

pkg.datasets refers to all the datasets that come with the package whereas datasets are the ones the user selected.

for (const dataset of pkg.datasets) {
configs.push(await getConfig(pkgkey, dataset));
}
}

await Promise.all([
installTemplates(pkg, callCluster),
saveDatasourceReferences({
Expand Down Expand Up @@ -184,3 +193,26 @@ async function ingestDatasourceCreate({
},
}).then(response => response.json());
}

async function getConfig(pkgkey: string, dataset: Dataset): Promise<string> {
const vars = dataset.vars;

// This searches for the /agent/input.yml file
const paths = await Registry.getArchiveInfo(pkgkey, (entry: Registry.ArchiveEntry) =>
isDatasetInput(entry, dataset.name)
);

if (paths.length === 1) {
const buffer = Registry.getAsset(paths[0]);
// Load input template from path
return createInput(vars, buffer.toString());
}
return '';
}

const isDatasetInput = ({ path }: Registry.ArchiveEntry, datasetName: string) => {
const pathParts = Registry.pathParts(path);
return !isDirectory({ path }) && pathParts.type === 'input' && pathParts.dataset === datasetName;
};

const isDirectory = ({ path }: Registry.ArchiveEntry) => path.endsWith('/');
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/epm/server/lib/agent/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('test converting input and manifest into template', () => {
);

const inputTemplate = fs.readFileSync(path.join(__dirname, 'tests/input.yml'), 'utf8');
const output = createInput(manifest, inputTemplate);
const output = createInput(manifest.vars, inputTemplate);

// Golden file path
const generatedFile = path.join(__dirname, './tests/input.generated.yaml');
Expand Down
15 changes: 4 additions & 11 deletions x-pack/legacy/plugins/epm/server/lib/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,16 @@
*/

import Handlebars from 'handlebars';
import { VarsEntry } from '../../../common/types';

interface Manifest {
vars: VarsEntry[];
}

interface VarsEntry {
name: string;
default: string;
}
/**
* This takes a manifest object as input and merges it with the input template.
* This takes a dataset object as input and merges it with the input template.
* It returns the resolved template as a string.
*/
export function createInput(manifest: Manifest, inputTemplate: string): string {
export function createInput(vars: VarsEntry[], inputTemplate: string): string {
const view: Record<VarsEntry['name'], VarsEntry['default']> = {};

for (const v of manifest.vars) {
for (const v of vars) {
view[v.name] = v.default;
}

Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/ingest/server/libs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface Datasource extends SavedObjectAttributes {
package: Package;
read_alias?: string;
streams: Stream[];
config: string[];
}

/**
Expand Down