diff --git a/common/utils.go b/common/utils.go index 1b34702..c8bcefb 100644 --- a/common/utils.go +++ b/common/utils.go @@ -18,6 +18,7 @@ const ( ErrEndpointGroupNotFound = Error("Endpoint group not found") ErrSeveralEndpointsAvailable = Error("Several endpoints available") ErrNoEndpointsAvailable = Error("No endpoints available") + ErrUserNotFound = Error("User not found") ) const ( @@ -215,3 +216,28 @@ func repr(t reflect.Type, margin, beforeMargin string) (r string) { } return } + +// GetUserByName returns an user by its name from the list of all users +func GetUserByName(name string) (user portainer.User, err error) { + portainerClient, err := GetClient() + if err != nil { + return + } + + // Get users list + users, err := portainerClient.UserList() + + // Find user + for _, listUser := range users { + if listUser.Username == name { + // User found + user = listUser + return + } + } + + // User not found + err = ErrUserNotFound + + return +}