Skip to content

Commit

Permalink
cleanup and test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Mar 13, 2020
1 parent eba69a6 commit 0c582ab
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { deserializeCluster, serializeCluster } from './cluster_serialization';
describe('cluster_serialization', () => {
describe('deserializeCluster()', () => {
it('should throw an error for invalid arguments', () => {
// @ts-ignore
expect(() => deserializeCluster('foo', 'bar')).toThrowError();
});

Expand Down Expand Up @@ -117,6 +118,7 @@ describe('cluster_serialization', () => {

describe('serializeCluster()', () => {
it('should throw an error for invalid arguments', () => {
// @ts-ignore
expect(() => serializeCluster('foo')).toThrowError();
});

Expand Down
69 changes: 57 additions & 12 deletions x-pack/plugins/remote_clusters/common/lib/cluster_serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,64 @@

import { PROXY_MODE } from '../constants';

// interface ClusterEs {
// seeds?: string[];
// }

// interface Cluster {}
export interface ClusterEs {
seeds?: string[];
mode?: 'proxy' | 'sniff';
connected?: boolean;
num_nodes_connected?: number;
max_connections_per_cluster?: number;
initial_connect_timeout?: string;
skip_unavailable?: boolean;
transport?: {
ping_schedule?: string;
compress?: boolean;
};
address?: string;
max_socket_connections?: number;
num_sockets_connected?: number;
}

// interface ClusterPayload {}
export interface Cluster {
name: string;
seeds?: string[];
skipUnavailable?: boolean;
nodeConnections?: number;
proxyAddress?: string;
proxySocketConnections?: number;
serverName?: string;
mode?: 'proxy' | 'sniff';
isConnected?: boolean;
transportPingSchedule?: string;
transportCompress?: boolean;
connectedNodesCount?: number;
maxConnectionsPerCluster?: number;
initialConnectTimeout?: string;
connectedSocketsCount?: number;
hasDeprecatedProxySetting?: boolean;
}
export interface ClusterPayload {
persistent: {
cluster: {
remote: {
[key: string]: {
skip_unavailable?: boolean | null;
mode?: 'sniff' | 'proxy' | null;
proxy_address?: string | null;
proxy_socket_connections?: number | null;
server_name?: string | null;
seeds?: string[] | null;
node_connections?: number | null;
};
};
};
};
}

export function deserializeCluster(
name: string,
esClusterObject: any,
esClusterObject: ClusterEs,
deprecatedProxyAddress?: string | undefined
): any {
): Cluster {
if (!name || !esClusterObject || typeof esClusterObject !== 'object') {
throw new Error('Unable to deserialize cluster');
}
Expand All @@ -37,7 +82,7 @@ export function deserializeCluster(
num_sockets_connected: connectedSocketsCount,
} = esClusterObject;

let deserializedClusterObject: any = {
let deserializedClusterObject: Cluster = {
name,
mode,
isConnected,
Expand Down Expand Up @@ -75,15 +120,15 @@ export function deserializeCluster(

// It's unnecessary to send undefined values back to the client, so we can remove them.
Object.keys(deserializedClusterObject).forEach(key => {
if (deserializedClusterObject[key] === undefined) {
delete deserializedClusterObject[key];
if (deserializedClusterObject[key as keyof Cluster] === undefined) {
delete deserializedClusterObject[key as keyof Cluster];
}
});

return deserializedClusterObject;
}

export function serializeCluster(deserializedClusterObject: any): any {
export function serializeCluster(deserializedClusterObject: Cluster): ClusterPayload {
if (!deserializedClusterObject || typeof deserializedClusterObject !== 'object') {
throw new Error('Unable to serialize cluster');
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/remote_clusters/common/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { deserializeCluster, serializeCluster } from './cluster_serialization';
export { deserializeCluster, serializeCluster, Cluster, ClusterEs } from './cluster_serialization';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { PROXY_MODE } from '../../../../../common/constants';
import { getRouterLinkProps } from '../../../services';
import { ConfiguredByNodeWarning } from '../../components';
import { ConnectionStatus, RemoveClusterButtonProvider } from '../components';
import { proxyModeUrl } from '../../../services/documentation';

export class DetailPanel extends Component {
static propTypes = {
Expand Down Expand Up @@ -111,7 +112,10 @@ export class DetailPanel extends Component {
);
}

renderClusterWithDeprecatedSettingWarning({ hasDeprecatedProxySetting }, clusterName) {
renderClusterWithDeprecatedSettingWarning(
{ hasDeprecatedProxySetting, isConfiguredByNode },
clusterName
) {
if (!hasDeprecatedProxySetting) {
return null;
}
Expand All @@ -121,7 +125,7 @@ export class DetailPanel extends Component {
title={
<FormattedMessage
id="xpack.remoteClusters.detailPanel.deprecatedSettingsTitle"
defaultMessage={`"{remoteCluster}" contains deprecated settings`}
defaultMessage='"{remoteCluster}" contains deprecated settings'
values={{
remoteCluster: clusterName,
}}
Expand All @@ -132,12 +136,20 @@ export class DetailPanel extends Component {
>
<FormattedMessage
id="xpack.remoteClusters.detailPanel.deprecatedSettingsMessage"
defaultMessage="We recommend updating this remote cluster to use the correct settings. {editLink}"
defaultMessage="We recommend updating this remote cluster to use the correct settings. {helpLink}"
values={{
editLink: (
helpLink: isConfiguredByNode ? (
// A remote cluster is not editable if configured in elasticsearch.yml, so we direct the user to documentation instead
<EuiLink href={proxyModeUrl} target="_blank">
<FormattedMessage
id="xpack.remoteClusters.detailPanel.deprecatedSettingsLearnMoreLinkLabel"
defaultMessage="Learn more."
/>
</EuiLink>
) : (
<EuiLink {...getRouterLinkProps(`${CRUD_APP_BASE_PATH}/edit/${clusterName}`)}>
<FormattedMessage
id="xpack.remoteClusters.detailPanel.deprecatedSettingsLinkLabel"
id="xpack.remoteClusters.detailPanel.deprecatedSettingsEditLinkLabel"
defaultMessage="Edit remote cluster."
/>
</EuiLink>
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/remote_clusters/server/routes/api/add_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { schema, TypeOf } from '@kbn/config-schema';
import { i18n } from '@kbn/i18n';
import { RequestHandler } from 'src/core/server';

import { serializeCluster } from '../../../common/lib';
import { serializeCluster, Cluster } from '../../../common/lib';
import { doesClusterExist } from '../../lib/does_cluster_exist';
import { API_BASE_PATH, PROXY_MODE, SNIFF_MODE } from '../../../common/constants';
import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory';
Expand Down Expand Up @@ -55,7 +55,7 @@ export const register = (deps: RouteDependencies): void => {
});
}

const addClusterPayload = serializeCluster(request.body);
const addClusterPayload = serializeCluster(request.body as Cluster);
const updateClusterResponse = await callAsCurrentUser('cluster.putSettings', {
body: addClusterPayload,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const register = (deps: RouteDependencies): void => {

// Pre-7.6, ES supported an undocumented "proxy" field
// ES does not handle migrating this to the new implementation, so we need to surface it in the UI
// This value is not available via the GET /_remote/info API, so we get it from the cluster settings
const deprecatedProxyAddress = isPersistent
? get(clusterSettings, `persistent.cluster.remote[${clusterName}].proxy`, undefined)
: undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { i18n } from '@kbn/i18n';
import { RequestHandler } from 'src/core/server';

import { API_BASE_PATH, SNIFF_MODE, PROXY_MODE } from '../../../common/constants';
import { serializeCluster, deserializeCluster } from '../../../common/lib';
import { serializeCluster, deserializeCluster, Cluster, ClusterEs } from '../../../common/lib';
import { doesClusterExist } from '../../lib/does_cluster_exist';
import { RouteDependencies } from '../../types';
import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory';
Expand Down Expand Up @@ -61,14 +61,14 @@ export const register = (deps: RouteDependencies): void => {
}

// Update cluster as new settings
const updateClusterPayload = serializeCluster({ ...request.body, name });
const updateClusterPayload = serializeCluster({ ...request.body, name } as Cluster);

const updateClusterResponse = await callAsCurrentUser('cluster.putSettings', {
body: updateClusterPayload,
});

const acknowledged = get(updateClusterResponse, 'acknowledged');
const cluster = get(updateClusterResponse, `persistent.cluster.remote.${name}`);
const cluster = get(updateClusterResponse, `persistent.cluster.remote.${name}`) as ClusterEs;

if (acknowledged && cluster) {
const body = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default function({ getService }) {
name: 'test_cluster',
seeds: [NODE_SEED],
skipUnavailable: true,
mode: 'sniff',
})
.expect(200);

Expand All @@ -58,6 +59,7 @@ export default function({ getService }) {
name: 'test_cluster',
seeds: [NODE_SEED],
skipUnavailable: false,
mode: 'sniff',
})
.expect(409);

Expand All @@ -79,6 +81,7 @@ export default function({ getService }) {
.send({
skipUnavailable: false,
seeds: [NODE_SEED],
mode: 'sniff',
})
.expect(200);

Expand All @@ -87,6 +90,7 @@ export default function({ getService }) {
skipUnavailable: 'false', // ES issue #35671
seeds: [NODE_SEED],
isConfiguredByNode: false,
mode: 'sniff',
});
});
});
Expand All @@ -109,6 +113,7 @@ export default function({ getService }) {
initialConnectTimeout: '30s',
skipUnavailable: false,
isConfiguredByNode: false,
mode: 'sniff',
},
]);
});
Expand Down Expand Up @@ -139,6 +144,7 @@ export default function({ getService }) {
name: 'test_cluster1',
seeds: [NODE_SEED],
skipUnavailable: true,
mode: 'sniff',
})
.expect(200);

Expand All @@ -149,6 +155,7 @@ export default function({ getService }) {
name: 'test_cluster2',
seeds: [NODE_SEED],
skipUnavailable: true,
mode: 'sniff',
})
.expect(200);

Expand Down

0 comments on commit 0c582ab

Please sign in to comment.