added 'zrok organization account-overview' ('zrok org overview') to allow admins to query overview data for any account in an org where they're an admin (#537)

This commit is contained in:
Michael Quigley 2024-12-10 12:55:34 -05:00
parent 65104e9e18
commit 5582ac0ea5
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 86 additions and 0 deletions

View File

@ -28,6 +28,7 @@ func init() {
rootCmd.AddCommand(adminCmd)
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(modifyCmd)
rootCmd.AddCommand(organizationCmd)
rootCmd.AddCommand(shareCmd)
rootCmd.AddCommand(testCmd)
rootCmd.AddCommand(gendoc.NewGendocCmd(rootCmd))
@ -94,6 +95,12 @@ var modifyCmd = &cobra.Command{
Short: "Modify resources",
}
var organizationCmd = &cobra.Command{
Use: "organization",
Aliases: []string{"org"},
Short: "Organization admin commands",
}
var shareCmd = &cobra.Command{
Use: "share",
Short: "Create backend access for shares",

View File

@ -0,0 +1,79 @@
package main
import (
"fmt"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/tui"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"io"
"net/http"
)
func init() {
organizationCmd.AddCommand(newOrgAccountOverviewCommand().cmd)
}
type orgAccountOverviewCommand struct {
cmd *cobra.Command
}
func newOrgAccountOverviewCommand() *orgAccountOverviewCommand {
cmd := &cobra.Command{
Use: "account-overview <organizationToken> <accountEmail>",
Aliases: []string{"overview"},
Args: cobra.ExactArgs(2),
}
command := &orgAccountOverviewCommand{cmd: cmd}
cmd.Run = command.run
return command
}
func (cmd *orgAccountOverviewCommand) run(_ *cobra.Command, args []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)
}
client := &http.Client{}
apiEndpoint, _ := root.ApiEndpoint()
req, err := http.NewRequest("GET", fmt.Sprintf("%v/api/v1/overview/%v/%v", apiEndpoint, args[0], args[1]), nil)
if err != nil {
if !panicInstead {
tui.Error("error creating request", err)
}
panic(err)
}
req.Header.Add("X-TOKEN", root.Environment().Token)
resp, err := client.Do(req)
if err != nil {
if !panicInstead {
tui.Error("error sending request", err)
}
panic(err)
}
if resp.StatusCode != http.StatusOK {
if !panicInstead {
tui.Error("received error response", errors.New(resp.Status))
}
panic(errors.New(resp.Status))
}
json, err := io.ReadAll(resp.Body)
if err != nil {
if !panicInstead {
tui.Error("error reading json", err)
}
panic(err)
}
_ = resp.Body.Close()
fmt.Println(string(json))
}