From e2c376e5caa3031209c5ed0297bd5515af3e6af8 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Fri, 2 Sep 2022 13:07:27 -0400 Subject: [PATCH] disable; working (#30) --- cmd/zrok/disable.go | 50 +++++++++++++++++++++++++++++++++++++++++++ controller/disable.go | 2 +- zrokdir/zrokdir.go | 11 ++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 cmd/zrok/disable.go diff --git a/cmd/zrok/disable.go b/cmd/zrok/disable.go new file mode 100644 index 00000000..d6c924e2 --- /dev/null +++ b/cmd/zrok/disable.go @@ -0,0 +1,50 @@ +package main + +import ( + httptransport "github.com/go-openapi/runtime/client" + "github.com/openziti-test-kitchen/zrok/rest_client_zrok/identity" + "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() { + rootCmd.AddCommand(newDisableCommand().cmd) +} + +type disableCommand struct { + cmd *cobra.Command +} + +func newDisableCommand() *disableCommand { + cmd := &cobra.Command{ + Use: "disable", + Short: "Disable (and clean up) the enabled zrok environment", + Args: cobra.NoArgs, + } + command := &disableCommand{cmd: cmd} + cmd.Run = command.run + return command +} + +func (cmd *disableCommand) run(_ *cobra.Command, args []string) { + env, err := zrokdir.LoadEnvironment() + if err != nil { + panic(err) + } + zrok := newZrokClient() + auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.ZrokToken) + req := identity.NewDisableParams() + req.Body = &rest_model_zrok.DisableRequest{ + Identity: env.ZitiIdentityId, + } + _, err = zrok.Identity.Disable(req, auth) + if err != nil { + panic(err) + } + if err := zrokdir.Delete(); err != nil { + panic(err) + } + logrus.Infof("environment disabled") +} diff --git a/controller/disable.go b/controller/disable.go index 0e2bb24f..d2ad673c 100644 --- a/controller/disable.go +++ b/controller/disable.go @@ -35,7 +35,7 @@ func (self *disableHandler) Handle(params identity.DisableParams, principal *res logrus.Errorf("identity check failed: %v", err) return identity.NewDisableUnauthorized() } - if err := self.removeEnvironment(envId, tx); err == nil { + if err := self.removeEnvironment(envId, tx); err != nil { logrus.Errorf("error removing environment: %v", err) return identity.NewDisableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) } diff --git a/zrokdir/zrokdir.go b/zrokdir/zrokdir.go index 2cf9b1c3..e263a6e3 100644 --- a/zrokdir/zrokdir.go +++ b/zrokdir/zrokdir.go @@ -13,6 +13,17 @@ type Environment struct { ZitiIdentityId string `json:"ziti_identity"` } +func Delete() error { + path, err := zrokDir() + if err != nil { + return err + } + if err := os.RemoveAll(path); err != nil { + return err + } + return nil +} + func LoadEnvironment() (*Environment, error) { ef, err := environmentFile() if err != nil {