Skip to content

Commit

Permalink
host host host host -> endpoint endpoint endpoint endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pzl committed Aug 6, 2020
1 parent 4645120 commit 14c0bec
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const querystringStringify: <ExpectedType extends object, ArgType>(
params: Exact<ExpectedType, ArgType>
) => string = querystring.stringify;

/** Make `selected_host` required */
/** Make `selected_endpoint` required */
type EndpointDetailsUrlProps = Omit<EndpointIndexUIQueryParams, 'selected_endpoint'> &
Required<Pick<EndpointIndexUIQueryParams, 'selected_endpoint'>>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import { endpointListReducer } from './reducer';
import { endpointMiddlewareFactory } from './middleware';
import { getEndpointListPath } from '../../../common/routing';

describe('host list middleware', () => {
describe('endpoint list middleware', () => {
let fakeCoreStart: jest.Mocked<CoreStart>;
let depsStart: DepsStartMock;
let fakeHttpServices: jest.Mocked<HttpSetup>;
type HostListStore = Store<Immutable<EndpointState>, Immutable<AppAction>>;
let store: HostListStore;
let getState: HostListStore['getState'];
let dispatch: HostListStore['dispatch'];
type EndpointListStore = Store<Immutable<EndpointState>, Immutable<AppAction>>;
let store: EndpointListStore;
let getState: EndpointListStore['getState'];
let dispatch: EndpointListStore['dispatch'];
let waitForAction: MiddlewareActionSpyHelper['waitForAction'];
let actionSpyMiddleware;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { HostInfo, HostResultList } from '../../../../../common/endpoint/types';
import { GetPolicyListResponse } from '../../policy/types';
import { ImmutableMiddlewareFactory } from '../../../../common/store';
import {
isOnHostPage,
hasSelectedHost,
isOnEndpointPage,
hasSelectedEndpoint,
uiQueryParams,
listData,
endpointPackageInfo,
Expand All @@ -29,11 +29,11 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
next(action);
const state = getState();

// Host list
// Endpoint list
if (
action.type === 'userChangedUrl' &&
isOnHostPage(state) &&
hasSelectedHost(state) !== true
isOnEndpointPage(state) &&
hasSelectedEndpoint(state) !== true
) {
if (!endpointPackageInfo(state)) {
sendGetEndpointSecurityPackage(coreStart.http)
Expand All @@ -50,24 +50,24 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
}

const { page_index: pageIndex, page_size: pageSize } = uiQueryParams(state);
let hostResponse;
let endpointResponse;

try {
hostResponse = await coreStart.http.post<HostResultList>('/api/endpoint/metadata', {
endpointResponse = await coreStart.http.post<HostResultList>('/api/endpoint/metadata', {
body: JSON.stringify({
paging_properties: [{ page_index: pageIndex }, { page_size: pageSize }],
}),
});
hostResponse.request_page_index = Number(pageIndex);
endpointResponse.request_page_index = Number(pageIndex);

dispatch({
type: 'serverReturnedEndpointList',
payload: hostResponse,
payload: endpointResponse,
});

getNonExistingPoliciesForHostsList(
getNonExistingPoliciesForEndpointsList(
coreStart.http,
hostResponse.hosts,
endpointResponse.hosts,
nonExistingPolicies(state)
)
.then((missingPolicies) => {
Expand All @@ -88,14 +88,14 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
});
}

// No hosts, so we should check to see if there are policies for onboarding
if (hostResponse && hostResponse.hosts.length === 0) {
// No endpoints, so we should check to see if there are policies for onboarding
if (endpointResponse && endpointResponse.hosts.length === 0) {
const http = coreStart.http;

// The original query to the list could have had an invalid param (ex. invalid page_size),
// so we check first if hosts actually do exist before pulling in data for the onboarding
// so we check first if endpoints actually do exist before pulling in data for the onboarding
// messages.
if (await doHostsExist(http)) {
if (await doEndpointsExist(http)) {
return;
}

Expand Down Expand Up @@ -135,13 +135,13 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
}
}

// Host Details
if (action.type === 'userChangedUrl' && hasSelectedHost(state) === true) {
// Endpoint Details
if (action.type === 'userChangedUrl' && hasSelectedEndpoint(state) === true) {
dispatch({
type: 'serverCancelledPolicyItemsLoading',
});

// If user navigated directly to a host details page, load the host list
// If user navigated directly to a endpoint details page, load the endpoint list
if (listData(state).length === 0) {
const { page_index: pageIndex, page_size: pageSize } = uiQueryParams(state);
try {
Expand All @@ -156,7 +156,7 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
payload: response,
});

getNonExistingPoliciesForHostsList(
getNonExistingPoliciesForEndpointsList(
coreStart.http,
response.hosts,
nonExistingPolicies(state)
Expand Down Expand Up @@ -184,17 +184,21 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
});
}

// call the host details api
const { selected_endpoint: selectedHost } = uiQueryParams(state);
// call the endpoint details api
const { selected_endpoint: selectedEndpoint } = uiQueryParams(state);
try {
const response = await coreStart.http.get<HostInfo>(
`/api/endpoint/metadata/${selectedHost}`
`/api/endpoint/metadata/${selectedEndpoint}`
);
dispatch({
type: 'serverReturnedEndpointDetails',
payload: response,
});
getNonExistingPoliciesForHostsList(coreStart.http, [response], nonExistingPolicies(state))
getNonExistingPoliciesForEndpointsList(
coreStart.http,
[response],
nonExistingPolicies(state)
)
.then((missingPolicies) => {
if (missingPolicies !== undefined) {
dispatch({
Expand All @@ -216,7 +220,7 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
// call the policy response api
try {
const policyResponse = await coreStart.http.get(`/api/endpoint/policy_response`, {
query: { hostId: selectedHost },
query: { hostId: selectedEndpoint },
});
dispatch({
type: 'serverReturnedEndpointPolicyResponse',
Expand All @@ -232,7 +236,7 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
};
};

const getNonExistingPoliciesForHostsList = async (
const getNonExistingPoliciesForEndpointsList = async (
http: HttpStart,
hosts: HostResultList['hosts'],
currentNonExistingPolicies: EndpointState['nonExistingPolicies']
Expand Down Expand Up @@ -291,7 +295,7 @@ const getNonExistingPoliciesForHostsList = async (
return nonExisting;
};

const doHostsExist = async (http: HttpStart): Promise<boolean> => {
const doEndpointsExist = async (http: HttpStart): Promise<boolean> => {
try {
return (
(
Expand All @@ -304,7 +308,7 @@ const doHostsExist = async (http: HttpStart): Promise<boolean> => {
);
} catch (error) {
// eslint-disable-next-line no-console
console.error(`error while trying to check if hosts exist`);
console.error(`error while trying to check if endpoints exist`);
// eslint-disable-next-line no-console
console.error(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const endpointListApiPathHandlerMocks = ({
};
},

// host list
// endpoint list
'/api/endpoint/metadata': (): HostResultList => {
return {
hosts: endpointsResults,
Expand All @@ -104,7 +104,7 @@ const endpointListApiPathHandlerMocks = ({
};
},

// Do policies referenced in host list exist
// Do policies referenced in endpoint list exist
// just returns 1 single agent config that includes all of the packageConfig IDs provided
[INGEST_API_AGENT_CONFIGS]: (): GetAgentConfigsResponse => {
const agentConfig = generator.generateAgentConfig();
Expand Down Expand Up @@ -137,7 +137,7 @@ const endpointListApiPathHandlerMocks = ({
},
};

// Build a GET route handler for each host details based on the list of Hosts passed on input
// Build a GET route handler for each endpoint details based on the list of Endpoints passed on input
if (endpointsResults) {
endpointsResults.forEach((host) => {
// @ts-ignore
Expand All @@ -149,7 +149,7 @@ const endpointListApiPathHandlerMocks = ({
};

/**
* Sets up mock impelementations in support of the Hosts list view
* Sets up mock impelementations in support of the Endpoints list view
*
* @param mockedHttpService
* @param endpointsResults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { isOnHostPage, hasSelectedHost } from './selectors';
import { isOnEndpointPage, hasSelectedEndpoint } from './selectors';
import { EndpointState } from '../types';
import { AppAction } from '../../../../common/store/actions';
import { ImmutableReducer } from '../../../../common/store';
Expand All @@ -29,7 +29,7 @@ export const initialEndpointListState: Immutable<EndpointState> = {
policyItemsLoading: false,
endpointPackageInfo: undefined,
nonExistingPolicies: {},
hostsExist: true,
endpointsExist: true,
};

/* eslint-disable-next-line complexity */
Expand Down Expand Up @@ -129,19 +129,19 @@ export const endpointListReducer: ImmutableReducer<EndpointState, AppAction> = (
} else if (action.type === 'serverReturnedEndpointExistValue') {
return {
...state,
hostsExist: action.payload,
endpointsExist: action.payload,
};
} else if (action.type === 'userChangedUrl') {
const newState: Immutable<EndpointState> = {
...state,
location: action.payload,
};
const isCurrentlyOnListPage = isOnHostPage(newState) && !hasSelectedHost(newState);
const wasPreviouslyOnListPage = isOnHostPage(state) && !hasSelectedHost(state);
const isCurrentlyOnDetailsPage = isOnHostPage(newState) && hasSelectedHost(newState);
const wasPreviouslyOnDetailsPage = isOnHostPage(state) && hasSelectedHost(state);
const isCurrentlyOnListPage = isOnEndpointPage(newState) && !hasSelectedEndpoint(newState);
const wasPreviouslyOnListPage = isOnEndpointPage(state) && !hasSelectedEndpoint(state);
const isCurrentlyOnDetailsPage = isOnEndpointPage(newState) && hasSelectedEndpoint(newState);
const wasPreviouslyOnDetailsPage = isOnEndpointPage(state) && hasSelectedEndpoint(state);

// if on the host list page for the first time, return new location and load list
// if on the endpoint list page for the first time, return new location and load list
if (isCurrentlyOnListPage) {
if (!wasPreviouslyOnListPage) {
return {
Expand All @@ -154,7 +154,7 @@ export const endpointListReducer: ImmutableReducer<EndpointState, AppAction> = (
};
}
} else if (isCurrentlyOnDetailsPage) {
// if previous page was the list or another host details page, load host details only
// if previous page was the list or another endpoint details page, load endpoint details only
if (wasPreviouslyOnDetailsPage || wasPreviouslyOnListPage) {
return {
...state,
Expand All @@ -166,7 +166,7 @@ export const endpointListReducer: ImmutableReducer<EndpointState, AppAction> = (
policyResponseError: undefined,
};
} else {
// if previous page was not host list or host details, load both list and details
// if previous page was not endpoint list or endpoint details, load both list and details
return {
...state,
location: action.payload,
Expand All @@ -180,14 +180,14 @@ export const endpointListReducer: ImmutableReducer<EndpointState, AppAction> = (
};
}
}
// otherwise we are not on a host list or details page
// otherwise we are not on a endpoint list or details page
return {
...state,
location: action.payload,
error: undefined,
detailsError: undefined,
policyResponseError: undefined,
hostsExist: true,
endpointsExist: true,
};
}
return state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const policyResponseLoading = (state: Immutable<EndpointState>): boolean

export const policyResponseError = (state: Immutable<EndpointState>) => state.policyResponseError;

export const isOnHostPage = (state: Immutable<EndpointState>) => {
export const isOnEndpointPage = (state: Immutable<EndpointState>) => {
return (
matchPath(state.location?.pathname ?? '', {
path: MANAGEMENT_ROUTING_ENDPOINTS_PATH,
Expand Down Expand Up @@ -171,10 +171,10 @@ export const uiQueryParams: (
}
);

export const hasSelectedHost: (state: Immutable<EndpointState>) => boolean = createSelector(
export const hasSelectedEndpoint: (state: Immutable<EndpointState>) => boolean = createSelector(
uiQueryParams,
({ selected_endpoint: selectedHost }) => {
return selectedHost !== undefined;
({ selected_endpoint: selectedEndpoint }) => {
return selectedEndpoint !== undefined;
}
);

Expand All @@ -197,15 +197,16 @@ export const policyResponseStatus: (state: Immutable<EndpointState>) => string =
);

/**
* returns the list of known non-existing polices that may have been in the Host API response.
* returns the list of known non-existing polices that may have been in the Endpoint API response.
* @param state
*/
export const nonExistingPolicies: (
state: Immutable<EndpointState>
) => Immutable<EndpointState['nonExistingPolicies']> = (state) => state.nonExistingPolicies;

/**
* Return boolean that indicates whether hosts exist
* Return boolean that indicates whether endpoints exist
* @param state
*/
export const hostsExist: (state: Immutable<EndpointState>) => boolean = (state) => state.hostsExist;
export const endpointsExist: (state: Immutable<EndpointState>) => boolean = (state) =>
state.endpointsExist;
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface EndpointState {
/** tracks the list of policies IDs used in Host metadata that may no longer exist */
nonExistingPolicies: Record<string, boolean>;
/** Tracks whether hosts exist and helps control if onboarding should be visible */
hostsExist: boolean;
endpointsExist: boolean;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ export const EndpointPolicyLink = memo<
);
});

EndpointPolicyLink.displayName = 'HostPolicyLink';
EndpointPolicyLink.displayName = 'EndpointPolicyLink';
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { getEndpointDetailsPath } from '../../../../common/routing';
import { SecurityPageName } from '../../../../../app/types';
import { useFormatUrl } from '../../../../../common/components/link_to';
import { AgentDetailsReassignConfigAction } from '../../../../../../../ingest_manager/public';
import { EndpointPolicyLink } from '../components/host_policy_link';
import { EndpointPolicyLink } from '../components/endpoint_policy_link';

const HostIds = styled(EuiListGroupItem)`
margin-top: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ export const EndpointDetailsFlyout = memo(() => {
const history = useHistory();
const toasts = useToasts();
const queryParams = useEndpointSelector(uiQueryParams);
const { selected_endpoint: selectedHost, ...queryParamsWithoutSelectedHost } = queryParams;
const {
selected_endpoint: selectedEndpoint,
...queryParamsWithoutSelectedEndpoint
} = queryParams;
const details = useEndpointSelector(detailsData);
const loading = useEndpointSelector(detailsLoading);
const error = useEndpointSelector(detailsError);
const show = useEndpointSelector(showView);

const handleFlyoutClose = useCallback(() => {
history.push(urlFromQueryParams(queryParamsWithoutSelectedHost));
}, [history, queryParamsWithoutSelectedHost]);
history.push(urlFromQueryParams(queryParamsWithoutSelectedEndpoint));
}, [history, queryParamsWithoutSelectedEndpoint]);

useEffect(() => {
if (error !== undefined) {
Expand Down
Loading

0 comments on commit 14c0bec

Please sign in to comment.