Chore/remove once per server (#49426) (#55500)

* [Reporting/np-k8] Remove several oncePerServer usages

* ts fixes 1

* ts fixes 2

* more ts fixes

* more ts fixes

* more ts fixes

* ts simplification

* improve ts

* remove any type for jobParams and define JobParamsSavedObject and JobParamsUrl

* ts simplification

* Fix ts

* ts simplification

* fix ts

* bug fix

* align with joels pr

* Move get_absolute_url to not use oncePerServer

* Two more removals of oncePerServer

* Final once-per-server removals

* AbsoluteURLFactory => AbsoluteURLFactoryOptions

* Fix absolute_url util

Co-authored-by: Joel Griffith <joel@joelgriffith.net>
This commit is contained in:
Tim Sullivan 2020-01-21 20:31:02 -07:00 committed by GitHub
parent bfe9b45a55
commit 1043489bbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 54 deletions

View file

@ -4,88 +4,80 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { createMockServer } from '../test_helpers/create_mock_server';
import { getAbsoluteUrlFactory } from './get_absolute_url';
test(`by default it builds url using information from server.info.protocol and the server.config`, () => {
const mockServer = createMockServer('');
const getAbsoluteUrl = getAbsoluteUrlFactory(mockServer);
const defaultOptions = {
defaultBasePath: 'sbp',
protocol: 'http:',
hostname: 'localhost',
port: 5601,
};
test(`by default it builds urls using information from server.info.protocol and the server.config`, () => {
const getAbsoluteUrl = getAbsoluteUrlFactory(defaultOptions);
const absoluteUrl = getAbsoluteUrl();
expect(absoluteUrl).toBe(`http://localhost:5601/sbp/app/kibana`);
});
test(`uses kibanaServer.protocol if specified`, () => {
const settings = {
'xpack.reporting.kibanaServer.protocol': 'https',
};
const mockServer = createMockServer({ settings });
const getAbsoluteUrl = getAbsoluteUrlFactory({
...defaultOptions,
protocol: 'https:',
});
const getAbsoluteUrl = getAbsoluteUrlFactory(mockServer);
const absoluteUrl = getAbsoluteUrl();
expect(absoluteUrl).toBe(`https://localhost:5601/sbp/app/kibana`);
});
test(`uses kibanaServer.hostname if specified`, () => {
const settings = {
'xpack.reporting.kibanaServer.hostname': 'something-else',
};
const mockServer = createMockServer({ settings });
const getAbsoluteUrl = getAbsoluteUrlFactory({
...defaultOptions,
hostname: 'something-else',
});
const getAbsoluteUrl = getAbsoluteUrlFactory(mockServer);
const absoluteUrl = getAbsoluteUrl();
expect(absoluteUrl).toBe(`http://something-else:5601/sbp/app/kibana`);
});
test(`uses kibanaServer.port if specified`, () => {
const settings = {
'xpack.reporting.kibanaServer.port': 8008,
};
const mockServer = createMockServer({ settings });
const getAbsoluteUrl = getAbsoluteUrlFactory({
...defaultOptions,
port: 8008,
});
const getAbsoluteUrl = getAbsoluteUrlFactory(mockServer);
const absoluteUrl = getAbsoluteUrl();
expect(absoluteUrl).toBe(`http://localhost:8008/sbp/app/kibana`);
});
test(`uses the provided hash`, () => {
const mockServer = createMockServer('');
const getAbsoluteUrl = getAbsoluteUrlFactory(mockServer);
const getAbsoluteUrl = getAbsoluteUrlFactory(defaultOptions);
const hash = '/hash';
const absoluteUrl = getAbsoluteUrl({ hash });
expect(absoluteUrl).toBe(`http://localhost:5601/sbp/app/kibana#${hash}`);
});
test(`uses the provided hash with queryString`, () => {
const mockServer = createMockServer('');
const getAbsoluteUrl = getAbsoluteUrlFactory(mockServer);
const getAbsoluteUrl = getAbsoluteUrlFactory(defaultOptions);
const hash = '/hash?querystring';
const absoluteUrl = getAbsoluteUrl({ hash });
expect(absoluteUrl).toBe(`http://localhost:5601/sbp/app/kibana#${hash}`);
});
test(`uses the provided basePath`, () => {
const mockServer = createMockServer('');
const getAbsoluteUrl = getAbsoluteUrlFactory(mockServer);
const getAbsoluteUrl = getAbsoluteUrlFactory(defaultOptions);
const absoluteUrl = getAbsoluteUrl({ basePath: '/s/marketing' });
expect(absoluteUrl).toBe(`http://localhost:5601/s/marketing/app/kibana`);
});
test(`uses the path`, () => {
const mockServer = createMockServer('');
const getAbsoluteUrl = getAbsoluteUrlFactory(mockServer);
const getAbsoluteUrl = getAbsoluteUrlFactory(defaultOptions);
const path = '/app/canvas';
const absoluteUrl = getAbsoluteUrl({ path });
expect(absoluteUrl).toBe(`http://localhost:5601/sbp${path}`);
});
test(`uses the search`, () => {
const mockServer = createMockServer('');
const getAbsoluteUrl = getAbsoluteUrlFactory(mockServer);
const getAbsoluteUrl = getAbsoluteUrlFactory(defaultOptions);
const search = '_t=123456789';
const absoluteUrl = getAbsoluteUrl({ search });
expect(absoluteUrl).toBe(`http://localhost:5601/sbp/app/kibana?${search}`);

View file

@ -5,24 +5,27 @@
*/
import url from 'url';
import { ServerFacade } from '../types';
export function getAbsoluteUrlFactory(server: ServerFacade) {
const config = server.config();
import { AbsoluteURLFactoryOptions } from '../types';
export const getAbsoluteUrlFactory = ({
protocol,
hostname,
port,
defaultBasePath,
}: AbsoluteURLFactoryOptions) => {
return function getAbsoluteUrl({
basePath = config.get('server.basePath'),
basePath = defaultBasePath,
hash = '',
path = '/app/kibana',
search = '',
} = {}) {
return url.format({
protocol: config.get('xpack.reporting.kibanaServer.protocol') || server.info.protocol,
hostname: config.get('xpack.reporting.kibanaServer.hostname') || config.get('server.host'),
port: config.get('xpack.reporting.kibanaServer.port') || config.get('server.port'),
protocol,
hostname,
port,
pathname: basePath + path,
hash,
search,
});
};
}
};

View file

@ -30,7 +30,14 @@ export function getFullUrls<JobDocPayloadType>({
server: ServerFacade;
job: JobDocPayloadPDF | JobDocPayloadPNG;
}) {
const getAbsoluteUrl = getAbsoluteUrlFactory(server);
const config = server.config();
const getAbsoluteUrl = getAbsoluteUrlFactory({
defaultBasePath: config.get('server.basePath'),
protocol: config.get('xpack.reporting.kibanaServer.protocol') || server.info.protocol,
hostname: config.get('xpack.reporting.kibanaServer.hostname') || config.get('server.host'),
port: config.get('xpack.reporting.kibanaServer.port') || config.get('server.port'),
});
// PDF and PNG job params put in the url differently
let relativeUrls: string[] = [];

View file

@ -6,11 +6,10 @@
import boom from 'boom';
import { getUserFactory } from '../../lib/get_user';
import { oncePerServer } from '../../lib/once_per_server';
const superuserRole = 'superuser';
function authorizedUserPreRoutingFn(server) {
export const authorizedUserPreRoutingFactory = function authorizedUserPreRoutingFn(server) {
const getUser = getUserFactory(server);
const config = server.config();
@ -43,6 +42,4 @@ function authorizedUserPreRoutingFn(server) {
return user;
};
}
export const authorizedUserPreRoutingFactory = oncePerServer(authorizedUserPreRoutingFn);
};

View file

@ -5,9 +5,8 @@
*/
import Boom from 'boom';
import { oncePerServer } from '../../lib/once_per_server';
function reportingFeaturePreRoutingFn(server) {
export const reportingFeaturePreRoutingFactory = function reportingFeaturePreRoutingFn(server) {
const xpackMainPlugin = server.plugins.xpack_main;
const pluginId = 'reporting';
@ -24,6 +23,4 @@ function reportingFeaturePreRoutingFn(server) {
}
};
};
}
export const reportingFeaturePreRoutingFactory = oncePerServer(reportingFeaturePreRoutingFn);
};