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

[Console] Filter autocomplete endpoints by availability #161781

Merged
3 changes: 3 additions & 0 deletions config/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ xpack.spaces.allowFeatureVisibility: false
# Temporarily allow unauthenticated access to task manager utilization & status/stats APIs for autoscaling
status.allowAnonymous: true
xpack.task_manager.unsafe.authenticate_background_task_utilization: false

# Only display console autocomplete suggestions for ES endpoints that are available in serverless
console.autocompleteDefinitions.endpointsAvailability: serverless
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
AutocompleteBodyParams,
AutocompleteDefinition,
AutocompleteUrlParams,
AutocompleteAvailability,
SpecificationTypes,
} from './types';
import { findTypeDefinition } from './utils';
Expand Down Expand Up @@ -65,6 +66,23 @@ const generateBodyParams = (
return {};
};

const DEFAULT_STACK_ENDPOINT_AVAILABILITY = true;
const DEFAULT_SERVERLESS_ENDPOINT_AVAILABILITY = true;

const generateAvailability = (endpoint: SpecificationTypes.Endpoint): AutocompleteAvailability => {
const availability: AutocompleteAvailability = {
stack: DEFAULT_STACK_ENDPOINT_AVAILABILITY,
serverless: DEFAULT_SERVERLESS_ENDPOINT_AVAILABILITY,
};
if (endpoint.availability.stack?.visibility) {
availability.stack = endpoint.availability.stack?.visibility === 'public';
}
if (endpoint.availability.serverless?.visibility) {
availability.serverless = endpoint.availability.serverless?.visibility === 'public';
}
return availability;
};

const addParams = (
definition: AutocompleteDefinition,
params: { urlParams: AutocompleteUrlParams; bodyParams: AutocompleteBodyParams }
Expand All @@ -86,12 +104,13 @@ const generateDefinition = (
const methods = generateMethods(endpoint);
const patterns = generatePatterns(endpoint);
const documentation = generateDocumentation(endpoint);
const availability = generateAvailability(endpoint);
let definition: AutocompleteDefinition = {};
const params = generateParams(endpoint, schema);
if (params) {
definition = addParams(definition, params);
}
definition = { ...definition, methods, patterns, documentation };
definition = { ...definition, methods, patterns, documentation, availability };

return definition;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ export interface AutocompleteBodyParams {
[key: string]: number | string;
}

export interface AutocompleteAvailability {
stack: boolean;
serverless: boolean;
}

export interface AutocompleteDefinition {
documentation?: string;
methods?: string[];
patterns?: string[];
url_params?: AutocompleteUrlParams;
data_autocomplete_rules?: AutocompleteBodyParams;
availability?: AutocompleteAvailability;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type {
AutocompleteDefinition,
AutocompleteUrlParams,
AutocompleteBodyParams,
AutocompleteAvailability,
} from './autocomplete_definition_types';

export * as SpecificationTypes from './specification_types';
6 changes: 6 additions & 0 deletions src/plugins/console/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ const schemaLatest = schema.object(
ui: schema.object({
enabled: schema.boolean({ defaultValue: true }),
}),
autocompleteDefinitions: schema.object({
// Only displays the endpoints that are available in the specified environment
// Current supported values are 'stack' and 'serverless'
endpointsAvailability: schema.string({ defaultValue: 'stack' }),
}),
},
{ defaultValue: undefined }
);

const configLatest: PluginConfigDescriptor<ConsoleConfig> = {
exposeToBrowser: {
ui: true,
autocompleteDefinitions: true,
},
schema: schemaLatest,
deprecations: () => [],
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/console/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ export class ConsoleServerPlugin implements Plugin<ConsoleSetup, ConsoleStart> {
}

start() {
this.specDefinitionsService.start();
const {
autocompleteDefinitions: { endpointsAvailability: endpointsAvailability },
} = this.ctx.config.get<ConsoleConfig>();
this.specDefinitionsService.start({ endpointsAvailability });
}

stop() {
Expand Down
19 changes: 15 additions & 4 deletions src/plugins/console/server/services/spec_definitions_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ interface EndpointDescription {
data_autocomplete_rules?: Record<string, unknown>;
url_components?: Record<string, unknown>;
priority?: number;
availability?: Record<string, boolean>;
}

export interface SpecDefinitionsDependencies {
endpointsAvailability: string;
}

export class SpecDefinitionsService {
Expand Down Expand Up @@ -92,9 +97,9 @@ export class SpecDefinitionsService {
};
}

public start() {
public start({ endpointsAvailability }: SpecDefinitionsDependencies) {
if (!this.hasLoadedSpec) {
this.loadJsonSpec();
this.loadJsonSpec(endpointsAvailability);
this.loadJSSpec();
this.hasLoadedSpec = true;
} else {
Expand Down Expand Up @@ -127,14 +132,20 @@ export class SpecDefinitionsService {
}, {} as Record<string, EndpointDescription>);
}

private loadJsonSpec() {
private loadJsonSpec(endpointsAvailability: string) {
const result = this.loadJSONSpecInDir(AUTOCOMPLETE_DEFINITIONS_FOLDER);
this.extensionSpecFilePaths.forEach((extensionSpecFilePath) => {
merge(result, this.loadJSONSpecInDir(extensionSpecFilePath));
});

Object.keys(result).forEach((endpoint) => {
this.addEndpointDescription(endpoint, result[endpoint]);
const description = result[endpoint];
const addEndpoint =
(endpointsAvailability === 'stack' && description.availability?.stack) ||
(endpointsAvailability === 'serverless' && description.availability?.serverless);
if (addEndpoint) {
this.addEndpointDescription(endpoint, description);
}
});
}

Expand Down