From 702ad23c991836249758982c87f8bfa533809d44 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Thu, 1 Dec 2022 16:41:08 -0500 Subject: [PATCH] roughed in zrok admin create frontend cli (#129) --- cmd/zrok/admin_create_frontend.go | 61 +++++++++++++++++++++++++++++++ cmd/zrok/admin_gc.go | 6 +-- cmd/zrok/main.go | 6 +++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 cmd/zrok/admin_create_frontend.go diff --git a/cmd/zrok/admin_create_frontend.go b/cmd/zrok/admin_create_frontend.go new file mode 100644 index 00000000..7a2f8e8e --- /dev/null +++ b/cmd/zrok/admin_create_frontend.go @@ -0,0 +1,61 @@ +package main + +import ( + httptransport "github.com/go-openapi/runtime/client" + "github.com/openziti-test-kitchen/zrok/rest_client_zrok/admin" + "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/zrokdir" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "os" +) + +func init() { + adminCreateCmd.AddCommand(newAdminCreateFrontendCommand().cmd) +} + +type adminCreateFrontendCommand struct { + cmd *cobra.Command +} + +func newAdminCreateFrontendCommand() *adminCreateFrontendCommand { + cmd := &cobra.Command{ + Use: "frontend ", + Short: "Create a global public frontend", + Args: cobra.ExactArgs(3), + } + command := &adminCreateFrontendCommand{cmd: cmd} + cmd.Run = command.run + return command +} + +func (cmd *adminCreateFrontendCommand) run(_ *cobra.Command, args []string) { + zId := args[0] + publicName := args[1] + urlTemplate := args[2] + + zrok, err := zrokdir.ZrokClient(apiEndpoint) + if err != nil { + panic(err) + } + + req := admin.NewCreateFrontendParams() + req.Body = &rest_model_zrok.CreateFrontendRequest{ + ZID: zId, + PublicName: publicName, + URLTemplate: urlTemplate, + } + + adminToken := os.Getenv("ZROK_ADMIN_TOKEN") + if adminToken == "" { + panic("please set ZROK_ADMIN_TOKEN to a valid admin token for your zrok instance") + } + auth := httptransport.APIKeyAuth("X-TOKEN", "header", adminToken) + + resp, err := zrok.Admin.CreateFrontend(req, auth) + if err != nil { + panic(err) + } + + logrus.Infof("created global public frontend '%v'", resp.Payload.Token) +} diff --git a/cmd/zrok/admin_gc.go b/cmd/zrok/admin_gc.go index 86a807ef..2733a3d9 100644 --- a/cmd/zrok/admin_gc.go +++ b/cmd/zrok/admin_gc.go @@ -21,9 +21,9 @@ func newAdminGcCommand() *adminGcCommand { Short: "Garbage collect a zrok instance", Args: cobra.ExactArgs(1), } - c := &adminGcCommand{cmd: cmd} - cmd.Run = c.run - return c + command := &adminGcCommand{cmd: cmd} + cmd.Run = command.run + return command } func (gc *adminGcCommand) run(_ *cobra.Command, args []string) { diff --git a/cmd/zrok/main.go b/cmd/zrok/main.go index 35a155bc..11d7b532 100644 --- a/cmd/zrok/main.go +++ b/cmd/zrok/main.go @@ -16,6 +16,7 @@ func init() { rootCmd.PersistentFlags().BoolVarP(&panicInstead, "panic", "p", false, "Panic instead of showing pretty errors") zrokdir.AddZrokApiEndpointFlag(&apiEndpoint, rootCmd.PersistentFlags()) rootCmd.AddCommand(accessCmd) + adminCmd.AddCommand(adminCreateCmd) rootCmd.AddCommand(adminCmd) rootCmd.AddCommand(shareCmd) rootCmd.AddCommand(testCmd) @@ -44,6 +45,11 @@ var adminCmd = &cobra.Command{ Short: "Administration and operations functions", } +var adminCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create global resources", +} + var shareCmd = &cobra.Command{ Use: "share", Short: "Create backend access for services",