mirror of
https://github.com/openziti/zrok.git
synced 2026-08-24 10:14:56 -05:00
namespace commands for 'zrok admin' (#726)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -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])
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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])
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user