def start_workspace(id_, start_with_latest_version=False, **kwargs): """Start a job :param id_: The ID of the workspace resource :type id_: str :param start_with_latest_version: Start job with the latest revision of the project :type start_with_latest_version: bool :rtype: StatusSerializer """ w = get_workspace_object(id_) check_workspace_permission(w, kwargs["token_info"]) stub = get_workspaces_services_stub() if start_with_latest_version: w.spec.revision = get_latest_project_revision(w.metadata.project) w = stub.Update(w) response = stub.Start(job_pb2.ID(id=w.id)) if response.status != 200: return ErrorSerializer(status=response.status, title="Api Error", detail=response.message), response.status return StatusSerializer.from_dict(util.deserialize_protobuf(response))
def retrieve_hardwaretier(id_, **kwargs): # noqa: E501 """Retrieve HardwareTier :param id_: The ID of the hardware resource :type id_: str :rtype: HardwareTierSerializer """ stub = get_environments_services_stub() res = stub.GetHardwareTier(job_pb2.ID(id=id_)) return HardwareTierSerializer.from_dict(util.deserialize_protobuf(res))
def retrieve_workspace(id_, **kwargs): """Retrieve a workspace :param id_: The ID of the workspace resource :type id_: str :rtype: WorkspaceSerializer """ w = get_workspace_object(id_) check_workspace_permission(w, kwargs["token_info"]) return WorkspaceSerializer.from_dict(util.deserialize_protobuf(w))
def retrieve_project(id_, **kwargs): """Retrieve a project :param id_: The ID of the project object to be retrieved :type id_: str :rtype: ProjectSerializer """ proj = get_project_object(id_) check_project_permission(proj, kwargs["token_info"]) return ProjectSerializer.from_dict(util.deserialize_protobuf(proj))
def retrieve_modelapi(id_, **kwargs): """Retrieve a modelapi :param id_: The ID of the modelapi resource :type id_: str :rtype: ModelApiSerializer """ modelapi = get_modelapi_object(id_) return ModelApiSerializer.from_dict(util.deserialize_protobuf(modelapi))
def create_hardwaretier(body, **kwargs): """Create HardwareTier :param body: HardwareTier payload :type body: dict | bytes :rtype: HardwareTierSerializer """ stub = get_environments_services_stub() hw = stub.CreateHardwareTier(job_pb2.HardwareTier(**body)) return HardwareTierSerializer.from_dict(util.deserialize_protobuf(hw))
def list_ide(limit=None, page=None, **kwargs): """List available Ide for workspaces :param limit: :type limit: int :param page: :type page: int :rtype: PageLimitListSerializer """ stub = get_environments_services_stub() res = stub.ListWorkspaceIde(job_pb2.Pagination(page=page, limit=limit)) return util.deserialize_protobuf(res)
def retrieve_experiment(id_, **kwargs): """Retrieve an experiment :param id_: The ID of the experiment resource :type id_: str :rtype: ExperimentSerializer """ exp = get_experiment_object(id_) check_experiment_permission(exp, kwargs["token_info"]) return ExperimentSerializer.from_dict(util.deserialize_protobuf(exp))
def create_project(body, **kwargs): """Create a project :param body: Project object to be created :type body: dict | bytes :rtype: ProjectSerializer """ stub = get_projects_services_stub() body["owner"] = kwargs["user"] response = stub.Create(project_pb2.Project(**body)) return ProjectSerializer.from_dict(util.deserialize_protobuf(response))
def status_workspace(id_, **kwargs): """Get state of a workspace. :param id_: The ID of the workspace resource :type id_: str :rtype: Object """ w = get_workspace_object(id_) check_workspace_permission(w, kwargs["token_info"]) stub = get_workspaces_services_stub() response = stub.State(job_pb2.ID(id=id_)) return util.deserialize_protobuf(response)
def create_workspace(body, **kwargs): """Create a workspace :param body: Workspace payload :type body: dict | bytes :rtype: WorkspaceSerializer """ serializer = WorkspaceSerializer.from_dict(body) check_workspace_permission(serializer, kwargs["token_info"]) stub = get_workspaces_services_stub() response = stub.Create(job_pb2.Workspace(**body)) return WorkspaceSerializer.from_dict(util.deserialize_protobuf(response))
def status_modelapi(id_, **kwargs): """Get state of modelapi. :param id_: The ID of the modelapi resource :type id_: str :rtype: object """ modelapi = get_modelapi_object(id_) check_modelapi_permission(modelapi, kwargs["token_info"]) stub = get_modelapis_services_stub() response = stub.State(job_pb2.ID(id=id_)) return util.deserialize_protobuf(response)
def state_experiment(id_, **kwargs): """Get state of a experiment. :param id_: The ID of the experiment resource :type id_: str :rtype: dict """ exp = get_experiment_object(id_) check_experiment_permission(exp, kwargs["token_info"]) stub = get_experiments_services_stub() state = stub.State(job_pb2.ID(id=id_)) return util.deserialize_protobuf(state)
def create_modelapi(body, **kwargs): """Create a modelapi :param body: modelapi payload :type body: dict | bytes :rtype: ModelApiSerializer """ serializer = ModelApiSerializer.from_dict(body) check_modelapi_permission(serializer, kwargs["token_info"]) stub = get_modelapis_services_stub() response = stub.Create(job_pb2.ModelApis(**body)) return ModelApiSerializer.from_dict(util.deserialize_protobuf(response))
def delete_hardwaretier(id_, **kwargs): """Delete HardwareTier :param id_: The ID of the hardware resource :type id_: str :rtype: StatusSerializer """ stub = get_environments_services_stub() response = stub.DeleteHardwareTier(job_pb2.ID(id=id_)) if response.status != 200: return ErrorSerializer(status=response.status, title="Api Error", detail=response.message), response.status return StatusSerializer.from_dict(util.deserialize_protobuf(response))
def list_modelapis(body=None, **kwargs): """List modelapis :param body: :type body: dict | bytes :rtype: PageLimitListSerializer """ payload = body if body is None: payload = {"query": {}} stub = get_modelapis_services_stub() response = stub.Search(job_pb2.SearchRequest(**payload)) return util.deserialize_protobuf(response)
def update_project(id_, body, **kwargs): """Update a project :param id_: The ID of the project object to be updated :type id_: str :param body: Project object to be updated :type body: dict | bytes :rtype: ProjectSerializer """ proj = get_project_object(id_) check_project_permission(proj, kwargs["token_info"]) stub = get_projects_services_stub() proj.description = body["description"] response = stub.Update(proj) return ProjectSerializer.from_dict(util.deserialize_protobuf(response))
def delete_project(id_, **kwargs): """Delete a project :param id_: The ID of the project object to be deleted :type id_: str :rtype: StatusSerializer """ proj = get_project_object(id_) check_project_permission(proj, kwargs["token_info"]) stub = get_projects_services_stub() response = stub.Delete(project_pb2.ID(id=proj.id)) if response.status != 200: return ErrorSerializer(status=response.status, title="Api Error", detail=response.message), response.status return StatusSerializer.from_dict(util.deserialize_protobuf(response))
def start_modelapi(id_, **kwargs): """Start a job :param id_: The ID of the modelapi resource :type id_: str :rtype: StatusSerializer """ modelapi = get_modelapi_object(id_) check_modelapi_permission(modelapi, kwargs["token_info"]) stub = get_modelapis_services_stub() response = stub.Start(job_pb2.ID(id=id_)) if response.status != 200: return ErrorSerializer(status=response.status, title="Api Error", detail=response.message), response.status return StatusSerializer.from_dict(util.deserialize_protobuf(response))
def list_projects(body=None, **kwargs): """List projects :param body: :type body: dict | bytes :rtype: PageLimitListSerializer """ query = body.get("query") if not permissions.IsAdmin.has_permission( kwargs["token_info"]) and query["visibility"] == "PRIVATE": query["member"] = kwargs["user"] stub = get_projects_services_stub() response = stub.Search( project_pb2.SearchProjectRequest(query=query, limit=body.get("limit"), page=body.get("page"))) return util.deserialize_protobuf(response)
def delete_workspace(id_, **kwargs): """Delete a workspace :param id_: The ID of the workspace resource :type id_: str :rtype: StatusSerializer """ w = get_workspace_object(id_) check_workspace_permission(w, kwargs["token_info"]) stub = get_workspaces_services_stub() response = stub.Delete(job_pb2.ID(id=id_)) if response.status != 200: return ErrorSerializer(status=response.status, title="Api Error", detail=response.message), response.status return StatusSerializer.from_dict(util.deserialize_protobuf(response))
def close_project(id_, **kwargs): """Close a project :param id_: The ID of the project object to be updated :type id_: str :rtype: ProjectSerializer """ proj = get_project_object(id_) check_project_permission(proj, kwargs["token_info"]) if proj.owner != kwargs["user"]: raise connexion.ProblemException( status=403, title="Permission Denied", detail="Doesn't have enough permissions to take this action") proj.state = project_pb2.STATE.CLOSED stub = get_projects_services_stub() response = stub.Update(proj) return ProjectSerializer.from_dict(util.deserialize_protobuf(response))
def create_project_revision(id_, **kwargs): """Create project's revision :param id_: The ID of the project resource :type id_: str :rtype: ProjectRevisionSerializer """ proj = get_project_object(id_) check_project_permission(proj, kwargs["token_info"]) # upload file in Minio files = connexion.request.files client = get_minio_client() # acquire lock to write etcd = etcd3.client(host=current_app.config.get("ETCD_HOST"), port=current_app.config.get("ETCD_PORT")) # acquire lock commit_message = "Write" stub = get_projects_services_stub() with etcd.lock(proj.id, ttl=1800) as lock: for file in files.getlist('files'): filename = secure_filename( file.filename ) # This is convenient to validate your filename, otherwise just use file.filename file_type, _ = mimetypes.guess_type(filename) bio = io.BytesIO(file.read()) client.put_object(data=bio, bucket_name=proj.repo_bucket, object_name=filename, content_type=file_type, length=len(bio.getvalue())) commit_message += " {}".format(filename) response = stub.CreateRevision( project_pb2.Revision(project=proj.id, commit=commit_message, author=kwargs["user"])) return ProjectRevisionSerializer.from_dict( util.deserialize_protobuf(response))
def list_project_runs(id_, limit=None, page=None, **kwargs): """List project's runs :param id_: The ID of the project resource :type id_: str :param limit: :type limit: int :param page: :type page: int :rtype: PageTokenListSerializer """ proj = get_project_object(id_) check_project_permission(proj, kwargs["token_info"]) stub = get_runs_services_stub() response = stub.Search( job_pb2.SearchRequest(query={"project": proj.id}, limit=limit, page=page)) return util.deserialize_protobuf(response)
def submit_experiment(body, **kwargs): """Submit an experiment :param body: experiment payload :type body: dict | bytes :rtype: StatusSerializer """ serializer = ExperimentSerializer.from_dict(body) check_experiment_permission(serializer, kwargs["token_info"]) stub = get_experiments_services_stub() response = stub.Submit(job_pb2.Experiment(**body)) if response.status != 200: return ErrorSerializer(status=response.status, title="Api Error", detail=response.message), response.status return StatusSerializer.from_dict(util.deserialize_protobuf(response))
def stop_experiment(id_, **kwargs): """Stop a running experiment. :param id_: The ID of the experiment resource :type id_: str :rtype: StatusSerializer """ exp = get_experiment_object(id_) check_experiment_permission(exp, kwargs["token_info"]) stub = get_experiments_services_stub() response = stub.Stop(job_pb2.ID(id=id_)) if response.status != 200: return ErrorSerializer(status=response.status, title="Api Error", detail=response.message), response.status return StatusSerializer.from_dict(util.deserialize_protobuf(response))
def update_hardwaretier(id_, body, **kwargs): # noqa: E501 """Update HardwareTier :param id_: The ID of the hardware resource :type id_: str :param body: HardwareTier payload :type body: dict | bytes :rtype: HardwareTierSerializer """ hw = retrieve_hardwaretier(id_, **kwargs) hw.name = body["name"] hw.cores = body["cores"] hw.memory = body["memory"] hw.instancegroup = body["instancegroup"] hw.is_default = body["is_default"] hw.deployment = body["deployment"] stub = get_environments_services_stub() res = stub.UpdateHardwareTier(job_pb2.HardwareTier(**hw.to_dict())) return HardwareTierSerializer.from_dict(util.deserialize_protobuf(res))
def remove_project_member(id_, body, **kwargs): """Close a project :param id_: The ID of the project object to be updated :type id_: str :param body: User Id :type body: dict | bytes :rtype: ProjectSerializer """ proj = get_project_object(id_) check_project_permission(proj, kwargs["token_info"]) if proj.owner != kwargs["user"]: raise connexion.ProblemException( status=403, title="Permission Denied", detail="Doesn't have enough permissions to take this action") if body["user"] in proj.members: proj.members.remove(body["user"]) stub = get_projects_services_stub() proj = stub.Update(proj) return ProjectSerializer.from_dict(util.deserialize_protobuf(proj))
def list_project_datasets(id_, limit=None, page=None, **kwargs): """List project's datasets :param id_: The ID of the project resource :type id_: str :param limit: :type limit: int :param page: :type page: int :rtype: PageLimitListSerializer """ proj = get_project_object(id_) check_project_permission(proj, kwargs["token_info"]) stub = get_datasets_services_stub() response = stub.SearchDatasets( dataset_pb2.SearchDatasetRequest(query={ "project": proj.id, "scope": "Local" }, limit=limit, page=page)) return util.deserialize_protobuf(response)