add/delete org plumbed through to CLI (#537)

This commit is contained in:
Michael Quigley 2024-12-09 13:44:56 -05:00
parent c98aaa8e00
commit 4473571b37
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 108 additions and 1 deletions

View File

@ -0,0 +1,52 @@
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(newAdminCreateOrganizationCommand().cmd)
}
type adminCreateOrganizationCommand struct {
cmd *cobra.Command
description string
}
func newAdminCreateOrganizationCommand() *adminCreateOrganizationCommand {
cmd := &cobra.Command{
Use: "organization",
Aliases: []string{"org"},
Short: "Create a new organization",
Args: cobra.NoArgs,
}
command := &adminCreateOrganizationCommand{cmd: cmd}
cmd.Flags().StringVarP(&command.description, "description", "d", "", "Organization description")
cmd.Run = command.run
return command
}
func (cmd *adminCreateOrganizationCommand) run(_ *cobra.Command, _ []string) {
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}
zrok, err := env.Client()
if err != nil {
panic(err)
}
req := admin.NewCreateOrganizationParams()
req.Body = admin.CreateOrganizationBody{Description: cmd.description}
resp, err := zrok.Admin.CreateOrganization(req, mustGetAdminAuth())
if err != nil {
panic(err)
}
logrus.Infof("created new organization with token '%v'", resp.Payload.Token)
}

View File

@ -0,0 +1,50 @@
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(newAdminDeleteOrganizationCommand().cmd)
}
type adminDeleteOrganizationCommand struct {
cmd *cobra.Command
}
func newAdminDeleteOrganizationCommand() *adminDeleteOrganizationCommand {
cmd := &cobra.Command{
Use: "organization <organizationToken>",
Aliases: []string{"org"},
Short: "Delete an organization",
Args: cobra.ExactArgs(1),
}
command := &adminDeleteOrganizationCommand{cmd: cmd}
cmd.Run = command.run
return command
}
func (cmd *adminDeleteOrganizationCommand) 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.NewDeleteOrganizationParams()
req.Body.Token = args[0]
_, err = zrok.Admin.DeleteOrganization(req, mustGetAdminAuth())
if err != nil {
panic(err)
}
logrus.Infof("deleted organization with token '%v'", args[0])
}

View File

@ -38,5 +38,10 @@ func (h *deleteOrganizationHandler) Handle(params admin.DeleteOrganizationParams
return admin.NewDeleteOrganizationInternalServerError()
}
if err := trx.Commit(); err != nil {
logrus.Errorf("error committing transaction: %v", err)
return admin.NewDeleteOrganizationInternalServerError()
}
return admin.NewDeleteOrganizationOK()
}

View File

@ -25,7 +25,7 @@ func (str *Store) CreateOrganization(org *Organization, trx *sqlx.Tx) (int, erro
func (str *Store) FindOrganizationByToken(token string, trx *sqlx.Tx) (*Organization, error) {
org := &Organization{}
if err := trx.QueryRowx("select * from organizations where token = $1", token).StructScan(org); err != nil {
if err := trx.QueryRowx("select * from organizations where token = $1 and not deleted", token).StructScan(org); err != nil {
return nil, errors.Wrap(err, "error selecting frontend by token")
}
return org, nil