From db19dfbb77a2090d8de3354c21e2ca5062169223 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Fri, 2 Dec 2022 09:08:38 -0500 Subject: [PATCH] zrok admin delete frontend (#129) --- cmd/zrok/admin_create_frontend.go | 10 +----- cmd/zrok/admin_delete_frontend.go | 47 ++++++++++++++++++++++++++ cmd/zrok/main.go | 6 ++++ cmd/zrok/{test.go => test_endpoint.go} | 0 cmd/zrok/util.go | 15 ++++++++ 5 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 cmd/zrok/admin_delete_frontend.go rename cmd/zrok/{test.go => test_endpoint.go} (100%) create mode 100644 cmd/zrok/util.go diff --git a/cmd/zrok/admin_create_frontend.go b/cmd/zrok/admin_create_frontend.go index 7a2f8e8e..b746b1f9 100644 --- a/cmd/zrok/admin_create_frontend.go +++ b/cmd/zrok/admin_create_frontend.go @@ -1,13 +1,11 @@ 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() { @@ -46,13 +44,7 @@ func (cmd *adminCreateFrontendCommand) run(_ *cobra.Command, args []string) { 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) + resp, err := zrok.Admin.CreateFrontend(req, mustGetAdminAuth()) if err != nil { panic(err) } diff --git a/cmd/zrok/admin_delete_frontend.go b/cmd/zrok/admin_delete_frontend.go new file mode 100644 index 00000000..bf0d2d1b --- /dev/null +++ b/cmd/zrok/admin_delete_frontend.go @@ -0,0 +1,47 @@ +package main + +import ( + "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" +) + +func init() { + adminDeleteCmd.AddCommand(newAdminDeleteFrontendCommand().cmd) +} + +type adminDeleteFrontendCommand struct { + cmd *cobra.Command +} + +func newAdminDeleteFrontendCommand() *adminDeleteFrontendCommand { + cmd := &cobra.Command{ + Use: "frontend ", + Short: "Delete a global public frontend", + Args: cobra.ExactArgs(1), + } + command := &adminDeleteFrontendCommand{cmd: cmd} + cmd.Run = command.run + return command +} + +func (cmd *adminDeleteFrontendCommand) run(_ *cobra.Command, args []string) { + feToken := args[0] + + zrok, err := zrokdir.ZrokClient(apiEndpoint) + if err != nil { + panic(err) + } + + req := admin.NewDeleteFrontendParams() + req.Body = &rest_model_zrok.DeleteFrontendRequest{FrontendToken: feToken} + + _, err = zrok.Admin.DeleteFrontend(req, mustGetAdminAuth()) + if err != nil { + panic(err) + } + + logrus.Infof("deleted global frontend '%v'", feToken) +} diff --git a/cmd/zrok/main.go b/cmd/zrok/main.go index 11d7b532..808001d7 100644 --- a/cmd/zrok/main.go +++ b/cmd/zrok/main.go @@ -17,6 +17,7 @@ func init() { zrokdir.AddZrokApiEndpointFlag(&apiEndpoint, rootCmd.PersistentFlags()) rootCmd.AddCommand(accessCmd) adminCmd.AddCommand(adminCreateCmd) + adminCmd.AddCommand(adminDeleteCmd) rootCmd.AddCommand(adminCmd) rootCmd.AddCommand(shareCmd) rootCmd.AddCommand(testCmd) @@ -50,6 +51,11 @@ var adminCreateCmd = &cobra.Command{ Short: "Create global resources", } +var adminDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete global resources", +} + var shareCmd = &cobra.Command{ Use: "share", Short: "Create backend access for services", diff --git a/cmd/zrok/test.go b/cmd/zrok/test_endpoint.go similarity index 100% rename from cmd/zrok/test.go rename to cmd/zrok/test_endpoint.go diff --git a/cmd/zrok/util.go b/cmd/zrok/util.go new file mode 100644 index 00000000..248b43f8 --- /dev/null +++ b/cmd/zrok/util.go @@ -0,0 +1,15 @@ +package main + +import ( + "github.com/go-openapi/runtime" + httptransport "github.com/go-openapi/runtime/client" + "os" +) + +func mustGetAdminAuth() runtime.ClientAuthInfoWriter { + adminToken := os.Getenv("ZROK_ADMIN_TOKEN") + if adminToken == "" { + panic("please set ZROK_ADMIN_TOKEN to a valid admin token for your zrok instance") + } + return httptransport.APIKeyAuth("X-TOKEN", "header", adminToken) +}