def createKey( user_email="", key_name="", key_value="", env_id: uuid.UUID = uuid.uuid4(), enterprise_id: uuid.UUID = uuid.uuid4(), project_id: uuid.UUID = uuid.uuid4() ): try: user = getUser(user_email) enterprise = getEnterpriseModel(user=user, enterprise_id=enterprise_id) if enterprise["error"]: return enterprise enterprise = enterprise["message"] project = getProjectModel(user=user, enterprise=enterprise, project_id=project_id) if project["error"]: return project project = project["message"] env = getEnvModel(user=user, enterprise=enterprise, project=project, env_id=env_id) if env["error"]: return env env = env["message"] ciphered_key_value = __cipherData(key_value) key = KeyModel(name = key_name, value=ciphered_key_value) env.key_list.append(key) env.save() return {"message": "Key created correctly", "error": False} except Exception as error: return {"message": f"Some error has happend in createKey function in key_consumer: {error}", "error": True}
def deleteEnterprise(user_email, enterprise_id): try: user = getUser(user_email) enterprises_ids = user.enterprises if (enterprise_id in enterprises_ids): enterprise = EnterpriseModel.objects.filter( id=enterprise_id).delete() enterprise.save() user.enterprises.remove(enterprise_id) user.save() return { "message": "Enterprise deleted correctly", "error": False, "data": None } return { "message": "Given enterprise id does not exist", "error": True, "data": None } except Exception as error: print( f"Some error has happend deleting some enterprise at enterprise_consumer.py. error: {error}" ) return { "message": f"Some error has happend: {error}", "error": True, "data": None }
def getKeys( user_email="", enterprise_id: uuid.UUID = uuid.uuid4(), project_id: uuid.UUID = uuid.uuid4(), env_id: uuid.UUID = uuid.uuid4(), user: UserModel = None, enterprise: EnterpriseModel = None, project: ProjectModel = None, ): try: if not user: user = getUser(user_email) if not enterprise: enterprise = getEnterpriseModel(user = user, enterprise_id = enterprise_id) if enterprise["error"]: return enterprise enterprise = enterprise["message"] if not project: project = getProjectModel(user=user, enterprise=enterprise, project_id=project_id) if project["error"]: return project project = project["message"] if not env: env = getEnvModel(user=user, enterprise=enterprise, project=project, env_id=env_id) if env["error"]: return env env = env["message"] keys = env["message"].key_list return {"message": None, "error": False, "data": keys} except Exception as error: return {"message": f"Some error has happend: {error}", "error": True}
def createEnv( user_email="", env_name="", enterprise_id: uuid.UUID = uuid.uuid4(), project_id: uuid.UUID = uuid.uuid4() ): try: user = getUser(user_email) enterprise = getEnterpriseModel(user=user, enterprise_id=enterprise_id) if enterprise["error"]: return enterprise enterprise = enterprise["message"] project = getProjectModel(user=user, enterprise=enterprise, project_id=project_id) if project["error"]: return project project = project["message"] env = EnvModel(name = env_name) project.envs.append(env.id) env.save() project.save() return {"message": "Env created correctly", "error": False} except Exception as error: return {"message": f"Some error has happend: {error}", "error": True}
def getEnvs( user_email="", enterprise_id: uuid.UUID = uuid.uuid4(), project_id: uuid.UUID = uuid.uuid4(), user: UserModel = None, enterprise: EnterpriseModel = None, project: ProjectModel = None ): try: if not user: user = getUser(user_email) if not enterprise: enterprise = getEnterpriseModel(user = user, enterprise_id = enterprise_id) if enterprise["error"]: return enterprise enterprise = enterprise["message"] if not project: project = getProjectModel(user=user, enterprise=enterprise, project_id=project_id) if project["error"]: return project project = project["message"] envs = EnvModel.objects(id__in = project.envs).aggregate({ "$project":{ "id": "$id.uuid", "name": 1, "created_at": 1, "image_src": 1 } }) return {"message": list(envs), "error": False} except Exception as error: return {"message": f"Some error has happend: {error}", "error": True}
def deleteEnv( user_email="", enterprise_id: uuid.UUID = uuid.uuid4(), project_id: uuid.UUID = uuid.uuid4(), env_id: uuid.UUID = uuid.uuid4(), user: UserModel = None, enterprise: EnterpriseModel = None, project: ProjectModel = None ): try: if not user: user = getUser(user_email) if not enterprise: enterprise = getEnterpriseModel(user=user, enterprise_id=enterprise_id) if enterprise["error"]: return enterprise enterprise = enterprise["message"] if not project: project = getProjectModel(user=user, enterprise=enterprise, project_id=project_id) if project["error"]: return project project = project["message"] if not enterprise_id in user.enterprises: return {"message": "Unable to find this enterprise in your profile", "error": True, "data": None} if not project_id in enterprise.projects: return {"message": "Unable to find this project in your profile", "error": True, "data": None} if not env_id in project.envs: return {"message": "Unable to find this env in your profile", "error": True, "data": None} env = EnvModel.objects.filter(id = env_id).delete() env.save() project.envs.remove(env_id) project.save() return {"message": "Key deleted correctly", "error": False, "data": None} except Exception as error: print(f"Some error has happend deleting key at key_consumer.py. error: {error}") return {"message": f"Some error has happend: {error}", "error": True}
def getEnvModel( user_email="", enterprise_id: uuid.UUID = uuid.uuid4(), project_id: uuid.UUID = uuid.uuid4(), env_id: uuid.UUID = uuid.uuid4(), user: UserModel = None, enterprise: EnterpriseModel = None, project: ProjectModel = None ): try: if not user: user = getUser(user_email) if not enterprise: enterprise = getEnterpriseModel(user=user, enterprise_id=enterprise_id) if enterprise["error"]: return enterprise enterprise = enterprise["message"] if not project: project = getProjectModel(user=user, enterprise_id=enterprise_id, project_id=project_id) if project["error"]: return project project = project["message"] if env_id in project.envs: env = EnvModel.objects(id = env_id).first() return {"message": env, "error": False} except Exception as error: return {"message": f"Some error has happend in getEnvModel at env_consumer: {error}", "error": True}
def getEnterpriseModel(user_email="", enterprise_id: uuid.UUID = uuid.uuid4(), user=None): try: enterprises_ids = [] if not user: user = getUser(user_email) enterprises_ids = user.enterprises if enterprise_id in enterprises_ids: enterprise = EnterpriseModel.objects(id=enterprise_id).first() return {"message": enterprise, "error": False} return {"message": None, "error": False} except Exception as error: return {"message": f"Some error has happend: {error}", "error": True}
def getEnterprises(user_email): try: user = getUser(user_email) enterprises_ids = user.enterprises enterprises = EnterpriseModel.objects.filter( id__in=enterprises_ids).aggregate( {"$project": { "id": "$id.uuid", "name": 1, "created_at": 1 }}) return {"message": list(enterprises), "error": False} except Exception as error: return {"message": f"Some error has happend: {error}", "error": True}
def getEnterprise(user_email="", enterprise_id: uuid.UUID = uuid.uuid4(), user=None): try: enterprises_ids = [] if not user: user = getUser(user_email) enterprises_ids = user.enterprises if enterprise_id in enterprises_ids: enterprise = EnterpriseModel.objects(id=enterprise_id).aggregate( {"$project": { "id": "$id.uuid", "name": 1, "created_at": 1 }}) return {"message": list(enterprise), "error": False} return {"message": None, "error": False} except Exception as error: return {"message": f"Some error has happend: {error}", "error": True}
def getProjectModel(user_email="", enterprise_id: uuid.UUID = uuid.uuid4(), project_id: uuid.UUID = uuid.uuid4(), user: UserModel = None, enterprise: EnterpriseModel = None): try: if not user: user = getUser(user_email) if not enterprise: enterprise = getEnterpriseModel(user=user, enterprise_id=enterprise_id) if enterprise["error"]: return enterprise enterprise = enterprise["message"] if project_id in enterprise.projects: project = ProjectModel.objects(id=project_id).first() return {"message": project, "error": False} except Exception as error: return {"message": f"Some error has happend: {error}", "error": True}
def getKey( user_email="", enterprise_id: uuid.UUID = uuid.uuid4(), project_id: uuid.UUID = uuid.uuid4(), env_id: uuid.UUID = uuid.uuid4(), key_id: uuid.UUID = uuid.uuid4(), user: UserModel = None, enterprise: EnterpriseModel = None, project: ProjectModel = None, env: EnvModel = None ): try: if not user: user = getUser(user_email) if not enterprise: enterprise = getEnterpriseModel(user=user, enterprise_id=enterprise_id) if enterprise["error"]: return enterprise enterprise = enterprise["message"] if not project: project = getProjectModel(user=user, enterprise=enterprise, project_id=project_id) if project["error"]: return project project = project["message"] if not env: env = getEnvModel(user=user, enterprise=enterprise, project=project, env_id=env_id) if env["error"]: return env env = env["message"] key = env.keys_list.object.filter(id = key_id).aggregate({ "$project":{ "id": "$id.uuid", "name": 1, "created_at": 1 } }) key = list(key) key_len = len(key) if key_len: return {"message": None, "error": False, "data": key} except Exception as error: return {"message": f"Some error has happend: {error}", "error": True}
def createProject(user_email="", project_name="", enterprise_id: uuid.UUID = uuid.uuid4()): try: user = getUser(user_email) enterprise = getEnterpriseModel(user=user, enterprise_id=enterprise_id) if enterprise["error"]: return enterprise project = ProjectModel(name=project_name) enterprise = enterprise["message"] enterprise.projects.append(project.id) project.save() enterprise.save() return {"message": "Project created correctly", "error": True} except Exception as error: return {"message": f"Some error has happend: {error}", "error": True}