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

Update http route registration in migration guide and examples #48518

Merged
merged 7 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 11 additions & 5 deletions src/core/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export default (kibana) => {
}
}

// HTTP functionality from core
// HTTP functionality from legacy
server.route({
path: '/api/demo_plugin/search',
method: 'POST',
Expand All @@ -450,8 +450,13 @@ We now move this logic into a new plugin definition, which is based off of the c
// server/plugin.ts
import { ElasticsearchPlugin } from '../elasticsearch';

interface CoreSetup {
// note: We use a name unique to our plugin for this type since our shimmed interface is not 100%
// compatible with NP's CoreSetup.
interface DemoPluginCoreSetup {
elasticsearch: ElasticsearchPlugin // note: Elasticsearch is in Core in NP, rather than a plugin
http: {
route: Legacy.Server['route'] // note: NP uses `http.createRouter()`
Copy link
Member

Choose a reason for hiding this comment

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

Is there any intention to provide core.http.route as a method in the NP, that works like this? If not, then I think showing this as a "shim" is confusing because I interpret "shim" as "massage the old way so that it looks and works like the new way".

Copy link
Member

@jasonrhodes jasonrhodes Oct 17, 2019

Choose a reason for hiding this comment

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

In other words if in NP core.http exists, but core.http.route isn't going to exist, then this shim breaks your future code in a hard to notice way.

If we need an example of how to keep using legacy router in the shim (do we need this?) I would say maybe shimming it as core.legacyHttp.route is a clearer way of indicating what's going on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

core.http.route will never exist.

If we need an example of how to keep using legacy router in the shim (do we need this?) I would say maybe shimming it as core.legacyHttp.route is a clearer way of indicating what's going on.

I think the intention is to use types to minimize the legacy API surface that's depended on and make these dependencies explicit. So instead of just say "I depend on Legacy.Server" you narrow it down to just the hapi router or just a dependency on another plugin exposed via the server object.

I agree that this introduces confusion in this case and like your idea of making it more explicit. The shimming instructions were written before most of the API's were available in CoreSetup. Now that the real coreSetup has many useful API's another approach might be to inject three variables into setup:

import { CoreSetup } from 'src/core/server';

export interface LegacySetup {
  server: {
    route: Legacy.Server['route']
  }
}

export class Plugin {
  public setup(core: CoreSetup, plugins: {}, legacy: LegacySetup) {
    legacy.route({...})
  }

This gives you the added safety that you're relying on Core's CoreSetup so you don't have to wonder if your shimmed MyPluginCoreSetup is accurately shimming core.

Copy link
Member

Choose a reason for hiding this comment

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

I like that idea a lot to be honest.

Copy link
Contributor

@joshdover joshdover Oct 17, 2019

Choose a reason for hiding this comment

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

Another option is to use the __legacy namespace that some plugins have been doing:

import { CoreSetup } from 'src/core/server';

export interface DemoPluginCoreSetup extends CoreSetup {
  __legacy: {
    server: {
      route: Legacy.Server['route'];
    };
  };
}

export class Plugin {
  public setup(core: CoreSetup, plugins: {}) {
    core.__legacy.server.route({...})
  }
}

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 what you have here and would be happy to see it merge.

core.http.route will never exist.

Is that correct? I opened #44174 about core.http.route and @restrry replied "should be done as a part of #44620" so I thought it was coming eventually

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have broad consensus that registering routes with a configuration object could be a nicer API but I don't think we have concrete designs for how it will be exposed. I imagined that we might still have a core.http.createRouter(path: string) method like today, but that the Router instance returned from this would allow you to do router.add({...}) or router.register({...})

However, this conversation kinda changed my mind on the our approach to shimming plugins. We used to recommend constructing a CoreSetup that looks as close as possible to what CoreSetup will look like in the New Platform. This has caused a lot of confusion and speculation which I realise now doesn't add much value.

As @jasonrhodes pointed out:

then this shim breaks your future code in a hard to notice way.

Instead of making it look the same, I think the goal should rather be to have legacy be as different and noticeable as possible, and make the exact legacy API surface explicit by picking only the legacy methods that you depend on.

Copy link
Contributor

Choose a reason for hiding this comment

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

Exposing a function on a router instance, vs core.http is also what I was expecting. Thanks for the explanation & confirmation. Sorry for the noise.

}
}

interface FooSetup {
Expand All @@ -465,7 +470,7 @@ interface PluginsSetup {
export type DemoPluginSetup = ReturnType<Plugin['setup']>;

export class Plugin {
public setup(core: CoreSetup, plugins: PluginsSetup) {
public setup(core: DemoPluginCoreSetup, plugins: PluginsSetup) {
const serverFacade: ServerFacade = {
plugins: {
// We're still using the legacy Elasticsearch here, but we're now accessing it
Expand All @@ -475,8 +480,9 @@ export class Plugin {
}
}

// HTTP functionality from legacy platform, accessed in the NP convention, just like Elasticsearch above.
core.http.route({ // note: we know routes will be created on core.http
// HTTP functionality from legacy platform, accessed in a way that's compatible with
// NP conventions even if not 100% the same
core.http.route({
path: '/api/demo_plugin/search',
method: 'POST',
async handler(request) {
Expand Down
141 changes: 137 additions & 4 deletions src/core/MIGRATION_EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
This document is a list of examples of how to migrate plugin code from legacy
APIs to their New Platform equivalents.

- [Migration Examples](#migration-examples)
- [Configuration](#configuration)
- [Declaring config schema](#declaring-config-schema)
- [Using New Platform config from a Legacy plugin](#using-new-platform-config-from-a-legacy-plugin)
- [Create a New Platform plugin](#create-a-new-platform-plugin)
- [HTTP Routes](#http-routes)
- [Route Registration](#route-registration)
- [1. Legacy route registration](#1-legacy-route-registration)
- [2. New Platform shim using legacy router](#2-new-platform-shim-using-legacy-router)
- [3. New Platform shim using New Platform router](#3-new-platform-shim-using-new-platform-router)
- [4. New Platform plugin](#4-new-platform-plugin)
- [Accessing Services](#accessing-services)
- [Chrome](#chrome)
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the TOC


## Configuration

### Declaring config schema
Expand Down Expand Up @@ -150,15 +164,18 @@ This interface has a different API with slightly different behaviors.
not have native support for converting Boom exceptions into HTTP responses.

### Route Registration
Because of the incompatibility between the legacy and New Platform HTTP Route
API's it might be helpful to break up your migration work into several stages.

#### 1. Legacy route registration
```ts
// Legacy route registration
// legacy/plugins/myplugin/index.ts
import Joi from 'joi';

new kibana.Plugin({
init(server) {
server.route({
path: '/api/my-plugin/my-route',
path: '/api/demoplugin/search',
method: 'POST',
options: {
validate: {
Expand All @@ -173,17 +190,133 @@ new kibana.Plugin({
});
}
});
```

#### 2. New Platform shim using legacy router
Create a New Platform shim and inject the legacy `server.route` into your
plugin's setup function. This shim isn't exactly the same as the New
Platform's API's but it allows us to leave all of our route registration
untouched.

// New Platform equivalent
```ts
// legacy/plugins/demoplugin/index.ts
import { Plugin, DemoPluginCoreSetup } from './server/plugin';
export default (kibana) => {
return new kibana.Plugin({
id: 'demo_plugin',

init(server) {
// core shim
const coreSetup: DemoPluginCoreSetup = {
http: {
route: server.route
},
};

new Plugin().setup(coreSetup);
}
}
}
```
```ts
// legacy/plugins/demoplugin/server/plugin.ts
import { Legacy } from 'kibana';

export interface DemoPluginCoreSetup {
http: {
route: Legacy.Server['route'];
};
};

export class Plugin {
public setup(core: DemoPluginCoreSetup, plugins: DemoPluginsSetup) {
// HTTP functionality from legacy platform, accessed in a way that's
// compatible with NP conventions even if not 100% the same
core.http.route({
path: '/api/demoplugin/search',
method: 'POST',
options: {
validate: {
payload: Joi.object({
field1: Joi.string().required(),
}),
}
},
async handler(req) {
return { message: `Received field1: ${req.payload.field1}` };
},
});
}
}
```

#### 3. New Platform shim using New Platform router
We now switch the shim to use the real New Platform HTTP API's in `coreSetup`
instead of relying on the legacy `server.route`. Since our plugin is now using
the New Platform API's we are guaranteed that our HTTP route handling is 100%
compatible with the New Platform. As a result, we will also have to adapt our
route registration accordingly.
```ts
// legacy/plugins/demoplugin/index.ts
import { Plugin } from './server/plugin';
export default (kibana) => {
return new kibana.Plugin({
id: 'demo_plugin',

init(server) {
// core shim
const coreSetup = {
http: server.newPlatform.setup.core.http,
};

new Plugin().setup(coreSetup);
}
}
}
```
```ts
// legacy/plugins/demoplugin/server/plugin.ts
import { schema } from '@kbn/config-schema';
import { CoreSetup } from 'src/core/server';

class Plugin {
public setup(core: CoreSetup) {
const router = core.http.createRouter();
router.post(
{
path: '/api/demoplugin/search',
validate: {
body: schema.object({
field1: schema.string(),
}),
}
},
(context, req, res) => {
return res.ok({
body: {
message: `Received field1: ${req.body.field1}`
}
});
}
)
}
}
```

#### 4. New Platform plugin
As the final step we delete the shim and move all our code into a New Platform
plugin. Since we were already consuming the New Platform API's no code changes
are necessary inside `plugin.ts`.
```ts
// plugins/demoplugin/server/plugin.ts
import { schema } from '@kbn/config-schema';

class Plugin {
public setup(core) {
const router = core.http.createRouter();
router.post(
{
path: '/api/my-plugin/my-route',
path: '/api/demoplugin/search',
validate: {
body: schema.object({
field1: schema.string(),
Expand Down