Skip to content
Snippets Groups Projects
Commit a5a9553b authored by Florent Gluck's avatar Florent Gluck
Browse files

libclient: rework in progress to make lib more useful

parent 69456895
No related branches found
No related tags found
No related merge requests found
......@@ -47,16 +47,18 @@ func (cmd *Create) Run(args []string) int {
u.PrintlnErr(err)
}
resp, err := libclient.TemplateCreate(vmID, name, access)
template, err := libclient.TemplateCreate(vmID, name, access)
if err != nil {
u.PrintlnErr("Error: " + err.Error())
return 1
}
if resp.IsSuccess() {
u.Println(resp)
return 0
} else {
u.PrintlnErr(resp.Status() + ": " + resp.String())
templateStr, err := template.String()
if err != nil {
u.PrintlnErr("Error: " + err.Error())
return 1
}
u.Println(templateStr)
return 0
}
......@@ -37,20 +37,13 @@ func (cmd *Del) Run(args []string) int {
}
tplID := args[0]
statusCode := 0
resp, err := libclient.TemplateDel(tplID)
err := libclient.TemplateDel(tplID)
if err != nil {
u.PrintlnErr("Failed deleting template \"" + tplID + "\": " + err.Error())
statusCode = 1
} else {
if resp.IsSuccess() {
u.Println("Deleted template \"" + tplID + "\"")
} else {
u.PrintlnErr("Failed deleting template \"" + tplID + "\": " + resp.Status() + ": " + resp.String())
statusCode = 1
}
return 1
}
return statusCode
u.Println("Template \"" + tplID + "\" deleted")
return 0
}
package template
import (
"encoding/json"
"errors"
"nexus-common/params"
"nexus-common/template"
g "nexus-libclient/globals"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
)
func TemplateCreate(vmID uuid.UUID, name, access string) (*resty.Response, error) {
func TemplateCreate(vmID uuid.UUID, name, access string) (*template.TemplateSerialized, error) {
client := g.GetInstance().Client
host := g.GetInstance().Host
......@@ -17,6 +19,13 @@ func TemplateCreate(vmID uuid.UUID, name, access string) (*resty.Response, error
if err != nil {
return nil, err
}
return resp, nil
if resp.IsSuccess() {
var tpl template.TemplateSerialized
if err := json.Unmarshal(resp.Body(), &tpl); err != nil {
return nil, err
}
return &tpl, nil
} else {
return nil, errors.New(resp.Status() + ": " + resp.String())
}
}
package template
import (
"errors"
g "nexus-libclient/globals"
"github.com/go-resty/resty/v2"
)
func TemplateDel(tplID string) (*resty.Response, error) {
func TemplateDel(tplID string) error {
client := g.GetInstance().Client
host := g.GetInstance().Host
resp, err := client.R().Delete(host + "/templates/" + tplID)
if err != nil {
return nil, err
return err
}
if resp.IsSuccess() {
return nil
} else {
return errors.New(resp.Status() + ": " + resp.String())
}
return resp, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment