Skip to content

Commit

Permalink
Populate policy combo box based on values from Ingest policy API
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Dec 20, 2019
1 parent a502cb0 commit 503dfec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/epm/common/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ export function getRemovePath(pkgkey: string) {
}

export const getInstallDatasourcePath = () => API_INSTALL_DATASOURCE_PATTERN;
export const getListPoliciesPath = () => '/api/ingest/policies';
15 changes: 15 additions & 0 deletions x-pack/legacy/plugins/epm/public/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getInstallDatasourcePath,
getInstallPath,
getListPath,
getListPoliciesPath,
getRemovePath,
ListParams,
} from '../common/routes';
Expand All @@ -23,6 +24,7 @@ import {
PackagesGroupedByStatus,
DatasourcePayload,
} from '../common/types';
import { ReturnTypeList } from '../../ingest/common/types/std_return_format';

const defaultClient: HttpHandler = (path: string, options?: HttpFetchOptions) =>
fetch(path, options).then(res => res.json());
Expand Down Expand Up @@ -89,3 +91,16 @@ export async function installDatasource(datasource: DatasourcePayload): Promise<
const body = JSON.stringify(datasource);
return _fetch(path, { body, method: 'POST' });
}

// This should come from the shared Ingest types
// However, they are in a /server directory so /public/* cannot access them
// Using this partial/placeholder type until we can figure out how to share
interface PlaceholderPolicy {
name: string;
id: string;
}

export async function getPolicies(): Promise<ReturnTypeList<PlaceholderPolicy>> {
const path = getListPoliciesPath();
return _fetch(path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
EuiSteps,
EuiCheckboxGroupIdToSelectedMap,
} from '@elastic/eui';
import React, { Fragment, useState } from 'react';
import React, { Fragment, useEffect, useState } from 'react';
import { Redirect } from 'react-router-dom';
import styled from 'styled-components';
import { installDatasource } from '../../data';
import { installDatasource, getPolicies } from '../../data';
import { useCore, useLinks } from '../../hooks';
import { StepOne } from './step_one';
import { PackageInfo } from '../../../common/types';
Expand All @@ -31,6 +31,10 @@ const StyledSteps = styled.div`
interface AddDataSourceStepsProps {
package: PackageInfo;
}
interface PolicyOption {
label: string;
value: string;
}
export interface FormState {
datasourceName: string;
datasets: EuiCheckboxGroupIdToSelectedMap;
Expand All @@ -42,7 +46,15 @@ const FormNav = styled.div`
`;

export const AddDataSourceForm = ({ package: pkg }: AddDataSourceStepsProps) => {
const defaultPolicyOption = { label: 'Default policy', value: 'default' };
const defaultPolicyOption: PolicyOption = { label: 'Default policy', value: 'default' };
const [policyOptions, setPolicyOptions] = useState<PolicyOption[]>([defaultPolicyOption]);
useEffect(() => {
getPolicies()
.then(response => response.list)
.then(policies => policies.map(policy => ({ label: policy.name, value: policy.id })))
.then(setPolicyOptions);
}, []);

const [addDataSourceSuccess, setAddDataSourceSuccess] = useState<boolean>(false);
const [datasourceName, setDatasourceName] = useState<FormState['datasourceName']>('');
const [selectedDatasets, setSelectedDatasets] = useState<FormState['datasets']>({});
Expand Down Expand Up @@ -102,12 +114,9 @@ export const AddDataSourceForm = ({ package: pkg }: AddDataSourceStepsProps) =>
children: (
<StepOne
datasetCheckboxes={checkboxes}
policyOptions={[
defaultPolicyOption,
{ label: 'Foo policy', value: 'd09bbe00-21e0-11ea-9786-4545a9e62b25' },
]}
onDatasetChange={onDatasetChange}
onNameChange={onNameChange}
policyOptions={policyOptions}
onPolicyChange={setSelectedPolicies}
formState={formState}
/>
Expand Down

0 comments on commit 503dfec

Please sign in to comment.