#!/usr/bin/env python# -*- coding: utf-8; -*-# Copyright (c) 2021, 2024 Oracle and/or its affiliates.# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/importloggingimportoci.artifactsfromoci.ai_languageimportAIServiceLanguageClientfromoci.artifactsimportArtifactsClientfromoci.data_catalogimportDataCatalogClientfromoci.data_flowimportDataFlowClientfromoci.data_labeling_serviceimportDataLabelingManagementClientfromoci.data_labeling_service_dataplaneimportDataLabelingClientfromoci.data_scienceimportDataScienceClientfromoci.identityimportIdentityClientfromoci.marketplaceimportMarketplaceClientfromoci.object_storageimportObjectStorageClientfromoci.resource_searchimportResourceSearchClientfromoci.secretsimportSecretsClientfromoci.vaultimportVaultsClientfromoci.loggingimportLoggingManagementClientfromoci.coreimportVirtualNetworkClientfromoci.limitsimportLimitsClientlogger=logging.getLogger(__name__)
[docs]classOCIClientFactory:""" A factory class to create OCI client objects. The constructor takes in config, signer and client_kwargs. `client_kwargs` is passed to the client constructor as key word arguments. Examples -------- from ads.common import auth as authutil from ads.common import oci_client as oc auth = authutil.default_signer() oc.OCIClientFactory(**auth).object_storage # Creates Object storage client auth = authutil.default_signer({"timeout": 6000}) oc.OCIClientFactory(**auth).object_storage # Creates Object storage client with timeout set to 6000 auth = authutil.api_keys(config="/home/datascience/.oci/config", profile="TEST", {"timeout": 6000}) oc.OCIClientFactory(**auth).object_storage # Creates Object storage client with timeout set to 6000 using API Key authentication auth = authutil.resource_principal({"timeout": 6000}) oc.OCIClientFactory(**auth).object_storage # Creates Object storage client with timeout set to 6000 using resource principal authentication auth = authutil.create_signer("instance_principal") oc.OCIClientFactory(**auth).object_storage # Creates Object storage client using instance principal authentication """def__init__(self,config=None,signer=None,client_kwargs=None):ifnotconfig:config={}self.config=configself.signer=signerself.client_kwargs=client_kwargs@staticmethoddef_client_impl(client):client_map={"object_storage":ObjectStorageClient,"data_science":DataScienceClient,"dataflow":DataFlowClient,"secret":SecretsClient,"vault":VaultsClient,"identity":IdentityClient,"ai_language":AIServiceLanguageClient,"data_labeling_dp":DataLabelingClient,"data_labeling_cp":DataLabelingManagementClient,"resource_search":ResourceSearchClient,"data_catalog":DataCatalogClient,"logging_management":LoggingManagementClient,"virtual_network":VirtualNetworkClient,"limits":LimitsClient,"marketplace":MarketplaceClient,"artifacts":ArtifactsClient,}assert(clientinclient_map),f"Invalid client name. Client name not found in {client_map.keys()}"returnclient_map[client]@staticmethoddef_validate_auth_param(auth):ifnotisinstance(auth,dict):raiseValueError("auth parameter should be of type dictionary")if"config"inauthandnotisinstance(auth["config"],dict):raiseValueError("auth[config] should be of type dict")if"signer"inauthandauth["signer"]isNoneandlen(auth["config"])==0:raiseValueError("Signer is None and auth[config] is empty. Either assign config or if you are using resource principal, set resource principal signer to signer and {} for config")returnTrue
[docs]defcreate_client(self,client_name):assert(client_nameisnotNoneandclient_name!=""),"Client name cannot be empty"client=(self._client_impl(client_name)ifisinstance(client_name,str)elseclient_name)kwargs=self.client_kwargsordict()returnclient(config=self.config,signer=self.signer,**kwargs)