Skip to main content

User

c.User calls the API's User group. The users of the host.

Methods​

MethodReturnsPermissionDescription
List(ctx, hostname)*Response[Collection[UserAccount]]user:readList the users of the host.
Create(ctx, hostname, input)*Response[Collection[UserResult]]user:writeAdd a user.
Get(ctx, hostname, name)*Response[Collection[UserAccount]]user:readGet one user.
Update(ctx, hostname, input)*Response[Collection[UserResult]]user:writeChange a user.
Delete(ctx, hostname, name)*Response[Collection[UserResult]]user:writeRemove 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​

FieldTypeDescription
ErrorstringError message if the agent failed.
GIDintIts primary group ID.
Groups[]stringThe groups it is a member of, besides its primary group.
HomestringIts home directory.
HostnamestringHostname of the agent that produced this result.
LockedboolWhether a login with its password is refused.
NamestringThe user.
ShellstringIts login shell.
StatusRowStatusWhat 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.
UIDintIts numeric user ID.

UserCreateInput​

FieldTypeDescription
GIDintIts primary group ID; a group of its own when it is 0.
Groups[]stringThe groups it is a member of, besides its primary group.
HomestringIts home directory; /home/ when empty.
NamestringThe user to add, such as alice.
PasswordstringIts password, which osapi hashes on the controller; none when empty. A secret: a reference, secret:///, or the value.
ShellstringIts login shell, such as /bin/bash; the system's default when empty.
SystemboolAdd it as a system user, with an ID from the system range.
UIDintIts numeric user ID; the system assigns one when it is 0.

UserResult​

FieldTypeDescription
ChangedboolWhether the host changed.
ErrorstringError message if the agent failed.
HostnamestringHostname of the agent that produced this result.
NamestringThe user.
StatusRowStatusWhat 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​

FieldTypeDescription
NamestringThe name of the path. The user to remove, with its home directory, such as alice.
Groups[]stringThe groups it is a member of, which replace the groups it has.
HomestringIts home directory, which usermod moves the files to.
LockLockLock is whether a user account refuses logins with its password.
ShellstringIts login shell, such as /bin/bash.

Example​

examples/sdk/client/user.go runs each example of this page. With no argument, it lists them.