def create(): from app.models import Datacenter dc_attrs = dict( [x for x in request.json.items() if x[0] in Datacenter.FIELDS]) dc = Datacenter(**dc_attrs) # TODO: check permissions! try: dc.save() except Exception as e: return json_exception(e, 500) if "parent_id" in dc_attrs and dc_attrs["parent_id"]: return set_parent(dc._id) return json_response({"data": dc.to_dict()}, 201)
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 show(datacenter_id=None): from app.models import Datacenter if datacenter_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} datacenters = Datacenter.find(query) else: datacenter_id = resolve_id(datacenter_id) datacenters = Datacenter.find( {"$or": [{ "_id": datacenter_id }, { "name": datacenter_id }]}) if datacenters.count() == 0: return json_response({"errors": ["Datacenter not found"]}, 404) data = paginated_data(datacenters.sort("name")) return json_response(data)
models = ["Server MX", "Server MY", "Server DL380", "Server x440", "Server x460"] datacenters = list() def get_random_name(names_list, size=1): return names_list[random.randrange(0, len(names_list))] def serial_generator(size=6, chars=string.ascii_uppercase + string.digits): return "".join(random.choice(chars) for _ in range(size)) for city in cities: datacenter = Datacenter() datacenter.name = "DC %s" % city datacenter.address = city datacenters.append(datacenter) db.session.add(datacenter) log.info(datacenter) try: db.session.commit() for num in range(1, DC_RACK_MAX): rack = Rack() rack.num = num rack.datacenter = datacenter db.session.add(rack) except Exception as e: log.error("Creating Datacenter: %s", e)
from app.models import Datacenter Datacenter.destroy_all() dc1 = Datacenter(name="dc1") dc11 = Datacenter(name="dc1.1") dc1.save() dc11.save() dc1.add_child(dc11) dc12 = Datacenter(name="dc1.2") dc121 = Datacenter(name="dc1.2.1") dc122 = Datacenter(name="dc1.2.2") dc1.add_child(dc12) dc12.save() dc121.save() dc122.save() dc121.set_parent(dc12) dc122.set_parent(dc12)
"Server MX", "Server MY", "Server DL380", "Server x440", "Server x460" ] datacenters = list() def get_random_name(names_list, size=1): return names_list[random.randrange(0, len(names_list))] def serial_generator(size=6, chars=string.ascii_uppercase + string.digits): return "".join(random.choice(chars) for _ in range(size)) for city in cities: datacenter = Datacenter() datacenter.name = "DC %s" % city datacenter.address = city datacenters.append(datacenter) db.session.add(datacenter) log.info(datacenter) try: db.session.commit() for num in range(1, DC_RACK_MAX): rack = Rack() rack.num = num rack.datacenter = datacenter db.session.add(rack) except Exception as e: log.error("Creating Datacenter: %s", e)
print "Setting children" for group_name, group_desc in data["Groups"].items(): child_ids = [] for child in group_desc["children"]: child_ids.append(group_cache[child]._id) if len(child_ids) > 0: g = group_cache[group_name] for cid in child_ids: g.add_child(cid) i += 1 if i % 100 == 0: print i print i print "Processing datacenters" Datacenter.destroy_all() datacenter_cache = {} for datacenter_name, datacenter_desc in data["Datacenters"].items(): dc = Datacenter(name=datacenter_name, human_readable=datacenter_desc["golem_name"]) dc.save() datacenter_cache[datacenter_name] = dc print "Setting parents" for datacenter_name, datacenter_desc in data["Datacenters"].items(): if datacenter_desc["parent"] is not None: dc = datacenter_cache[datacenter_name] parent = datacenter_cache[datacenter_desc["parent"]] dc.set_parent(parent) print "Processing hosts" Host.destroy_all()
def _get_dc_by_id(dc_id): from app.models import Datacenter dc_id = resolve_id(dc_id) return Datacenter.find_one({"$or": [{"_id": dc_id}, {"name": dc_id}]})