Skip to content

Commit 7add8b9

Browse files
committed
Update (de)serializer to use global repo value
1 parent 175a16b commit 7add8b9

File tree

8 files changed

+77
-17
lines changed

8 files changed

+77
-17
lines changed

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/components/phases/shared_fields/searchable_snapshot_field/searchable_snapshot_field.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export const SearchableSnapshotField: FunctionComponent<Props> = ({ phase }) =>
164164
<div className="ilmSearchableSnapshotField">
165165
<UseField
166166
path={searchableSnapshotRepoPath}
167-
defaultValue={[searchableSnapshotGlobalRepo]}
167+
defaultValue={!!searchableSnapshotGlobalRepo ? [searchableSnapshotGlobalRepo] : []}
168168
component={RepositoryComboBoxField}
169169
componentProps={{
170170
globalRepository: searchableSnapshotGlobalRepo,

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/edit_policy.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
EuiTitle,
3131
} from '@elastic/eui';
3232

33-
import { TextField, useForm, useFormData } from '../../../shared_imports';
33+
import { TextField, useForm, useFormData, useKibana } from '../../../shared_imports';
3434
import { toasts } from '../../services/notification';
3535
import { createDocLink } from '../../services/documentation';
3636
import { UseField } from './form';
@@ -44,7 +44,13 @@ import {
4444
Timeline,
4545
FormErrorsCallout,
4646
} from './components';
47-
import { createPolicyNameValidations, createSerializer, deserializer, Form, schema } from './form';
47+
import {
48+
createPolicyNameValidations,
49+
createSerializer,
50+
deserializer,
51+
Form,
52+
getSchema,
53+
} from './form';
4854
import { useEditPolicyContext } from './edit_policy_context';
4955
import { FormInternal } from './types';
5056

@@ -67,6 +73,10 @@ export const EditPolicy: React.FunctionComponent<Props> = ({ history }) => {
6773
policyName,
6874
} = useEditPolicyContext();
6975

76+
const {
77+
services: { cloud },
78+
} = useKibana();
79+
7080
const [saveAsNew, setSaveAsNew] = useState(false);
7181
const originalPolicyName: string = isNewPolicy ? '' : policyName!;
7282

@@ -82,6 +92,10 @@ export const EditPolicy: React.FunctionComponent<Props> = ({ history }) => {
8292
[currentPolicy, originalPolicyName]
8393
);
8494

95+
const schema = useMemo(() => {
96+
return getSchema(Boolean(cloud?.isCloudEnabled));
97+
}, [cloud?.isCloudEnabled]);
98+
8599
const { form } = useForm({
86100
schema,
87101
defaultValue,

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/form/deserializer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88
import { produce } from 'immer';
99

1010
import { SerializedPolicy } from '../../../../../common/types';
11-
1211
import { splitSizeAndUnits } from '../../../lib/policies';
13-
1412
import { determineDataTierAllocationType, isUsingDefaultRollover } from '../../../lib';
15-
13+
import { getDefaultRepository } from '../lib';
1614
import { FormInternal } from '../types';
1715

1816
export const deserializer = (policy: SerializedPolicy): FormInternal => {
1917
const {
2018
phases: { hot, warm, cold, delete: deletePhase },
2119
} = policy;
2220

21+
const defaultRepository = getDefaultRepository([
22+
hot?.actions.searchable_snapshot,
23+
cold?.actions.searchable_snapshot,
24+
]);
25+
2326
const _meta: FormInternal['_meta'] = {
2427
hot: {
2528
isUsingDefaultRollover: isUsingDefaultRollover(policy),
@@ -44,6 +47,9 @@ export const deserializer = (policy: SerializedPolicy): FormInternal => {
4447
delete: {
4548
enabled: Boolean(deletePhase),
4649
},
50+
searchableSnapshot: {
51+
repository: defaultRepository,
52+
},
4753
};
4854

4955
return produce<FormInternal>(

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/form/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export { deserializer } from './deserializer';
99

1010
export { createSerializer } from './serializer';
1111

12-
export { schema } from './schema';
12+
export { getSchema } from './schema';
1313

1414
export * from './validations';
1515

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/form/schema.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ import { i18n } from '@kbn/i18n';
1010
import { FormSchema, fieldValidators } from '../../../../shared_imports';
1111
import { defaultIndexPriority } from '../../../constants';
1212
import { ROLLOVER_FORM_PATHS } from '../constants';
13-
14-
const rolloverFormPaths = Object.values(ROLLOVER_FORM_PATHS);
15-
13+
import { i18nTexts } from '../i18n_texts';
1614
import {
1715
ifExistsNumberGreaterThanZero,
1816
ifExistsNumberNonNegative,
1917
rolloverThresholdsValidator,
2018
minAgeValidator,
2119
} from './validations';
2220

23-
import { i18nTexts } from '../i18n_texts';
21+
const rolloverFormPaths = Object.values(ROLLOVER_FORM_PATHS);
22+
23+
/**
24+
* This repository is provisioned by Elastic Cloud and will always
25+
* exist as a "managed" repository.
26+
*/
27+
const CLOUD_DEFAULT_REPO = 'found-snapshots';
2428

2529
const { emptyField, numberGreaterThanField } = fieldValidators;
2630

@@ -34,11 +38,10 @@ const searchableSnapshotFields = {
3438
validations: [
3539
{ validator: emptyField(i18nTexts.editPolicy.errors.searchableSnapshotRepoRequired) },
3640
],
37-
defaultValue: [],
3841
},
3942
};
4043

41-
export const schema: FormSchema = {
44+
export const getSchema = (isCloudEnabled: boolean): FormSchema => ({
4245
_meta: {
4346
hot: {
4447
isUsingDefaultRollover: {
@@ -130,6 +133,11 @@ export const schema: FormSchema = {
130133
defaultValue: 'd',
131134
},
132135
},
136+
searchableSnapshot: {
137+
repository: {
138+
defaultValue: isCloudEnabled ? CLOUD_DEFAULT_REPO : '',
139+
},
140+
},
133141
},
134142
phases: {
135143
hot: {
@@ -377,4 +385,4 @@ export const schema: FormSchema = {
377385
},
378386
},
379387
},
380-
};
388+
});

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/form/serializer/serializer.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ export const createSerializer = (originalPolicy?: SerializedPolicy) => (
124124
/**
125125
* HOT PHASE SEARCHABLE SNAPSHOT
126126
*/
127-
if (!updatedPolicy.phases.hot!.actions?.searchable_snapshot) {
127+
if (updatedPolicy.phases.hot!.actions?.searchable_snapshot) {
128+
hotPhaseActions.searchable_snapshot = {
129+
...hotPhaseActions.searchable_snapshot,
130+
snapshot_repository: _meta.searchableSnapshot.repository,
131+
};
132+
} else {
128133
delete hotPhaseActions.searchable_snapshot;
129134
}
130135
}
@@ -234,7 +239,12 @@ export const createSerializer = (originalPolicy?: SerializedPolicy) => (
234239
/**
235240
* COLD PHASE SEARCHABLE SNAPSHOT
236241
*/
237-
if (!updatedPolicy.phases.cold?.actions?.searchable_snapshot) {
242+
if (updatedPolicy.phases.cold?.actions?.searchable_snapshot) {
243+
coldPhase.actions.searchable_snapshot = {
244+
...coldPhase.actions.searchable_snapshot,
245+
snapshot_repository: _meta.searchableSnapshot.repository,
246+
};
247+
} else {
238248
delete coldPhase.actions.searchable_snapshot;
239249
}
240250
} else {
@@ -254,7 +264,7 @@ export const createSerializer = (originalPolicy?: SerializedPolicy) => (
254264
deletePhase.actions.delete = deletePhase.actions.delete ?? {};
255265

256266
/**
257-
* DELETE PHASE SEARCHABLE SNAPSHOT
267+
* DELETE PHASE MIN AGE
258268
*/
259269
if (updatedPolicy.phases.delete?.min_age) {
260270
deletePhase.min_age = `${updatedPolicy.phases.delete!.min_age}${_meta.delete.minAgeUnit}`;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { SearchableSnapshotAction } from '../../../../../common/types';
9+
10+
export const getDefaultRepository = (
11+
configs: Array<SearchableSnapshotAction | undefined>
12+
): string => {
13+
if (configs.length === 0) {
14+
return '';
15+
}
16+
if (Boolean(configs[0]?.snapshot_repository)) {
17+
return configs[0]!.snapshot_repository;
18+
}
19+
return getDefaultRepository(configs.slice(1));
20+
};

x-pack/plugins/index_lifecycle_management/public/application/sections/edit_policy/lib/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ export {
1212
PhaseAgeInMilliseconds,
1313
RelativePhaseTimingInMs,
1414
} from './absolute_timing_to_relative_timing';
15+
16+
export { getDefaultRepository } from './get_default_repository';

0 commit comments

Comments
 (0)