Exemplo n.º 1
0
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)
}
Exemplo n.º 2
0
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)
}
Exemplo n.º 3
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)
}
Exemplo n.º 4
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)
}
Exemplo n.º 5
0
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)
}
Exemplo n.º 6
0
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)
}
Exemplo n.º 7
0
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)
}
Exemplo n.º 8
0
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)
}
Exemplo n.º 9
0
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)
}
Exemplo n.º 10
0
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")
}
Exemplo n.º 11
0
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)
}
Exemplo n.º 12
0
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)
}
Exemplo n.º 13
0
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)
}
Exemplo n.º 14
0
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)
}
Exemplo n.º 15
0
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")
}
Exemplo n.º 16
0
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)
}
Exemplo n.º 17
0
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)
}
Exemplo n.º 18
0
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")
}
Exemplo n.º 19
0
func getConfig(res http.ResponseWriter, req *http.Request) {
	helpers.Respond(res, req, http.StatusOK, cfg)
}
Exemplo n.º 20
0
func sitemapV1(res http.ResponseWriter, req *http.Request) {
	helpers.Respond(res, req, http.StatusOK, routes["v1"])
}
Exemplo n.º 21
0
func whoAmI(res http.ResponseWriter, req *http.Request) {
	username := req.Header.Get(middleware.HeaderUserName)

	helpers.Respond(res, req, http.StatusOK, username)
}
Exemplo n.º 22
0
func getNodeVarsProviders(res http.ResponseWriter, req *http.Request) {
	helpers.Respond(res, req, http.StatusOK, np)
}