Skip to content

Commit

Permalink
[Snapshot Restore] Fix initial policy form state (#83928)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth authored Dec 2, 2020
1 parent fb48e90 commit 23dccb7
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ describe('<PolicyEdit />', () => {
const { snapshotName } = POLICY_EDIT;

// Complete step 1, change snapshot name
form.setInputValue('snapshotNameInput', `${snapshotName}-edited`);
const editedSnapshotName = `${snapshotName}-edited`;
form.setInputValue('snapshotNameInput', editedSnapshotName);
actions.clickNextButton();

// Complete step 2, enable ignore unavailable indices switch
Expand All @@ -143,20 +144,24 @@ describe('<PolicyEdit />', () => {

const latestRequest = server.requests[server.requests.length - 1];

const { name, isManagedPolicy, schedule, repository, retention } = POLICY_EDIT;

const expected = {
...POLICY_EDIT,
...{
config: {
ignoreUnavailable: true,
},
retention: {
...POLICY_EDIT.retention,
expireAfterValue: Number(EXPIRE_AFTER_VALUE),
expireAfterUnit: EXPIRE_AFTER_UNIT,
},
snapshotName: `${POLICY_EDIT.snapshotName}-edited`,
name,
isManagedPolicy,
schedule,
repository,
config: {
ignoreUnavailable: true,
},
retention: {
...retention,
expireAfterValue: Number(EXPIRE_AFTER_VALUE),
expireAfterUnit: EXPIRE_AFTER_UNIT,
},
snapshotName: editedSnapshotName,
};

expect(JSON.parse(JSON.parse(latestRequest.requestBody).body)).toEqual(expected);
});

Expand All @@ -180,10 +185,25 @@ describe('<PolicyEdit />', () => {

const latestRequest = server.requests[server.requests.length - 1];

const {
name,
isManagedPolicy,
schedule,
repository,
retention,
config,
snapshotName,
} = POLICY_EDIT;

const expected = {
...POLICY_EDIT,
name,
isManagedPolicy,
schedule,
repository,
config,
snapshotName,
retention: {
...POLICY_EDIT.retention,
...retention,
expireAfterValue: Number(EXPIRE_AFTER_VALUE),
expireAfterUnit: TIME_UNITS.DAY, // default value
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,22 @@ export const PolicyEdit: React.FunctionComponent<RouteComponentProps<MatchParams

// Update policy state when data is loaded
useEffect(() => {
if (policyData && policyData.policy) {
setPolicy(policyData.policy);
if (policyData?.policy) {
const { policy: policyToEdit } = policyData;

// The policy response includes data not pertinent to the form
// that we need to remove, e.g., lastSuccess, lastFailure, stats
const policyFormData: SlmPolicyPayload = {
name: policyToEdit.name,
snapshotName: policyToEdit.snapshotName,
schedule: policyToEdit.schedule,
repository: policyToEdit.repository,
config: policyToEdit.config,
retention: policyToEdit.retention,
isManagedPolicy: policyToEdit.isManagedPolicy,
};

setPolicy(policyFormData);
}
}, [policyData]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,12 @@ const snapshotRetentionSchema = schema.object({

export const policySchema = schema.object({
name: schema.string(),
version: schema.maybe(schema.number()),
modifiedDate: schema.maybe(schema.string()),
modifiedDateMillis: schema.maybe(schema.number()),
snapshotName: schema.string(),
schedule: schema.string(),
repository: schema.string(),
nextExecution: schema.maybe(schema.string()),
nextExecutionMillis: schema.maybe(schema.number()),
config: schema.maybe(snapshotConfigSchema),
retention: schema.maybe(snapshotRetentionSchema),
isManagedPolicy: schema.boolean(),
stats: schema.maybe(schema.object({}, { unknowns: 'allow' })),
lastFailure: schema.maybe(schema.object({}, { unknowns: 'allow' })),
lastSuccess: schema.maybe(schema.object({}, { unknowns: 'allow' })),
});

const fsRepositorySettings = schema.object({
Expand Down
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export default function ({ loadTestFile }) {
loadTestFile(require.resolve('./index_management'));
loadTestFile(require.resolve('./index_lifecycle_management'));
loadTestFile(require.resolve('./ingest_pipelines'));
loadTestFile(require.resolve('./snapshot_restore'));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 { FtrProviderContext } from '../../../ftr_provider_context';

export default function ({ loadTestFile }: FtrProviderContext) {
describe('Snapshot and Restore', () => {
loadTestFile(require.resolve('./snapshot_restore'));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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 { FtrProviderContext } from '../../../../ftr_provider_context';

interface SlmPolicy {
name: string;
snapshotName: string;
schedule: string;
repository: string;
isManagedPolicy: boolean;
config?: {
indices?: string | string[];
ignoreUnavailable?: boolean;
includeGlobalState?: boolean;
partial?: boolean;
metadata?: Record<string, string>;
};
retention?: {
expireAfterValue?: number | '';
expireAfterUnit?: string;
maxCount?: number | '';
minCount?: number | '';
};
}

/**
* Helpers to create and delete SLM policies on the Elasticsearch instance
* during our tests.
* @param {ElasticsearchClient} es The Elasticsearch client instance
*/
export const registerEsHelpers = (getService: FtrProviderContext['getService']) => {
let policiesCreated: string[] = [];

const es = getService('legacyEs');

const createRepository = (repoName: string) => {
return es.snapshot.createRepository({
repository: repoName,
body: {
type: 'fs',
settings: {
location: '/tmp/',
},
},
verify: false,
});
};

const createPolicy = (policy: SlmPolicy, cachePolicy?: boolean) => {
if (cachePolicy) {
policiesCreated.push(policy.name);
}

return es.sr.updatePolicy({
name: policy.name,
body: policy,
});
};

const getPolicy = (policyName: string) => {
return es.sr.policy({
name: policyName,
human: true,
});
};

const deletePolicy = (policyName: string) => es.sr.deletePolicy({ name: policyName });

const cleanupPolicies = () =>
Promise.all(policiesCreated.map(deletePolicy))
.then(() => {
policiesCreated = [];
})
.catch((err) => {
// eslint-disable-next-line no-console
console.log(`[Cleanup error] Error deleting ES resources: ${err.message}`);
});

return {
createRepository,
createPolicy,
deletePolicy,
cleanupPolicies,
getPolicy,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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.
*/

export { registerEsHelpers } from './elasticsearch';
Loading

0 comments on commit 23dccb7

Please sign in to comment.