Skip to content

Commit

Permalink
[Fleet] Enrollment api key UI (#51495)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Nov 25, 2019
1 parent a081bf4 commit d47ab5d
Show file tree
Hide file tree
Showing 15 changed files with 575 additions and 171 deletions.
24 changes: 24 additions & 0 deletions x-pack/legacy/plugins/fleet/public/hooks/use_input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';

export function useInput(defaultValue = '') {
const [value, setValue] = React.useState<string>(defaultValue);

return {
value,
props: {
onChange: (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>) => {
setValue(e.target.value);
},
value,
},
clear: () => {
setValue('');
},
};
}
10 changes: 6 additions & 4 deletions x-pack/legacy/plugins/fleet/public/hooks/use_pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import { useState } from 'react';
import { DEFAULT_AGENTS_PAGE_SIZE, AGENTS_PAGE_SIZE_OPTIONS } from '../../common/constants';

export interface Pagination {
currentPage: number;
pageSize: number;
}

export function usePagination() {
const [pagination, setPagination] = useState<{
currentPage: number;
pageSize: number;
}>({
const [pagination, setPagination] = useState<Pagination>({
currentPage: 1,
pageSize: DEFAULT_AGENTS_PAGE_SIZE,
});
Expand Down
3 changes: 3 additions & 0 deletions x-pack/legacy/plugins/fleet/public/lib/compose/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { FrontendLibs } from '../types';
import { PLUGIN } from '../../../common/constants/plugin';
import { FrameworkLib } from '../framework';
import { INDEX_NAMES } from '../../../common/constants';
import { EnrollmentApiKeyLib } from '../enrollment_api_key';

// A super early spot in kibana loading that we can use to hook before most other things
const onKibanaReady = chrome.dangerouslyGetActiveInjector;
Expand All @@ -35,6 +36,7 @@ export function compose(): FrontendLibs {
const elasticsearchLib = new ElasticsearchLib(esAdapter);
const agents = new AgentsLib(new RestAgentAdapter(api));
const policies = new PoliciesLib(new RestPolicyAdapter(api));
const enrollmentApiKeys = new EnrollmentApiKeyLib(api);

const framework = new FrameworkLib(
new KibanaFrameworkAdapter(
Expand All @@ -54,6 +56,7 @@ export function compose(): FrontendLibs {
elasticsearch: elasticsearchLib,
agents,
policies,
enrollmentApiKeys,
};
return libs;
}
66 changes: 0 additions & 66 deletions x-pack/legacy/plugins/fleet/public/lib/compose/memory.ts

This file was deleted.

63 changes: 0 additions & 63 deletions x-pack/legacy/plugins/fleet/public/lib/compose/scripts.ts

This file was deleted.

46 changes: 46 additions & 0 deletions x-pack/legacy/plugins/fleet/public/lib/enrollment_api_key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import {
ReturnTypeList,
ReturnTypeGet,
ReturnTypeCreate,
ReturnTypeDelete,
} from '../../common/return_types';
import { RestAPIAdapter } from './adapters/rest_api/adapter_types';
import { Pagination } from '../hooks/use_pagination';
import { EnrollmentApiKey } from '../../common/types/domain_data';

export class EnrollmentApiKeyLib {
constructor(private readonly rest: RestAPIAdapter) {}

public async listKeys(pagination: Pagination) {
return await this.rest.get<ReturnTypeList<EnrollmentApiKey>>('/api/fleet/enrollment-api-keys', {
page: pagination.currentPage,
perPage: pagination.pageSize,
});
}

public async get(keyId: string) {
return await this.rest.get<ReturnTypeGet<EnrollmentApiKey>>(
`/api/fleet/enrollment-api-keys/${keyId}`
);
}

public async delete(keyId: string) {
return await this.rest.delete<ReturnTypeDelete>(`/api/fleet/enrollment-api-keys/${keyId}`);
}

public async create(data: { name: string; policyId: string }) {
return await this.rest.post<ReturnTypeCreate<EnrollmentApiKey>>(
`/api/fleet/enrollment-api-keys`,
{
name: data.name,
policy_id: data.policyId,
}
);
}
}
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/fleet/public/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { AgentsLib } from './agent';
import { PoliciesLib } from './policy';
import { ElasticsearchLib } from './elasticsearch';
import { FrameworkLib } from './framework';
import { EnrollmentApiKeyLib } from './enrollment_api_key';

export interface FrontendLibs {
elasticsearch: ElasticsearchLib;
framework: FrameworkLib;
agents: AgentsLib;
policies: PoliciesLib;
enrollmentApiKeys: EnrollmentApiKeyLib;
}

export type FramworkAdapterConstructable = new (uiModule: IModule) => FrameworkAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,7 @@ import { AxiosError } from 'axios';
import { Agent } from '../../../../common/types/domain_data';
import { useLibs } from '../../../hooks/use_libs';
import { useAgentRefresh } from '../hooks/use_agent';

function useInput() {
const [value, setValue] = useState<string>('');

return {
value,
onChange: (e: React.ChangeEvent<HTMLInputElement>) => {
setValue(e.target.value);
},
clear: () => {
setValue('');
},
};
}
import { useInput } from '../../../hooks/use_input';

function useAddMetadataForm(agent: Agent, done: () => void) {
const libs = useLibs();
Expand Down Expand Up @@ -132,11 +119,7 @@ export const MetadataForm: SFC<{ agent: Agent }> = ({ agent }) => {
defaultMessage: 'Key',
})}
>
<EuiFieldText
required={true}
onChange={keyInput.onChange}
value={keyInput.value}
/>
<EuiFieldText required={true} {...keyInput.props} />
</EuiFormRow>
</EuiFlexItem>
<EuiFlexItem>
Expand All @@ -145,11 +128,7 @@ export const MetadataForm: SFC<{ agent: Agent }> = ({ agent }) => {
defaultMessage: 'Value',
})}
>
<EuiFieldText
required={true}
onChange={valueInput.onChange}
value={valueInput.value}
/>
<EuiFieldText required={true} {...valueInput.props} />
</EuiFormRow>
</EuiFlexItem>
<EuiFlexItem grow={false}>
Expand Down
Loading

0 comments on commit d47ab5d

Please sign in to comment.