From 421adf39143139e6f4393bcfb6afc7d412d03e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederik=20Jerl=C3=B8v?= Date: Thu, 24 Oct 2024 09:38:26 +0200 Subject: [PATCH] Add export-all cmd to all triggers: Alerts, Aggregate Alerts, Filter Alerts and Scheduled Searches --- cmd/humioctl/aggregate_alerts.go | 1 + cmd/humioctl/aggregate_alerts_export.go | 39 ++++++++++++++++++++ cmd/humioctl/alerts.go | 1 + cmd/humioctl/alerts_export.go | 43 +++++++++++++++++++++-- cmd/humioctl/filter_alerts.go | 1 + cmd/humioctl/filter_alerts_export.go | 39 ++++++++++++++++++++ cmd/humioctl/scheduled_searches.go | 1 + cmd/humioctl/scheduled_searches_export.go | 39 ++++++++++++++++++++ cmd/humioctl/util.go | 5 +++ 9 files changed, 167 insertions(+), 2 deletions(-) diff --git a/cmd/humioctl/aggregate_alerts.go b/cmd/humioctl/aggregate_alerts.go index f5d69c16..f9318f32 100644 --- a/cmd/humioctl/aggregate_alerts.go +++ b/cmd/humioctl/aggregate_alerts.go @@ -27,6 +27,7 @@ func newAggregateAlertsCmd() *cobra.Command { cmd.AddCommand(newAggregateAlertsListCmd()) cmd.AddCommand(newAggregateAlertsInstallCmd()) cmd.AddCommand(newAggregateAlertsExportCmd()) + cmd.AddCommand(newAggregateAlertsExportAllCmd()) cmd.AddCommand(newAggregateAlertsRemoveCmd()) cmd.AddCommand(newAggregateAlertsShowCmd()) diff --git a/cmd/humioctl/aggregate_alerts_export.go b/cmd/humioctl/aggregate_alerts_export.go index 14e43154..7f98dc50 100644 --- a/cmd/humioctl/aggregate_alerts_export.go +++ b/cmd/humioctl/aggregate_alerts_export.go @@ -65,3 +65,42 @@ func newAggregateAlertsExportCmd() *cobra.Command { return &cmd } + +func newAggregateAlertsExportAllCmd() *cobra.Command { + var outputDirectory string + + cmd := cobra.Command{ + Use: "export-all ", + Short: "Export all aggregate alerts", + Long: `Export all aggregate alerts to yaml files with naming .yaml.`, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + view := args[0] + client := NewApiClient(cmd) + + var aggregateAlerts []*api.AggregateAlert + aggregateAlerts, err := client.AggregateAlerts().List(view) + exitOnError(cmd, err, "Error fetching aggregate alerts") + + for _, aggregateAlert := range aggregateAlerts { + yamlData, err := yaml.Marshal(&aggregateAlert) + exitOnError(cmd, err, "Failed to serialize the aggregate alert") + newAlertName := sanitizeTriggerName(aggregateAlert.Name) + + var outFilePath string + if outputDirectory != "" { + outFilePath = outputDirectory + "/" + newAlertName + ".yaml" + } else { + outFilePath = newAlertName + ".yaml" + } + + err = os.WriteFile(outFilePath, yamlData, 0600) + exitOnError(cmd, err, "Error saving the aggregate alert to file") + } + }, + } + + cmd.Flags().StringVarP(&outputDirectory, "outputDirectory", "dir", "", "The file path where the aggregate alerts should be written. Defaults to current directory.") + + return &cmd +} diff --git a/cmd/humioctl/alerts.go b/cmd/humioctl/alerts.go index ec364d20..aec35e8e 100644 --- a/cmd/humioctl/alerts.go +++ b/cmd/humioctl/alerts.go @@ -27,6 +27,7 @@ func newAlertsCmd() *cobra.Command { cmd.AddCommand(newAlertsListCmd()) cmd.AddCommand(newAlertsInstallCmd()) cmd.AddCommand(newAlertsExportCmd()) + cmd.AddCommand(newAlertsExportAllCmd()) cmd.AddCommand(newAlertsRemoveCmd()) cmd.AddCommand(newAlertsShowCmd()) diff --git a/cmd/humioctl/alerts_export.go b/cmd/humioctl/alerts_export.go index bf5781bc..228e0a84 100644 --- a/cmd/humioctl/alerts_export.go +++ b/cmd/humioctl/alerts_export.go @@ -15,10 +15,10 @@ package main import ( - "os" - + "github.com/humio/cli/api" "github.com/spf13/cobra" "gopkg.in/yaml.v2" + "os" ) func newAlertsExportCmd() *cobra.Command { @@ -53,3 +53,42 @@ func newAlertsExportCmd() *cobra.Command { return &cmd } + +func newAlertsExportAllCmd() *cobra.Command { + var outputDirectory string + + cmd := cobra.Command{ + Use: "export-all ", + Short: "Export all alerts", + Long: `Export all alerts to yaml files with naming .yaml.`, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + view := args[0] + client := NewApiClient(cmd) + + var legacyAlerts []api.Alert + legacyAlerts, err := client.Alerts().List(view) + exitOnError(cmd, err, "Error fetching legacy alerts") + + for _, alert := range legacyAlerts { + yamlData, err := yaml.Marshal(&alert) + exitOnError(cmd, err, "Failed to serialize the alert") + newAlertName := sanitizeTriggerName(alert.Name) + + var outFilePath string + if outputDirectory != "" { + outFilePath = outputDirectory + "/" + newAlertName + ".yaml" + } else { + outFilePath = newAlertName + ".yaml" + } + + err = os.WriteFile(outFilePath, yamlData, 0600) + exitOnError(cmd, err, "Error saving the alert to file") + } + }, + } + + cmd.Flags().StringVarP(&outputDirectory, "outputDirectory", "dir", "", "The file path where the alerts should be written. Defaults to current directory.") + + return &cmd +} diff --git a/cmd/humioctl/filter_alerts.go b/cmd/humioctl/filter_alerts.go index f0500dc0..8ef5962e 100644 --- a/cmd/humioctl/filter_alerts.go +++ b/cmd/humioctl/filter_alerts.go @@ -27,6 +27,7 @@ func newFilterAlertsCmd() *cobra.Command { cmd.AddCommand(newFilterAlertsListCmd()) cmd.AddCommand(newFilterAlertsInstallCmd()) cmd.AddCommand(newFilterAlertsExportCmd()) + cmd.AddCommand(newFilterAlertsExportAllCmd()) cmd.AddCommand(newFilterAlertsRemoveCmd()) cmd.AddCommand(newFilterAlertsShowCmd()) diff --git a/cmd/humioctl/filter_alerts_export.go b/cmd/humioctl/filter_alerts_export.go index d83c1a55..8e6f3a96 100644 --- a/cmd/humioctl/filter_alerts_export.go +++ b/cmd/humioctl/filter_alerts_export.go @@ -65,3 +65,42 @@ func newFilterAlertsExportCmd() *cobra.Command { return &cmd } + +func newFilterAlertsExportAllCmd() *cobra.Command { + var outputDirectory string + + cmd := cobra.Command{ + Use: "export-all ", + Short: "Export all filter alerts", + Long: `Export all filter alerts to yaml files with naming .yaml.`, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + view := args[0] + client := NewApiClient(cmd) + + var filterAlerts []api.FilterAlert + filterAlerts, err := client.FilterAlerts().List(view) + exitOnError(cmd, err, "Error fetching filter alerts") + + for _, filterAlert := range filterAlerts { + yamlData, err := yaml.Marshal(&filterAlert) + exitOnError(cmd, err, "Failed to serialize the filter alert") + newAlertName := sanitizeTriggerName(filterAlert.Name) + + var outFilePath string + if outputDirectory != "" { + outFilePath = outputDirectory + "/" + newAlertName + ".yaml" + } else { + outFilePath = newAlertName + ".yaml" + } + + err = os.WriteFile(outFilePath, yamlData, 0600) + exitOnError(cmd, err, "Error saving the filter alert to file") + } + }, + } + + cmd.Flags().StringVarP(&outputDirectory, "outputDirectory", "dir", "", "The file path where the filter alerts should be written. Defaults to current directory.") + + return &cmd +} diff --git a/cmd/humioctl/scheduled_searches.go b/cmd/humioctl/scheduled_searches.go index 34868705..aafd57b9 100644 --- a/cmd/humioctl/scheduled_searches.go +++ b/cmd/humioctl/scheduled_searches.go @@ -27,6 +27,7 @@ func newScheduledSearchesCmd() *cobra.Command { cmd.AddCommand(newScheduledSearchesListCmd()) cmd.AddCommand(newScheduledSearchesInstallCmd()) cmd.AddCommand(newScheduledSearchesExportCmd()) + cmd.AddCommand(newScheduledSearchesExportAllCmd()) cmd.AddCommand(newScheduledSearchesRemoveCmd()) cmd.AddCommand(newScheduledSearchesShowCmd()) diff --git a/cmd/humioctl/scheduled_searches_export.go b/cmd/humioctl/scheduled_searches_export.go index 9dcee503..a011b26d 100644 --- a/cmd/humioctl/scheduled_searches_export.go +++ b/cmd/humioctl/scheduled_searches_export.go @@ -65,3 +65,42 @@ func newScheduledSearchesExportCmd() *cobra.Command { return &cmd } + +func newScheduledSearchesExportAllCmd() *cobra.Command { + var outputDirectory string + + cmd := cobra.Command{ + Use: "export-all ", + Short: "Export all scheduled searches", + Long: `Export all scheduled searches to yaml files with naming .yaml.`, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + view := args[0] + client := NewApiClient(cmd) + + var scheduledSearches []api.ScheduledSearch + scheduledSearches, err := client.ScheduledSearches().List(view) + exitOnError(cmd, err, "Error fetching scheduled searches") + + for _, scheduledSearch := range scheduledSearches { + yamlData, err := yaml.Marshal(&scheduledSearch) + exitOnError(cmd, err, "Failed to serialize the scheduled search") + newScheduledSearchName := sanitizeTriggerName(scheduledSearch.Name) + + var outFilePath string + if outputDirectory != "" { + outFilePath = outputDirectory + "/" + newScheduledSearchName + ".yaml" + } else { + outFilePath = newScheduledSearchName + ".yaml" + } + + err = os.WriteFile(outFilePath, yamlData, 0600) + exitOnError(cmd, err, "Error saving the scheduled search to file") + } + }, + } + + cmd.Flags().StringVarP(&outputDirectory, "outputDirectory", "dir", "", "The file path where the scheduled searches should be written. Defaults to current directory.") + + return &cmd +} diff --git a/cmd/humioctl/util.go b/cmd/humioctl/util.go index 319bf487..b38834ac 100644 --- a/cmd/humioctl/util.go +++ b/cmd/humioctl/util.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "strconv" + "strings" "github.com/humio/cli/cmd/internal/format" "github.com/spf13/cobra" @@ -158,3 +159,7 @@ func getBytesFromURL(url string) ([]byte, error) { }() return io.ReadAll(response.Body) } + +func sanitizeTriggerName(name string) string { + return strings.ReplaceAll(strings.ReplaceAll(name, "/", "?"), "\\", "?") +}