Skip to content

Commit

Permalink
disable delete for active templates
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Jun 2, 2020
1 parent 808435a commit 78f939b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
EuiButton,
EuiInMemoryTableProps,
EuiTableFieldDataColumnType,
EuiIcon,
} from '@elastic/eui';

interface ComponentTemplate {
Expand All @@ -38,6 +39,13 @@ export const ComponentTable: FunctionComponent<Props> = ({
sorting: { sort: { field: 'name', direction: 'asc' } },
selection: {
onSelectionChange: setSelection,
selectable: ({ isActive }) => !isActive,
selectableMessage: (selectable) =>
!selectable
? i18n.translate('xpack.idxMgmt.componentTemplatesList.table.disabledSelectionLabel', {
defaultMessage: 'Component template is in use',
})
: undefined,
},
rowProps: () => ({
'data-test-subj': 'componentTemplateTableRow',
Expand All @@ -63,9 +71,7 @@ export const ComponentTable: FunctionComponent<Props> = ({
values={{ count: selection.length }}
/>
</EuiButton>
) : (
undefined
),
) : undefined,
toolsRight: [
<EuiButton
key="reloadButton"
Expand Down Expand Up @@ -115,6 +121,14 @@ export const ComponentTable: FunctionComponent<Props> = ({
</EuiLink>
),
},
{
field: 'isActive',
name: i18n.translate('xpack.idxMgmt.componentTemplatesList.table.isActiveColumnTitle', {
defaultMessage: 'Active',
}),
sortable: true,
render: (isActive: boolean) => (isActive ? <EuiIcon type="check" /> : null),
},
{
name: (
<FormattedMessage
Expand Down Expand Up @@ -167,6 +181,7 @@ export const ComponentTable: FunctionComponent<Props> = ({
icon: 'trash',
color: 'danger',
onClick: ({ name }) => onDeleteClick([name]),
enabled: ({ isActive }) => !isActive,
},
],
},
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/index_management/server/client/elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const elasticsearchJsPlugin = (Client: any, config: any, components: any)
Client.prototype.dataManagement = components.clientAction.namespaceFactory();
const dataManagement = Client.prototype.dataManagement.prototype;

// Component template routes
dataManagement.getComponentTemplates = ca({
urls: [
{
Expand Down Expand Up @@ -60,4 +61,14 @@ export const elasticsearchJsPlugin = (Client: any, config: any, components: any)
],
method: 'DELETE',
});

// Index templates v2
dataManagement.getComposableIndexTemplates = ca({
urls: [
{
fmt: '/_index_template',
},
],
method: 'GET',
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const paramsSchema = schema.object({
name: schema.string(),
});

const getComponentTemplatesInUse = (indexTemplates) => {
const componentTemplates = indexTemplates.map(({ index_template: indexTemplate }) => {
return indexTemplate.composed_of;
});
return [].concat.apply([], componentTemplates); // return flattened array
};

export function registerGetAllRoute({ router, license, lib: { isEsError } }: RouteDependencies) {
// Get all component templates
router.get(
Expand All @@ -20,9 +27,25 @@ export function registerGetAllRoute({ router, license, lib: { isEsError } }: Rou
const { callAsCurrentUser } = ctx.dataManagement!.client;

try {
const response = await callAsCurrentUser('dataManagement.getComponentTemplates');
const { component_templates: componentTemplates } = await callAsCurrentUser(
'dataManagement.getComponentTemplates'
);

const { index_templates: indexTemplates } = await callAsCurrentUser(
'dataManagement.getComposableIndexTemplates'
);

const activeComponentTemplates = getComponentTemplatesInUse(indexTemplates);

const body = componentTemplates.map((component) => {
const isActive = activeComponentTemplates.includes(component.name);
return {
...component,
isActive,
};
});

return res.ok({ body: response.component_templates });
return res.ok({ body });
} catch (error) {
if (isEsError(error)) {
return res.customError({
Expand Down

0 comments on commit 78f939b

Please sign in to comment.