-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
show collective admin messages in cli
- Loading branch information
Showing
3 changed files
with
172 additions
and
0 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,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 | ||
} |
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,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) | ||
} | ||
} |
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