Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
atrubachev committed Apr 20, 2020
1 parent 19cdfa7 commit 4ecbf32
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions providers/dell/idrac8/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

// Return bool value if the role is valid.
func isRoleValid(role string) bool {

validRoles := []string{"admin", "user"}
for _, v := range validRoles {
if role == v {
Expand All @@ -22,22 +21,18 @@ func isRoleValid(role string) bool {
}

// Return bool value if the role is valid.
func (i *IDrac8) validateUserCfg(cfgUsers []*cfgresources.User) (err error) {

func (i *IDrac8) validateUserCfg(cfgUsers []*cfgresources.User) error {
for _, cfgUser := range cfgUsers {
if cfgUser.Name == "" {
msg := "User resource expects parameter: Name."
return errors.New(msg)
return errors.New("user resource expects parameter: Name")
}

if cfgUser.Password == "" {
msg := "User resource expects parameter: Password."
return errors.New(msg)
return errors.New("user resource expects parameter: Password")
}

if !isRoleValid(cfgUser.Role) {
msg := "User resource expects parameter Role to be one of 'admin', 'user'"
return errors.New(msg)
return errors.New("user resource expects parameter Role to be one of 'admin', 'user'")
}
}

Expand All @@ -48,7 +43,6 @@ func (i *IDrac8) validateUserCfg(cfgUsers []*cfgresources.User) (err error) {
// this function returns an empty user slot that can be used for a new user account
func getEmptyUserSlot(idracUsers UserInfo) (userID int, user User, err error) {
for userID, user := range idracUsers {

if userID == 1 {
continue
}
Expand All @@ -58,12 +52,11 @@ func getEmptyUserSlot(idracUsers UserInfo) (userID int, user User, err error) {
}
}

return 0, user, errors.New("All user account slots in use, remove an account before adding a new one")
return 0, user, errors.New("all user account slots in use, remove an account before adding a new one")
}

// checks if a user is present in a given list
func userInIdrac(user string, usersInfo UserInfo) (userID int, userInfo User, exists bool) {

for userID, userInfo := range usersInfo {
if userInfo.UserName == user {
return userID, userInfo, true
Expand All @@ -74,23 +67,24 @@ func userInIdrac(user string, usersInfo UserInfo) (userID int, userInfo User, ex
}

// PUTs user config
func (i *IDrac8) putUser(userID int, user User) (err error) {

func (i *IDrac8) putUser(userID int, user User) error {
idracPayload := make(map[string]User)
idracPayload["iDRAC.Users"] = user

payload, err := json.Marshal(idracPayload)
if err != nil {
msg := fmt.Sprintf("Error unmarshalling User payload: %s", err)
return errors.New(msg)
return fmt.Errorf("error unmarshalling User payload: %w", err)
}

endpoint := fmt.Sprintf("sysmgmt/2012/server/configgroup/iDRAC.Users.%d", userID)
statusCode, _, err := i.put(endpoint, payload)
if err != nil || statusCode != 200 {
msg := fmt.Sprintf("PUT request to set User config returned error, return code: %d", statusCode)
return errors.New(msg)
if err != nil {
return fmt.Errorf("PUT request to set User config returned error: %w", err)
}

if statusCode != 200 {
return fmt.Errorf("PUT request to set User config returned status code: %d", statusCode)
}

return err
return nil
}

0 comments on commit 4ecbf32

Please sign in to comment.