Skip to content

Commit

Permalink
Mike/view connection types (#141)
Browse files Browse the repository at this point in the history
* api accepts slice of connection strings

* also modify Views.Create()

* also update `views create`

* views: Expose ViewConnectionInput type directly in the Create/Update API and update humioctl to use it

---------

Co-authored-by: Brian Derr <brian.derr@getcruise.com>
  • Loading branch information
SaaldjorMike and bderrly committed Nov 1, 2023
1 parent 94748da commit 25a6743
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 36 deletions.
28 changes: 4 additions & 24 deletions api/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,18 @@ type ViewConnectionInput struct {
Filter graphql.String `json:"filter"`
}

func (c *Views) Create(name, description string, connections map[string]string) error {
func (c *Views) Create(name, description string, connections []ViewConnectionInput) error {
var mutation struct {
CreateView struct {
Name string
Description string
} `graphql:"createView(name: $name, description: $description, connections: $connections)"`
}

var viewConnections []ViewConnectionInput
for k, v := range connections {
viewConnections = append(
viewConnections,
ViewConnectionInput{
RepositoryName: graphql.String(k),
Filter: graphql.String(v),
})
}

variables := map[string]interface{}{
"name": graphql.String(name),
"description": graphql.String(description),
"connections": viewConnections,
"connections": connections,
}

return c.client.Mutate(&mutation, variables)
Expand All @@ -132,26 +122,16 @@ func (c *Views) Delete(name, reason string) error {
return c.client.Mutate(&mutation, variables)
}

func (c *Views) UpdateConnections(name string, connections map[string]string) error {
func (c *Views) UpdateConnections(name string, connections []ViewConnectionInput) error {
var mutation struct {
View struct {
Name string
} `graphql:"updateView(viewName: $viewName, connections: $connections)"`
}

var viewConnections []ViewConnectionInput
for k, v := range connections {
viewConnections = append(
viewConnections,
ViewConnectionInput{
RepositoryName: graphql.String(k),
Filter: graphql.String(v),
})
}

variables := map[string]interface{}{
"viewName": graphql.String(name),
"connections": viewConnections,
"connections": connections,
}

return c.client.Mutate(&mutation, variables)
Expand Down
31 changes: 26 additions & 5 deletions cmd/humioctl/views_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ package main

import (
"fmt"
graphql "github.com/cli/shurcooL-graphql"
"github.com/humio/cli/api"
"strings"

"github.com/spf13/cobra"
)

func newViewsCreateCmd() *cobra.Command {
connections := make(map[string]string)
connsFlag := []string{}
connections := []api.ViewConnectionInput{}
description := ""

cmd := &cobra.Command{
Expand All @@ -33,28 +37,45 @@ The "description" flag is a string, and the "connections" flag is a comma-separa
where the key is the repository name and the value being the filter applied to the queries in that repository.
If you want to query all events you can specify a wildcard as the filter.
Here's an example that updates a view named "important-view" to search all data in the two repositories,
Here's an example that creates a view named "important-view" to search all data in the two repositories,
namely "repo1" and "repo2":
$ humioctl views update important-view --connection "repo1=*,repo2=*" --description "very important view"
$ humioctl views create important-view --connection "repo1=*" --connection "repo2=*" --description "very important view"
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
viewName := args[0]
client := NewApiClient(cmd)

if len(connections) == 0 {
if len(connsFlag) == 0 {
exitOnError(cmd, fmt.Errorf("you must specify at least view connection"), "Error creating view")
}

for _, v := range connsFlag {
parts := strings.SplitN(v, "=", 2)
if len(parts) != 2 {
exitOnError(cmd, fmt.Errorf("all connections must follow the format: <repoName>=<filterString>"), "Error updating view connections")
}

repo := parts[0]
filter := parts[1]

connections = append(
connections,
api.ViewConnectionInput{
RepositoryName: graphql.String(repo),
Filter: graphql.String(filter),
})
}

err := client.Views().Create(viewName, description, connections)
exitOnError(cmd, err, "Error creating view")

fmt.Fprintf(cmd.OutOrStdout(), "Successfully created view: %q\n", viewName)
},
}

cmd.Flags().StringToStringVar(&connections, "connection", connections, "Sets a repository connection with the chosen filter.")
cmd.Flags().StringArrayVar(&connsFlag, "connection", connsFlag, "Sets a repository connection with the chosen filter in format: <repoName>=<filterString>")
cmd.Flags().StringVar(&description, "description", description, "Sets an optional description")

return cmd
Expand Down
34 changes: 27 additions & 7 deletions cmd/humioctl/views_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,58 @@ package main

import (
"fmt"
"strings"

"github.com/humio/cli/api"

graphql "github.com/cli/shurcooL-graphql"
"github.com/spf13/cobra"
)

func newViewsUpdateCmd() *cobra.Command {
connections := make(map[string]string)
connsFlag := []string{}
connections := []api.ViewConnectionInput{}
description := ""

cmd := cobra.Command{
Use: "update [flags] <view>",
Short: "Updates the settings of a view",
Long: `Updates the settings of a view with the provided arguments.
The "description" flag is a string, and the "connections" flag is a comma-separated list of key-value pairs
where the key is the repository name and the value being the filter applied to the queries in that repository.
The "description" flag can be specified multiple times for adding multiple connections to the view.
If you want to query all events you can specify a wildcard as the filter.
Here's an example that updates a view named "important-view" to search all data in the two repositories,
namely "repo1" and "repo2":
$ humioctl views update important-view --connection "repo1=*,repo2=*" --description "very important view"
$ humioctl views update important-view --connection "repo1=*" --connection="repo2=*" --description "very important view"
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
viewName := args[0]
client := NewApiClient(cmd)

if len(connections) == 0 && description == "" {
if len(connsFlag) == 0 && description == "" {
exitOnError(cmd, fmt.Errorf("you must specify at least one flag"), "Nothing specified to update")
}

if len(connections) > 0 {
if len(connsFlag) > 0 {
for _, v := range connsFlag {
parts := strings.SplitN(v, "=", 2)
if len(parts) != 2 {
exitOnError(cmd, fmt.Errorf("all connections must follow the format: <repoName>=<filterString>"), "Error updating view connections")
}

repo := parts[0]
filter := parts[1]

connections = append(
connections,
api.ViewConnectionInput{
RepositoryName: graphql.String(repo),
Filter: graphql.String(filter),
})
}
err := client.Views().UpdateConnections(viewName, connections)
exitOnError(cmd, err, "Error updating view connections")
}
Expand All @@ -61,7 +81,7 @@ namely "repo1" and "repo2":
},
}

cmd.Flags().StringToStringVar(&connections, "connection", connections, "Sets a repository connection with the chosen filter.")
cmd.Flags().StringArrayVar(&connsFlag, "connection", connsFlag, "Sets a repository connection with the chosen filter in format: <repoName>=<filterString>")
cmd.Flags().StringVar(&description, "description", description, "Sets the view description.")

return &cmd
Expand Down

0 comments on commit 25a6743

Please sign in to comment.