Skip to content

Commit

Permalink
Add Alerting Contact Points
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Oct 18, 2024
1 parent 51e78d0 commit d8b14cd
Show file tree
Hide file tree
Showing 48 changed files with 1,400 additions and 206 deletions.
34 changes: 34 additions & 0 deletions cli/backup/alerting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package backup

import (
"context"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/spf13/cobra"
)

func newAlertingCommand() simplecobra.Commander {
description := "Manage Alerting resources"
return &support.SimpleCommand{
NameP: "alerting",
Short: description,
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"alert"}
// connections := cmd
// connections.PersistentFlags().StringP("connection", "", "", "filter by connection slug")
},
CommandsList: []simplecobra.Commander{
newAlertingContactCommand(),
// newClearConnectionsCmd(),
// newUploadConnectionsCmd(),
// newDownloadConnectionsCmd(),
// newListConnectionsCmd(),
// newConnectionsPermissionCmd(),
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
return cd.CobraCommand.Help()
},

Check warning on line 32 in cli/backup/alerting.go

View check run for this annotation

Codecov / codecov/patch

cli/backup/alerting.go#L30-L32

Added lines #L30 - L32 were not covered by tests
}
}
166 changes: 166 additions & 0 deletions cli/backup/alerting_contacts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package backup

import (
"context"
"encoding/json"
"log"
"log/slog"

"github.com/bep/simplecobra"
"github.com/esnet/gdg/cli/support"
"github.com/esnet/gdg/internal/service"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
)

func newAlertingContactCommand() simplecobra.Commander {
description := "Manage Alerting ContactPoints "
return &support.SimpleCommand{
NameP: "contactpoint",
Short: description,
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"contact", "contacts", "contactpoints"}
},
CommandsList: []simplecobra.Commander{
newListContactPointsCmd(),
newClearContactPointsCmd(),
newUploadContactPointsCmd(),
newDownloadContactPointsCmd(),
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
return cd.CobraCommand.Help()
},

Check warning on line 33 in cli/backup/alerting_contacts.go

View check run for this annotation

Codecov / codecov/patch

cli/backup/alerting_contacts.go#L31-L33

Added lines #L31 - L33 were not covered by tests
}
}

func logWarning() {
slog.Warn("GDG does not manage the 'email receiver' entity. It has a very odd behavior compared to all " +
"other entities. If you need to manage email contacts, please create a new contact. GDG will ignore the default contact.")
}

func newListContactPointsCmd() simplecobra.Commander {
description := "List all contact points for the given Organization"
return &support.SimpleCommand{
NameP: "list",
Short: description,
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"l"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
rootCmd.TableObj.AppendHeader(table.Row{"uid", "name", "slug", "type", "provenance", "settings"})
contactPoints, err := rootCmd.GrafanaSvc().ListContactPoints()
slog.Info("Listing contact points for context",
slog.String("Organization", GetOrganizationName()),
slog.String("context", GetContext()))

logWarning()
if err != nil {
log.Fatal("unable to retrieve Orgs contact points", slog.Any("err", err))
}

Check warning on line 61 in cli/backup/alerting_contacts.go

View check run for this annotation

Codecov / codecov/patch

cli/backup/alerting_contacts.go#L60-L61

Added lines #L60 - L61 were not covered by tests
if len(contactPoints) == 0 {
slog.Info("No contact points found")
} else {
for _, link := range contactPoints {
rawBytes, err := json.Marshal(link.Settings)
if err != nil {
slog.Warn("unable to marshall settings to valid JSON")
}

Check warning on line 69 in cli/backup/alerting_contacts.go

View check run for this annotation

Codecov / codecov/patch

cli/backup/alerting_contacts.go#L68-L69

Added lines #L68 - L69 were not covered by tests
typeVal := ""
if link.Type != nil {
typeVal = *link.Type
}
rootCmd.TableObj.AppendRow(table.Row{link.UID, link.Name, service.GetSlug(link.Name), typeVal, link.Provenance, string(rawBytes)})
}
rootCmd.Render(cd.CobraCommand, contactPoints)
}
return nil
},
}
}

func newDownloadContactPointsCmd() simplecobra.Commander {
description := "Download all contact points for the given Organization"
return &support.SimpleCommand{
NameP: "download",
Short: description,
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"d"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Download contact points for context",
slog.String("Organization", GetOrganizationName()),
slog.String("context", GetContext()))
logWarning()
file, err := rootCmd.GrafanaSvc().DownloadContactPoints()
if err != nil {
slog.Error("unable to download contact points")
} else {
slog.Info("contact points successfully downloaded", slog.Any("file", file))
}
return nil
},
}
}

func newUploadContactPointsCmd() simplecobra.Commander {
description := "Upload all contact points for the given Organization"
return &support.SimpleCommand{
NameP: "upload",
Short: description,
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"u"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Upload contact points for context",
slog.String("Organization", GetOrganizationName()),
slog.String("context", GetContext()))
newItems, err := rootCmd.GrafanaSvc().UploadContactPoints()
logWarning()
if err != nil {
slog.Error("unable to upload contact points", slog.Any("err", err))
} else {
slog.Info("contact points successfully uploaded")
rootCmd.TableObj.AppendHeader(table.Row{"name"})
for _, item := range newItems {
rootCmd.TableObj.AppendRow(table.Row{item})
}

rootCmd.Render(cd.CobraCommand, newItems)
}
return nil
},
}
}

func newClearContactPointsCmd() simplecobra.Commander {
description := "Clear all contact points for the given Organization"
return &support.SimpleCommand{
NameP: "clear",
Short: description,
Long: description,
WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
cmd.Aliases = []string{"l"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Clear contact points for context",
slog.String("Organization", GetOrganizationName()),
slog.String("context", GetContext()))
removedItems, err := rootCmd.GrafanaSvc().ClearContactPoints()
logWarning()
if err != nil {
slog.Error("unable to clear Contact Points")
} else {
slog.Info("Contact Points successfully removed")
rootCmd.TableObj.AppendHeader(table.Row{"name"})
for _, item := range removedItems {
rootCmd.TableObj.AppendRow(table.Row{item})
}
}
return nil
},
}
}
1 change: 1 addition & 0 deletions cli/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ limited to clear/delete, list, download and upload. Any other functionality wil
newOrganizationsCommand(),
newTeamsCommand(),
newUsersCommand(),
newAlertingCommand(),
},
}
}
Expand Down
4 changes: 3 additions & 1 deletion cli/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func Execute(defaultCfg string, args []string, options ...support.RootOption) er
cd, err := x.Execute(context.Background(), args)

if err != nil || len(args) == 0 {
_ = cd.CobraCommand.Help()
if cd != nil {
_ = cd.CobraCommand.Help()
}
return err
}

Expand Down
Loading

0 comments on commit d8b14cd

Please sign in to comment.