Beispiel #1
0
 def insert_many(self, model, payload: list):
     ret = self.base_connection[model].insert_many(payload)
     resp_arr = []
     for obj in ret.inserted_ids:
         resp_arr.append({"object_id": f"{obj}"})
     result = ResponseBasic(status="success", result=resp_arr).dict()
     return result
Beispiel #2
0
 def insert_one(self, model, payload: dict):
     ret = self.base_connection[model].insert_one(payload)
     result = ResponseBasic(status="success",
                            result=[{
                                "object_id": f"{ret.inserted_id}"
                            }]).dict()
     return result
Beispiel #3
0
 def find(self, model: str):
     ret = self.base_connection[model].find()
     temp_json_result = dumps(ret)
     loaded_result = json.loads(temp_json_result)
     final_result = self.egress_parse_object_id(loaded_result)
     if final_result is None:
         final_result = []
     result = ResponseBasic(status="success", result=final_result).dict()
     return result
Beispiel #4
0
 def delete_file(self, payload: Dict[str, str]):
     template_path = self.path_lookup[payload["route_type"]][
         "path"] + payload["name"] + self.path_lookup[
             payload["route_type"]]["extn"]
     os.remove(template_path)
     resultdata = ResponseBasic(status="success",
                                result=[{
                                    "deleted": payload["name"]
                                }]).dict()
     return resultdata
Beispiel #5
0
 def query(self, model: str, payload: dict):
     """ wrapper for find with filtering """
     cleaned_data = self.ingress_parse_object_id(payload)
     ret = self.base_connection[model].find(cleaned_data)
     temp_json_result = dumps(ret)
     loaded_result = json.loads(temp_json_result)
     final_result = self.egress_parse_object_id(loaded_result)
     if final_result is None or len(loaded_result) < 1:
         final_result = []
     result = ResponseBasic(status="success", result=final_result).dict()
     return result
Beispiel #6
0
 def retrieve_file(self, payload: Dict[str, str]):
     template_path = self.path_lookup[payload["route_type"]][
         "path"] + payload["name"] + self.path_lookup[
             payload["route_type"]]["extn"]
     result = None
     with open(template_path, "r") as file:
         result = file.read()
     raw_base = base64.b64encode(result.encode('utf-8'))
     resultdata = ResponseBasic(status="success",
                                result=[{
                                    "base64_payload": raw_base
                                }]).dict()
     return resultdata
Beispiel #7
0
 def create_file(self, payload: Dict[str, str]):
     raw_base = base64.b64decode(payload["base64_payload"]).decode('utf-8')
     template_path = self.path_lookup[payload["route_type"]][
         "path"] + payload["name"] + self.path_lookup[
             payload["route_type"]]["extn"]
     # if os.path.exists(template_path):
     #     raise CMDBOSSFileExists
     with open(template_path, "w") as file:
         file.write(raw_base)
     resultdata = ResponseBasic(status="success",
                                result=[{
                                    "created": payload["name"]
                                }]).dict()
     return resultdata
Beispiel #8
0
 def update(self, model_instance_data: dict, object_id: str, path: str):
     final_result = []
     model_name = self.get_model_name(path)
     set_data = model_instance_data.dict()
     new_data = {"$set": set_data}
     query = {}
     query["filter"] = {}
     if object_id:
         query["filter"]["object_id"] = object_id
     cleaned_data = self.ingress_parse_object_id(query["filter"])
     ret = self.base_connection[model_name].update_many(
         cleaned_data, new_data)
     if ret.modified_count >= 1:
         final_result = [{"updated_object_count": ret.modified_count}]
     result = ResponseBasic(status="success", result=final_result).dict()
     self.run_hooks(operation="update", model=model_name, data=result)
     return result
Beispiel #9
0
 def delete(self, query: dict, object_id: str, path: str):
     final_result = []
     model_name = self.get_model_name(path)
     if query.get("filter", False):
         cleaned_data = self.ingress_parse_object_id(query["filter"])
         ret = self.base_connection[model_name].delete_many(cleaned_data)
         if ret.deleted_count >= 1:
             final_result = [{"deleted_object_count": ret.deleted_count}]
     if object_id:
         query["filter"] = {}
         query["filter"]["object_id"] = object_id
         cleaned_data = self.ingress_parse_object_id(query["filter"])
         ret = self.base_connection[model_name].delete_many(cleaned_data)
         if ret.deleted_count >= 1:
             final_result = [{"deleted_object_count": ret.deleted_count}]
     result = ResponseBasic(status="success", result=final_result).dict()
     self.run_hooks(operation="delete", model=model_name, data=result)
     return result
Beispiel #10
0
 def retrieve_files(self, payload: Dict[str, str]):
     path = self.path_lookup[payload["route_type"]]["path"]
     strip_exten = self.path_lookup[payload["route_type"]]["extn"]
     files = []
     fileresult = []
     for r, d, f in os.walk(path):
         for file in f:
             file.strip(path)
             files.append(os.path.join(r, file))
     if len(files) > 0:
         for f in files:
             if "__init__" not in f:
                 if "__pycache__" not in f:
                     if strip_exten:
                         if strip_exten in f:
                             ftmpfile = f.replace(strip_exten, '')
                             fileresult.append(ftmpfile.replace(path, ''))
     resultdata = ResponseBasic(status="success", result=fileresult).dict()
     return resultdata