Skip to content

Commit

Permalink
Add export-all cmd to all triggers: Alerts, Aggregate Alerts, Filter …
Browse files Browse the repository at this point in the history
…Alerts and Scheduled Searches
  • Loading branch information
fjerlov-cs committed Oct 24, 2024
1 parent 14f9db8 commit 421adf3
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/humioctl/aggregate_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
39 changes: 39 additions & 0 deletions cmd/humioctl/aggregate_alerts_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,42 @@ func newAggregateAlertsExportCmd() *cobra.Command {

return &cmd
}

func newAggregateAlertsExportAllCmd() *cobra.Command {
var outputDirectory string

cmd := cobra.Command{
Use: "export-all <view>",
Short: "Export all aggregate alerts",
Long: `Export all aggregate alerts to yaml files with naming <sanitized-aggregate-alert-name>.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
}
1 change: 1 addition & 0 deletions cmd/humioctl/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
43 changes: 41 additions & 2 deletions cmd/humioctl/alerts_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -53,3 +53,42 @@ func newAlertsExportCmd() *cobra.Command {

return &cmd
}

func newAlertsExportAllCmd() *cobra.Command {
var outputDirectory string

cmd := cobra.Command{
Use: "export-all <view>",
Short: "Export all alerts",
Long: `Export all alerts to yaml files with naming <sanitized-alert-name>.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
}
1 change: 1 addition & 0 deletions cmd/humioctl/filter_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
39 changes: 39 additions & 0 deletions cmd/humioctl/filter_alerts_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,42 @@ func newFilterAlertsExportCmd() *cobra.Command {

return &cmd
}

func newFilterAlertsExportAllCmd() *cobra.Command {
var outputDirectory string

cmd := cobra.Command{
Use: "export-all <view>",
Short: "Export all filter alerts",
Long: `Export all filter alerts to yaml files with naming <sanitized-filter-alert-name>.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
}
1 change: 1 addition & 0 deletions cmd/humioctl/scheduled_searches.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
39 changes: 39 additions & 0 deletions cmd/humioctl/scheduled_searches_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,42 @@ func newScheduledSearchesExportCmd() *cobra.Command {

return &cmd
}

func newScheduledSearchesExportAllCmd() *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.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
}
5 changes: 5 additions & 0 deletions cmd/humioctl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"
"os"
"strconv"
"strings"

"github.com/humio/cli/cmd/internal/format"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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, "/", "?"), "\\", "?")
}

0 comments on commit 421adf3

Please sign in to comment.