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 2 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
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
38 changes: 37 additions & 1 deletion 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 inputs: 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) {
inputs.push(await getConfig(pkgkey, dataset));
}
}

await Promise.all([
installTemplates(pkg, callCluster),
saveDatasourceReferences({
Expand All @@ -47,6 +56,7 @@ export async function createDatasource(options: {
datasets,
toSave,
request,
inputs,
}),
]);

Expand All @@ -73,8 +83,9 @@ async function saveDatasourceReferences(options: {
datasourceName: string;
toSave: AssetReference[];
request: Request;
inputs: string[];
}) {
const { savedObjectsClient, pkg, toSave, datasets, datasourceName, request } = options;
const { savedObjectsClient, pkg, toSave, datasets, datasourceName, request, inputs } = options;
const savedDatasource = await getDatasource({ savedObjectsClient, pkg });
const savedAssets = savedDatasource?.package.assets;
const assetsReducer = (current: Asset[] = [], pending: Asset) => {
Expand All @@ -90,6 +101,8 @@ async function saveDatasourceReferences(options: {
datasets,
assets: toInstall,
});

datasource.inputs = inputs;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jfsiii I'm adding this here in the hope this also gets stored as part of the datasource which is then available for fleet.

Copy link
Member

Choose a reason for hiding this comment

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

@ruflin you need to update /x-pack/legacy/plugins/ingest/server/mappings.ts too, but I think we are able to build streams here

// ideally we'd call .create from /x-pack/legacy/plugins/ingest/server/libs/datasources.ts#L22
// or something similar, but it's a class not an object so many pieces are missing
// we'd still need `user` from the request object, but that's not terrible
Expand Down Expand Up @@ -184,3 +197,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
2 changes: 2 additions & 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,8 @@ export interface Datasource extends SavedObjectAttributes {
package: Package;
read_alias?: string;
streams: Stream[];
// TODO: These are the input parts of the streams. To be remove again when we have the streams
inputs: string[];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nchaulet My temporary hack to get the inputs into the datasource. This is an array of input configs. Each must be completed with the output to become a full stream.

Copy link
Member

Choose a reason for hiding this comment

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

@ruflin what about assigning `output_id: 'default', as we only support default output for now so we can have streams directly?

}

/**
Expand Down