namespace commands for 'zrok admin' (#726)

This commit is contained in:
Michael Quigley
2025-08-20 15:45:15 -04:00
parent 2893af6b9a
commit d52b85fb3d
5 changed files with 227 additions and 3 deletions
+55
View File
@@ -0,0 +1,55 @@
package main
import (
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
adminCreateCmd.AddCommand(newAdminCreateNamespaceCommand().cmd)
}
type adminCreateNamespaceCommand struct {
cmd *cobra.Command
name string
description string
}
func newAdminCreateNamespaceCommand() *adminCreateNamespaceCommand {
cmd := &cobra.Command{
Use: "namespace <name>",
Short: "Create a new namespace",
Args: cobra.ExactArgs(1),
}
command := &adminCreateNamespaceCommand{cmd: cmd}
cmd.Flags().StringVarP(&command.description, "description", "d", "", "namespace description")
cmd.Run = command.run
return command
}
func (cmd *adminCreateNamespaceCommand) run(_ *cobra.Command, args []string) {
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}
zrok, err := env.Client()
if err != nil {
panic(err)
}
req := admin.NewCreateNamespaceParams()
req.Body = admin.CreateNamespaceBody{
Name: args[0],
Description: cmd.description,
}
resp, err := zrok.Admin.CreateNamespace(req, mustGetAdminAuth())
if err != nil {
panic(err)
}
logrus.Infof("created namespace '%v' with token '%v'", args[0], resp.Payload.NamespaceToken)
}
+51
View File
@@ -0,0 +1,51 @@
package main
import (
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
adminDeleteCmd.AddCommand(newAdminDeleteNamespaceCommand().cmd)
}
type adminDeleteNamespaceCommand struct {
cmd *cobra.Command
}
func newAdminDeleteNamespaceCommand() *adminDeleteNamespaceCommand {
cmd := &cobra.Command{
Use: "namespace <token>",
Short: "Delete a namespace",
Args: cobra.ExactArgs(1),
}
command := &adminDeleteNamespaceCommand{cmd: cmd}
cmd.Run = command.run
return command
}
func (cmd *adminDeleteNamespaceCommand) run(_ *cobra.Command, args []string) {
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}
zrok, err := env.Client()
if err != nil {
panic(err)
}
req := admin.NewDeleteNamespaceParams()
req.Body = admin.DeleteNamespaceBody{
NamespaceToken: args[0],
}
_, err = zrok.Admin.DeleteNamespace(req, mustGetAdminAuth())
if err != nil {
panic(err)
}
logrus.Infof("deleted namespace '%v'", args[0])
}
+61
View File
@@ -0,0 +1,61 @@
package main
import (
"fmt"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/spf13/cobra"
"os"
"time"
)
func init() {
adminListCmd.AddCommand(newAdminListNamespacesCommand().cmd)
}
type adminListNamespacesCommand struct {
cmd *cobra.Command
}
func newAdminListNamespacesCommand() *adminListNamespacesCommand {
cmd := &cobra.Command{
Use: "namespaces",
Short: "List all namespaces",
Args: cobra.NoArgs,
}
command := &adminListNamespacesCommand{cmd}
cmd.Run = command.run
return command
}
func (c *adminListNamespacesCommand) run(_ *cobra.Command, _ []string) {
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}
zrok, err := env.Client()
if err != nil {
panic(err)
}
req := admin.NewListNamespacesParams()
resp, err := zrok.Admin.ListNamespaces(req, mustGetAdminAuth())
if err != nil {
panic(err)
}
fmt.Println()
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleColoredDark)
t.AppendHeader(table.Row{"Namespace Token", "Name", "Description", "Created", "Updated"})
for _, ns := range resp.Payload {
created := time.Unix(ns.CreatedAt, 0).Format("2006-01-02 15:04:05")
updated := time.Unix(ns.UpdatedAt, 0).Format("2006-01-02 15:04:05")
t.AppendRow(table.Row{ns.NamespaceToken, ns.Name, ns.Description, created, updated})
}
t.Render()
fmt.Println()
}
+57
View File
@@ -0,0 +1,57 @@
package main
import (
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
adminUpdateCmd.AddCommand(newAdminUpdateNamespaceCommand().cmd)
}
type adminUpdateNamespaceCommand struct {
cmd *cobra.Command
name string
description string
}
func newAdminUpdateNamespaceCommand() *adminUpdateNamespaceCommand {
cmd := &cobra.Command{
Use: "namespace <token>",
Short: "Update a namespace",
Args: cobra.ExactArgs(1),
}
command := &adminUpdateNamespaceCommand{cmd: cmd}
cmd.Flags().StringVarP(&command.name, "name", "n", "", "namespace name")
cmd.Flags().StringVarP(&command.description, "description", "d", "", "namespace description")
cmd.Run = command.run
return command
}
func (cmd *adminUpdateNamespaceCommand) run(_ *cobra.Command, args []string) {
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}
zrok, err := env.Client()
if err != nil {
panic(err)
}
req := admin.NewUpdateNamespaceParams()
req.Body = admin.UpdateNamespaceBody{
NamespaceToken: args[0],
Name: cmd.name,
Description: cmd.description,
}
_, err = zrok.Admin.UpdateNamespace(req, mustGetAdminAuth())
if err != nil {
panic(err)
}
logrus.Infof("updated namespace '%v'", args[0])
}
+3 -3
View File
@@ -65,11 +65,11 @@ func (str *Store) FindNamespaceByToken(token string, tx *sqlx.Tx) (*Namespace, e
}
func (str *Store) UpdateNamespace(ns *Namespace, tx *sqlx.Tx) error {
stmt, err := tx.Prepare("update namespaces set name = $2, description = $3, updated_at = current_timestamp where id = $1")
stmt, err := tx.Prepare("update namespaces set name = $1, description = $2, updated_at = current_timestamp where id = $3")
if err != nil {
return errors.Wrap(err, "error preparing namespace update statement")
}
_, err = stmt.Exec(ns.Id, ns.Name, ns.Description)
_, err = stmt.Exec(ns.Name, ns.Description, ns.Id)
if err != nil {
return errors.Wrap(err, "error executing namespace update statement")
}
@@ -86,4 +86,4 @@ func (str *Store) DeleteNamespace(id int, tx *sqlx.Tx) error {
return errors.Wrap(err, "error executing namespace delete statement")
}
return nil
}
}