[Fleet] Fix space_ids validation and spaces UI (#191083)

This commit is contained in:
Nicolas Chaulet 2024-08-23 10:07:47 -04:00 committed by GitHub
parent bd9dde8ba7
commit 8f7ea3c34e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 11 deletions

View file

@ -77,9 +77,8 @@ export const AgentPolicyAdvancedOptionsContent: React.FunctionComponent<Props> =
validation,
disabled = false,
}) => {
const useSpaceAwareness = ExperimentalFeaturesService.get()?.useSpaceAwareness ?? false;
const { docLinks } = useStartServices();
const { spaceId } = useFleetStatus();
const { spaceId, isSpaceAwarenessEnabled } = useFleetStatus();
const { getAbsolutePath } = useLink();
const AgentTamperProtectionWrapper = useUIExtension(
@ -263,21 +262,21 @@ export const AgentPolicyAdvancedOptionsContent: React.FunctionComponent<Props> =
/>
</EuiFormRow>
</EuiDescribedFormGroup>
{useSpaceAwareness ? (
{isSpaceAwarenessEnabled ? (
<EuiDescribedFormGroup
fullWidth
title={
<h3>
<FormattedMessage
id="xpack.fleet.agentPolicyForm.spaceFieldLabel"
defaultMessage="Space"
defaultMessage="Spaces"
/>
</h3>
}
description={
<FormattedMessage
id="xpack.fleet.agentPolicyForm.spaceDescription"
defaultMessage="Select a space for this policy or create a new one. {link}"
defaultMessage="Select one or more spaces for this policy or create a new one. {link}"
values={{
link: (
<EuiLink
@ -312,6 +311,9 @@ export const AgentPolicyAdvancedOptionsContent: React.FunctionComponent<Props> =
: [spaceId || 'default']
}
onChange={(newValue) => {
if (newValue.length === 0) {
return;
}
updateAgentPolicy({
space_ids: newValue,
});

View file

@ -41,11 +41,7 @@ function isInteger(n: number) {
export const AgentPolicyBaseSchema = {
id: schema.maybe(schema.string()),
space_ids: schema.maybe(
schema.arrayOf(schema.string(), {
minSize: 1,
})
),
space_ids: schema.maybe(schema.arrayOf(schema.string())),
name: schema.string({ minLength: 1, validate: validateNonEmptyString }),
namespace: AgentPolicyNamespaceSchema,
description: schema.maybe(schema.string()),

View file

@ -906,6 +906,7 @@ export default function (providerContext: FtrProviderContext) {
describe('PUT /api/fleet/agent_policies/{agentPolicyId}', () => {
before(async () => {
await esArchiver.load('x-pack/test/functional/es_archives/fleet/empty_fleet_server');
await kibanaServer.savedObjects.cleanStandardList();
});
const createdPolicyIds: string[] = [];
after(async () => {
@ -967,6 +968,53 @@ export default function (providerContext: FtrProviderContext) {
});
});
it('should support empty space_ids', async () => {
const {
body: { item: originalPolicy },
} = await supertest
.post(`/api/fleet/agent_policies`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'Initial name 2',
space_ids: [],
description: 'Initial description',
namespace: 'default',
})
.expect(200);
agentPolicyId = originalPolicy.id;
const {
body: { item: updatedPolicy },
} = await supertest
.put(`/api/fleet/agent_policies/${agentPolicyId}`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'Updated name 2',
space_ids: [],
description: 'Updated description',
namespace: 'default',
is_protected: false,
})
.expect(200);
createdPolicyIds.push(updatedPolicy.id);
// eslint-disable-next-line @typescript-eslint/naming-convention
const { id, updated_at, version, ...newPolicy } = updatedPolicy;
expect(newPolicy).to.eql({
status: 'active',
name: 'Updated name 2',
description: 'Updated description',
namespace: 'default',
is_managed: false,
revision: 2,
schema_version: FLEET_AGENT_POLICIES_SCHEMA_VERSION,
updated_by: 'elastic',
inactivity_timeout: 1209600,
package_policies: [],
is_protected: false,
space_ids: [],
});
});
it('should return a 409 if policy already exists with name given', async () => {
const sharedBody = {
name: 'Initial name',
@ -1053,7 +1101,7 @@ export default function (providerContext: FtrProviderContext) {
.put(`/api/fleet/agent_policies/${originalPolicy.id}`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'Updated name',
name: `Updated name ${Date.now()}`,
description: 'Initial description',
namespace: 'default',
})