Skip to content

Commit

Permalink
[Fleet] Move SOAttributes definition to server/types (#157989)
Browse files Browse the repository at this point in the history
Part of elastic/ingest-dev#1753

## Summary
[Fleet] Move SOAttributes definition to server/types

- Decouple all `*SOAttributes` from types in `/common/types` and move
`*SOAttributes` to the server-side typings directory at `/server/types`

### Checklist

- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
criamico and kibanamachine authored May 18, 2023
1 parent ba00502 commit a19921e
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 75 deletions.
6 changes: 2 additions & 4 deletions x-pack/plugins/fleet/common/types/models/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export interface AgentAction extends NewAgentAction {
export interface AgentMetadata {
[x: string]: any;
}

// SO definition for this type is declared in server/types/interfaces
interface AgentBase {
type: AgentType;
active: boolean;
Expand Down Expand Up @@ -124,10 +126,6 @@ export interface Agent extends AgentBase {
metrics?: AgentMetrics;
}

export interface AgentSOAttributes extends AgentBase {
packages?: string[];
}

export interface CurrentUpgrade {
actionId: string;
complete: boolean;
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/fleet/common/types/models/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface NewAgentPolicy {
is_protected?: boolean;
}

// SO definition for this type is declared in server/types/interfaces
export interface AgentPolicy extends Omit<NewAgentPolicy, 'id'> {
id: string;
status: ValueOf<AgentPolicyStatus>;
Expand All @@ -48,8 +49,6 @@ export interface AgentPolicy extends Omit<NewAgentPolicy, 'id'> {
is_protected: boolean;
}

export type AgentPolicySOAttributes = Omit<AgentPolicy, 'id'>;

export interface FullAgentPolicyInputStream {
id: string;
data_stream: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

// SO definition for this type is declared in server/types/interfaces
export interface EnrollmentAPIKey {
id: string;
api_key_id: string;
Expand All @@ -15,8 +16,6 @@ export interface EnrollmentAPIKey {
created_at: string;
}

export type EnrollmentAPIKeySOAttributes = Omit<EnrollmentAPIKey, 'id'>;

// Generated

/**
Expand Down
5 changes: 1 addition & 4 deletions x-pack/plugins/fleet/common/types/models/fleet_proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ interface BaseFleetProxy {
is_preconfigured: boolean;
}

// SO definition for this type is declared in server/types/interfaces
export interface NewFleetProxy extends BaseFleetProxy {
proxy_headers?: Record<string, string | number | boolean> | null;
}

export interface FleetProxy extends NewFleetProxy {
id: string;
}

export interface FleetProxySOAttributes extends BaseFleetProxy {
proxy_headers?: string | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

// SO definition for this type is declared in server/types/interfaces
export interface NewFleetServerHost {
name: string;
host_urls: string[];
Expand All @@ -16,5 +17,3 @@ export interface NewFleetServerHost {
export interface FleetServerHost extends NewFleetServerHost {
id: string;
}

export type FleetServerHostSOAttributes = NewFleetServerHost;
5 changes: 0 additions & 5 deletions x-pack/plugins/fleet/common/types/models/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ export interface NewOutput {
allow_edit?: string[];
}

export type OutputSOAttributes = NewOutput & {
output_id?: string;
ssl?: string | null; // encrypted ssl field
};

export type Output = NewOutput & {
id: string;
};
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/fleet/common/types/models/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export interface UpdatePackagePolicy extends NewPackagePolicy {
version?: string;
}

// SO definition for this type is declared in server/types/interfaces
export interface PackagePolicy extends Omit<NewPackagePolicy, 'inputs'> {
id: string;
inputs: PackagePolicyInput[];
Expand All @@ -105,8 +106,6 @@ export interface PackagePolicy extends Omit<NewPackagePolicy, 'inputs'> {
created_by: string;
}

export type PackagePolicySOAttributes = Omit<PackagePolicy, 'id' | 'version'>;

export type DryRunPackagePolicy = NewPackagePolicy & {
errors?: Array<{ key: string | undefined; message: string }>;
missingVars?: string[];
Expand Down
4 changes: 0 additions & 4 deletions x-pack/plugins/fleet/common/types/models/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* 2.0.
*/

import type { SavedObjectAttributes } from '@kbn/core/public';

export interface BaseSettings {
has_seen_add_data_notice?: boolean;
fleet_server_hosts?: string[];
Expand All @@ -17,5 +15,3 @@ export interface Settings extends BaseSettings {
id: string;
preconfigured_fields?: Array<'fleet_server_hosts'>;
}

export interface SettingsSOAttributes extends BaseSettings, SavedObjectAttributes {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import {
} from '@kbn/core-test-helpers-kbn-server';

import { AGENT_POLICY_INDEX } from '../../common';
import type { PackagePolicySOAttributes, OutputSOAttributes } from '../../common/types';
import type { AgentPolicySOAttributes } from '../types';
import type {
AgentPolicySOAttributes,
PackagePolicySOAttributes,
OutputSOAttributes,
} from '../types';

import { useDockerRegistry, waitForFleetSetup } from './helpers';
import {
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,10 @@ class AgentPolicyService {
const filter = kuery ? normalizeKuery(SAVED_OBJECT_TYPE, kuery) : undefined;
let agentPoliciesSO;
try {
agentPoliciesSO = await soClient.find<AgentPolicySOAttributes>({ ...baseFindParams, filter });
agentPoliciesSO = await soClient.find<AgentPolicySOAttributes>({
...baseFindParams,
filter,
});
} catch (e) {
const isBadRequest = e.output?.statusCode === 400;
const isKQLSyntaxError = e.message?.startsWith('KQLSyntaxError');
Expand Down
26 changes: 0 additions & 26 deletions x-pack/plugins/fleet/server/services/agents/saved_objects.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { SavedObjectsErrorHelpers } from '@kbn/core/server';
import { savedObjectsClientMock } from '@kbn/core/server/mocks';

import { PACKAGES_SAVED_OBJECT_TYPE, PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '../../../../common';
import type { PackagePolicySOAttributes, RegistryPackage } from '../../../../common/types';
import type { RegistryPackage } from '../../../../common/types';
import type { PackagePolicySOAttributes } from '../../../types';

import { createAppContextStartContractMock } from '../../../mocks';
import { appContextService } from '../../app_context';
Expand Down
8 changes: 2 additions & 6 deletions x-pack/plugins/fleet/server/services/epm/packages/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@ import {
SO_SEARCH_LIMIT,
} from '../../../../common/constants';
import { isPackageLimited } from '../../../../common/services';
import type {
PackageUsageStats,
PackagePolicySOAttributes,
Installable,
} from '../../../../common/types';
import type { PackageUsageStats, Installable } from '../../../../common/types';
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../constants';
import type {
ArchivePackage,
RegistryPackage,
EpmPackageAdditions,
GetCategoriesRequest,
} from '../../../../common/types';
import type { Installation, PackageInfo } from '../../../types';
import type { Installation, PackageInfo, PackagePolicySOAttributes } from '../../../types';
import {
FleetError,
PackageFailedVerificationError,
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/fleet/server/services/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ async function validateTypeChanges(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
id: string,
data: Partial<Output>,
data: Nullable<Partial<OutputSOAttributes>>,
originalOutput: Output,
defaultDataOutputId: string | null,
fromPreconfiguration: boolean
Expand Down Expand Up @@ -194,18 +194,18 @@ async function validateTypeChanges(
async function updateFleetServerPoliciesDataOutputId(
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
data: Partial<Output>,
data: Nullable<Partial<OutputSOAttributes>>,
isDefault: boolean,
defaultDataOutputId: string | null,
fleetServerPolicies: AgentPolicy[],
fromPreconfiguration: boolean
) {
// if a logstash output is updated to become default
// if fleet server policies are don't have data_output_id or if they are using the new output
// if fleet server policies don't have data_output_id
// update them to use the default output
if (data?.type === outputType.Logstash && isDefault) {
for (const policy of fleetServerPolicies) {
if (!policy.data_output_id || policy.data_output_id === data?.id) {
if (!policy.data_output_id) {
await agentPolicyService.update(
soClient,
esClient,
Expand Down Expand Up @@ -582,7 +582,7 @@ class OutputService {
soClient,
esClient,
id,
data,
updateData,
originalOutput,
defaultDataOutputId,
fromPreconfiguration
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/fleet/server/services/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type { SavedObjectsClientContract } from '@kbn/core/server';

import { normalizeHostsForAgents } from '../../common/services';
import { GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, GLOBAL_SETTINGS_ID } from '../../common/constants';
import type { SettingsSOAttributes, Settings, BaseSettings } from '../../common/types';
import type { Settings, BaseSettings } from '../../common/types';
import type { SettingsSOAttributes } from '../types';

import { appContextService } from './app_context';
import { listFleetServerHosts } from './fleet_server_host';
Expand Down
9 changes: 1 addition & 8 deletions x-pack/plugins/fleet/server/types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
export type {
Agent,
AgentMetadata,
AgentSOAttributes,
AgentStatus,
AgentType,
AgentAction,
Expand All @@ -22,29 +21,23 @@ export type {
NewPackagePolicy,
UpdatePackagePolicy,
DryRunPackagePolicy,
PackagePolicySOAttributes,
FullAgentPolicyInput,
FullAgentPolicy,
FullAgentPolicyOutput,
AgentPolicy,
AgentPolicySOAttributes,
NewAgentPolicy,
PreconfiguredAgentPolicy,
AgentPolicyStatus,
DataStream,
ShipperOutput,
Output,
NewOutput,
OutputSOAttributes,
OutputType,
EnrollmentAPIKey,
EnrollmentAPIKeySOAttributes,
NewFleetServerHost,
FleetServerHost,
FleetServerHostSOAttributes,
NewFleetProxy,
FleetProxy,
FleetProxySOAttributes,
Installation,
EpmPackageInstallStatus,
InstallationStatus,
Expand Down Expand Up @@ -73,7 +66,6 @@ export type {
TemplateMap,
TemplateMapEntry,
Settings,
SettingsSOAttributes,
InstallType,
InstallSource,
InstallResult,
Expand Down Expand Up @@ -119,4 +111,5 @@ export interface PackageVerificationResult {
export * from './models';
export * from './rest_spec';
export * from './extensions';
export * from './so_attributes';
export type { FleetRequestHandler, FleetRequestHandlerContext } from './request_context';
Loading

0 comments on commit a19921e

Please sign in to comment.