Skip to content

Commit

Permalink
Convert users
Browse files Browse the repository at this point in the history
  • Loading branch information
anagrius committed Nov 23, 2018
1 parent e1d52b3 commit f48699a
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 74 deletions.
8 changes: 8 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,11 @@ func (c *Client) httpDELETE(path string) (*http.Response, error) {
}
return client.Do(req)
}

func optBoolArg(v *bool) *graphql.Boolean {
var argPtr *graphql.Boolean
if v != nil {
argPtr = graphql.NewBoolean(graphql.Boolean(*v))
}
return argPtr
}
61 changes: 61 additions & 0 deletions api/users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package api

import "github.com/shurcooL/graphql"

type Users struct {
client *Client
}

type User struct {
Username string
FullName string
IsRoot bool
CreatedAt string
}

type UserChangeSet struct {
IsRoot *bool
}

func (c *Client) Users() *Users { return &Users{client: c} }

func (c *Users) List() ([]User, error) {
var q struct {
Users []User `graphql:"accounts"`
}

variables := map[string]interface{}{}

graphqlErr := c.client.Query(&q, variables)

return q.Users, graphqlErr
}

func (c *Users) Get(username string) (User, error) {
var q struct {
User User `graphql:"account(username: $username)"`
}

variables := map[string]interface{}{
"username": graphql.String(username),
}

graphqlErr := c.client.Query(&q, variables)

return q.User, graphqlErr
}

func (c *Users) Update(username string, changeset UserChangeSet) (User, error) {
var mutation struct {
Result struct{ User User } `graphql:"updateUser(input: {username: $username, isRoot: $isRoot})"`
}

variables := map[string]interface{}{
"username": graphql.String(username),
"isRoot": optBoolArg(changeset.IsRoot),
}

graphqlErr := c.client.Mutate(&mutation, variables)

return mutation.Result.User, graphqlErr
}
15 changes: 15 additions & 0 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ func Commands(metaPtr *Meta, agentUi cli.Ui) map[string]cli.CommandFactory {
Meta: meta,
}, nil
},
"users list": func() (cli.Command, error) {
return &UsersListCommand{
Meta: meta,
}, nil
},
"users show": func() (cli.Command, error) {
return &UsersShowCommand{
Meta: meta,
}, nil
},
"users update": func() (cli.Command, error) {
return &UsersUpdateCommand{
Meta: meta,
}, nil
},
}

return all
Expand Down
2 changes: 1 addition & 1 deletion command/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ func yesNo(isTrue bool) string {
if isTrue {
return "yes"
}
return ""
return "no"
}
1 change: 0 additions & 1 deletion command/token_assign_parser.go

This file was deleted.

13 changes: 5 additions & 8 deletions command/users.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package command

import "strings"
import (
"strings"

type simpleAccount struct {
Username string
FullName string
IsRoot bool
CreatedAt string
}
"github.com/humio/cli/api"
)

func formatSimpleAccount(account simpleAccount) string {
func formatSimpleAccount(account api.User) string {
columns := []string{account.Username, account.FullName, yesNo(account.IsRoot), account.CreatedAt}
return strings.Join(columns, " | ")
}
73 changes: 57 additions & 16 deletions command/users_list.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,76 @@
package command

import (
"context"

cli "gopkg.in/urfave/cli.v2"
"fmt"
"strings"
)

func UsersList(c *cli.Context) error {
config, _ := getServerConfig(c)
type UsersListCommand struct {
Meta
}

func (f *UsersListCommand) Help() string {
helpText := `
Usage: humio users list
Lists all users. This command requires root permissions on your access token.
To see members in a repository or view use:
ensureToken(config)
ensureURL(config)
$ humio members list <repo>
var q struct {
Accounts []simpleAccount `graphql:"accounts"`
General Options:
` + generalOptionsUsage() + `
`
return strings.TrimSpace(helpText)
}

func (f *UsersListCommand) Synopsis() string {
return "List all user in the cluster."
}

func (f *UsersListCommand) Name() string { return "users list" }

func (f *UsersListCommand) Run(args []string) int {
flags := f.Meta.FlagSet(f.Name(), FlagSetClient)
flags.Usage = func() { f.Ui.Output(f.Help()) }

if err := flags.Parse(args); err != nil {
return 1
}

// Check that we got one argument
args = flags.Args()
if l := len(args); l != 0 {
f.Ui.Error("This command takes no arguments")
f.Ui.Error(commandErrorText(f))
return 1
}

// Get the HTTP client
client, err := f.Meta.Client()
if err != nil {
f.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}

variables := map[string]interface{}{}
users, err := client.Users().List()

graphqlErr := newGraphQLClient(config).Query(context.Background(), &q, variables)
check(graphqlErr)
if err != nil {
f.Ui.Error(fmt.Sprintf("Error fetching token list: %s", err))
return 1
}

rows := make([]string, len(q.Accounts))
for i, account := range q.Accounts {
rows[i] = formatSimpleAccount(account)
rows := make([]string, len(users))
for i, user := range users {
rows[i] = formatSimpleAccount(user)
}

printTable(append([]string{
"Username | Name | Root | Created"},
rows...,
))

return nil
return 0
}
82 changes: 60 additions & 22 deletions command/users_show.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,81 @@
package command

import (
"context"
"fmt"
"strings"

"github.com/shurcooL/graphql"
cli "gopkg.in/urfave/cli.v2"
"github.com/humio/cli/api"
)

func UsersShow(c *cli.Context) error {
config, _ := getServerConfig(c)
type UsersShowCommand struct {
Meta
}

func (f *UsersShowCommand) Help() string {
helpText := `
Usage: humio users show <username>
Shows details about a users. This command requires root access.
To see members in a repository or view use:
$ humio members show <repo> <username>
General Options:
` + generalOptionsUsage() + `
`
return strings.TrimSpace(helpText)
}

func (f *UsersShowCommand) Synopsis() string {
return "Shows details about a user."
}

ensureToken(config)
ensureURL(config)
func (f *UsersShowCommand) Name() string { return "users show" }

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

var q struct {
Account struct {
Username string
FullName string
IsRoot bool
CreatedAt string
} `graphql:"account(username: $username)"`
if err := flags.Parse(args); err != nil {
return 1
}

variables := map[string]interface{}{
"username": graphql.String(username),
// Check that we got one argument
args = flags.Args()
if l := len(args); l != 1 {
f.Ui.Error("This command takes one argument: <username>")
f.Ui.Error(commandErrorText(f))
return 1
}

graphqlErr := newGraphQLClient(config).Query(context.Background(), &q, variables)
check(graphqlErr)
username := args[0]

userData := []string{q.Account.Username, q.Account.FullName, q.Account.CreatedAt, yesNo(q.Account.IsRoot)}
// Get the HTTP client
client, err := f.Meta.Client()
if err != nil {
f.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}

user, err := client.Users().Get(username)

if err != nil {
f.Ui.Error(fmt.Sprintf("Error fetching token list: %s", err))
return 1
}

printUserTable(user)

return 0
}

func printUserTable(user api.User) {
userData := []string{user.Username, user.FullName, user.CreatedAt, yesNo(user.IsRoot)}

printTable([]string{
"Username | Name | Created At | Is Root",
strings.Join(userData, "|"),
})

return nil
}
Loading

0 comments on commit f48699a

Please sign in to comment.