Skip to content

Commit f48699a

Browse files
committed
Convert users
1 parent e1d52b3 commit f48699a

File tree

10 files changed

+292
-74
lines changed

10 files changed

+292
-74
lines changed

api/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,11 @@ func (c *Client) httpDELETE(path string) (*http.Response, error) {
109109
}
110110
return client.Do(req)
111111
}
112+
113+
func optBoolArg(v *bool) *graphql.Boolean {
114+
var argPtr *graphql.Boolean
115+
if v != nil {
116+
argPtr = graphql.NewBoolean(graphql.Boolean(*v))
117+
}
118+
return argPtr
119+
}

api/users.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package api
2+
3+
import "github.com/shurcooL/graphql"
4+
5+
type Users struct {
6+
client *Client
7+
}
8+
9+
type User struct {
10+
Username string
11+
FullName string
12+
IsRoot bool
13+
CreatedAt string
14+
}
15+
16+
type UserChangeSet struct {
17+
IsRoot *bool
18+
}
19+
20+
func (c *Client) Users() *Users { return &Users{client: c} }
21+
22+
func (c *Users) List() ([]User, error) {
23+
var q struct {
24+
Users []User `graphql:"accounts"`
25+
}
26+
27+
variables := map[string]interface{}{}
28+
29+
graphqlErr := c.client.Query(&q, variables)
30+
31+
return q.Users, graphqlErr
32+
}
33+
34+
func (c *Users) Get(username string) (User, error) {
35+
var q struct {
36+
User User `graphql:"account(username: $username)"`
37+
}
38+
39+
variables := map[string]interface{}{
40+
"username": graphql.String(username),
41+
}
42+
43+
graphqlErr := c.client.Query(&q, variables)
44+
45+
return q.User, graphqlErr
46+
}
47+
48+
func (c *Users) Update(username string, changeset UserChangeSet) (User, error) {
49+
var mutation struct {
50+
Result struct{ User User } `graphql:"updateUser(input: {username: $username, isRoot: $isRoot})"`
51+
}
52+
53+
variables := map[string]interface{}{
54+
"username": graphql.String(username),
55+
"isRoot": optBoolArg(changeset.IsRoot),
56+
}
57+
58+
graphqlErr := c.client.Mutate(&mutation, variables)
59+
60+
return mutation.Result.User, graphqlErr
61+
}

command/commands.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ func Commands(metaPtr *Meta, agentUi cli.Ui) map[string]cli.CommandFactory {
6868
Meta: meta,
6969
}, nil
7070
},
71+
"users list": func() (cli.Command, error) {
72+
return &UsersListCommand{
73+
Meta: meta,
74+
}, nil
75+
},
76+
"users show": func() (cli.Command, error) {
77+
return &UsersShowCommand{
78+
Meta: meta,
79+
}, nil
80+
},
81+
"users update": func() (cli.Command, error) {
82+
return &UsersUpdateCommand{
83+
Meta: meta,
84+
}, nil
85+
},
7186
}
7287

7388
return all

command/output.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ func yesNo(isTrue bool) string {
1919
if isTrue {
2020
return "yes"
2121
}
22-
return ""
22+
return "no"
2323
}

command/token_assign_parser.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

command/users.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package command
22

3-
import "strings"
3+
import (
4+
"strings"
45

5-
type simpleAccount struct {
6-
Username string
7-
FullName string
8-
IsRoot bool
9-
CreatedAt string
10-
}
6+
"github.com/humio/cli/api"
7+
)
118

12-
func formatSimpleAccount(account simpleAccount) string {
9+
func formatSimpleAccount(account api.User) string {
1310
columns := []string{account.Username, account.FullName, yesNo(account.IsRoot), account.CreatedAt}
1411
return strings.Join(columns, " | ")
1512
}

command/users_list.go

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,76 @@
11
package command
22

33
import (
4-
"context"
5-
6-
cli "gopkg.in/urfave/cli.v2"
4+
"fmt"
5+
"strings"
76
)
87

9-
func UsersList(c *cli.Context) error {
10-
config, _ := getServerConfig(c)
8+
type UsersListCommand struct {
9+
Meta
10+
}
11+
12+
func (f *UsersListCommand) Help() string {
13+
helpText := `
14+
Usage: humio users list
15+
16+
Lists all users. This command requires root permissions on your access token.
17+
18+
To see members in a repository or view use:
1119
12-
ensureToken(config)
13-
ensureURL(config)
20+
$ humio members list <repo>
1421
15-
var q struct {
16-
Accounts []simpleAccount `graphql:"accounts"`
22+
General Options:
23+
24+
` + generalOptionsUsage() + `
25+
`
26+
return strings.TrimSpace(helpText)
27+
}
28+
29+
func (f *UsersListCommand) Synopsis() string {
30+
return "List all user in the cluster."
31+
}
32+
33+
func (f *UsersListCommand) Name() string { return "users list" }
34+
35+
func (f *UsersListCommand) Run(args []string) int {
36+
flags := f.Meta.FlagSet(f.Name(), FlagSetClient)
37+
flags.Usage = func() { f.Ui.Output(f.Help()) }
38+
39+
if err := flags.Parse(args); err != nil {
40+
return 1
41+
}
42+
43+
// Check that we got one argument
44+
args = flags.Args()
45+
if l := len(args); l != 0 {
46+
f.Ui.Error("This command takes no arguments")
47+
f.Ui.Error(commandErrorText(f))
48+
return 1
49+
}
50+
51+
// Get the HTTP client
52+
client, err := f.Meta.Client()
53+
if err != nil {
54+
f.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
55+
return 1
1756
}
1857

19-
variables := map[string]interface{}{}
58+
users, err := client.Users().List()
2059

21-
graphqlErr := newGraphQLClient(config).Query(context.Background(), &q, variables)
22-
check(graphqlErr)
60+
if err != nil {
61+
f.Ui.Error(fmt.Sprintf("Error fetching token list: %s", err))
62+
return 1
63+
}
2364

24-
rows := make([]string, len(q.Accounts))
25-
for i, account := range q.Accounts {
26-
rows[i] = formatSimpleAccount(account)
65+
rows := make([]string, len(users))
66+
for i, user := range users {
67+
rows[i] = formatSimpleAccount(user)
2768
}
2869

2970
printTable(append([]string{
3071
"Username | Name | Root | Created"},
3172
rows...,
3273
))
3374

34-
return nil
75+
return 0
3576
}

command/users_show.go

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,81 @@
11
package command
22

33
import (
4-
"context"
4+
"fmt"
55
"strings"
66

7-
"github.com/shurcooL/graphql"
8-
cli "gopkg.in/urfave/cli.v2"
7+
"github.com/humio/cli/api"
98
)
109

11-
func UsersShow(c *cli.Context) error {
12-
config, _ := getServerConfig(c)
10+
type UsersShowCommand struct {
11+
Meta
12+
}
13+
14+
func (f *UsersShowCommand) Help() string {
15+
helpText := `
16+
Usage: humio users show <username>
17+
18+
Shows details about a users. This command requires root access.
19+
20+
To see members in a repository or view use:
21+
22+
$ humio members show <repo> <username>
23+
24+
General Options:
25+
26+
` + generalOptionsUsage() + `
27+
`
28+
return strings.TrimSpace(helpText)
29+
}
30+
31+
func (f *UsersShowCommand) Synopsis() string {
32+
return "Shows details about a user."
33+
}
1334

14-
ensureToken(config)
15-
ensureURL(config)
35+
func (f *UsersShowCommand) Name() string { return "users show" }
1636

17-
username := c.Args().First()
37+
func (f *UsersShowCommand) Run(args []string) int {
38+
flags := f.Meta.FlagSet(f.Name(), FlagSetClient)
39+
flags.Usage = func() { f.Ui.Output(f.Help()) }
1840

19-
var q struct {
20-
Account struct {
21-
Username string
22-
FullName string
23-
IsRoot bool
24-
CreatedAt string
25-
} `graphql:"account(username: $username)"`
41+
if err := flags.Parse(args); err != nil {
42+
return 1
2643
}
2744

28-
variables := map[string]interface{}{
29-
"username": graphql.String(username),
45+
// Check that we got one argument
46+
args = flags.Args()
47+
if l := len(args); l != 1 {
48+
f.Ui.Error("This command takes one argument: <username>")
49+
f.Ui.Error(commandErrorText(f))
50+
return 1
3051
}
3152

32-
graphqlErr := newGraphQLClient(config).Query(context.Background(), &q, variables)
33-
check(graphqlErr)
53+
username := args[0]
3454

35-
userData := []string{q.Account.Username, q.Account.FullName, q.Account.CreatedAt, yesNo(q.Account.IsRoot)}
55+
// Get the HTTP client
56+
client, err := f.Meta.Client()
57+
if err != nil {
58+
f.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
59+
return 1
60+
}
61+
62+
user, err := client.Users().Get(username)
63+
64+
if err != nil {
65+
f.Ui.Error(fmt.Sprintf("Error fetching token list: %s", err))
66+
return 1
67+
}
68+
69+
printUserTable(user)
70+
71+
return 0
72+
}
73+
74+
func printUserTable(user api.User) {
75+
userData := []string{user.Username, user.FullName, user.CreatedAt, yesNo(user.IsRoot)}
3676

3777
printTable([]string{
3878
"Username | Name | Created At | Is Root",
3979
strings.Join(userData, "|"),
4080
})
41-
42-
return nil
4381
}

0 commit comments

Comments
 (0)