示例#1
0
func unlinkNodeFromRole(res http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	node := entities.Node{Name: vars["node"]}
	node, err := ni.Get(vars["node"])
	if err != nil {
		helpers.Respond(res, req, http.StatusNotFound, err.Error())
		return
	}

	role, err := ri.Get(vars["role"])
	if err != nil {
		helpers.Respond(res, req, http.StatusNotFound, err.Error())
		return
	}

	for i, roleName := range node.Roles {
		if roleName == role.Name {
			node.Roles = append(node.Roles[:i], node.Roles[i+1:]...)
			break
		}
	}

	err = ni.Save(node)
	if err != nil {
		helpers.Respond(res, req, http.StatusInternalServerError, err.Error())
		return
	}

	helpers.Respond(res, req, http.StatusOK, node)
}
示例#2
0
func linkNodeToRole(res http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	node := entities.Node{Name: vars["node"]}
	node, err := ni.Get(vars["node"])
	if err != nil {
		helpers.Respond(res, req, http.StatusNotFound, err.Error())
		return
	}

	role, err := ri.Get(vars["role"])
	if err != nil {
		helpers.Respond(res, req, http.StatusNotFound, err.Error())
		return
	}

	for _, roleName := range node.Roles {
		if roleName == role.Name {
			helpers.Respond(res, req, http.StatusNotModified, node)
			return
		}
	}

	node.Roles = append(node.Roles, role.Name)

	err = ni.Save(node)
	if err != nil {
		helpers.Respond(res, req, http.StatusInternalServerError, err.Error())
		return
	}

	helpers.Respond(res, req, http.StatusCreated, node)
}