Skip to content

Commit

Permalink
Add list users
Browse files Browse the repository at this point in the history
  • Loading branch information
anagrius committed Nov 22, 2018
1 parent 0e054b5 commit afd7ed2
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 68 deletions.
88 changes: 45 additions & 43 deletions command/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,26 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/hpcloud/tail"
uuid "github.com/satori/go.uuid"
cli "gopkg.in/urfave/cli.v2"
)

var batchLimit = 500
var events = make(chan event, 500)
var events = make(chan string, batchLimit)

type eventList struct {
Tags map[string]string `json:"tags"`
Events []event `json:"events"`
Type string `json:"type"`
Fields map[string]string `json:"fields"`
Messages []string `json:"messages"`
}

type event struct {
Timestamp string `json:"timestamp"`
Attributes map[string]string `json:"attributes"`
RawString string `json:"rawstring"`
RawString string `json:"rawstring"`
}

func tailFile(server server, name string, sessionID string, filepath string) {
func tailFile(server server, filepath string) {

// Join Tail

Expand All @@ -43,7 +41,7 @@ func tailFile(server server, name string, sessionID string, filepath string) {
}

for line := range t.Lines {
sendLine(server, name, sessionID, line.Text)
sendLine(server, line.Text)
}

tailError := t.Wait()
Expand All @@ -53,11 +51,13 @@ func tailFile(server server, name string, sessionID string, filepath string) {
}
}

func streamStdin(server server, name string, sessionID string) {
func streamStdin(server server) {
log.Println("Humio Attached to StdIn")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
sendLine(server, name, sessionID, scanner.Text())
text := scanner.Text()
sendLine(server, text)
fmt.Println(text)
}

if scanner.Err() != nil {
Expand Down Expand Up @@ -105,6 +105,13 @@ func Ingest(c *cli.Context) error {
name = c.String("name")
}

var parserName string
if c.String("parser") != "" {
parserName = c.String("parser")
} else {
parserName = "default"
}

u, _ := uuid.NewV4()

sessionID := u.String()
Expand All @@ -122,30 +129,38 @@ func Ingest(c *cli.Context) error {
// }
// open.Run(server.ServerURL + server.RepoID + "/search?live=true&start=1d&query=" + key)

startSending(config)
fields := map[string]string{
"@session": sessionID,
}

if name != "" {
fields["@name"] = name
}

startSending(config, fields, parserName)

if filepath != "" {
tailFile(config, name, sessionID, filepath)
tailFile(config, filepath)
} else {
streamStdin(config, name, sessionID)
streamStdin(config)
}
return nil
}

func startSending(server server) {
func startSending(server server, fields map[string]string, parserName string) {
go func() {
var batch []event
var batch []string
for {
select {
case v := <-events:
batch = append(batch, v)
if len(batch) >= batchLimit {
sendBatch(server, batch)
sendBatch(server, batch, fields, parserName)
batch = batch[:0]
}
default:
if len(batch) > 0 {
sendBatch(server, batch)
sendBatch(server, batch, fields, parserName)
batch = batch[:0]
}
// Avoid busy waiting
Expand All @@ -155,50 +170,37 @@ func startSending(server server) {
}()
}

func sendLine(server server, name string, sessionID string, line string) {
theEvent := event{
Timestamp: time.Now().UTC().Format(time.RFC3339),
Attributes: map[string]string{
"@session": sessionID,
"@name": name,
},
RawString: line,
}

events <- theEvent
func sendLine(server server, line string) {
events <- line
}

func sendBatch(server server, events []event) {
func sendBatch(server server, messages []string, fields map[string]string, parserName string) {

lineJSON, marshalErr := json.Marshal([1]eventList{
eventList{
Tags: map[string]string{},
Events: events,
Type: parserName,
Fields: fields,
Messages: messages,
}})

if marshalErr != nil {
log.Fatal(marshalErr)
}

ingestURL := server.URL + "/api/v1/dataspaces/" + server.Repo + "/ingest"
ingestURL := server.URL + "api/v1/repositories/" + server.Repo + "/ingest-messages"
lineReq, reqErr := http.NewRequest("POST", ingestURL, bytes.NewBuffer(lineJSON))
lineReq.Header.Set("Authorization", "Bearer "+server.Token)
lineReq.Header.Set("Content-Type", "application/json")

if reqErr != nil {
panic(reqErr)
}
check(reqErr)

resp, clientErr := client.Do(lineReq)
if clientErr != nil {
panic(clientErr)
}
check(clientErr)

if resp.StatusCode > 400 {
responseData, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
panic(readErr)
}
check(readErr)
log.Fatal(string(responseData))
}

resp.Body.Close()
}
14 changes: 14 additions & 0 deletions command/users.go
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
package command

import "strings"

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

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

import (
"context"

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

func UsersList(c *cli.Context) error {
config, _ := getServerConfig(c)

ensureToken(config)
ensureURL(config)

var q struct {
Accounts []simpleAccount `graphql:"accounts"`
}

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

graphqlErr := newGraphQLClient(config).Query(context.Background(), &q, variables)
check(graphqlErr)

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

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

return nil
}
31 changes: 12 additions & 19 deletions command/users_add_root.go → command/users_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@ package command

import (
"context"
"log"

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

func UsersAddRoot(c *cli.Context) error {
return updateUser(c, true)
}

func UsersRemoveRoot(c *cli.Context) error {
return updateUser(c, false)
}

func updateUser(c *cli.Context, isRoot bool) error {
func UpdateUser(c *cli.Context) error {
config, _ := getServerConfig(c)

ensureToken(config)
ensureURL(config)

Expand All @@ -34,18 +24,21 @@ func updateUser(c *cli.Context, isRoot bool) error {

variables := map[string]interface{}{
"username": graphql.String(username),
"isRoot": graphql.Boolean(isRoot),
"isRoot": optBoolFlag(c, "root"),
}

graphqlErr := client.Mutate(context.Background(), &m, variables)
check(graphqlErr)

if graphqlErr != nil {
log.Fatal(graphqlErr)
} else if isRoot {
log.Println(username + " now has root access to " + config.URL)
} else {
log.Println(username + " no longer has root access to " + config.URL)
}
UsersShow(c)

return nil
}

func optBoolFlag(c *cli.Context, flag string) *graphql.Boolean {
var isRootOpt *graphql.Boolean
if c.IsSet(flag) {
isRootOpt = graphql.NewBoolean(graphql.Boolean(c.Bool(flag)))
}
return isRootOpt
}
23 changes: 17 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,23 @@ func main() {
Action: command.UsersShow,
},
{
Name: "add-root",
Action: command.UsersAddRoot,
Name: "update",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "root",
Usage: "Grant root permission to the user.",
},
},
Action: command.UpdateUser,
},
{
Name: "remove-root",
Action: command.UsersRemoveRoot,
Name: "list",
Action: command.UsersList,
},
},
},
{
Name: "token",
Name: "tokens",
Subcommands: []*cli.Command{
{
Name: "add",
Expand All @@ -96,7 +102,7 @@ func main() {
},
},
{
Name: "parser",
Name: "parsers",
Subcommands: []*cli.Command{
{
Name: "get",
Expand Down Expand Up @@ -137,6 +143,11 @@ func main() {
Aliases: []string{"n"},
Usage: "A name to make it easier to find results for this stream in your repository. e.g. @name=MyName\nIf `NAME` is not specified and you are tailing a file, the filename is used.",
},
&cli.StringFlag{
Name: "parser",
Aliases: []string{"p"},
Usage: "The name of the parser to use for ingest. This will have no effect if you have assigned parser to the ingest token used.",
},
},
},
},
Expand Down

0 comments on commit afd7ed2

Please sign in to comment.