Skip to content

Commit

Permalink
[Fleet] Fix space_ids validation and spaces UI (#191083)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Aug 23, 2024
1 parent bd9dde8 commit 8f7ea3c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -312,6 +311,9 @@ export const AgentPolicyAdvancedOptionsContent: React.FunctionComponent<Props> =
: [spaceId || 'default']
}
onChange={(newValue) => {
if (newValue.length === 0) {
return;
}
updateAgentPolicy({
space_ids: newValue,
});
Expand Down
6 changes: 1 addition & 5 deletions x-pack/plugins/fleet/server/types/models/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
})
Expand Down

0 comments on commit 8f7ea3c

Please sign in to comment.