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

feat(xo-server/rest-api): ability to update group and its users #8278

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

> Users must be able to say: “Nice enhancement, I'm eager to test it”

- [REST API] Ability to update a group's name and its users (PR [#8278](https://github.com/vatesfr/xen-orchestra/pull/8278))
Copy link
Member

Choose a reason for hiding this comment

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

I don't know if this message is clear enough. It can be confusing, for example: "you can update the properties of users in the group".
this PR expose the possibility to ADD a user in a group

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about "Allow updating a group's name and manage its users list (add/remove users)" ?


### Bug fixes

> Users must be able to say: “I had this issue, happy to know it's fixed”
Expand All @@ -31,4 +33,6 @@

<!--packages-start-->

- xo-server minor

<!--packages-end-->
32 changes: 32 additions & 0 deletions packages/xo-server/src/xo-mixins/rest-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,29 @@ export default class RestApi {
},
},
}
collections.groups.actions = {
update: withParams(
async ({ id, name, userIds }, req) => {
const group = await app.getGroup(id)
if (group.provider !== undefined) {
throw new Error('Cannot edit synchronized group')
}

if (name !== undefined) {
await app.updateGroup(id, { name })
}
Comment on lines +946 to +953
Copy link
Member

Choose a reason for hiding this comment

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

Avoid adding business logic into the REST API code.
consider the REST API as much as possible as an interface that exposes xo-servers methods via HTTP requests.
IMHO, you can move this logic to xo-mixins/subjects.mjs#updateGroup.

@fbeauchamp What do you think?


if (Array.isArray(userIds)) {
await app.setGroupUsers(id, userIds)
}
},
{
id: { type: 'string' },
name: { type: 'string', optional: true },
userIds: { type: 'array', items: { type: 'string' }, optional: true },
}
),
}
Comment on lines +943 to +965
Copy link
Member

Choose a reason for hiding this comment

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

Don't create action like that. Same as: #8276 (comment)

Suggested change
collections.groups.actions = {
update: withParams(
async ({ id, name, userIds }, req) => {
const group = await app.getGroup(id)
if (group.provider !== undefined) {
throw new Error('Cannot edit synchronized group')
}
if (name !== undefined) {
await app.updateGroup(id, { name })
}
if (Array.isArray(userIds)) {
await app.setGroupUsers(id, userIds)
}
},
{
id: { type: 'string' },
name: { type: 'string', optional: true },
userIds: { type: 'array', items: { type: 'string' }, optional: true },
}
),
}

collections.restore = {}
collections.tasks = {
async getObject(id, req) {
Expand Down Expand Up @@ -1514,6 +1537,15 @@ export default class RestApi {
res.sendStatus(200)
})
)

api.patch(
'/groups/update/:id',
json(),
wrap(async (req, res) => {
await collections.groups.actions.update(req.body, req)
res.sendStatus(200)
Copy link
Contributor

Choose a reason for hiding this comment

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

If you try to update a group with an ID that doesn't exist, does it return an error code?

})
)
Comment on lines +1541 to +1548
Copy link
Member

Choose a reason for hiding this comment

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

IMHO this can be split into 2 routes.
Update group name:

PATCH /:collection(groups)/:id
request body: {name?: string}

Add user to group:

POST /:collection(groups)/:id/users/:id

First route we update a property of the group collection
Second we add a user to the users subresource of the group collection

}

registerRestApi(spec, base = '/') {
Expand Down
Loading