cli wiring for 'zrok modify share' (#432)

This commit is contained in:
Michael Quigley 2024-03-06 12:42:34 -05:00
parent 8d368b2b1e
commit 749e229505
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 81 additions and 0 deletions

View File

@ -26,6 +26,7 @@ func init() {
testCmd.AddCommand(loopCmd)
rootCmd.AddCommand(adminCmd)
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(modifyCmd)
rootCmd.AddCommand(shareCmd)
rootCmd.AddCommand(testCmd)
transport.AddAddressParser(tcp.AddressParser{})
@ -85,6 +86,12 @@ var loopCmd = &cobra.Command{
Short: "Loopback testing utilities",
}
var modifyCmd = &cobra.Command{
Use: "modify",
Aliases: []string{"mod"},
Short: "Modify resources",
}
var shareCmd = &cobra.Command{
Use: "share",
Short: "Create backend access for shares",

74
cmd/zrok/modifyShare.go Normal file
View File

@ -0,0 +1,74 @@
package main
import (
"fmt"
httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/share"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/tui"
"github.com/spf13/cobra"
)
func init() {
modifyCmd.AddCommand(newModifyShareCommand().cmd)
}
type modifyShareCommand struct {
addAccessGrants []string
removeAccessGrants []string
cmd *cobra.Command
}
func newModifyShareCommand() *modifyShareCommand {
cmd := &cobra.Command{
Use: "share <shareToken>",
Short: "Modify a share",
}
command := &modifyShareCommand{cmd: cmd}
cmd.Flags().StringArrayVar(&command.addAccessGrants, "add-access-grant", []string{}, "Add an additional access grant")
cmd.Flags().StringArrayVar(&command.removeAccessGrants, "remove-access-grant", []string{}, "Remove an access grant")
cmd.Run = command.run
return command
}
func (cmd *modifyShareCommand) run(_ *cobra.Command, args []string) {
shrToken := args[0]
root, err := environment.LoadRoot()
if err != nil {
if !panicInstead {
tui.Error("error loading environment", err)
}
panic(err)
}
if !root.IsEnabled() {
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
}
zrok, err := root.Client()
if err != nil {
if !panicInstead {
tui.Error("unable to create zrok client", err)
}
panic(err)
}
auth := httptransport.APIKeyAuth("X-TOKEN", "header", root.Environment().Token)
if len(cmd.addAccessGrants) > 0 || len(cmd.removeAccessGrants) > 0 {
req := share.NewUpdateShareParams()
req.Body = &rest_model_zrok.UpdateShareRequest{
ShrToken: shrToken,
AddAccessGrants: cmd.addAccessGrants,
RemoveAccessGrants: cmd.removeAccessGrants,
}
if _, err := zrok.Share.UpdateShare(req, auth); err != nil {
if !panicInstead {
tui.Error("unable to update share", err)
}
panic(err)
}
fmt.Println("updated")
}
}