func addRoleVars(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) role, err := ri.Get(vars["role"]) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } var roleVars map[string]interface{} err = helpers.ParseBodyAsMap(req, &roleVars) if err != nil { helpers.Respond(res, req, http.StatusBadRequest, err.Error()) return } v := entities.VarBucket{ Prio: 0, Vars: roleVars, Name: "native", } role.Vars.AddOrReplaceBucket(v) err = ri.Save(role) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } helpers.Respond(res, req, http.StatusCreated, role) }
func triggerNodeVarsProviders(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node, err := ni.Get(vars["node"]) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } buckets, err := np.GetVars(node.Name) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } for _, bucket := range buckets { node.Vars.AddOrReplaceBucket(bucket) } err = ni.Save(node) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } helpers.Respond(res, req, http.StatusCreated, node) }
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) }
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) }
func ansibleRenderer(res http.ResponseWriter, req *http.Request) { out, err := NewAnsibleInventory() if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } helpers.Respond(res, req, http.StatusOK, out) }
func listNodes(res http.ResponseWriter, req *http.Request) { out, err := ni.List() if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } helpers.Respond(res, req, http.StatusOK, out) }
func listRoles(res http.ResponseWriter, req *http.Request) { out, err := ri.List() if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } helpers.Respond(res, req, http.StatusOK, out) }
func getNode(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node, err := ni.Get(vars["node"]) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } helpers.Respond(res, req, http.StatusOK, node) }
func getRole(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) role, err := ri.Get(vars["role"]) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } helpers.Respond(res, req, http.StatusOK, role) }
func delNode(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node, err := ni.Get(vars["node"]) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } err = ni.Delete(node) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } helpers.Respond(res, req, http.StatusNoContent, "deleted") }
func treeRenderer(res http.ResponseWriter, req *http.Request) { rootRole := "" root := req.URL.Query()["role"] if len(root) > 0 { rootRole = root[0] } out := &treeNode{} role, err := ri.Get(rootRole) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } addToTree(role, out) helpers.Respond(res, req, http.StatusCreated, out) }
func addRole(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) role, err := ri.Get(vars["role"]) if err == nil { helpers.Respond(res, req, http.StatusConflict, "already exists") return } role = entities.Role{Name: vars["role"]} err = ri.Save(role) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } helpers.Respond(res, req, http.StatusCreated, role) }
func addNode(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node, err := ni.Get(vars["node"]) if err == nil { helpers.Respond(res, req, http.StatusConflict, "already exists") return } node = entities.Node{Name: vars["node"]} err = ni.Save(node) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } helpers.Respond(res, req, http.StatusCreated, node) }
func replaceNodeVars(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node, err := ni.Get(vars["node"]) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } var nodeVars map[string]interface{} err = helpers.ParseBodyAsMap(req, &nodeVars) if err != nil { helpers.Respond(res, req, http.StatusBadRequest, err.Error()) return } vb := entities.VarBucket{ Prio: 0, Name: "native", } for _, b := range node.Vars { if b.Name == "native" { vb = b } } for key, val := range nodeVars { vb.Vars[key] = val } node.Vars.AddOrReplaceBucket(vb) err = ni.Save(node) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } helpers.Respond(res, req, http.StatusOK, node) }
func delRoleParent(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) role, err := ri.Get(vars["role"]) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } parent, err := ri.Get(role.Parent) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } err = ri.UnlinkChild(&parent, &role) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } helpers.Respond(res, req, http.StatusOK, "unlinked") }
func addRoleParent(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) role, err := ri.Get(vars["role"]) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } parent, err := ri.Get(vars["parent"]) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } err = ri.LinkParent(&role, &parent) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } helpers.Respond(res, req, http.StatusOK, role.Parent) }
func getNodeVar(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) node, err := ni.Get(vars["node"]) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } nvs := ni.GetVars(node) var out interface{} for _, nv := range nvs { if nv.Key == vars["var"] { out = nv.Value helpers.Respond(res, req, http.StatusOK, out) return } } helpers.Respond(res, req, http.StatusNotFound, out) }
func delRole(res http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) role, err := ri.Get(vars["role"]) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } // TODO: the relations must be sanitized on store level and must not be // done via handlers. Also, if a role is deleted, related nodes must be // unlinked. if role.Parent != "" { parent, err := ri.Get(role.Parent) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } err = ri.UnlinkChild(&parent, &role) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } } for _, childName := range role.Children { child, err := ri.Get(childName) if err != nil { helpers.Respond(res, req, http.StatusNotFound, err.Error()) return } err = ri.UnlinkChild(&role, &child) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } } err = ri.Delete(role) if err != nil { helpers.Respond(res, req, http.StatusInternalServerError, err.Error()) return } helpers.Respond(res, req, http.StatusOK, "deleted") }
func getConfig(res http.ResponseWriter, req *http.Request) { helpers.Respond(res, req, http.StatusOK, cfg) }
func sitemapV1(res http.ResponseWriter, req *http.Request) { helpers.Respond(res, req, http.StatusOK, routes["v1"]) }
func whoAmI(res http.ResponseWriter, req *http.Request) { username := req.Header.Get(middleware.HeaderUserName) helpers.Respond(res, req, http.StatusOK, username) }
func getNodeVarsProviders(res http.ResponseWriter, req *http.Request) { helpers.Respond(res, req, http.StatusOK, np) }