Skip to content

Commit

Permalink
new cmd: admin messages
Browse files Browse the repository at this point in the history
show collective admin messages in cli
  • Loading branch information
dlnilsson committed Jul 19, 2023
1 parent e595661 commit 8d2ffd4
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
74 changes: 74 additions & 0 deletions cmd/admin_messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cmd

import (
"context"
"io"
"text/template"

"github.com/appgate/sdpctl/pkg/api"
"github.com/appgate/sdpctl/pkg/configuration"
"github.com/appgate/sdpctl/pkg/factory"
"github.com/appgate/sdpctl/pkg/util"
"github.com/spf13/cobra"
)

type adminMessageOpts struct {
config *configuration.Config
factory *factory.Factory
out io.Writer
json bool
}

// NewAdminMessageCmd return a new admin message command
func NewAdminMessageCmd(f *factory.Factory) *cobra.Command {
opts := adminMessageOpts{
config: f.Config,
out: f.IOOutWriter,
factory: f,
}
cmd := &cobra.Command{
Use: "admin-messages",
Short: "list of all Admin Messages generated by the system",
RunE: func(c *cobra.Command, args []string) error {
return adminMessagesRun(c, args, &opts)
},
}
cmd.Flags().BoolVar(&opts.json, "json", false, "Display in JSON format")

return cmd
}

const messageTemplate = `Messages:
{{- range . }}
{{ .GetLevel }} from {{ range .GetSources }}{{ . }}{{ end }}
{{ .GetCreated }}{{if gt .GetCount 1.0}} - {{ .GetCount }} occurrences{{end}}
{{ .GetMessage }}
{{ end }}
`

func adminMessagesRun(cmd *cobra.Command, args []string, opts *adminMessageOpts) error {
cfg := opts.config
client, err := opts.factory.APIClient(cfg)
if err != nil {
return err
}
token, err := cfg.GetBearTokenHeaderValue()
if err != nil {
return err
}
adminMessagesAPI := client.AdminMessagesApi
ctx := context.Background()
list, response, err := adminMessagesAPI.AdminMessagesSummarizeGet(ctx).Authorization(token).Execute()
if err != nil {
return api.HTTPErrorResponse(response, err)
}
if opts.json {
return util.PrintJSON(opts.out, list)
}

t := template.Must(template.New("").Parse(messageTemplate))
if err := t.Execute(opts.out, list.GetData()); err != nil {
return err
}
return nil
}
97 changes: 97 additions & 0 deletions cmd/admin_messages_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cmd

import (
"bytes"
"fmt"
"io"
"net/http"
"testing"

"github.com/appgate/sdp-api-client-go/api/v19/openapi"
"github.com/appgate/sdpctl/pkg/appliance"
"github.com/appgate/sdpctl/pkg/configuration"
"github.com/appgate/sdpctl/pkg/factory"
"github.com/appgate/sdpctl/pkg/httpmock"
"github.com/google/go-cmp/cmp"
)

func TestAdminMessageCommandOutput(t *testing.T) {
registry := httpmock.NewRegistry(t)
registry.Register(
"/admin-messages/summarize",
func(rw http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
rw.Header().Set("Content-Type", "application/vnd.appgate.peer-v19+json")
rw.WriteHeader(http.StatusOK)
fmt.Fprint(rw, `{
"data": [
{
"category": "Configuration",
"count": 2,
"created": "2023-07-18T08:48:01.354845Z",
"id": "e2689a4c-88c5-49ce-b257-436027459266",
"level": "Information",
"message": "Appliance 'second-controller' has joined the collective. This Appliance operates as a spare.",
"source": "primary-controller",
"sources": [
"primary-controller"
]
}
]
}`)
}
},
)
defer registry.Teardown()
registry.Serve()
stdout := &bytes.Buffer{}
stdin := &bytes.Buffer{}
stderr := &bytes.Buffer{}
in := io.NopCloser(stdin)
f := &factory.Factory{
Config: &configuration.Config{
Debug: false,
URL: fmt.Sprintf("http://127.0.0.1:%d", registry.Port),
},
IOOutWriter: stdout,
Stdin: in,
StdErr: stderr,
}
f.APIClient = func(c *configuration.Config) (*openapi.APIClient, error) {
return registry.Client, nil
}
f.Appliance = func(c *configuration.Config) (*appliance.Appliance, error) {
api, _ := f.APIClient(c)

a := &appliance.Appliance{
APIClient: api,
HTTPClient: api.GetConfig().HTTPClient,
Token: "",
}
return a, nil
}
cmd := NewAdminMessageCmd(f)

cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)

_, err := cmd.ExecuteC()
if err != nil {
t.Fatalf("executeC %s", err)
}

got, err := io.ReadAll(stdout)
if err != nil {
t.Fatalf("unable to read stdout %s", err)
}
gotStr := string(got)
want := `Messages:
Information from primary-controller
2023-07-18 08:48:01.354845 +0000 UTC - 2 occurrences
Appliance 'second-controller' has joined the collective. This Appliance operates as a spare.
`
if !cmp.Equal(want, gotStr) {
t.Fatalf("\nGot: \n %q \n\n Want: \n %q \n", gotStr, want)
}
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func NewCmdRoot(currentProfile *string) *cobra.Command {
rootCmd.AddCommand(generateCmd)
rootCmd.AddCommand(serviceusers.NewServiceUsersCMD(f))
rootCmd.AddCommand(license.NewLicenseCmd(f))
rootCmd.AddCommand(NewAdminMessageCmd(f))
rootCmd.SetUsageTemplate(UsageTemplate())
rootCmd.SetHelpTemplate(HelpTemplate())
rootCmd.PersistentPreRunE = rootPersistentPreRunEFunc(f, cfg)
Expand Down

0 comments on commit 8d2ffd4

Please sign in to comment.