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

Licensing: allow multiple registration of the same feature #76272

Merged
merged 1 commit into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ describe('FeatureUsageService', () => {

describe('#setup', () => {
describe('#register', () => {
it('throws when registering the same feature twice', () => {
it('does not throw when registering the same feature twice with the same license', () => {
const setup = service.setup();
setup.register('foo', 'basic');
expect(() => {
setup.register('foo', 'basic');
}).toThrowErrorMatchingInlineSnapshot(`"Feature 'foo' has already been registered."`);
}).not.toThrow();
});

it('throws when registering the same feature again with a different license', () => {
const setup = service.setup();
setup.register('foo', 'basic');
expect(() => {
setup.register('foo', 'enterprise');
}).toThrowErrorMatchingInlineSnapshot(
`"Feature 'foo' has already been registered with another license type. (current: basic, new: enterprise)"`
);
});
});
});
Expand Down
20 changes: 13 additions & 7 deletions x-pack/plugins/licensing/server/services/feature_usage_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ export class FeatureUsageService {
public setup(): FeatureUsageServiceSetup {
return {
register: (featureName, licenseType) => {
if (this.lastUsages.has(featureName)) {
throw new Error(`Feature '${featureName}' has already been registered.`);
const registered = this.lastUsages.get(featureName);
if (registered) {
if (registered.licenseType !== licenseType) {
throw new Error(
`Feature '${featureName}' has already been registered with another license type. (current: ${registered.licenseType}, new: ${licenseType})`
);
}
} else {
Comment on lines +46 to +53
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@restrry This is the easiest fix for the problem, and answer the need.

Another option would be to instead adapt the client-side service to fetch the list of already registered features from the server-side during setup and to not perform calls to the server when trying to register an already existing feature. This is just a little trickier if we want to keep the client-side service synchronous

export class FeatureUsageService {
public setup({ http }: SetupDeps): FeatureUsageServiceSetup {
return {
register: async (featureName, licenseType) => {
await http.post('/internal/licensing/feature_usage/register', {
body: JSON.stringify({
featureName,
licenseType,
}),
});
},
};
}

as it would mean creating the fetch promise during setup and await for its result in every call to register. But nothing that can't be done. Do you think this would be better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the solution you have in PR. It's simpler and less likely to break IMO.

this.lastUsages.set(featureName, {
name: featureName,
lastUsed: null,
licenseType,
});
}
this.lastUsages.set(featureName, {
name: featureName,
lastUsed: null,
licenseType,
});
},
};
}
Expand Down