Skip to content

Commit

Permalink
use union of strings instead of enum (elastic#62493) (elastic#62582)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
mshustov and elasticmachine committed Apr 6, 2020
1 parent 78a247b commit 379af6d
Show file tree
Hide file tree
Showing 45 changed files with 120 additions and 184 deletions.
3 changes: 1 addition & 2 deletions x-pack/legacy/plugins/maps/server/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { getEcommerceSavedObjects } from './sample_data/ecommerce_saved_objects'
import { getFlightsSavedObjects } from './sample_data/flights_saved_objects.js';
import { getWebLogsSavedObjects } from './sample_data/web_logs_saved_objects.js';
import { registerMapsUsageCollector } from './maps_telemetry/collectors/register';
import { LICENSE_CHECK_STATE } from '../../../../plugins/licensing/server';
import { initRoutes } from './routes';
import { emsBoundariesSpecProvider } from './tutorials/ems';

Expand Down Expand Up @@ -52,7 +51,7 @@ export class MapPlugin {

licensing.license$.subscribe(license => {
const { state } = license.check('maps', 'basic');
if (state === LICENSE_CHECK_STATE.Valid && !routesInitialized) {
if (state === 'valid' && !routesInitialized) {
routesInitialized = true;
initRoutes(__LEGACY, license.uid);
}
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/actions/server/lib/license_state.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { ILicenseState } from './license_state';
import { LICENSE_CHECK_STATE } from '../../../licensing/server';

export const createLicenseStateMock = () => {
const licenseState: jest.Mocked<ILicenseState> = {
Expand All @@ -14,7 +13,7 @@ export const createLicenseStateMock = () => {
ensureLicenseForActionType: jest.fn(),
isLicenseValidForActionType: jest.fn(),
checkLicense: jest.fn().mockResolvedValue({
state: LICENSE_CHECK_STATE.Valid,
state: 'valid',
}),
};
return licenseState;
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/actions/server/lib/license_state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ActionType } from '../types';
import { BehaviorSubject } from 'rxjs';
import { LicenseState, ILicenseState } from './license_state';
import { licensingMock } from '../../../licensing/server/mocks';
import { LICENSE_CHECK_STATE, ILicense } from '../../../licensing/server';
import { ILicense } from '../../../licensing/server';

describe('checkLicense()', () => {
let getRawLicense: any;
Expand All @@ -21,7 +21,7 @@ describe('checkLicense()', () => {
beforeEach(() => {
const license = licensingMock.createLicense({ license: { status: 'invalid' } });
license.check = jest.fn(() => ({
state: LICENSE_CHECK_STATE.Invalid,
state: 'invalid',
}));
getRawLicense.mockReturnValue(license);
});
Expand All @@ -38,7 +38,7 @@ describe('checkLicense()', () => {
beforeEach(() => {
const license = licensingMock.createLicense({ license: { status: 'active' } });
license.check = jest.fn(() => ({
state: LICENSE_CHECK_STATE.Valid,
state: 'valid',
}));
getRawLicense.mockReturnValue(license);
});
Expand Down
18 changes: 9 additions & 9 deletions x-pack/plugins/actions/server/lib/license_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { i18n } from '@kbn/i18n';
import { Observable, Subscription } from 'rxjs';
import { assertNever } from '../../../../../src/core/utils';
import { ILicense, LICENSE_CHECK_STATE } from '../../../licensing/common/types';
import { ILicense } from '../../../licensing/common/types';
import { PLUGIN } from '../constants/plugin';
import { ActionType } from '../types';
import { ActionTypeDisabledError } from './errors';
Expand Down Expand Up @@ -52,13 +52,13 @@ export class LicenseState {
const check = this.license.check(actionType.id, actionType.minimumLicenseRequired);

switch (check.state) {
case LICENSE_CHECK_STATE.Expired:
case 'expired':
return { isValid: false, reason: 'expired' };
case LICENSE_CHECK_STATE.Invalid:
case 'invalid':
return { isValid: false, reason: 'invalid' };
case LICENSE_CHECK_STATE.Unavailable:
case 'unavailable':
return { isValid: false, reason: 'unavailable' };
case LICENSE_CHECK_STATE.Valid:
case 'valid':
return { isValid: true };
default:
return assertNever(check.state);
Expand Down Expand Up @@ -125,20 +125,20 @@ export class LicenseState {
const check = license.check(PLUGIN.ID, PLUGIN.MINIMUM_LICENSE_REQUIRED);

switch (check.state) {
case LICENSE_CHECK_STATE.Expired:
case 'expired':
return {
showAppLink: true,
enableAppLink: false,
message: check.message || '',
};
case LICENSE_CHECK_STATE.Invalid:
case LICENSE_CHECK_STATE.Unavailable:
case 'invalid':
case 'unavailable':
return {
showAppLink: false,
enableAppLink: false,
message: check.message || '',
};
case LICENSE_CHECK_STATE.Valid:
case 'valid':
return {
showAppLink: true,
enableAppLink: true,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/alerting/server/lib/license_state.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { of } from 'rxjs';
import { LicenseState } from './license_state';
import { LICENSE_CHECK_STATE, ILicense } from '../../../licensing/server';
import { ILicense } from '../../../licensing/server';

export const mockLicenseState = () => {
const license: ILicense = {
Expand All @@ -24,7 +24,7 @@ export const mockLicenseState = () => {
},
check() {
return {
state: LICENSE_CHECK_STATE.Valid,
state: 'valid',
};
},
getFeature() {
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/alerting/server/lib/license_state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import expect from '@kbn/expect';
import { LicenseState } from './license_state';
import { licensingMock } from '../../../../plugins/licensing/server/mocks';
import { LICENSE_CHECK_STATE } from '../../../../plugins/licensing/server';

describe('license_state', () => {
let getRawLicense: any;
Expand All @@ -20,7 +19,7 @@ describe('license_state', () => {
beforeEach(() => {
const license = licensingMock.createLicense({ license: { status: 'invalid' } });
license.check = jest.fn(() => ({
state: LICENSE_CHECK_STATE.Invalid,
state: 'invalid',
}));
getRawLicense.mockReturnValue(license);
});
Expand All @@ -37,7 +36,7 @@ describe('license_state', () => {
beforeEach(() => {
const license = licensingMock.createLicense({ license: { status: 'active' } });
license.check = jest.fn(() => ({
state: LICENSE_CHECK_STATE.Valid,
state: 'valid',
}));
getRawLicense.mockReturnValue(license);
});
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/alerting/server/lib/license_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Boom from 'boom';
import { i18n } from '@kbn/i18n';
import { Observable, Subscription } from 'rxjs';
import { ILicense, LICENSE_CHECK_STATE } from '../../../../plugins/licensing/common/types';
import { ILicense } from '../../../../plugins/licensing/common/types';
import { assertNever } from '../../../../../src/core/utils';
import { PLUGIN } from '../constants/plugin';

Expand Down Expand Up @@ -55,20 +55,20 @@ export class LicenseState {
const check = license.check(PLUGIN.ID, PLUGIN.MINIMUM_LICENSE_REQUIRED);

switch (check.state) {
case LICENSE_CHECK_STATE.Expired:
case 'expired':
return {
showAppLink: true,
enableAppLink: false,
message: check.message || '',
};
case LICENSE_CHECK_STATE.Invalid:
case LICENSE_CHECK_STATE.Unavailable:
case 'invalid':
case 'unavailable':
return {
showAppLink: false,
enableAppLink: false,
message: check.message || '',
};
case LICENSE_CHECK_STATE.Valid:
case 'valid':
return {
showAppLink: true,
enableAppLink: true,
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/graph/common/check_license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { i18n } from '@kbn/i18n';
import { ILicense, LICENSE_CHECK_STATE } from '../../licensing/common/types';
import { ILicense } from '../../licensing/common/types';
import { assertNever } from '../../../../src/core/utils';

export interface GraphLicenseInformation {
Expand Down Expand Up @@ -43,20 +43,20 @@ export function checkLicense(license: ILicense | undefined): GraphLicenseInforma
const check = license.check('graph', 'platinum');

switch (check.state) {
case LICENSE_CHECK_STATE.Expired:
case 'expired':
return {
showAppLink: true,
enableAppLink: false,
message: check.message || '',
};
case LICENSE_CHECK_STATE.Invalid:
case LICENSE_CHECK_STATE.Unavailable:
case 'invalid':
case 'unavailable':
return {
showAppLink: false,
enableAppLink: false,
message: check.message || '',
};
case LICENSE_CHECK_STATE.Valid:
case 'valid':
return {
showAppLink: true,
enableAppLink: true,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/index_management/server/services/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'kibana/server';

import { LicensingPluginSetup } from '../../../licensing/server';
import { LicenseType, LICENSE_CHECK_STATE } from '../../../licensing/common/types';
import { LicenseType } from '../../../licensing/common/types';

export interface LicenseStatus {
isValid: boolean;
Expand All @@ -37,7 +37,7 @@ export class License {
) {
licensing.license$.subscribe(license => {
const { state, message } = license.check(pluginId, minimumLicenseType);
const hasRequiredLicense = state === LICENSE_CHECK_STATE.Valid;
const hasRequiredLicense = state === 'valid';

if (hasRequiredLicense) {
this.licenseStatus = { isValid: true };
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/licensing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ chrome.navLinks.update('myPlugin', {
"requiredPlugins": ["licensing"],

// my_plugin/server/plugin.ts
import { LicensingPluginSetup, LICENSE_CHECK_STATE } from '../licensing/server'
import { LicensingPluginSetup } from '../licensing/server'

interface SetupDeps {
licensing: LicensingPluginSetup;
Expand All @@ -69,7 +69,7 @@ class MyPlugin {
setup(core: CoreSetup, deps: SetupDeps) {
deps.licensing.license$.subscribe(license => {
const { state, message } = license.check('myPlugin', 'gold')
const hasRequiredLicense = state === LICENSE_CHECK_STATE.Valid;
const hasRequiredLicense = state === 'valid';
if (hasRequiredLicense && license.getFeature('name').isAvailable) {
// enable some server side logic
} else {
Expand All @@ -81,12 +81,12 @@ class MyPlugin {
}

// my_plugin/public/plugin.ts
import { LicensingPluginSetup, LICENSE_CHECK_STATE } from '../licensing/public'
import { LicensingPluginSetup } from '../licensing/public'
class MyPlugin {
setup(core: CoreSetup, deps: SetupDeps) {
deps.licensing.license$.subscribe(license => {
const { state, message } = license.check('myPlugin', 'gold')
const hasRequiredLicense = state === LICENSE_CHECK_STATE.Valid;
const hasRequiredLicense = state === 'valid';
const showLinks = hasRequiredLicense && license.getFeature('name').isAvailable;

chrome.navLinks.update('myPlugin', {
Expand Down
21 changes: 10 additions & 11 deletions x-pack/plugins/licensing/common/license.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { License } from './license';
import { LICENSE_CHECK_STATE } from './types';
import { licenseMock } from './licensing.mock';

describe('License', () => {
Expand Down Expand Up @@ -86,21 +85,21 @@ describe('License', () => {

describe('check', () => {
it('provides availability status', () => {
expect(basicLicense.check('ccr', 'gold').state).toBe(LICENSE_CHECK_STATE.Invalid);
expect(basicLicense.check('ccr', 'gold').state).toBe('invalid');

expect(goldLicense.check('ccr', 'gold').state).toBe(LICENSE_CHECK_STATE.Valid);
expect(goldLicense.check('ccr', 'basic').state).toBe(LICENSE_CHECK_STATE.Valid);
expect(goldLicense.check('ccr', 'gold').state).toBe('valid');
expect(goldLicense.check('ccr', 'basic').state).toBe('valid');

expect(basicExpiredLicense.check('ccr', 'gold').state).toBe(LICENSE_CHECK_STATE.Expired);
expect(basicExpiredLicense.check('ccr', 'gold').state).toBe('expired');

expect(errorLicense.check('ccr', 'basic').state).toBe(LICENSE_CHECK_STATE.Unavailable);
expect(errorLicense.check('ccr', 'gold').state).toBe(LICENSE_CHECK_STATE.Unavailable);
expect(errorLicense.check('ccr', 'basic').state).toBe('unavailable');
expect(errorLicense.check('ccr', 'gold').state).toBe('unavailable');

expect(unavailableLicense.check('ccr', 'basic').state).toBe(LICENSE_CHECK_STATE.Unavailable);
expect(unavailableLicense.check('ccr', 'gold').state).toBe(LICENSE_CHECK_STATE.Unavailable);
expect(unavailableLicense.check('ccr', 'basic').state).toBe('unavailable');
expect(unavailableLicense.check('ccr', 'gold').state).toBe('unavailable');

expect(enterpriseLicense.check('ccr', 'gold').state).toBe(LICENSE_CHECK_STATE.Valid);
expect(enterpriseLicense.check('ccr', 'enterprise').state).toBe(LICENSE_CHECK_STATE.Valid);
expect(enterpriseLicense.check('ccr', 'gold').state).toBe('valid');
expect(enterpriseLicense.check('ccr', 'enterprise').state).toBe('valid');
});

it('throws in case of unknown license type', () => {
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/licensing/common/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
LicenseType,
ILicense,
LicenseStatus,
LICENSE_CHECK_STATE,
LicenseCheck,
LICENSE_TYPE,
PublicLicenseJSON,
PublicLicense,
Expand Down Expand Up @@ -98,10 +98,10 @@ export class License implements ILicense {
return LICENSE_TYPE[minimumLicenseRequired] <= LICENSE_TYPE[type];
}

check(pluginName: string, minimumLicenseRequired: LicenseType) {
check(pluginName: string, minimumLicenseRequired: LicenseType): LicenseCheck {
if (!this.isAvailable) {
return {
state: LICENSE_CHECK_STATE.Unavailable,
state: 'unavailable',
message: i18n.translate('xpack.licensing.check.errorUnavailableMessage', {
defaultMessage:
'You cannot use {pluginName} because license information is not available at this time.',
Expand All @@ -112,7 +112,7 @@ export class License implements ILicense {

if (!this.isActive) {
return {
state: LICENSE_CHECK_STATE.Expired,
state: 'expired',
message: i18n.translate('xpack.licensing.check.errorExpiredMessage', {
defaultMessage:
'You cannot use {pluginName} because your {licenseType} license has expired.',
Expand All @@ -123,7 +123,7 @@ export class License implements ILicense {

if (!this.hasAtLeast(minimumLicenseRequired)) {
return {
state: LICENSE_CHECK_STATE.Invalid,
state: 'invalid',
message: i18n.translate('xpack.licensing.check.errorUnsupportedMessage', {
defaultMessage:
'Your {licenseType} license does not support {pluginName}. Please upgrade your license.',
Expand All @@ -132,7 +132,7 @@ export class License implements ILicense {
};
}

return { state: LICENSE_CHECK_STATE.Valid };
return { state: 'valid' };
}

getFeature(name: string) {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/licensing/common/licensing.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { ILicense, PublicLicense, PublicFeatures, LICENSE_CHECK_STATE } from './types';
import { ILicense, PublicLicense, PublicFeatures } from './types';
import { License } from './license';

function createLicense({
Expand Down Expand Up @@ -51,7 +51,7 @@ const createLicenseMock = () => {
check: jest.fn(),
hasAtLeast: jest.fn(),
};
mock.check.mockReturnValue({ state: LICENSE_CHECK_STATE.Valid });
mock.check.mockReturnValue({ state: 'valid' });
mock.hasAtLeast.mockReturnValue(true);
return mock;
};
Expand Down
Loading

0 comments on commit 379af6d

Please sign in to comment.