From 49eb08113052505a806631f82e896982a1c6796d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Mon, 9 Sep 2019 00:16:14 -0400 Subject: [PATCH] Add function to get user by its name --- common/utils.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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 +}