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
Branches master
No related tags found
No related merge requests found
...@@ -2,10 +2,10 @@ package cmdUser ...@@ -2,10 +2,10 @@ package cmdUser
import ( import (
"encoding/csv" "encoding/csv"
"errors"
"io" "io"
u "nexus-client/utils" u "nexus-client/utils"
g "nexus-libclient/globals" g "nexus-libclient/globals"
libclient "nexus-libclient/user"
"os" "os"
) )
...@@ -70,27 +70,27 @@ func (cmd *Del) Run(args []string) int { ...@@ -70,27 +70,27 @@ func (cmd *Del) Run(args []string) int {
break break
} }
if err != nil { if err != nil {
u.PrintlnErr("FAILED reading " + err.Error()) u.PrintlnErr("Failed reading " + err.Error())
statusCode = 1 statusCode = 1
continue continue
} }
columnCount := len(columns) columnCount := len(columns)
if columnCount != 1 { 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 statusCode = 1
continue continue
} }
email := columns[0] email := columns[0]
if !u.IsEmail(email) { 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 statusCode = 1
continue continue
} }
if err := cmd.runRequest(email); err != nil { if err := libclient.UserDelete(email); err != nil {
u.PrintlnErr(err.Error()) u.PrintlnErr(err)
statusCode = 1 statusCode = 1
} else { } else {
u.Println("Successfully deleted user " + email) u.Println("Successfully deleted user " + email)
...@@ -102,7 +102,7 @@ func (cmd *Del) Run(args []string) int { ...@@ -102,7 +102,7 @@ func (cmd *Del) Run(args []string) int {
// Iterates through each user (email) to delete // Iterates through each user (email) to delete
for i := range args { for i := range args {
email := args[i] email := args[i]
if err := cmd.runRequest(email); err != nil { if err := libclient.UserDelete(email); err != nil {
u.PrintlnErr(err.Error()) u.PrintlnErr(err.Error())
statusCode = 1 statusCode = 1
} else { } else {
...@@ -113,19 +113,3 @@ func (cmd *Del) Run(args []string) int { ...@@ -113,19 +113,3 @@ func (cmd *Del) Run(args []string) int {
return statusCode 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 ...@@ -2,9 +2,9 @@ package login
import ( import (
"encoding/json" "encoding/json"
"errors"
"nexus-common/params" "nexus-common/params"
g "nexus-libclient/globals" g "nexus-libclient/globals"
"nexus-libclient/response"
) )
// Returns the token if the authentication was successful or an error if it wasn't. // 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) { ...@@ -25,22 +25,14 @@ func GetToken(user, pwd string) (string, error) {
type Response struct { type Response struct {
Token string Token string
} }
var response Response var r Response
err = json.Unmarshal(resp.Body(), &response) err = json.Unmarshal(resp.Body(), &r)
if err != nil { if err != nil {
return "", err return "", err
} }
return response.Token, nil return r.Token, nil
} else { } else {
type Response struct { return "", response.ErrorToMsg(resp)
Message string
}
var response Response
err = json.Unmarshal(resp.Body(), &response)
if err != nil {
return "", err
}
return "", errors.New(response.Message)
} }
} }
...@@ -64,7 +56,7 @@ func RefreshToken() (string, error) { ...@@ -64,7 +56,7 @@ func RefreshToken() (string, error) {
} }
return response.Token, nil return response.Token, nil
} else { } 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 ...@@ -2,10 +2,10 @@ package template
import ( import (
"encoding/json" "encoding/json"
"errors"
"nexus-common/params" "nexus-common/params"
"nexus-common/template" "nexus-common/template"
g "nexus-libclient/globals" g "nexus-libclient/globals"
"nexus-libclient/response"
"github.com/google/uuid" "github.com/google/uuid"
) )
...@@ -26,6 +26,6 @@ func TemplateCreate(vmID uuid.UUID, name, access string) (*template.TemplateSeri ...@@ -26,6 +26,6 @@ func TemplateCreate(vmID uuid.UUID, name, access string) (*template.TemplateSeri
} }
return &tpl, nil return &tpl, nil
} else { } else {
return nil, errors.New(resp.Status() + ": " + resp.String()) return nil, response.ErrorToMsg(resp)
} }
} }
package template package template
import ( import (
"errors"
g "nexus-libclient/globals" g "nexus-libclient/globals"
"nexus-libclient/response"
) )
func TemplateDel(tplID string) error { func TemplateDel(tplID string) error {
...@@ -16,6 +16,6 @@ func TemplateDel(tplID string) error { ...@@ -16,6 +16,6 @@ func TemplateDel(tplID string) error {
if resp.IsSuccess() { if resp.IsSuccess() {
return nil return nil
} else { } else {
return errors.New(resp.Status() + ": " + resp.String()) return response.ErrorToMsg(resp)
} }
} }
...@@ -2,10 +2,10 @@ package template ...@@ -2,10 +2,10 @@ package template
import ( import (
"encoding/json" "encoding/json"
"errors"
"nexus-common/params" "nexus-common/params"
"nexus-common/template" "nexus-common/template"
g "nexus-libclient/globals" g "nexus-libclient/globals"
"nexus-libclient/response"
) )
func TemplateEdit(tplID string, p params.TplEdit) (*template.TemplateSerialized, error) { func TemplateEdit(tplID string, p params.TplEdit) (*template.TemplateSerialized, error) {
...@@ -23,6 +23,6 @@ func TemplateEdit(tplID string, p params.TplEdit) (*template.TemplateSerialized, ...@@ -23,6 +23,6 @@ func TemplateEdit(tplID string, p params.TplEdit) (*template.TemplateSerialized,
} }
return &tpl, nil return &tpl, nil
} else { } else {
return nil, errors.New(resp.Status() + ": " + resp.String()) return nil, response.ErrorToMsg(resp)
} }
} }
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"errors" "errors"
u "nexus-client/utils" u "nexus-client/utils"
g "nexus-libclient/globals" g "nexus-libclient/globals"
"nexus-libclient/response"
) )
func TemplateExportDisk(tplID, outputFile string) error { func TemplateExportDisk(tplID, outputFile string) error {
...@@ -18,7 +19,7 @@ func TemplateExportDisk(tplID, outputFile string) error { ...@@ -18,7 +19,7 @@ func TemplateExportDisk(tplID, outputFile string) error {
return nil return nil
} else { } else {
errorMsg, _ := u.FileToString(outputFile) errorMsg, _ := u.FileToString(outputFile)
return errors.New("Failed: " + resp.Status() + ": " + errorMsg) return errors.New(response.ErrorToMsg(resp).Error() + ": " + errorMsg)
} }
} }
} }
package template package template
import ( import (
"errors"
t "nexus-common/template" t "nexus-common/template"
g "nexus-libclient/globals" g "nexus-libclient/globals"
"nexus-libclient/response"
) )
func GetTemplates() ([]t.TemplateSerialized, error) { func GetTemplates() ([]t.TemplateSerialized, error) {
...@@ -22,7 +22,7 @@ func GetTemplates() ([]t.TemplateSerialized, error) { ...@@ -22,7 +22,7 @@ func GetTemplates() ([]t.TemplateSerialized, error) {
} }
return templates, nil return templates, nil
} else { } 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) { ...@@ -42,6 +42,6 @@ func GetTemplate(uuid string) (*t.TemplateSerialized, error) {
} }
return template, nil return template, nil
} else { } else {
return nil, errors.New(resp.Status() + ": " + resp.String()) return nil, response.ErrorToMsg(resp)
} }
} }
package template package template
import ( import (
"errors"
"nexus-common/params" "nexus-common/params"
g "nexus-libclient/globals" g "nexus-libclient/globals"
"nexus-libclient/response"
) )
func UserCreate(p params.UserWithPwd) error { func UserCreate(p params.UserWithPwd) error {
...@@ -17,6 +17,6 @@ func UserCreate(p params.UserWithPwd) error { ...@@ -17,6 +17,6 @@ func UserCreate(p params.UserWithPwd) error {
if resp.IsSuccess() { if resp.IsSuccess() {
return nil return nil
} else { } 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