From 8804bead4ec357d9c53cef00764c0419b4e654e1 Mon Sep 17 00:00:00 2001 From: Florent Gluck <florent.gluck@hesge.ch> Date: Fri, 11 Nov 2022 17:13:51 +0100 Subject: [PATCH] Ongoing work on tpledit --- README.md | 2 + src/cmdTemplate/templateDel.go | 44 +++++-------- src/cmdTemplate/templateEdit.go | 113 ++++++++++++++++++++++++++++++++ src/cmdVM/vmEdit.go | 2 +- src/nexush/nexush.go | 1 + 5 files changed, 135 insertions(+), 27 deletions(-) create mode 100644 src/cmdTemplate/templateEdit.go diff --git a/README.md b/README.md index faa33da..836e973 100644 --- a/README.md +++ b/README.md @@ -586,6 +586,8 @@ The table below lists all potential capabilities associated to a user: | VM_WRITEFS_ANY | Can import files into **ANY** VM | | VM_SET_ACCESS | Can change (edit or delete) a VM's access | | TPL_CREATE | Can create a template | +| TPL_EDIT | Can edit a template | +| TPL_EDIT_ANY | Can edit **ANY** template | | TPL_DESTROY | Can destroy a template | | TPL_DESTROY_ANY | Can destroy **ANY** template | | TPL_LIST | Can list public or owned templates | diff --git a/src/cmdTemplate/templateDel.go b/src/cmdTemplate/templateDel.go index caebd19..c2dc40e 100644 --- a/src/cmdTemplate/templateDel.go +++ b/src/cmdTemplate/templateDel.go @@ -20,7 +20,13 @@ func (cmd *Del)GetDesc() []string { } func (cmd *Del)PrintUsage() { - printUsage(cmd, "deleted") + for _, desc := range cmd.GetDesc() { + u.PrintlnErr(desc) + } + u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――") + u.PrintlnErr("USAGE: ",cmd.GetName()," ID") + u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――") + u.PrintlnErr("ID ID of the template to delete.") } func (cmd *Del)Run(args []string) int { @@ -28,39 +34,25 @@ func (cmd *Del)Run(args []string) int { host := g.GetInstance().Host argc := len(args) - if argc < 1 { + if argc != 1 { cmd.PrintUsage() return 1 } - templates, err := getFilteredTemplates("/templates", args) - if err != nil { - u.PrintlnErr(err.Error()) - return 1 - } - - if len(templates) == 0 { - u.PrintlnErr("No templates to delete!") - return 1 - } - + tplID := args[0] statusCode := 0 - for _, template := range(templates) { - uuid := template.ID.String() - resp, err := client.R().Delete(host+"/templates/"+uuid) - if err != nil { - u.PrintlnErr("Failed deleting template \""+template.Name+"\": "+err.Error()) - statusCode = 1 + resp, err := client.R().Delete(host+"/templates/"+tplID) + if err != nil { + u.PrintlnErr("Failed deleting template \""+tplID+"\": "+err.Error()) + statusCode = 1 + } else { + if resp.IsSuccess() { + u.Println("Deleted template \""+tplID+"\"") } else { - if resp.IsSuccess() { - u.Println("Deleted template \""+template.Name+"\"") - } else { - u.PrintlnErr("Failed deleting template \""+template.Name+"\": "+resp.Status()+": "+resp.String()) - statusCode = 1 - } + u.PrintlnErr("Failed deleting template \""+tplID+"\": "+resp.Status()+": "+resp.String()) + statusCode = 1 } - } return statusCode diff --git a/src/cmdTemplate/templateEdit.go b/src/cmdTemplate/templateEdit.go new file mode 100644 index 0000000..8663849 --- /dev/null +++ b/src/cmdTemplate/templateEdit.go @@ -0,0 +1,113 @@ +package cmdTemplate + +import ( + "strings" + u "nexus-client/utils" + g "nexus-client/globals" +) + +type Edit struct { + Name string +} + +type templateEditParameters struct { + Name string + Access string +} + +func (cmd *Edit)GetName() string { + return cmd.Name +} + +func (cmd *Edit)GetDesc() []string { + return []string{ + "Edits one or more template's properties: name, access.", + "Requires TPL_EDIT or TPL_EDIT_ANY user capability."} +} + +func (cmd *Edit)PrintUsage() { + for _, desc := range cmd.GetDesc() { + u.PrintlnErr(desc) + } + u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――") + u.PrintlnErr("USAGE: "+cmd.GetName()+" ID [name=\"new name\"] [access=private/public]") + u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――") + u.PrintlnErr("ID ID of the template to edit.") + const usage string = `Parameters that can be changed (at least one must be specified): +name Name of the template. +access Access type, either "private" or "public".` +} + +func (cmd *Edit)Run(args []string) int { + client := g.GetInstance().Client + host := g.GetInstance().Host + + argc := len(args) + if argc < 2 { + cmd.PrintUsage() + return 1 + } + + templateParams, err := cmd.parseArgs(args) + if err != nil { + u.PrintlnErr(err.Error()) + return 1 + } + + if templateParams == nil { + cmd.PrintUsage() + return 1 + } + + tplID := args[0] + statusCode := 0 + + resp, err := client.R().SetBody(templateParams).Put(host+"/templates/"+tplID) + if err != nil { + u.PrintlnErr("Failed editing template \""+tplID+"\": "+err.Error()) + statusCode = 1 + } else { + if resp.IsSuccess() { + u.Println("Edited template \""+tplID+"\"") + } else { + u.PrintlnErr("Failed editing template \""+tplID+"\": "+resp.Status()+": "+resp.String()) + statusCode = 1 + } + } + + return statusCode +} + +func (cmd *Edit)parseArgs(args []string) (*templateEditParameters, error) { + templateParams := &templateEditParameters {} + atLeastOneArg := false + + getStringVal := func(s string, prefix string) string { + if strings.HasPrefix(s, prefix) { + parts := strings.Split(s, prefix) + return parts[1] + } + return "" + } + + for _, arg := range args { + s := getStringVal(arg, "name=") + if s != "" { + templateParams.Name = s + atLeastOneArg = true + continue + } + s = getStringVal(arg, "access=") + if s != "" { + templateParams.Access = s + atLeastOneArg = true + continue + } + } + + if atLeastOneArg { + return templateParams, nil + } else { + return nil, nil + } +} diff --git a/src/cmdVM/vmEdit.go b/src/cmdVM/vmEdit.go index 6da0134..c9338e7 100644 --- a/src/cmdVM/vmEdit.go +++ b/src/cmdVM/vmEdit.go @@ -36,7 +36,7 @@ func (cmd *Edit)PrintUsage() { u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――") u.PrintlnErr("USAGE: "+cmd.GetName()+" [ID ...] [regex ...] [name=\"new name\"] [cpus=n] [ram=n] [nic=none/user]") u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――") - const usage string = `Parameters that can be changed (all optional): + const usage string = `Parameters that can be changed (at least one must be specified): name Name of the VM. cpus Number of CPUs, between 1 and 16. ram Amount of RAM in MB, between 512 and 32768. diff --git a/src/nexush/nexush.go b/src/nexush/nexush.go index a052644..1b0ced3 100644 --- a/src/nexush/nexush.go +++ b/src/nexush/nexush.go @@ -55,6 +55,7 @@ var cmdList = []cmd.Command { &cmdTemplate.List{"tpllist"}, &cmdTemplate.Create{"tplcreate"}, + &cmdTemplate.Edit{"tpledit"}, &cmdTemplate.Del{"tpldel"}, &cmdTemplate.ExportDisk{"tplexportdisk"}, } -- GitLab