def get_record_detail(record_id): from api.lib.cmdb.ci import CIManager record = OperationRecord.get_by_id(record_id) or abort( 404, "Record <{0}> is not found".format(record_id)) username = UserCache.get(record.uid).nickname or UserCache.get( record.uid).username timestamp = record.created_at.strftime("%Y-%m-%d %H:%M:%S") attr_history = AttributeHistory.get_By(record_id=record_id, to_dict=False) rel_history = CIRelationHistory.get_by(record_id=record_id, to_dict=False) attr_dict, rel_dict = dict(), {"add": [], "delete": []} for attr_h in attr_history: attr_dict[AttributeCache.get(attr_h.attr_id).alias] = dict( old=attr_h.old, new=attr_h.new, operate_type=attr_h.operate_type) for rel_h in rel_history: first = CIManager.get_ci_by_id(rel_h.first_ci_id) second = CIManager.get_ci_by_id(rel_h.second_ci_id) rel_dict[rel_h.operate_type].append( (first, RelationTypeCache.get(rel_h.relation_type_id).name, second)) return username, timestamp, attr_dict, rel_dict
def put(self, ci_id): params = request.values unique_name = params.keys()[0] unique_value = params.values()[0] CIManager.update_unique_value(ci_id, unique_name, unique_value) return self.jsonify(ci_id=ci_id)
def init_ci_with_type(ci_types): force_add_user() cis = [] manager = CIManager() for ci_type in ci_types: attrs = CITypeAttributeManager.get_attributes_by_type_id(ci_type.id) ci_id = manager.add(ci_type.name, **fake_attr_value(attrs[0])) cis.append(manager.get_ci_by_id_from_db(ci_id)) return cis
def get(self, ci_id): fields = handle_arg_list(request.values.get("fields", "")) ret_key = request.values.get("ret_key", RetKey.NAME) if ret_key not in (RetKey.NAME, RetKey.ALIAS, RetKey.ID): ret_key = RetKey.NAME manager = CIManager() ci = manager.get_ci_by_id_from_db(ci_id, ret_key=ret_key, fields=fields) return self.jsonify(ci_id=ci_id, ci=ci)
def post(self): ci_type = request.values.get("ci_type") _no_attribute_policy = request.values.get("_no_attribute_policy", ExistPolicy.IGNORE) ci_dict = self._wrap_ci_dict() manager = CIManager() current_app.logger.debug(ci_dict) ci_id = manager.add(ci_type, exist_policy=ExistPolicy.REJECT, _no_attribute_policy=_no_attribute_policy, **ci_dict) return self.jsonify(ci_id=ci_id)
def init_ci(num=1): # store ci need has user force_add_user() ci_type = init_ci_types(1)[0] attrs = CITypeAttributeManager.get_attributes_by_type_id(ci_type.id) manager = CIManager() result = [] for i in range(num): ci_id = manager.add(ci_type.name, **fake_attr_value(attrs[0])) result.append(manager.get_ci_by_id_from_db(ci_id)) return result
def put(self, ci_id=None): args = request.values ci_type = args.get("ci_type") _no_attribute_policy = args.get("_no_attribute_policy", ExistPolicy.IGNORE) ci_dict = self._wrap_ci_dict() manager = CIManager() if ci_id is not None: manager.update(ci_id, **ci_dict) else: ci_id = manager.add(ci_type, exist_policy=ExistPolicy.REPLACE, _no_attribute_policy=_no_attribute_policy, **ci_dict) return self.jsonify(ci_id=ci_id)
def post(self, ci_type, unique): if not unique or not ci_type: return self.jsonify(message="error") msg, cmd = CIManager().add_heartbeat(ci_type, unique) return self.jsonify(message=msg, cmd=cmd)
def get(self, type_id): fields = handle_arg_list(request.values.get("fields", "")) ret_key = request.values.get("ret_key", RetKey.NAME) if ret_key not in (RetKey.NAME, RetKey.ALIAS, RetKey.ID): ret_key = RetKey.NAME page = get_page(request.values.get("page", 1)) count = get_page_size(request.values.get("count")) manager = CIManager() res = manager.get_cis_by_type(type_id, ret_key=ret_key, fields=fields, page=page, per_page=count) return self.jsonify(type_id=type_id, numfound=res[0], total=len(res[2]), page=res[1], cis=res[2])
def get(self): page = get_page(request.values.get("page", 1)) ci_type = request.values.get("ci_type", "").strip() try: type_id = CITypeCache.get(ci_type).type_id except AttributeError: return self.jsonify(numfound=0, result=[]) agent_status = request.values.get("agent_status") if agent_status: agent_status = int(agent_status) numfound, result = CIManager.get_heartbeat(page, type_id, agent_status=agent_status) return self.jsonify(numfound=numfound, result=result)
def test_create_ci(session, client): ci_type = init_ci_types(1)[0] attrs = CITypeAttributeManager.get_attributes_by_type_id(ci_type.id) url = "/api/v0.1/ci" fake_value = fake_attr_value(attrs[0]) payload = {"ci_type": ci_type.id, **fake_value} resp = client.post(url, json=payload) assert resp.status_code == 200 assert resp.json["ci_id"] ci_id = resp.json["ci_id"] ci = CIManager().get_ci_by_id_from_db(ci_id) assert ci[attrs[0]["name"]] == fake_value[attrs[0]['name']]
def test_update_ci(session, client): ci = init_ci(1)[0] ci_id = ci.get("ci_id") ci_type = CITypeManager.get_ci_types(ci.get("ci_type"))[0] attrs = CITypeAttributeManager.get_attributes_by_type_id(ci_type["id"]) url = "/api/v0.1/ci/{}".format(ci_id) fake_value = fake_attr_value(attrs[0]) payload = {**fake_value} resp = client.put(url, json=payload) assert resp.status_code == 200 assert resp.json["ci_id"] == ci_id ci = CIManager().get_ci_by_id_from_db(ci_id) assert ci[attrs[0]['name']] == fake_value[attrs[0]['name']]
def search(self): numfound, ci_ids = self._query_build_raw() ci_ids = list(map(str, ci_ids)) _fl = self._fl_build() if self.facet_field and numfound: facet = self._facet_build() else: facet = dict() response, counter = [], {} if ci_ids: response = CIManager.get_cis_by_ids(ci_ids, ret_key=self.ret_key, fields=_fl) for res in response: ci_type = res.get("ci_type") if ci_type not in counter.keys(): counter[ci_type] = 0 counter[ci_type] += 1 total = len(response) return response, counter, total, self.page, numfound, facet
def delete(self, ci_id): manager = CIManager() manager.delete(ci_id) return self.jsonify(message="ok")