[cloud plugin] Add serverless projectName to configuration and contract (#166330)

## Summary

Part of https://github.com/elastic/kibana/issues/166182
Similar to https://github.com/elastic/kibana/pull/161728

Add the `serverless.project_name` config setting to the cloud plugin,
and expose the `serverless.projectName` info from the cloud plugin's
API.
This commit is contained in:
Pierre Gayvallet 2023-09-14 11:28:31 +02:00 committed by GitHub
parent f1fb5e0a7e
commit b1bfe92637
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 88 additions and 3 deletions

View file

@ -230,6 +230,7 @@ export default function ({ getService }: PluginFunctionalProviderContext) {
'xpack.cloud.projects_url (any)', // It's a string (any because schema.conditional)
// can't be used to infer urls or customer id from the outside
'xpack.cloud.serverless.project_id (string)',
'xpack.cloud.serverless.project_name (string)',
'xpack.discoverEnhanced.actions.exploreDataInChart.enabled (boolean)',
'xpack.discoverEnhanced.actions.exploreDataInContextMenu.enabled (boolean)',
'xpack.fleet.agents.enabled (boolean)',

View file

@ -29,6 +29,7 @@ function createSetupMock(): jest.Mocked<CloudSetup> {
isServerlessEnabled: false,
serverless: {
projectId: undefined,
projectName: undefined,
},
};
}

View file

@ -148,6 +148,16 @@ describe('Cloud Plugin', () => {
});
expect(setup.serverless.projectId).toBe('my-awesome-project');
});
it('exposes `serverless.projectName`', () => {
const { setup } = setupPlugin({
serverless: {
project_id: 'my-awesome-project',
project_name: 'My Awesome Project',
},
});
expect(setup.serverless.projectName).toBe('My Awesome Project');
});
});
});
@ -222,5 +232,17 @@ describe('Cloud Plugin', () => {
const start = plugin.start(coreStart);
expect(start.serverless.projectId).toBe('my-awesome-project');
});
it('exposes `serverless.projectName`', () => {
const { plugin } = startPlugin({
serverless: {
project_id: 'my-awesome-project',
project_name: 'My Awesome Project',
},
});
const coreStart = coreMock.createStart();
const start = plugin.start(coreStart);
expect(start.serverless.projectName).toBe('My Awesome Project');
});
});
});

View file

@ -31,6 +31,7 @@ export interface CloudConfigType {
is_elastic_staff_owned?: boolean;
serverless?: {
project_id: string;
project_name?: string;
};
}
@ -91,6 +92,7 @@ export class CloudPlugin implements Plugin<CloudSetup> {
isServerlessEnabled: this.isServerlessEnabled,
serverless: {
projectId: this.config.serverless?.project_id,
projectName: this.config.serverless?.project_name,
},
registerCloudService: (contextProvider) => {
this.contextProviders.push(contextProvider);
@ -145,6 +147,7 @@ export class CloudPlugin implements Plugin<CloudSetup> {
isServerlessEnabled: this.isServerlessEnabled,
serverless: {
projectId: this.config.serverless?.project_id,
projectName: this.config.serverless?.project_name,
},
performanceUrl,
usersAndRolesUrl,

View file

@ -72,6 +72,11 @@ export interface CloudStart {
* Will always be present if `isServerlessEnabled` is `true`
*/
projectId?: string;
/**
* The serverless project name.
* Will always be present if `isServerlessEnabled` is `true`
*/
projectName?: string;
};
}
@ -172,5 +177,10 @@ export interface CloudSetup {
* Will always be present if `isServerlessEnabled` is `true`
*/
projectId?: string;
/**
* The serverless project name.
* Will always be present if `isServerlessEnabled` is `true`
*/
projectName?: string;
};
}

View file

@ -20,6 +20,7 @@ Object {
"projectsUrl": "https://cloud.elastic.co/projects/",
"serverless": Object {
"projectId": undefined,
"projectName": undefined,
},
"trialEndDate": undefined,
}

View file

@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { config } from './config';
describe('Cloud plugin config', () => {
it('evicts unknown properties under the `serverless` structure', () => {
const output = config.schema.validate({
serverless: {
project_id: 'project_id',
unknown_prop: 'some unknown prop',
},
});
expect(output.serverless).toEqual({
project_id: 'project_id',
});
});
});

View file

@ -33,9 +33,14 @@ const configSchema = schema.object({
trial_end_date: schema.maybe(schema.string()),
is_elastic_staff_owned: schema.maybe(schema.boolean()),
serverless: schema.maybe(
schema.object({
project_id: schema.string(),
})
schema.object(
{
project_id: schema.string(),
project_name: schema.maybe(schema.string()),
},
// avoid future chicken-and-egg situation with the component populating the config
{ unknowns: 'ignore' }
)
),
});
@ -57,6 +62,7 @@ export const config: PluginConfigDescriptor<CloudConfigType> = {
is_elastic_staff_owned: true,
serverless: {
project_id: true,
project_name: true,
},
},
schema: configSchema,

View file

@ -28,6 +28,7 @@ function createSetupMock(): jest.Mocked<CloudSetup> {
isServerlessEnabled: false,
serverless: {
projectId: undefined,
projectName: undefined,
},
};
}

View file

@ -124,6 +124,16 @@ describe('Cloud Plugin', () => {
});
expect(setup.serverless.projectId).toBe('my-awesome-project');
});
it('exposes `serverless.projectName`', () => {
const { setup } = setupPlugin({
serverless: {
project_id: 'my-awesome-project',
project_name: 'My Awesome Project',
},
});
expect(setup.serverless.projectName).toBe('My Awesome Project');
});
});
});

View file

@ -106,6 +106,11 @@ export interface CloudSetup {
* Will always be present if `isServerlessEnabled` is `true`
*/
projectId?: string;
/**
* The serverless project name.
* Will always be present if `isServerlessEnabled` is `true`
*/
projectName?: string;
};
}
@ -175,6 +180,7 @@ export class CloudPlugin implements Plugin<CloudSetup, CloudStart> {
isServerlessEnabled,
serverless: {
projectId: this.config.serverless?.project_id,
projectName: this.config.serverless?.project_name,
},
};
}

View file

@ -21,6 +21,7 @@ export const getCloud = ({ isCloudEnabled }: { isCloudEnabled: boolean }) => {
isServerlessEnabled: false,
serverless: {
projectId: undefined,
projectName: undefined,
},
};