User
c.User calls the API's User group. The users of the host.
Methods
| Method | Returns | Permission | Description |
|---|---|---|---|
List(ctx, hostname) | *Response[Collection[UserAccount]] | user:read | List the users of the host. |
Create(ctx, hostname, input) | *Response[Collection[UserResult]] | user:write | Add a user. |
Get(ctx, hostname, name) | *Response[Collection[UserAccount]] | user:read | Get one user. |
Update(ctx, hostname, input) | *Response[Collection[UserResult]] | user:write | Change a user. |
Delete(ctx, hostname, name) | *Response[Collection[UserResult]] | user:write | Remove a user. |
List
func (s *UserService) List(
ctx context.Context,
hostname string,
) (*Response[Collection[UserAccount]], error)
List the users of the host.
Lists each user with its IDs, home, shell, groups and whether it is locked, through gohai: from /etc/passwd, /etc/group and /etc/shadow on Linux, and from the directory service with dscl on macOS. The groups are those that list the user as a member, besides its primary group.
It sends GET /api/node/{hostname}/user, and needs the permission user:read.
The examples of the method's definition:
A host with users, go run user.go list:
resp, err := c.User.List(ctx, target)
Create
func (s *UserService) Create(
ctx context.Context,
hostname string,
input UserCreateInput,
) (*Response[Collection[UserResult]], error)
Add a user.
Adds the user with useradd, with its home directory, and sets its password with chpasswd when one is given. The controller hashes the password, so the job and NATS have only the hash. A user that the host has already does not change and reports changed: false. It runs on the Debian family only, and elsewhere reports skipped.
It sends POST /api/node/{hostname}/user, and needs the permission
user:write.
The examples of the method's definition:
Add a user, go run user.go create:
resp, err := c.User.Create(ctx, target, client.UserCreateInput{Name: "osapitest"})
Add a user with a shell, groups and a password, go run user.go create-2:
resp, err := c.User.Create(ctx, target, client.UserCreateInput{
Groups: []string{"root"},
Name: "osapitest",
Password: "-",
Shell: "/bin/sh",
})
Add a system user with its IDs and its home, go run user.go create-3:
resp, err := c.User.Create(ctx, target, client.UserCreateInput{
GID: 100,
Home: "/home/osapitest",
Name: "osapitest",
System: true,
UID: 1999,
})
Get
func (s *UserService) Get(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[UserAccount]], error)
Get one user.
Reads one user from the same list. A user that the host does not have is not found.
It sends GET /api/node/{hostname}/user/{name}, and needs the permission
user:read.
The examples of the method's definition:
A user, go run user.go get:
resp, err := c.User.Get(ctx, target, "alice")
Update
func (s *UserService) Update(
ctx context.Context,
hostname string,
input UserUpdateInput,
) (*Response[Collection[UserResult]], error)
Change a user.
Changes what the input gives and the user does not have already, with usermod: its shell, its home, which usermod moves the files to, its groups, which replace the groups it has, and whether it is locked. The same values report changed: false. A user that the host does not have is not found. It runs on the Debian family only, and elsewhere reports skipped.
It sends PUT /api/node/{hostname}/user/{name}, and needs the permission
user:write.
The examples of the method's definition:
Set the shell, go run user.go update:
resp, err := c.User.Update(ctx, target, client.UserUpdateInput{
Name: "osapitest",
Shell: "/bin/bash",
})
Move the home, set the groups and lock it, go run user.go update-2:
resp, err := c.User.Update(ctx, target, client.UserUpdateInput{
Groups: []string{"root"},
Home: "/home/osapitest2",
Lock: types.Lock("locked"),
Name: "osapitest",
})
Unlock it, go run user.go update-3:
resp, err := c.User.Update(ctx, target, client.UserUpdateInput{
Lock: types.Lock("unlocked"),
Name: "osapitest",
})
Delete
func (s *UserService) Delete(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[UserResult]], error)
Remove a user.
Removes the user and its home directory with userdel -r. A user that the host does not have does not change and reports changed: false. It runs on the Debian family only, and elsewhere reports skipped.
It sends DELETE /api/node/{hostname}/user/{name}, and needs the permission
user:write.
The examples of the method's definition:
Remove a user, go run user.go delete:
resp, err := c.User.Delete(ctx, target, "osapitest")
Types
The fields of each type are the properties of its schema in the API's spec.
Lock
Lock is whether a user account refuses logins with its password.
A string, one of locked, unlocked.
RowStatus
What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.
A string, one of ok, failed, skipped, timeout.
UserAccount
| Field | Type | Description |
|---|---|---|
Error | string | Error message if the agent failed. |
GID | int | Its primary group ID. |
Groups | []string | The groups it is a member of, besides its primary group. |
Home | string | Its home directory. |
Hostname | string | Hostname of the agent that produced this result. |
Locked | bool | Whether a login with its password is refused. |
Name | string | The user. |
Shell | string | Its login shell. |
Status | RowStatus | What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran. |
UID | int | Its numeric user ID. |
UserCreateInput
| Field | Type | Description |
|---|---|---|
GID | int | Its primary group ID; a group of its own when it is 0. |
Groups | []string | The groups it is a member of, besides its primary group. |
Home | string | Its home directory; /home/ |
Name | string | The user to add, such as alice. |
Password | string | Its password, which osapi hashes on the controller; none when empty. A secret: a reference, secret:// |
Shell | string | Its login shell, such as /bin/bash; the system's default when empty. |
System | bool | Add it as a system user, with an ID from the system range. |
UID | int | Its numeric user ID; the system assigns one when it is 0. |
UserResult
| Field | Type | Description |
|---|---|---|
Changed | bool | Whether the host changed. |
Error | string | Error message if the agent failed. |
Hostname | string | Hostname of the agent that produced this result. |
Name | string | The user. |
Status | RowStatus | What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran. |
UserUpdateInput
| Field | Type | Description |
|---|---|---|
Name | string | The name of the path. The user to remove, with its home directory, such as alice. |
Groups | []string | The groups it is a member of, which replace the groups it has. |
Home | string | Its home directory, which usermod moves the files to. |
Lock | Lock | Lock is whether a user account refuses logins with its password. |
Shell | string | Its login shell, such as /bin/bash. |
Example
examples/sdk/client/user.go
runs each example of this page. With no argument, it lists them.