-
Notifications
You must be signed in to change notification settings - Fork 25
Scheduled Search V2 #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SaaldjorMike
merged 1 commit into
humio:master
from
fjerlov-cs:fjerlov/scheduled-search-v2
Mar 26, 2025
Merged
Scheduled Search V2 #185
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Copyright © 2025 CrowdStrike | ||
| // | ||
| // 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 ( | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func newScheduledSearchesV2Cmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "scheduled-searches-v2", | ||
| Short: "Manage scheduled searches", | ||
| } | ||
|
|
||
| cmd.AddCommand(newScheduledSearchesV2ListCmd()) | ||
| cmd.AddCommand(newScheduledSearchesV2InstallCmd()) | ||
| cmd.AddCommand(newScheduledSearchesV2ExportCmd()) | ||
| cmd.AddCommand(newScheduledSearchesV2ExportAllCmd()) | ||
| cmd.AddCommand(newScheduledSearchesV2RemoveCmd()) | ||
| cmd.AddCommand(newScheduledSearchesV2ShowCmd()) | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // Copyright © 2025 CrowdStrike | ||
| // | ||
| // 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 ( | ||
| "os" | ||
|
|
||
| "github.com/humio/cli/internal/api" | ||
| "github.com/spf13/cobra" | ||
| "gopkg.in/yaml.v2" | ||
| ) | ||
|
|
||
| func newScheduledSearchesV2ExportCmd() *cobra.Command { | ||
| var outputName string | ||
|
|
||
| cmd := cobra.Command{ | ||
| Use: "export [flags] <view> <scheduled-search>", | ||
| Short: "Export a scheduled search <scheduled-search> in <view> to a file.", | ||
| Args: cobra.ExactArgs(2), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| view := args[0] | ||
| scheduledSearchName := args[1] | ||
| client := NewApiClient(cmd) | ||
|
|
||
| scheduledSearches, err := client.ScheduledSearchesV2().List(view) | ||
| exitOnError(cmd, err, "Could not list scheduled searches") | ||
|
|
||
| var scheduledSearch api.ScheduledSearchV2 | ||
| for _, ss := range scheduledSearches { | ||
| if ss.Name == scheduledSearchName { | ||
| scheduledSearch = ss | ||
| } | ||
| } | ||
|
|
||
| if scheduledSearch.ID == "" { | ||
| exitOnError(cmd, api.ScheduledSearchNotFound(scheduledSearchName), "Could not find scheduled search") | ||
| } | ||
|
|
||
| yamlData, err := yaml.Marshal(&scheduledSearch) | ||
| exitOnError(cmd, err, "Failed to serialize the scheduled search") | ||
|
|
||
| if outputName == "" { | ||
| outputName = sanitizeTriggerName(scheduledSearch.Name) | ||
| } | ||
|
|
||
| outFilePath := outputName + ".yaml" | ||
| err = os.WriteFile(outFilePath, yamlData, 0600) | ||
| exitOnError(cmd, err, "Error saving the scheduled search file") | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVarP(&outputName, "output", "o", "", "The file path where the scheduled search should be written. Defaults to ./<scheduled-search-name>.yaml") | ||
|
|
||
| return &cmd | ||
| } | ||
|
|
||
| func newScheduledSearchesV2ExportAllCmd() *cobra.Command { | ||
| var outputDirectory string | ||
|
|
||
| cmd := cobra.Command{ | ||
| Use: "export-all <view>", | ||
| Short: "Export all scheduled searches", | ||
| Long: `Export all scheduled searches to yaml files with naming <sanitized-scheduled-search-name>.yaml.`, | ||
| Args: cobra.ExactArgs(1), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| view := args[0] | ||
| client := NewApiClient(cmd) | ||
|
|
||
| var scheduledSearches []api.ScheduledSearchV2 | ||
| scheduledSearches, err := client.ScheduledSearchesV2().List(view) | ||
| exitOnError(cmd, err, "Error fetching scheduled searches") | ||
|
|
||
| for i := range scheduledSearches { | ||
| yamlData, err := yaml.Marshal(&scheduledSearches[i]) | ||
| exitOnError(cmd, err, "Failed to serialize the scheduled search") | ||
| scheduledSearchFilename := sanitizeTriggerName(scheduledSearches[i].Name) + ".yaml" | ||
|
|
||
| var outFilePath string | ||
| if outputDirectory != "" { | ||
| outFilePath = outputDirectory + "/" + scheduledSearchFilename | ||
| } else { | ||
| outFilePath = scheduledSearchFilename | ||
| } | ||
|
|
||
| err = os.WriteFile(outFilePath, yamlData, 0600) | ||
| exitOnError(cmd, err, "Error saving the scheduled search to file") | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVarP(&outputDirectory, "outputDirectory", "d", "", "The file path where the scheduled searches should be written. Defaults to current directory.") | ||
|
|
||
| return &cmd | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // Copyright © 2025 CrowdStrike | ||
| // | ||
| // 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" | ||
| "os" | ||
|
|
||
| "github.com/humio/cli/internal/api" | ||
| "github.com/spf13/cobra" | ||
| "gopkg.in/yaml.v2" | ||
| ) | ||
|
|
||
| func newScheduledSearchesV2InstallCmd() *cobra.Command { | ||
| var filePath, url, name string | ||
|
|
||
| cmd := cobra.Command{ | ||
| Use: "install [flags] <view>", | ||
| Short: "Installs a scheduled search in a view", | ||
| Long: `Install a scheduled search from a URL or from a local file. | ||
|
|
||
| The install command allows you to install scheduled searches from a URL or from a local file, e.g. | ||
|
|
||
| $ humioctl scheduled-searches-v2 install viewName --url=https://example.com/acme/scheduled-search-v2.yaml | ||
|
|
||
| $ humioctl scheduled-searches-v2 install viewName --file=./scheduled-searches-v2.yaml | ||
|
|
||
| $ humioctl scheduled-searches-v2 install viewName --file=./scheduled-search-v1.yaml | ||
| This will install the legacy scheduled search with the CreateScheduledSearch mutation (deprecated for removal in 1.231). | ||
| `, | ||
| Args: cobra.ExactArgs(1), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| var content []byte | ||
| var err error | ||
|
|
||
| // Check that we got the right number of argument | ||
| // if we only got <view> you must supply --file or --url. | ||
| if l := len(args); l == 1 { | ||
| if filePath != "" { | ||
| content, err = getBytesFromFile(filePath) | ||
| } else if url != "" { | ||
| content, err = getBytesFromURL(url) | ||
| } else { | ||
| cmd.Printf("You must specify a path using --file or --url\n") | ||
| os.Exit(1) | ||
| } | ||
| } | ||
| exitOnError(cmd, err, "Could to load the scheduled search") | ||
|
|
||
| client := NewApiClient(cmd) | ||
| viewName := args[0] | ||
|
|
||
| var scheduledSearchV2 api.ScheduledSearchV2 | ||
| scheduledSearchV2, parseErr := tryParseScheduledSearchV2(content) | ||
| if parseErr != nil { | ||
| _, _ = fmt.Fprintf(cmd.OutOrStdout(), "Yaml file could be parsed as scheduled search v2, err=%s. Attempting to parse as scheduled search v1 and create using deprecated CreateScheduledSearch mutation.\n", parseErr) | ||
| var scheduledSearchV1 api.ScheduledSearch | ||
| err = yaml.Unmarshal(content, &scheduledSearchV1) | ||
|
|
||
| exitOnError(cmd, err, "Could not unmarshal the scheduled search as a scheduled search v1.") | ||
|
|
||
| if name != "" { | ||
| scheduledSearchV2.Name = name | ||
| } | ||
|
|
||
| _, err = client.ScheduledSearchesV2().Create(viewName, &scheduledSearchV2) | ||
| exitOnError(cmd, err, "Could not create the scheduled search using deprecated CreateScheduledSearch mutation.") | ||
|
|
||
| _, _ = fmt.Fprintln(cmd.OutOrStdout(), "Scheduled search v1 created using deprecated CreateScheduledSearch mutation.") | ||
| } else { | ||
| if name != "" { | ||
| scheduledSearchV2.Name = name | ||
| } | ||
|
|
||
| _, err = client.ScheduledSearchesV2().Create(viewName, &scheduledSearchV2) | ||
| exitOnError(cmd, err, "Could not create the scheduled search") | ||
|
|
||
| _, _ = fmt.Fprintln(cmd.OutOrStdout(), "Scheduled search created") | ||
| os.Exit(1) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&filePath, "file", "", "The local file path to the scheduled search to install.") | ||
| cmd.Flags().StringVar(&url, "url", "", "A URL to fetch the scheduled search file from.") | ||
| cmd.Flags().StringVarP(&name, "name", "n", "", "Install the scheduled search under a specific name, ignoring the `name` attribute in the scheduled search file.") | ||
| cmd.MarkFlagsMutuallyExclusive("file", "url") | ||
| return &cmd | ||
| } | ||
|
|
||
| func tryParseScheduledSearchV2(content []byte) (api.ScheduledSearchV2, error) { | ||
| var scheduledSearchV2 api.ScheduledSearchV2 | ||
| err := yaml.Unmarshal(content, &scheduledSearchV2) | ||
| if err != nil { | ||
| return api.ScheduledSearchV2{}, err | ||
| } else { | ||
| return scheduledSearchV2, nil | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.