Skip to content

Commit

Permalink
refactor(api) simplify validateParent
Browse files Browse the repository at this point in the history
  • Loading branch information
helderbetiol committed Jun 13, 2024
1 parent 5bf942a commit cb0b490
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 87 deletions.
108 changes: 32 additions & 76 deletions API/models/validateEntity.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,114 +84,70 @@ func validateParent(ent string, entNum int, t map[string]interface{}) (map[strin
}
return nil, &u.Error{Type: u.ErrBadFormat, Message: "ParentID is not valid"}
}
req := bson.M{"id": t["parentId"].(string)}

parent := map[string]interface{}{"parent": ""}
// Anyone can have a stray parent
stray, _ := GetObject(req, "stray_object", u.RequestFilters{}, nil)
if stray != nil {
parent["parent"] = "rack"
parent["domain"] = stray["domain"]
parent["id"] = stray["id"]
if parent := getParent([]string{"stray_object"}, t); parent != nil {
return parent, nil
}

// If not, search specific possibilities
switch entNum {
case u.DEVICE:
x, _ := GetObject(req, "rack", u.RequestFilters{}, nil)
if x != nil {
parent["parent"] = "rack"
parent["domain"] = x["domain"]
parent["id"] = x["id"]
if err := validateDeviceSlotExists(t, x); err != nil {
return nil, err
}
return parent, nil
}

y, _ := GetObject(req, "device", u.RequestFilters{}, nil)
if y != nil {
parent["parent"] = "device"
parent["domain"] = y["domain"]
parent["id"] = y["id"]
if err := validateDeviceSlotExists(t, y); err != nil {
if parent := getParent([]string{"rack", "device"}, t); parent != nil {
if err := validateDeviceSlotExists(t, parent); err != nil {
return nil, err
}
delete(parent, "attributes") // only used to check slots
return parent, nil
}

return nil, &u.Error{Type: u.ErrInvalidValue,
Message: "ParentID should correspond to existing rack or device ID"}

case u.GROUP:
x, _ := GetObject(req, "rack", u.RequestFilters{}, nil)
if x != nil {
parent["parent"] = "rack"
parent["domain"] = x["domain"]
parent["id"] = x["id"]
return parent, nil
}

y, _ := GetObject(req, "room", u.RequestFilters{}, nil)
if y != nil {
parent["parent"] = "room"
parent["domain"] = y["domain"]
parent["id"] = y["id"]
if parent := getParent([]string{"rack", "room"}, t); parent != nil {
return parent, nil
}

return nil, &u.Error{Type: u.ErrInvalidValue,
Message: "Group parent should correspond to existing rack or room"}

case u.GENERIC:
x, _ := GetObject(req, "room", u.RequestFilters{}, nil)
if x != nil {
parent["parent"] = "room"
parent["domain"] = x["domain"]
parent["id"] = x["id"]
return parent, nil
}

return nil, &u.Error{Type: u.ErrInvalidValue,
Message: "ParentID should correspond to existing room ID"}

case u.VIRTUALOBJ:
x, _ := GetObject(req, "device", u.RequestFilters{}, nil)
if x != nil {
parent["parent"] = "device"
parent["domain"] = x["domain"]
parent["id"] = x["id"]
return parent, nil
}

y, _ := GetObject(req, "virtual_obj", u.RequestFilters{}, nil)
if y != nil {
parent["parent"] = "virtual_obj"
parent["domain"] = y["domain"]
parent["id"] = y["id"]
if parent := getParent([]string{"device", "virtual_obj"}, t); parent != nil {
return parent, nil
}

return nil, &u.Error{Type: u.ErrInvalidValue,
Message: "Group parent should correspond to existing device or virtual_obj"}
default:
parentInt := u.GetParentOfEntityByInt(entNum)
parentStr := u.EntityToString(parentInt)

p, err := GetObject(req, parentStr, u.RequestFilters{}, nil)
if len(p) > 0 {
parent["parent"] = parentStr
parent["domain"] = p["domain"]
parent["id"] = p["id"]
parentStr := u.EntityToString(u.GetParentOfEntityByInt(entNum))
if parent := getParent([]string{parentStr}, t); parent != nil {
return parent, nil
} else if err != nil {
println("ENTITY VALUE: ", ent)
println("We got Parent: ", parent, " with ID:", t["parentId"].(string))
return nil, &u.Error{Type: u.ErrInvalidValue,
Message: fmt.Sprintf("ParentID should correspond to existing %s ID", parentStr)}
}

return nil, &u.Error{Type: u.ErrInvalidValue,
Message: fmt.Sprintf("ParentID should correspond to existing %s ID", parentStr)}

}
}

func getParent(parentEntities []string, t map[string]any) map[string]any {
parent := map[string]any{"parent": ""}
req := bson.M{"id": t["parentId"].(string)}
for _, parentEnt := range parentEntities {
obj, _ := GetObject(req, parentEnt, u.RequestFilters{}, nil)
if obj != nil {
parent["parent"] = parentEnt
parent["domain"] = obj["domain"]
parent["id"] = obj["id"]
if t["category"] == "device" {
// need attributes to check slots
parent["attributes"] = obj["attributes"]
}
return parent
}
}
return nil, nil
return nil
}

func validateDeviceSlotExists(deviceData map[string]interface{}, parentData map[string]interface{}) *u.Error {
Expand Down
21 changes: 10 additions & 11 deletions CLI/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,19 +720,18 @@ func updateVirtualLink(path string, attr string, value string) (map[string]any,
} else if attr == "vlinks-" {
if !hasVlinks {
return nil, fmt.Errorf("no vlinks defined for this object")
} else {
found := false
for i, vlink := range vlinks {
if vlink == value {
vlinks = append(vlinks[:i], vlinks[i+1:]...)
found = true
break
}
}
if !found {
return nil, fmt.Errorf("vlink to remove not found")
}
found := false
for i, vlink := range vlinks {
if vlink == value {
vlinks = append(vlinks[:i], vlinks[i+1:]...)
found = true
break
}
}
if !found {
return nil, fmt.Errorf("vlink to remove not found")
}
} else {
return nil, fmt.Errorf("invalid vlink update command")
}
Expand Down

0 comments on commit cb0b490

Please sign in to comment.