From 284f2bef333aa8c7c469fdeff39358a09f80118b Mon Sep 17 00:00:00 2001 From: Brad Sherwood Date: Tue, 25 Jun 2024 18:10:30 +0930 Subject: [PATCH] Add enable/disable automatic search option on repos and view (#163) --- api/repositories.go | 17 +++++++++++++++ api/views.go | 42 ++++++++++++++++++++++++++---------- cmd/humioctl/repos.go | 1 + cmd/humioctl/repos_update.go | 17 +++++++++++++-- cmd/humioctl/views_list.go | 8 ++++--- cmd/humioctl/views_update.go | 16 +++++++++++++- 6 files changed, 84 insertions(+), 17 deletions(-) diff --git a/api/repositories.go b/api/repositories.go index 4583fe4..a37b70d 100644 --- a/api/repositories.go +++ b/api/repositories.go @@ -21,6 +21,7 @@ type Repository struct { StorageRetentionSizeGB float64 `graphql:"storageSizeBasedRetention"` SpaceUsed int64 `graphql:"compressedByteSize"` S3ArchivingConfiguration humiographql.S3Configuration `graphql:"s3ArchivingConfiguration"` + AutomaticSearch bool `graphql:"automaticSearch"` } func (c *Client) Repositories() *Repositories { return &Repositories{client: c} } @@ -329,3 +330,19 @@ func (r *Repositories) UpdateS3ArchivingConfiguration(name string, bucket string return r.client.Mutate(&mutation, variables) } + +func (r *Repositories) UpdateAutomaticSearch(name string, automaticSearch bool) error { + var mutation struct { + SetAutomaticSearching struct { + // We have to make a selection, so just take __typename + Typename graphql.String `graphql:"__typename"` + } `graphql:"setAutomaticSearching(name: $name, automaticSearch: $automaticSearch)"` + } + + variables := map[string]interface{}{ + "name": graphql.String(name), + "automaticSearch": graphql.Boolean(automaticSearch), + } + + return r.client.Mutate(&mutation, variables) +} diff --git a/api/views.go b/api/views.go index 966be27..7f058b9 100644 --- a/api/views.go +++ b/api/views.go @@ -17,9 +17,10 @@ type ViewConnection struct { } type ViewQueryData struct { - Name string - Description string - ViewInfo struct { + Name string + Description string + AutomaticSearch bool + ViewInfo struct { Connections []struct { Repository struct{ Name string } Filter string @@ -28,9 +29,10 @@ type ViewQueryData struct { } type View struct { - Name string - Description string - Connections []ViewConnection + Name string + Description string + Connections []ViewConnection + AutomaticSearch bool } func (c *Client) Views() *Views { return &Views{client: c} } @@ -58,17 +60,19 @@ func (c *Views) Get(name string) (*View, error) { } view := View{ - Name: query.Result.Name, - Description: query.Result.Description, - Connections: connections, + Name: query.Result.Name, + Description: query.Result.Description, + Connections: connections, + AutomaticSearch: query.Result.AutomaticSearch, } return &view, nil } type ViewListItem struct { - Name string - Typename string `graphql:"__typename"` + Name string + Typename string `graphql:"__typename"` + AutomaticSearch bool } func (c *Views) List() ([]ViewListItem, error) { @@ -152,3 +156,19 @@ func (c *Views) UpdateDescription(name string, description string) error { return c.client.Mutate(&mutation, variables) } + +func (c *Views) UpdateAutomaticSearch(name string, automaticSearch bool) error { + var mutation struct { + SetAutomaticSearching struct { + // We have to make a selection, so just take __typename + Typename graphql.String `graphql:"__typename"` + } `graphql:"setAutomaticSearching(name: $name, automaticSearch: $automaticSearch)"` + } + + variables := map[string]interface{}{ + "name": graphql.String(name), + "automaticSearch": graphql.Boolean(automaticSearch), + } + + return c.client.Mutate(&mutation, variables) +} diff --git a/cmd/humioctl/repos.go b/cmd/humioctl/repos.go index 314c7bf..9527e5c 100644 --- a/cmd/humioctl/repos.go +++ b/cmd/humioctl/repos.go @@ -49,6 +49,7 @@ func printRepoDetailsTable(cmd *cobra.Command, repo api.Repository) { {format.String("S3 Archiving Bucket"), format.String(repo.S3ArchivingConfiguration.Bucket)}, {format.String("S3 Archiving Region"), format.String(repo.S3ArchivingConfiguration.Region)}, {format.String("S3 Archiving Format"), format.String(repo.S3ArchivingConfiguration.Format)}, + {format.String("Automatic Search"), format.Bool(repo.AutomaticSearch)}, } printDetailsTable(cmd, details) diff --git a/cmd/humioctl/repos_update.go b/cmd/humioctl/repos_update.go index b6b8d78..604db01 100644 --- a/cmd/humioctl/repos_update.go +++ b/cmd/humioctl/repos_update.go @@ -21,7 +21,7 @@ import ( ) func newReposUpdateCmd() *cobra.Command { - var allowDataDeletionFlag, enableS3ArchivingFlag, disableS3ArchivingFlag bool + var allowDataDeletionFlag, enableS3ArchivingFlag, disableS3ArchivingFlag, enableAutomaticSearchFlag, disableAutomaticSearchFlag bool var descriptionFlag, s3ArchivingBucketFlag, s3ArchivingRegionFlag, s3ArchivingFormatFlag stringPtrFlag var retentionTimeFlag, ingestSizeBasedRetentionFlag, storageSizeBasedRetentionFlag float64PtrFlag @@ -34,7 +34,8 @@ func newReposUpdateCmd() *cobra.Command { client := NewApiClient(cmd) if descriptionFlag.value == nil && retentionTimeFlag.value == nil && ingestSizeBasedRetentionFlag.value == nil && storageSizeBasedRetentionFlag.value == nil && - !enableS3ArchivingFlag && !disableS3ArchivingFlag && s3ArchivingBucketFlag.value == nil && s3ArchivingRegionFlag.value == nil && s3ArchivingFormatFlag.value == nil { + !enableS3ArchivingFlag && !disableS3ArchivingFlag && s3ArchivingBucketFlag.value == nil && s3ArchivingRegionFlag.value == nil && s3ArchivingFormatFlag.value == nil && + !enableAutomaticSearchFlag && !disableAutomaticSearchFlag { exitOnError(cmd, fmt.Errorf("you must specify at least one flag to update"), "Nothing specified to update") } if descriptionFlag.value != nil { @@ -69,6 +70,15 @@ func newReposUpdateCmd() *cobra.Command { exitOnError(cmd, err, "Error enabling S3 archiving") } + if disableAutomaticSearchFlag { + err := client.Repositories().UpdateAutomaticSearch(repoName, false) + exitOnError(cmd, err, "Error disabling automatic search") + } + if enableAutomaticSearchFlag { + err := client.Repositories().UpdateAutomaticSearch(repoName, true) + exitOnError(cmd, err, "Error enabling automatic search") + } + fmt.Fprintf(cmd.OutOrStdout(), "Successfully updated repository %q\n", repoName) }, } @@ -85,6 +95,9 @@ func newReposUpdateCmd() *cobra.Command { cmd.Flags().Var(&s3ArchivingFormatFlag, "s3-archiving-format", "The S3 archiving format to be used for S3 Archiving. Formats: RAW, NDJSON") cmd.MarkFlagsRequiredTogether("s3-archiving-bucket", "s3-archiving-region", "s3-archiving-format") cmd.MarkFlagsMutuallyExclusive("enable-s3-archiving", "disable-s3-archiving") + cmd.Flags().BoolVar(&enableAutomaticSearchFlag, "enable-automatic-search", false, "Enable automatic search for the repository.") + cmd.Flags().BoolVar(&disableAutomaticSearchFlag, "disable-automatic-search", false, "Disable automatic search for the repository.") + cmd.MarkFlagsMutuallyExclusive("enable-automatic-search", "disable-automatic-search") return &cmd } diff --git a/cmd/humioctl/views_list.go b/cmd/humioctl/views_list.go index 264081d..aaf3fca 100644 --- a/cmd/humioctl/views_list.go +++ b/cmd/humioctl/views_list.go @@ -37,14 +37,16 @@ func newViewsListCmd() *cobra.Command { for i, view := range views { if viewOnly { if view.Typename == viewTypeName { - rows[i] = []format.Value{format.String(view.Name)} + rows[i] = []format.Value{format.String(view.Name), + format.Bool(view.AutomaticSearch)} } } else { - rows[i] = []format.Value{format.String(view.Name)} + rows[i] = []format.Value{format.String(view.Name), + format.Bool(view.AutomaticSearch)} } } - printOverviewTable(cmd, []string{"Name"}, rows) + printOverviewTable(cmd, []string{"Name", "Automatic Search"}, rows) }, } diff --git a/cmd/humioctl/views_update.go b/cmd/humioctl/views_update.go index fc2fbac..7554c69 100644 --- a/cmd/humioctl/views_update.go +++ b/cmd/humioctl/views_update.go @@ -25,6 +25,8 @@ import ( ) func newViewsUpdateCmd() *cobra.Command { + var enableAutomaticSearchFlag, disableAutomaticSearchFlag bool + connsFlag := []string{} connections := []api.ViewConnectionInput{} description := "" @@ -47,7 +49,7 @@ namely "repo1" and "repo2": viewName := args[0] client := NewApiClient(cmd) - if len(connsFlag) == 0 && description == "" { + if len(connsFlag) == 0 && description == "" && !enableAutomaticSearchFlag && !disableAutomaticSearchFlag { exitOnError(cmd, fmt.Errorf("you must specify at least one flag"), "Nothing specified to update") } @@ -77,12 +79,24 @@ namely "repo1" and "repo2": exitOnError(cmd, err, "Error updating view description") } + if disableAutomaticSearchFlag { + err := client.Views().UpdateAutomaticSearch(viewName, false) + exitOnError(cmd, err, "Error disabling automatic search") + } + if enableAutomaticSearchFlag { + err := client.Views().UpdateAutomaticSearch(viewName, true) + exitOnError(cmd, err, "Error enabling automatic search") + } + fmt.Fprintf(cmd.OutOrStdout(), "Successfully updated view %q\n", viewName) }, } cmd.Flags().StringArrayVar(&connsFlag, "connection", connsFlag, "Sets a repository connection with the chosen filter in format: =") cmd.Flags().StringVar(&description, "description", description, "Sets the view description.") + cmd.Flags().BoolVar(&enableAutomaticSearchFlag, "enable-automatic-search", false, "Enable automatic search for the view.") + cmd.Flags().BoolVar(&disableAutomaticSearchFlag, "disable-automatic-search", false, "Disable automatic search for the view.") + cmd.MarkFlagsMutuallyExclusive("enable-automatic-search", "disable-automatic-search") return &cmd }