Skip to content

Commit

Permalink
Enrollment api keys route service
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Feb 13, 2020
1 parent 6e34675 commit 6d6c8e7
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 25 deletions.
9 changes: 9 additions & 0 deletions x-pack/plugins/ingest_manager/common/services/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AGENT_CONFIG_API_ROUTES,
FLEET_SETUP_API_ROUTES,
AGENT_API_ROUTES,
ENROLLMENT_API_KEY_ROUTES,
} from '../constants';

export const epmRouteService = {
Expand Down Expand Up @@ -90,3 +91,11 @@ export const agentRouteService = {
getUnenrollPath: () => AGENT_API_ROUTES.UNENROLL_PATTERN,
getListPath: () => AGENT_API_ROUTES.LIST_PATTERN,
};

export const enrollmentAPIKeyRouteService = {
getListPath: () => ENROLLMENT_API_KEY_ROUTES.LIST_PATTERN,
getCreatePath: () => ENROLLMENT_API_KEY_ROUTES.CREATE_PATTERN,
getInfoPath: (keyId: string) => ENROLLMENT_API_KEY_ROUTES.INFO_PATTERN.replace('{keyId}', keyId),
getDeletePath: (keyId: string) =>
ENROLLMENT_API_KEY_ROUTES.DELETE_PATTERN.replace('{keyId}', keyId),
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

export { useCore, CoreContext } from './use_core';
export { useConfig, ConfigContext } from './use_config';
export { useDeps, DepsContext } from './use_deps';
export { useSetupDeps, useStartDeps, DepsContext } from './use_deps';
export { useLink } from './use_link';
export { usePagination, Pagination } from './use_pagination';
export { useDebounce } from './use_debounce';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
import React, { useContext } from 'react';
import { IngestManagerSetupDeps, IngestManagerStartDeps } from '../../../plugin';

export const DepsContext = React.createContext<IngestManagerSetupDeps | null>(null);
export const StartDepsContext = React.createContext<IngestManagerStartDeps | null>(null);
export const DepsContext = React.createContext<{
setup: IngestManagerSetupDeps;
start: IngestManagerStartDeps;
} | null>(null);

export function useDeps() {
export function useSetupDeps() {
const deps = useContext(DepsContext);
if (deps === null) {
throw new Error('DepsContext not initialized');
}
return deps;
return deps.setup;
}

export function useStartDeps() {
const deps = useContext(StartDepsContext);
const deps = useContext(DepsContext);
if (deps === null) {
throw new Error('StartDepsContext not initialized');
}
return deps;
return deps.start;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { EPM_PATH, FLEET_PATH, AGENT_CONFIG_PATH } from './constants';
import { DefaultLayout } from './layouts';
import { IngestManagerOverview, EPMApp, AgentConfigApp, FleetApp } from './sections';
import { CoreContext, DepsContext, ConfigContext, setHttpClient } from './hooks';
import { StartDepsContext } from './hooks/use_deps';

const IngestManagerRoutes = ({ ...rest }) => (
<EuiErrorBoundary>
Expand Down Expand Up @@ -65,14 +64,12 @@ const IngestManagerApp = ({
return (
<coreStart.i18n.Context>
<CoreContext.Provider value={coreStart}>
<DepsContext.Provider value={setupDeps}>
<StartDepsContext.Provider value={startDeps}>
<ConfigContext.Provider value={config}>
<EuiThemeProvider darkMode={isDarkMode}>
<IngestManagerRoutes />
</EuiThemeProvider>
</ConfigContext.Provider>
</StartDepsContext.Provider>
<DepsContext.Provider value={{ setup: setupDeps, start: startDeps }}>
<ConfigContext.Provider value={config}>
<EuiThemeProvider darkMode={isDarkMode}>
<IngestManagerRoutes />
</EuiThemeProvider>
</ConfigContext.Provider>
</DepsContext.Provider>
</CoreContext.Provider>
</coreStart.i18n.Context>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export const AgentConfigDetailsPage: React.FC = () => {
</EuiFlexItem>
<EuiFlexItem grow={false}>
{/* TODO: Make this link to filtered agents list and change to real agent count */}
<ConnectedLink color="primary" path={`/agents`}>
<ConnectedLink color="primary" path={`/fleet/agents`}>
<FormattedMessage
id="xpack.ingestManager.policyDetails.viewAgentsLinkText"
defaultMessage="View agents"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { useInput, sendRequest } from '../../../../../hooks';
import { usePolicies } from './hooks';
import { enrollmentAPIKeyRouteService } from '../../../../../services';

export const CreateApiKeyForm: React.FC<{ onChange: () => void }> = ({ onChange }) => {
const { data: policies } = usePolicies();
Expand Down Expand Up @@ -73,7 +74,7 @@ function useCreateApiKey(onSuccess: () => void) {
setSubmitted(true);
await sendRequest({
method: 'post',
path: '/api/ingest_manager/fleet/enrollment-api-keys',
path: enrollmentAPIKeyRouteService.getCreatePath(),
body: JSON.stringify({
name: inputs.nameInput.value,
policy_id: inputs.policyIdInput.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import {
GetOneEnrollmentAPIKeyResponse,
GetAgentConfigsResponse,
} from '../../../../../types';
import { enrollmentAPIKeyRouteService, agentConfigRouteService } from '../../../../../services';

export function useEnrollmentApiKeys(pagination: Pagination) {
const request = useRequest<GetEnrollmentAPIKeysResponse>({
method: 'get',
path: '/api/ingest_manager/fleet/enrollment-api-keys',
path: enrollmentAPIKeyRouteService.getListPath(),
});

return {
Expand All @@ -27,7 +28,7 @@ export function useEnrollmentApiKeys(pagination: Pagination) {
export function usePolicies() {
const request = useRequest<GetAgentConfigsResponse>({
method: 'get',
path: '/api/ingest_manager/agent_configs',
path: agentConfigRouteService.getListPath(),
});

return {
Expand All @@ -39,7 +40,7 @@ export function usePolicies() {
export function useEnrollmentApiKey(apiKeyId: string | null) {
const request = useRequest<GetOneEnrollmentAPIKeyResponse>({
method: 'get',
path: `/api/ingest_manager/fleet/enrollment-api-keys/${apiKeyId}`,
path: enrollmentAPIKeyRouteService.getInfoPath(apiKeyId as string),
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useEnrollmentApiKeys, useEnrollmentApiKey } from './hooks';
import { ConfirmDeleteModal } from './confirm_delete_modal';
import { CreateApiKeyForm } from './create_api_key_form';
import { EnrollmentAPIKey } from '../../../../../types';
import { enrollmentAPIKeyRouteService } from '../../../../../services';
export { useEnrollmentApiKeys, useEnrollmentApiKey } from './hooks';

export const EnrollmentApiKeysTable: React.FC<{
Expand Down Expand Up @@ -63,7 +64,7 @@ export const EnrollmentApiKeysTable: React.FC<{
onConfirm={async () => {
await sendRequest({
method: 'delete',
path: `/api/ingest_manager/fleet/enrollment-api-keys/${confirmDeleteApiKeyId}`,
path: enrollmentAPIKeyRouteService.getDeletePath(confirmDeleteApiKeyId),
});
setConfirmDeleteApiKeyId(null);
refresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export {
agentConfigRouteService,
fleetSetupRouteService,
agentRouteService,
enrollmentAPIKeyRouteService,
} from '../../../../common';
19 changes: 16 additions & 3 deletions x-pack/plugins/ingest_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import { LicensingPluginSetup, ILicense } from '../../licensing/server';
import { EncryptedSavedObjectsPluginStart } from '../../encrypted_saved_objects/server';
import { SecurityPluginSetup } from '../../security/server';
import { PluginSetupContract as FeaturesPluginSetup } from '../../features/server';
import { PLUGIN_ID } from './constants';
import {
PLUGIN_ID,
AGENT_SAVED_OBJECT_TYPE,
AGENT_EVENT_SAVED_OBJECT_TYPE,
ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE,
} from './constants';
import { licenseService, configService, appContextService } from './services';
import {
registerEPMRoutes,
Expand Down Expand Up @@ -78,7 +83,11 @@ export class IngestManagerPlugin implements Plugin {
all: {
api: [PLUGIN_ID],
savedObject: {
all: ['agents', 'events', 'enrollment_api_keys'],
all: [
AGENT_SAVED_OBJECT_TYPE,
AGENT_EVENT_SAVED_OBJECT_TYPE,
ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE,
],
read: [],
},
ui: ['show', 'read', 'write'],
Expand All @@ -87,7 +96,11 @@ export class IngestManagerPlugin implements Plugin {
api: [PLUGIN_ID],
savedObject: {
all: [],
read: ['agents', 'events', 'enrollment_api_keys'],
read: [
AGENT_SAVED_OBJECT_TYPE,
AGENT_EVENT_SAVED_OBJECT_TYPE,
ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE,
],
},
ui: ['show', 'read'],
},
Expand Down

0 comments on commit 6d6c8e7

Please sign in to comment.