-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e5f2a2
commit 62e39cf
Showing
11 changed files
with
628 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,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() | ||
}, | ||
} | ||
} |
This file contains 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,148 @@ | ||
package backup | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"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() | ||
}, | ||
} | ||
} | ||
|
||
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 := rootCmd.GrafanaSvc().ListContactPoints() | ||
slog.Info("Listing contact points for context", | ||
slog.String("Organization", GetOrganizationName()), | ||
slog.String("context", GetContext())) | ||
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") | ||
} | ||
rootCmd.TableObj.AppendRow(table.Row{link.UID, link.Name, service.GetSlug(link.Name), link.Type, 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 { | ||
file, err := rootCmd.GrafanaSvc().DownloadContactPoints() | ||
slog.Info("Download contact points for context", | ||
slog.String("Organization", GetOrganizationName()), | ||
slog.String("context", GetContext())) | ||
if err != nil { | ||
slog.Error("unable to contact point") | ||
} 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 { | ||
removedItems, err := rootCmd.GrafanaSvc().UploadContactPoints() | ||
slog.Info("Upload contact points for context", | ||
slog.String("Organization", GetOrganizationName()), | ||
slog.String("context", GetContext())) | ||
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 removedItems { | ||
rootCmd.TableObj.AppendRow(table.Row{item}) | ||
} | ||
|
||
rootCmd.Render(cd.CobraCommand, removedItems) | ||
} | ||
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 { | ||
removedItems, err := rootCmd.GrafanaSvc().ClearContactPoints() | ||
slog.Info("Clear contact points for context", | ||
slog.String("Organization", GetOrganizationName()), | ||
slog.String("context", GetContext())) | ||
if err != nil { | ||
slog.Error("unable to contact point") | ||
} 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 | ||
}, | ||
} | ||
} |
This file contains 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 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 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 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 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,133 @@ | ||
package service | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"log/slog" | ||
|
||
"github.com/esnet/gdg/internal/config" | ||
"github.com/esnet/gdg/internal/tools" | ||
"github.com/grafana/grafana-openapi-client-go/client/provisioning" | ||
"github.com/grafana/grafana-openapi-client-go/models" | ||
) | ||
|
||
func (s *DashNGoImpl) ListContactPoints() []*models.EmbeddedContactPoint { | ||
p := provisioning.NewGetContactpointsParams() | ||
result, err := s.GetClient().Provisioning.GetContactpoints(p) | ||
if err != nil { | ||
log.Fatalf("unable to retrieve contact points, err:%s", err.Error()) | ||
} | ||
return result.GetPayload() | ||
} | ||
|
||
func (s *DashNGoImpl) DownloadContactPoints() (string, error) { | ||
var ( | ||
dsPacked []byte | ||
err error | ||
) | ||
p := provisioning.NewGetContactpointsExportParams() | ||
p.Download = tools.PtrOf(true) | ||
p.Decrypt = tools.PtrOf(true) | ||
p.Format = tools.PtrOf("json") | ||
data, err := s.GetClient().Provisioning.GetContactpointsExport(p) | ||
if err != nil { | ||
log.Fatalf("unable to retrieve Contact Points, err: %s", err.Error()) | ||
} | ||
|
||
dsPath := buildResourcePath("contacts", config.AlertingResource) | ||
if dsPacked, err = json.MarshalIndent(data.GetPayload(), "", " "); err != nil { | ||
return "", fmt.Errorf("unable to serialize data to JSON. %w", err) | ||
} | ||
if err = s.storage.WriteFile(dsPath, dsPacked); err != nil { | ||
return "", fmt.Errorf("unable to write file. %w", err) | ||
} | ||
|
||
return dsPath, nil | ||
} | ||
|
||
func (s *DashNGoImpl) UploadContactPoints() ([]string, error) { | ||
var ( | ||
err error | ||
rawDS []byte | ||
result []string | ||
) | ||
data := new(models.AlertingFileExport) | ||
currentContacts := s.ListContactPoints() | ||
m := make(map[string]*models.EmbeddedContactPoint) | ||
for ndx, i := range currentContacts { | ||
m[i.UID] = currentContacts[ndx] | ||
} | ||
|
||
fileLocation := buildResourcePath("contacts", config.AlertingResource) | ||
if rawDS, err = s.storage.ReadFile(fileLocation); err != nil { | ||
return nil, fmt.Errorf("failed to read file. file: %s, err: %w", fileLocation, err) | ||
} | ||
if err = json.Unmarshal(rawDS, data); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshall file, file:%s, err: %w", fileLocation, err) | ||
} | ||
for _, i := range data.ContactPoints { | ||
for _, r := range i.Receivers { | ||
if _, ok := m[r.UID]; ok { | ||
// do update | ||
p := provisioning.NewPutContactpointParams() | ||
p.UID = r.UID | ||
p.Body = &models.EmbeddedContactPoint{ | ||
DisableResolveMessage: false, | ||
Name: i.Name, | ||
Provenance: "", | ||
Settings: r.Settings, | ||
Type: tools.PtrOf(r.Type), | ||
UID: r.UID, | ||
} | ||
_, err := s.GetClient().Provisioning.PutContactpoint(p) | ||
if err != nil { | ||
slog.Error("failed to update contact point", slog.Any("uid", r.UID)) | ||
continue | ||
} | ||
result = append(result, i.Name) | ||
|
||
} else { | ||
p := provisioning.NewPostContactpointsParams() | ||
p.Body = &models.EmbeddedContactPoint{ | ||
DisableResolveMessage: false, | ||
Name: i.Name, | ||
UID: r.UID, | ||
Provenance: "", | ||
Settings: r.Settings, | ||
Type: tools.PtrOf(r.Type), | ||
} | ||
_, err := s.GetClient().Provisioning.PostContactpoints(p) | ||
if err != nil { | ||
slog.Error("failed to create contact point", slog.Any("uid", r.UID)) | ||
continue | ||
} | ||
|
||
result = append(result, i.Name) | ||
} | ||
} | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (s *DashNGoImpl) ClearContactPoints() ([]string, error) { | ||
var ( | ||
err error | ||
results []string | ||
) | ||
contacts := s.ListContactPoints() | ||
for _, contact := range contacts { | ||
_, err = s.GetClient().Provisioning.DeleteContactpoints(contact.UID) | ||
if err != nil { | ||
slog.Error("unable to delete contact point", | ||
slog.Any("name", contact.Name), | ||
slog.Any("uid", contact.UID), | ||
) | ||
continue | ||
} | ||
results = append(results, contact.Name) | ||
} | ||
|
||
return results, nil | ||
} |
This file contains 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
Oops, something went wrong.