From 1c81a1510e0c92e5d323420f5e5131d9bbc726a0 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Fri, 27 Jun 2025 14:23:35 -0400 Subject: [PATCH] added 'zrok overview public-frontends' to allow CLI listing (#996) --- CHANGELOG.md | 2 +- cmd/zrok/orgMemberships.go | 5 ++- cmd/zrok/overview.go | 6 ++- cmd/zrok/overviewFrontends.go | 78 +++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 cmd/zrok/overviewFrontends.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 00cb51e0..2ad11102 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ FEATURE: New add and delete API endpoints for frontend grants. New `zrok admin c FEATURE: New admin endpoint for deleting accounts. New `zrok admin delete account` CLI for invoking the API endpoint from the command line (https://github.com/openziti/zrok/issues/993) -FEATURE: New API endpoint (`/overview/public-frontends`) that returns the public frontends available to authenticated account. The public frontends include those marked with the `open` permission mode, and those marked `closed` where the user has a frontend grant allowing them to access the frontend (https://github.com/openziti/zrok/issues/996) +FEATURE: New API endpoint (`/overview/public-frontends`) that returns the public frontends available to authenticated account. The public frontends include those marked with the `open` permission mode, and those marked `closed` where the user has a frontend grant allowing them to access the frontend. New CLI command `zrok overview public-frontends` to allow end users to list the public frontends their account can use (https://github.com/openziti/zrok/issues/996) ## v1.0.6 diff --git a/cmd/zrok/orgMemberships.go b/cmd/zrok/orgMemberships.go index 8dd04128..42e27b9f 100644 --- a/cmd/zrok/orgMemberships.go +++ b/cmd/zrok/orgMemberships.go @@ -2,12 +2,13 @@ package main import ( "fmt" + "os" + httptransport "github.com/go-openapi/runtime/client" "github.com/jedib0t/go-pretty/v6/table" "github.com/openziti/zrok/environment" "github.com/openziti/zrok/tui" "github.com/spf13/cobra" - "os" ) func init() { @@ -45,7 +46,7 @@ func (c *orgMembershipsCommand) run(_ *cobra.Command, _ []string) { zrok, err := root.Client() if err != nil { if !panicInstead { - tui.Error("error loading zrokdir", err) + tui.Error("error getting zrok client", err) } panic(err) } diff --git a/cmd/zrok/overview.go b/cmd/zrok/overview.go index f05fe6b4..e9ff39a4 100644 --- a/cmd/zrok/overview.go +++ b/cmd/zrok/overview.go @@ -2,14 +2,18 @@ package main import ( "fmt" + "github.com/openziti/zrok/environment" "github.com/openziti/zrok/sdk/golang/sdk" "github.com/openziti/zrok/tui" "github.com/spf13/cobra" ) +var overviewCmd *cobra.Command + func init() { - rootCmd.AddCommand(newOverviewCommand().cmd) + overviewCmd = newOverviewCommand().cmd + rootCmd.AddCommand(overviewCmd) } type overviewCommand struct { diff --git a/cmd/zrok/overviewFrontends.go b/cmd/zrok/overviewFrontends.go new file mode 100644 index 00000000..2306e9da --- /dev/null +++ b/cmd/zrok/overviewFrontends.go @@ -0,0 +1,78 @@ +package main + +import ( + "fmt" + "os" + + httptransport "github.com/go-openapi/runtime/client" + "github.com/jedib0t/go-pretty/v6/table" + "github.com/openziti/zrok/environment" + "github.com/openziti/zrok/tui" + "github.com/spf13/cobra" +) + +func init() { + overviewCmd.AddCommand(newOverviewFrontendsCommand().cmd) +} + +type overviewPublicFrontendsCommand struct { + cmd *cobra.Command +} + +func newOverviewFrontendsCommand() *overviewPublicFrontendsCommand { + cmd := &cobra.Command{ + Use: "public-frontends", + Short: "Show the available public frontends", + Aliases: []string{"pf"}, + Args: cobra.NoArgs, + } + command := &overviewPublicFrontendsCommand{cmd: cmd} + cmd.Run = command.run + return command +} + +func (cmd *overviewPublicFrontendsCommand) run(_ *cobra.Command, _ []string) { + root, err := environment.LoadRoot() + if err != nil { + if !panicInstead { + tui.Error("error loading zrokdir", err) + } + panic(err) + } + + if !root.IsEnabled() { + tui.Error("unable to load environment; did you 'zrok enable'?", nil) + } + + zrok, err := root.Client() + if err != nil { + if !panicInstead { + tui.Error("error getting zrok client", err) + } + panic(err) + } + + auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().AccountToken) + resp, err := zrok.Metadata.ListPublicFrontendsForAccount(nil, auth) + if err != nil { + if !panicInstead { + tui.Error("error listing public frontends", err) + } + panic(err) + } + + if len(resp.Payload.PublicFrontends) > 0 { + fmt.Println() + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.SetStyle(table.StyleColoredDark) + t.AppendHeader(table.Row{"Frontend Name", "URL Template"}) + for _, i := range resp.Payload.PublicFrontends { + t.AppendRow(table.Row{i.PublicName, i.URLTemplate}) + } + t.Render() + fmt.Println() + } else { + fmt.Println("no public frontends found") + } +}