Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Snapshot Restore] Fix initial policy form state #83928

Merged
merged 5 commits into from Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks for this comment!

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
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
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'));
});
}
@@ -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'));
});
}
@@ -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,
};
};
@@ -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';