Skip to content

Commit

Permalink
Merge pull request #3 from nchaulet/fix-ingest-manager-ts-upgrade
Browse files Browse the repository at this point in the history
[Ingest Manager] Fix ingest manager saved object attributes typings
  • Loading branch information
mshustov authored May 30, 2020
2 parents efe7b08 + 315c7a0 commit dea0122
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface AgentConfig extends NewAgentConfig {
revision: number;
}

export type AgentConfigSOAttributes = Omit<AgentConfig, 'id'>;

export type FullAgentConfigDatasource = Pick<
Datasource,
'id' | 'name' | 'namespace' | 'enabled'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ export interface Datasource extends Omit<NewDatasource, 'inputs'> {
created_at: string;
created_by: string;
}

export type DatasourceSOAttributes = Omit<Datasource, 'id'>;
2 changes: 2 additions & 0 deletions x-pack/plugins/ingest_manager/common/types/models/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface NewOutput {
config?: Record<string, any>;
}

export type OutputSOAttributes = NewOutput;

export type Output = NewOutput & {
id: string;
};
29 changes: 17 additions & 12 deletions x-pack/plugins/ingest_manager/server/services/agent_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Datasource,
NewAgentConfig,
AgentConfig,
AgentConfigSOAttributes,
FullAgentConfig,
AgentConfigStatus,
ListWithKuery,
Expand All @@ -39,7 +40,7 @@ class AgentConfigService {
private async _update(
soClient: SavedObjectsClientContract,
id: string,
agentConfig: Partial<AgentConfig>,
agentConfig: Partial<AgentConfigSOAttributes>,
user?: AuthenticatedUser
): Promise<AgentConfig> {
const oldAgentConfig = await this.get(soClient, id, false);
Expand All @@ -57,7 +58,7 @@ class AgentConfigService {
);
}

await soClient.update<AgentConfig>(SAVED_OBJECT_TYPE, id, {
await soClient.update<AgentConfigSOAttributes>(SAVED_OBJECT_TYPE, id, {
...agentConfig,
revision: oldAgentConfig.revision + 1,
updated_at: new Date().toISOString(),
Expand All @@ -70,7 +71,7 @@ class AgentConfigService {
}

public async ensureDefaultAgentConfig(soClient: SavedObjectsClientContract) {
const configs = await soClient.find<AgentConfig>({
const configs = await soClient.find<AgentConfigSOAttributes>({
type: AGENT_CONFIG_SAVED_OBJECT_TYPE,
filter: `${AGENT_CONFIG_SAVED_OBJECT_TYPE}.attributes.is_default:true`,
});
Expand All @@ -83,15 +84,18 @@ class AgentConfigService {
return this.create(soClient, newDefaultAgentConfig);
}

return configs.saved_objects[0].attributes;
return {
id: configs.saved_objects[0],
...configs.saved_objects[0].attributes,
};
}

public async create(
soClient: SavedObjectsClientContract,
agentConfig: NewAgentConfig,
options?: { id?: string; user?: AuthenticatedUser }
): Promise<AgentConfig> {
const newSo = await soClient.create<AgentConfig>(
const newSo = await soClient.create<AgentConfigSOAttributes>(
SAVED_OBJECT_TYPE,
{
...agentConfig,
Expand All @@ -106,15 +110,15 @@ class AgentConfigService {
await this.triggerAgentConfigUpdatedEvent(soClient, 'created', newSo.id);
}

return newSo.attributes;
return { id: newSo.id, ...newSo.attributes };
}

public async get(
soClient: SavedObjectsClientContract,
id: string,
withDatasources: boolean = true
): Promise<AgentConfig | null> {
const agentConfigSO = await soClient.get<AgentConfig>(SAVED_OBJECT_TYPE, id);
const agentConfigSO = await soClient.get<AgentConfigSOAttributes>(SAVED_OBJECT_TYPE, id);
if (!agentConfigSO) {
return null;
}
Expand All @@ -123,7 +127,7 @@ class AgentConfigService {
throw new Error(agentConfigSO.error.message);
}

const agentConfig = agentConfigSO.attributes;
const agentConfig = { id: agentConfigSO.id, ...agentConfigSO.attributes };

if (withDatasources) {
agentConfig.datasources =
Expand All @@ -142,7 +146,7 @@ class AgentConfigService {
): Promise<{ items: AgentConfig[]; total: number; page: number; perPage: number }> {
const { page = 1, perPage = 20, kuery } = options;

const agentConfigs = await soClient.find<AgentConfig>({
const agentConfigs = await soClient.find<AgentConfigSOAttributes>({
type: SAVED_OBJECT_TYPE,
page,
perPage,
Expand All @@ -156,9 +160,10 @@ class AgentConfigService {
});

return {
items: agentConfigs.saved_objects.map<AgentConfig>(
(agentConfigSO) => agentConfigSO.attributes
),
items: agentConfigs.saved_objects.map<AgentConfig>((agentConfigSO) => ({
id: agentConfigSO.id,
...agentConfigSO.attributes,
})),
total: agentConfigs.total,
page,
perPage,
Expand Down
27 changes: 18 additions & 9 deletions x-pack/plugins/ingest_manager/server/services/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
PackageInfo,
} from '../../common';
import { DATASOURCE_SAVED_OBJECT_TYPE } from '../constants';
import { NewDatasource, Datasource, ListWithKuery } from '../types';
import { NewDatasource, Datasource, ListWithKuery, DatasourceSOAttributes } from '../types';
import { agentConfigService } from './agent_config';
import { getPackageInfo, getInstallation } from './epm/packages';
import { outputService } from './output';
Expand All @@ -32,7 +32,7 @@ class DatasourceService {
options?: { id?: string; user?: AuthenticatedUser }
): Promise<Datasource> {
const isoDate = new Date().toISOString();
const newSo = await soClient.create<Omit<Datasource, 'id'>>(
const newSo = await soClient.create<DatasourceSOAttributes>(
SAVED_OBJECT_TYPE,
{
...datasource,
Expand All @@ -57,7 +57,7 @@ class DatasourceService {
}

public async get(soClient: SavedObjectsClientContract, id: string): Promise<Datasource | null> {
const datasourceSO = await soClient.get<Datasource>(SAVED_OBJECT_TYPE, id);
const datasourceSO = await soClient.get<DatasourceSOAttributes>(SAVED_OBJECT_TYPE, id);
if (!datasourceSO) {
return null;
}
Expand All @@ -66,14 +66,17 @@ class DatasourceService {
throw new Error(datasourceSO.error.message);
}

return datasourceSO.attributes;
return {
id: datasourceSO.id,
...datasourceSO.attributes,
};
}

public async getByIDs(
soClient: SavedObjectsClientContract,
ids: string[]
): Promise<Datasource[] | null> {
const datasourceSO = await soClient.bulkGet<Datasource>(
const datasourceSO = await soClient.bulkGet<DatasourceSOAttributes>(
ids.map((id) => ({
id,
type: SAVED_OBJECT_TYPE,
Expand All @@ -83,7 +86,10 @@ class DatasourceService {
return null;
}

return datasourceSO.saved_objects.map((so) => so.attributes);
return datasourceSO.saved_objects.map((so) => ({
id: so.id,
...so.attributes,
}));
}

public async list(
Expand All @@ -92,7 +98,7 @@ class DatasourceService {
): Promise<{ items: Datasource[]; total: number; page: number; perPage: number }> {
const { page = 1, perPage = 20, kuery } = options;

const datasources = await soClient.find<Datasource>({
const datasources = await soClient.find<DatasourceSOAttributes>({
type: SAVED_OBJECT_TYPE,
page,
perPage,
Expand All @@ -106,7 +112,10 @@ class DatasourceService {
});

return {
items: datasources.saved_objects.map<Datasource>((datasourceSO) => datasourceSO.attributes),
items: datasources.saved_objects.map<Datasource>((datasourceSO) => ({
id: datasourceSO.id,
...datasourceSO.attributes,
})),
total: datasources.total,
page,
perPage,
Expand All @@ -125,7 +134,7 @@ class DatasourceService {
throw new Error('Datasource not found');
}

await soClient.update<Datasource>(SAVED_OBJECT_TYPE, id, {
await soClient.update<DatasourceSOAttributes>(SAVED_OBJECT_TYPE, id, {
...datasource,
revision: oldDatasource.revision + 1,
updated_at: new Date().toISOString(),
Expand Down
23 changes: 15 additions & 8 deletions x-pack/plugins/ingest_manager/server/services/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { SavedObjectsClientContract } from 'src/core/server';
import { NewOutput, Output } from '../types';
import { NewOutput, Output, OutputSOAttributes } from '../types';
import { DEFAULT_OUTPUT, OUTPUT_SAVED_OBJECT_TYPE } from '../constants';
import { appContextService } from './app_context';
import { decodeCloudId } from '../../common';
Expand All @@ -13,7 +13,7 @@ const SAVED_OBJECT_TYPE = OUTPUT_SAVED_OBJECT_TYPE;

class OutputService {
public async ensureDefaultOutput(soClient: SavedObjectsClientContract) {
const outputs = await soClient.find<Output>({
const outputs = await soClient.find<OutputSOAttributes>({
type: OUTPUT_SAVED_OBJECT_TYPE,
filter: `${OUTPUT_SAVED_OBJECT_TYPE}.attributes.is_default:true`,
});
Expand Down Expand Up @@ -44,7 +44,7 @@ class OutputService {
id: string,
data: Partial<NewOutput>
) {
await soClient.update<NewOutput>(SAVED_OBJECT_TYPE, id, data);
await soClient.update<OutputSOAttributes>(SAVED_OBJECT_TYPE, id, data);
}

public async getDefaultOutputId(soClient: SavedObjectsClientContract) {
Expand All @@ -67,7 +67,7 @@ class OutputService {
}
const so = await appContextService
.getEncryptedSavedObjects()
?.getDecryptedAsInternalUser<Output>(OUTPUT_SAVED_OBJECT_TYPE, defaultOutputId);
?.getDecryptedAsInternalUser<OutputSOAttributes>(OUTPUT_SAVED_OBJECT_TYPE, defaultOutputId);

if (!so || !so.attributes.fleet_enroll_username || !so.attributes.fleet_enroll_password) {
return null;
Expand All @@ -84,35 +84,41 @@ class OutputService {
output: NewOutput,
options?: { id?: string }
): Promise<Output> {
const newSo = await soClient.create<Output>(SAVED_OBJECT_TYPE, output as Output, options);
const newSo = await soClient.create<OutputSOAttributes>(
SAVED_OBJECT_TYPE,
output as Output,
options
);

return {
id: newSo.id,
...newSo.attributes,
};
}

public async get(soClient: SavedObjectsClientContract, id: string): Promise<Output> {
const outputSO = await soClient.get<Output>(SAVED_OBJECT_TYPE, id);
const outputSO = await soClient.get<OutputSOAttributes>(SAVED_OBJECT_TYPE, id);

if (outputSO.error) {
throw new Error(outputSO.error.message);
}

return {
id: outputSO.id,
...outputSO.attributes,
};
}

public async update(soClient: SavedObjectsClientContract, id: string, data: Partial<Output>) {
const outputSO = await soClient.update<Output>(SAVED_OBJECT_TYPE, id, data);
const outputSO = await soClient.update<OutputSOAttributes>(SAVED_OBJECT_TYPE, id, data);

if (outputSO.error) {
throw new Error(outputSO.error.message);
}
}

public async list(soClient: SavedObjectsClientContract) {
const outputs = await soClient.find<Output>({
const outputs = await soClient.find<OutputSOAttributes>({
type: SAVED_OBJECT_TYPE,
page: 1,
perPage: 1000,
Expand All @@ -121,6 +127,7 @@ class OutputService {
return {
items: outputs.saved_objects.map<Output>((outputSO) => {
return {
id: outputSO.id,
...outputSO.attributes,
};
}),
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/ingest_manager/server/types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ export {
AgentActionSOAttributes,
Datasource,
NewDatasource,
DatasourceSOAttributes,
FullAgentConfigDatasource,
FullAgentConfig,
AgentConfig,
AgentConfigSOAttributes,
NewAgentConfig,
AgentConfigStatus,
DataStream,
Output,
NewOutput,
OutputSOAttributes,
OutputType,
EnrollmentAPIKey,
EnrollmentAPIKeySOAttributes,
Expand Down

0 comments on commit dea0122

Please sign in to comment.