disable; working (#30)

This commit is contained in:
Michael Quigley 2022-09-02 13:07:27 -04:00
parent c59f3cd722
commit e2c376e5ca
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 62 additions and 1 deletions

50
cmd/zrok/disable.go Normal file
View File

@ -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")
}

View File

@ -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()))
}

View File

@ -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 {