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 pre existing policy with no existing repository #76861

Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ export type PolicyFormTestSubjects =
| 'showAdvancedCronLink'
| 'snapshotNameInput'
| 'dataStreamBadge'
| 'repositoryNotFoundWarning'
| 'repositorySelect'
| 'submitButton';
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ describe('<PolicyAdd />', () => {
expect(find('nextButton').props().disabled).toBe(true);
});

test('should not show repository-not-found warning', () => {
const { exists } = testBed;
expect(exists('repositoryNotFoundWarning')).toBe(false);
});

describe('form validation', () => {
describe('logistics (step 1)', () => {
test('should require a policy name', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,33 @@ describe('<PolicyEdit />', () => {
expect(find('pageTitle').text()).toEqual('Edit policy');
});

describe('policy with pre-existing repository that was deleted', () => {
beforeEach(async () => {
httpRequestsMockHelpers.setGetPolicyResponse({ policy: POLICY_EDIT });
httpRequestsMockHelpers.setLoadIndicesResponse({
indices: ['my_index'],
dataStreams: ['my_data_stream'],
});
httpRequestsMockHelpers.setLoadRepositoriesResponse({
repositories: [{ name: 'this-is-a-new-repository' }],
});

testBed = await setup();

await act(async () => {
await nextTick();
testBed.component.update();
});
});

test('should show repository-not-found warning', () => {
const { exists, find } = testBed;
expect(exists('repositoryNotFoundWarning')).toBe(true);
// The select should be an empty string to allow users to select a new repository
expect(find('repositorySelect').props().value).toBe('');
});
});

/**
* As the "edit" policy component uses the same form underneath that
* the "create" policy, we won't test it again but simply make sure that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
EuiLink,
EuiSpacer,
EuiText,
EuiCallOut,
} from '@elastic/eui';

import { Repository } from '../../../../../common/types';
Expand Down Expand Up @@ -256,29 +257,57 @@ export const PolicyStepLogistics: React.FunctionComponent<StepProps> = ({
}
}

const policyRepositoryExists =
!!policy.repository &&
repositories.some((r: { name: string }) => r.name === policy.repository);

return (
<EuiSelect
options={repositories.map(({ name }: Repository) => ({
value: name,
text: name,
}))}
value={policy.repository || repositories[0].name}
onBlur={() => setTouched({ ...touched, repository: true })}
onChange={(e) => {
updatePolicy(
{
repository: e.target.value,
},
{
managedRepository,
isEditing,
policyName: policy.name,
}
);
}}
fullWidth
data-test-subj="repositorySelect"
/>
<>
{!policyRepositoryExists && (
<>
<EuiCallOut
Copy link
Contributor

Choose a reason for hiding this comment

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

A screenshot would be 💣 !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

data-test-subj="repositoryNotFoundWarning"
title={
<FormattedMessage
id="xpack.snapshotRestore.policyForm.stepLogistics.selectRepository.policyRepositoryNotFoundTitle"
defaultMessage="Policy repository not found"
/>
}
color="warning"
iconType="alert"
>
<FormattedMessage
id="xpack.snapshotRestore.policyForm.stepLogistics.selectRepository.policyRepositoryNotFoundDescription"
defaultMessage="Select another repository."
/>
</EuiCallOut>
<EuiSpacer size="m" />
</>
)}
<EuiSelect
options={repositories.map(({ name }: Repository) => ({
value: name,
text: name,
}))}
hasNoInitialSelection={!policyRepositoryExists}
value={!policyRepositoryExists ? '' : policy.repository}
onBlur={() => setTouched({ ...touched, repository: true })}
onChange={(e) => {
updatePolicy(
{
repository: e.target.value,
},
{
managedRepository,
isEditing,
policyName: policy.name,
}
);
}}
fullWidth
data-test-subj="repositorySelect"
/>
</>
);
};

Expand Down