def show(group_id=None): from app.models import Group if group_id is None: query = {} if "_filter" in request.values: name_filter = request.values["_filter"] if len(name_filter) >= 2: query["name"] = {"$regex": "^%s" % name_filter} groups = Group.find(query) else: group_id = resolve_id(group_id) groups = Group.find({"$or": [{"_id": group_id}, {"name": group_id}]}) data = paginated_data(groups.sort("name")) return json_response(data)
def get_executer_data(query, recursive=False, include_unattached=False): from app.models import Project, Datacenter, Group, Host host_fields = list(Host.FIELDS) group_fields = list(Group.FIELDS) if recursive: host_fields += ["all_tags", "all_custom_fields"] group_fields += ["all_tags", "all_custom_fields"] projects = Project.find(query) projects = cursor_to_list(projects) project_ids = [x["_id"] for x in projects] groups = Group.find({"project_id": {"$in": project_ids}}) groups = cursor_to_list(groups, fields=group_fields) group_ids = [x["_id"] for x in groups] if include_unattached: hosts = Host.find({}) else: hosts = Host.find({"group_id": {"$in": group_ids}}) hosts = cursor_to_list(hosts, fields=host_fields) datacenters = Datacenter.find({}) datacenters = cursor_to_list(datacenters) return { "datacenters": datacenters, "projects": projects, "groups": groups, "hosts": hosts }
def executer_data(): from app.models import Project, Datacenter, Group, Host if "projects" not in request.values: return json_response( { "errors": ["'projects' parameter is required for executer_data handler"] }, 400) project_names = request.values["projects"].split(",") projects = Project.find({"name": {"$in": project_names}}) projects = cursor_to_list(projects) project_ids = [x["_id"] for x in projects] groups = Group.find({"project_id": {"$in": project_ids}}) groups = cursor_to_list(groups) group_ids = [x["_id"] for x in groups] hosts = Host.find({"group_id": {"$in": group_ids}}) hosts = cursor_to_list(hosts) datacenters = Datacenter.find({}) datacenters = cursor_to_list(datacenters) results = { "datacenters": datacenters, "projects": projects, "groups": groups, "hosts": hosts } return json_response({"data": results})
def _get_hosts(group_names=None, tags=None): from app.models import Group if group_names is None: tag_query = [{"tags": x} for x in tags] groups = Group.find({"$or": tag_query}) else: groups = Group.find({"name": {"$in": group_names}}) all_hosts = set() for group in groups: all_hosts = all_hosts.union(group.all_hosts.all()) if tags is not None: hosts = [] for host in all_hosts: for tag in tags: if tag in host.all_tags: hosts.append(host) else: hosts = list(all_hosts) return hosts
def test_mass_delete(self): g1 = Group(name="g1", project_id=self.project1._id) g1.save() g2 = Group(name="g2", project_id=self.project1._id) g2.save() g3 = Group(name="g3", project_id=self.project1._id) g3.save() g4 = Group(name="g4", project_id=self.project1._id) g4.save() g1.add_child(g2) g2.add_child(g3) g3.add_child(g4) group_ids = [str(g2._id), str(g3._id)] r = self.post_json("/api/v1/groups/mass_delete", {"group_ids": group_ids}) self.assertEqual(200, r.status_code) deleted_groups = Group.find({"_id": {"$in": [g2._id, g3._id]}}) self.assertEqual(0, deleted_groups.count()) g1 = Group.find_one({"_id": g1._id}) self.assertEqual(0, len(g1.child_ids)) g4 = Group.find_one({"_id": g4._id}) self.assertEqual(0, len(g4.parent_ids))