这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (r *Repositories) Delete(name, reason string, allowDataDeletion bool) error
}

var m struct {
CreateRepository struct {
Type string `graphql:"__typename"`
DeleteSearchDomain struct {
ClientMutationId string
} `graphql:"deleteSearchDomain(name: $name, deleteMessage: $reason)"`
}
variables := map[string]interface{}{
Expand Down
110 changes: 110 additions & 0 deletions api/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,113 @@ func (c *Views) List() ([]ViewListItem, error) {

return q.View, graphqlErr
}

type ViewConnectionInput struct {
RepositoryName graphql.String `json:"repositoryName"`
Filter graphql.String `json:"filter"`
}

func (c *Views) Create(name, description string, connections map[string]string) error {
var m 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,
}

err := c.client.Mutate(&m, variables)

if err != nil {
return err
}

return nil
}

func (c *Views) Delete(name, reason string) error {
var m struct {
DeleteSearchDomain struct {
ClientMutationId string
} `graphql:"deleteSearchDomain(name: $name, deleteMessage: $reason)"`
}
variables := map[string]interface{}{
"name": graphql.String(name),
"reason": graphql.String(reason),
}

err := c.client.Mutate(&m, variables)

if err != nil {
return err
}

return nil
}

func (c *Views) UpdateConnections(name string, connections map[string]string) error {
var m 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,
}

err := c.client.Mutate(&m, variables)

if err != nil {
return err
}

return nil
}

func (c *Views) UpdateDescription(name string, description string) error {
var m struct {
UpdateDescriptionMutation struct {
ClientMutationId string
} `graphql:"updateDescriptionForSearchDomain(name: $name, newDescription: $description)"`
}

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

err := c.client.Mutate(&m, variables)

if err != nil {
return err
}

return nil
}
3 changes: 3 additions & 0 deletions cmd/humioctl/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func newViewsCmd() *cobra.Command {

cmd.AddCommand(newViewsShowCmd())
cmd.AddCommand(newViewsListCmd())
cmd.AddCommand(newViewsCreateCmd())
cmd.AddCommand(newViewsUpdateCmd())
cmd.AddCommand(newViewsDeleteCmd())

return cmd
}
Expand Down
52 changes: 52 additions & 0 deletions cmd/humioctl/views_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright © 2018 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"github.com/spf13/cobra"
)

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

c := &cobra.Command{
Use: "create <view-name>",
Short: "Create a view.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
viewName := args[0]

client := NewApiClient(cmd)

apiErr := client.Views().Create(viewName, description, connections)
exitOnError(cmd, apiErr, "Error creating view")
fmt.Printf("Successfully created view %s\n", viewName)

view, apiErr := client.Views().Get(viewName)
exitOnError(cmd, apiErr, "error fetching view")

printViewTable(view)

fmt.Println()
},
}

c.Flags().StringToStringVar(&connections, "connection", connections, "Sets a repository connection with the chosen filter.")
c.Flags().StringVar(&description, "description", description, "Sets an optional description")

return c
}
39 changes: 39 additions & 0 deletions cmd/humioctl/views_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright © 2018 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"github.com/spf13/cobra"
)

func newViewsDeleteCmd() *cobra.Command {
return &cobra.Command{
Use: "delete <view> \"descriptive reason for why it is being deleted\"",
Short: "Delete a view.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
view := args[0]
reason := args[1]

fmt.Printf("Deleting view %s with reason %q\n", view, reason)

client := NewApiClient(cmd)

apiError := client.Views().Delete(view, reason)
exitOnError(cmd, apiError, "error removing view")
},
}
}
61 changes: 61 additions & 0 deletions cmd/humioctl/views_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright © 2020 Humio Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"

"github.com/spf13/cobra"
)

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

cmd := cobra.Command{
Use: "update",
Short: "Updates the settings of a view",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
viewName := args[0]

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

client := NewApiClient(cmd)

if len(connections) > 0 {
err := client.Views().UpdateConnections(viewName, connections)
exitOnError(cmd, err, "error updating view connections")
}

if description != "" {
err := client.Views().UpdateDescription(viewName, description)
exitOnError(cmd, err, "error updating view description")
}

view, apiErr := client.Views().Get(viewName)
exitOnError(cmd, apiErr, "error fetching view")
printViewTable(view)
fmt.Println()
},
}

cmd.Flags().StringToStringVar(&connections, "connection", connections, "Sets a repository connection with the chosen filter.")
cmd.Flags().StringVar(&description, "description", description, "Sets the view description.")

return &cmd
}