# -----------------------------------------------------------------------------------------# (C) Copyright IBM Corp. 2023-2024.# https://opensource.org/licenses/BSD-3-Clause# -----------------------------------------------------------------------------------------from__future__importannotationsfromtypingimportAny,TYPE_CHECKING,Literalfromibm_watsonx_ai.wml_resourceimportWMLResourcefromibm_watsonx_ai.utils.utilsimport_get_id_from_deprecated_uidfromibm_watsonx_ai.wml_client_errorimport(CannotSetProjectOrSpace,ExceededLimitOfAPICalls,)fromibm_watsonx_ai.service_instanceimportServiceInstanceifTYPE_CHECKING:fromibm_watsonx_aiimportAPIClient
[docs]classSet(WMLResource):"""Set a space_id or a project_id to be used in the subsequent actions."""def__init__(self,client:APIClient):WMLResource.__init__(self,__name__,client)
[docs]defdefault_space(self,space_id:str|None=None,**kwargs:Any)->Literal["SUCCESS"]:"""Set a space ID. :param space_id: ID of the space to be used :type space_id: str :return: status ("SUCCESS" if succeeded) :rtype: str **Example:** .. code-block:: python client.set.default_space(space_id) """space_id=_get_id_from_deprecated_uid(kwargs,space_id,"space",can_be_none=False)space_endpoint=(self._client.service_instance._href_definitions.get_platform_space_href(space_id))space_details=self._client._session.get(space_endpoint,headers=self._client._get_headers())ifspace_details.status_code==404:error_msg="Space with id '{}' does not exist".format(space_id)raiseCannotSetProjectOrSpace(reason=error_msg)elifspace_details.status_code==200:self._client.default_space_id=space_idifself._client.default_project_idisnotNone:print("Unsetting the project_id ...")self._client.default_project_id=Noneself._client.project_type=Noneifself._client.CLOUD_PLATFORM_SPACES:instance_id="not found"comp_obj_type=Nonespace_details_json=space_details.json()if"compute"inspace_details.json()["entity"].keys():if"type"inspace_details_json["entity"]["compute"][0]:ifspace_details_json["entity"]["compute"][0]["type"]in["machine_learning","code-assistant",]:instance_id=space_details_json["entity"]["compute"][0]["guid"]comp_obj_type=space_details_json["entity"]["compute"][0]["type"]self._client.service_instance=ServiceInstance(self._client)self._client.service_instance._instance_id=instance_idifcomp_obj_type=="code-assistant":self._client.WCA=Trueself._client.service_instance.details=Noneelse:self._client.service_instance._refresh_details=Trueelse:# It's possible that a previous space is used in the context of# this client which had compute but this space doesn't haveself._client.service_instance=ServiceInstance(self._client)self._client.service_instance.details=Nonereturn"SUCCESS"else:raiseCannotSetProjectOrSpace(reason=space_details.text)
# Setting project ID
[docs]defdefault_project(self,project_id:str)->Literal["SUCCESS"]:"""Set a project ID. :param project_id: ID of the project to be used :type project_id: str :return: status ("SUCCESS" if succeeded) :rtype: str **Example:** .. code-block:: python client.set.default_project(project_id) """ifproject_idisnotNone:self._client.default_project_id=project_idifself._client.default_space_idisnotNone:print("Unsetting the space_id ...")self._client.default_space_id=Noneproject_endpoint=(self._client.service_instance._href_definitions.get_project_href(project_id))project_details=self._client._session.get(project_endpoint,headers=self._client._get_headers())ifproject_details.status_code==429:raiseExceededLimitOfAPICalls(project_endpoint,reason=project_details.text)elif(project_details.status_code!=200andproject_details.status_code!=204):raiseCannotSetProjectOrSpace(reason=project_details.text)else:self._client.project_type=project_details.json()["entity"]["storage"]["type"]ifself._client.CLOUD_PLATFORM_SPACES:instance_id="not_found"comp_obj_type=Noneif"compute"inproject_details.json()["entity"].keys():forcomp_objinproject_details.json()["entity"]["compute"]:ifcomp_obj["type"]in["machine_learning","code-assistant",]:comp_obj_type=comp_obj["type"]instance_id=comp_obj["guid"]breakself._client.service_instance=ServiceInstance(self._client)self._client.service_instance._instance_id=instance_idifcomp_obj_type=="code-assistant":self._client.service_instance.details=Noneself._client.WCA=Trueelse:self._client.service_instance._refresh_details=Trueelse:# It`s possible that a previous project is used in the context of# this client which had compute but this project doesn't haveself._client.service_instance=ServiceInstance(self._client)self._client.service_instance.details=Noneelse:self._client.service_instance=ServiceInstance(self._client)return"SUCCESS"else:error_msg="Project id cannot be None."raiseCannotSetProjectOrSpace(reason=error_msg)