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

libclient: added userDel

Reworked error handling to print more user-friendly/readable msg
parent 02f26b71
No related branches found
No related tags found
No related merge requests found
......@@ -2,10 +2,10 @@ package cmdUser
import (
"encoding/csv"
"errors"
"io"
u "nexus-client/utils"
g "nexus-libclient/globals"
libclient "nexus-libclient/user"
"os"
)
......@@ -70,27 +70,27 @@ func (cmd *Del) Run(args []string) int {
break
}
if err != nil {
u.PrintlnErr("FAILED reading " + err.Error())
u.PrintlnErr("Failed reading " + err.Error())
statusCode = 1
continue
}
columnCount := len(columns)
if columnCount != 1 {
u.PrintlnErr("FAILED reading record on line ", line, ": expecting 1 column")
u.PrintlnErr("Failed reading record on line ", line, ": expecting 1 column")
statusCode = 1
continue
}
email := columns[0]
if !u.IsEmail(email) {
u.PrintlnErr("FAILED reading record on line ", line, ": ", email, " is not a valid email")
u.PrintlnErr("Failed reading record on line ", line, ": ", email, " is not a valid email")
statusCode = 1
continue
}
if err := cmd.runRequest(email); err != nil {
u.PrintlnErr(err.Error())
if err := libclient.UserDelete(email); err != nil {
u.PrintlnErr(err)
statusCode = 1
} else {
u.Println("Successfully deleted user " + email)
......@@ -102,7 +102,7 @@ func (cmd *Del) Run(args []string) int {
// Iterates through each user (email) to delete
for i := range args {
email := args[i]
if err := cmd.runRequest(email); err != nil {
if err := libclient.UserDelete(email); err != nil {
u.PrintlnErr(err.Error())
statusCode = 1
} else {
......@@ -113,19 +113,3 @@ func (cmd *Del) Run(args []string) int {
return statusCode
}
func (cmd *Del) runRequest(email string) error {
client := g.GetInstance().Client
host := g.GetInstance().Host
resp, err := client.R().Delete(host + "/users/" + email)
if err != nil {
return errors.New("Error: " + err.Error())
}
if resp.IsSuccess() {
return nil
} else {
return errors.New(resp.Status() + ": " + resp.String())
}
}
......@@ -2,9 +2,9 @@ package login
import (
"encoding/json"
"errors"
"nexus-common/params"
g "nexus-libclient/globals"
"nexus-libclient/response"
)
// Returns the token if the authentication was successful or an error if it wasn't.
......@@ -25,22 +25,14 @@ func GetToken(user, pwd string) (string, error) {
type Response struct {
Token string
}
var response Response
err = json.Unmarshal(resp.Body(), &response)
var r Response
err = json.Unmarshal(resp.Body(), &r)
if err != nil {
return "", err
}
return response.Token, nil
return r.Token, nil
} else {
type Response struct {
Message string
}
var response Response
err = json.Unmarshal(resp.Body(), &response)
if err != nil {
return "", err
}
return "", errors.New(response.Message)
return "", response.ErrorToMsg(resp)
}
}
......@@ -64,7 +56,7 @@ func RefreshToken() (string, error) {
}
return response.Token, nil
} else {
return "", errors.New(resp.Status() + ": " + resp.String())
return "", response.ErrorToMsg(resp)
}
}
}
package response
import (
"encoding/json"
"errors"
"github.com/go-resty/resty/v2"
)
type Message struct {
Message string
}
func ErrorToMsg(resp *resty.Response) error {
var msg Message
if err := json.Unmarshal(resp.Body(), &msg); err != nil {
return errors.New("Error: " + err.Error())
}
return errors.New(msg.Message)
}
......@@ -2,10 +2,10 @@ package template
import (
"encoding/json"
"errors"
"nexus-common/params"
"nexus-common/template"
g "nexus-libclient/globals"
"nexus-libclient/response"
"github.com/google/uuid"
)
......@@ -26,6 +26,6 @@ func TemplateCreate(vmID uuid.UUID, name, access string) (*template.TemplateSeri
}
return &tpl, nil
} else {
return nil, errors.New(resp.Status() + ": " + resp.String())
return nil, response.ErrorToMsg(resp)
}
}
package template
import (
"errors"
g "nexus-libclient/globals"
"nexus-libclient/response"
)
func TemplateDel(tplID string) error {
......@@ -16,6 +16,6 @@ func TemplateDel(tplID string) error {
if resp.IsSuccess() {
return nil
} else {
return errors.New(resp.Status() + ": " + resp.String())
return response.ErrorToMsg(resp)
}
}
......@@ -2,10 +2,10 @@ package template
import (
"encoding/json"
"errors"
"nexus-common/params"
"nexus-common/template"
g "nexus-libclient/globals"
"nexus-libclient/response"
)
func TemplateEdit(tplID string, p params.TplEdit) (*template.TemplateSerialized, error) {
......@@ -23,6 +23,6 @@ func TemplateEdit(tplID string, p params.TplEdit) (*template.TemplateSerialized,
}
return &tpl, nil
} else {
return nil, errors.New(resp.Status() + ": " + resp.String())
return nil, response.ErrorToMsg(resp)
}
}
......@@ -4,6 +4,7 @@ import (
"errors"
u "nexus-client/utils"
g "nexus-libclient/globals"
"nexus-libclient/response"
)
func TemplateExportDisk(tplID, outputFile string) error {
......@@ -18,7 +19,7 @@ func TemplateExportDisk(tplID, outputFile string) error {
return nil
} else {
errorMsg, _ := u.FileToString(outputFile)
return errors.New("Failed: " + resp.Status() + ": " + errorMsg)
return errors.New(response.ErrorToMsg(resp).Error() + ": " + errorMsg)
}
}
}
package template
import (
"errors"
t "nexus-common/template"
g "nexus-libclient/globals"
"nexus-libclient/response"
)
func GetTemplates() ([]t.TemplateSerialized, error) {
......@@ -22,7 +22,7 @@ func GetTemplates() ([]t.TemplateSerialized, error) {
}
return templates, nil
} else {
return nil, errors.New(resp.Status() + ": " + resp.String())
return nil, response.ErrorToMsg(resp)
}
}
......@@ -42,6 +42,6 @@ func GetTemplate(uuid string) (*t.TemplateSerialized, error) {
}
return template, nil
} else {
return nil, errors.New(resp.Status() + ": " + resp.String())
return nil, response.ErrorToMsg(resp)
}
}
package template
import (
"errors"
"nexus-common/params"
g "nexus-libclient/globals"
"nexus-libclient/response"
)
func UserCreate(p params.UserWithPwd) error {
......@@ -17,6 +17,6 @@ func UserCreate(p params.UserWithPwd) error {
if resp.IsSuccess() {
return nil
} else {
return errors.New(resp.Status() + ": " + resp.String())
return response.ErrorToMsg(resp)
}
}
package template
import (
g "nexus-libclient/globals"
"nexus-libclient/response"
)
func UserDelete(email string) error {
client := g.GetInstance().Client
host := g.GetInstance().Host
resp, err := client.R().Delete(host + "/users/" + email)
if err != nil {
return err
}
if resp.IsSuccess() {
return nil
} else {
return response.ErrorToMsg(resp)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment