def ecs_client(self, region): self.log('Geting ECS client') config = HttpConfig.get_default_config() config.ignore_ssl_verification = True if self.credentials["project_id"] and self.credentials[ "access_key"] and self.credentials["secret_key"] and region: _credentials = BasicCredentials(self.credentials["access_key"], self.credentials["secret_key"], self.credentials["project_id"]) if self.credentials["identity_endpoint"]: _credentials.iam_endpoint = self.credentials[ "identity_endpoint"] if self.credentials["cloud"]: endpoint = 'https://ecs.{}.{}'.format( region, self.credentials["cloud"]) else: endpoint = 'https://ecs.{}.myhuaweicloud.com'.format(region) ecs_client = EcsClient.new_builder() \ .with_http_config(config) \ .with_credentials(_credentials) \ .with_endpoint(endpoint) \ .build() else: raise HwcModuleException( 'Getting ECS client failed: "project_id", "access_key", "access_key" and "region" are required for ECS client' ) return ecs_client
def cce_client(self): self.log('Geting CCE client') if not self._cce_client: config = HttpConfig.get_default_config() config.ignore_ssl_verification = True if self.module.params["project_id"]: credentials = BasicCredentials( self.module.params["access_key"], self.module.params["secret_key"], self.module.params["project_id"]) if self.module.params["identity_endpoint"]: credentials.iam_endpoint = self.module.params[ "identity_endpoint"] endpoint = 'https://cce.{}.{}'.format( self.module.params["region"], self.module.params["cloud"]) self._cce_client = CceClient.new_builder() \ .with_http_config(config) \ .with_credentials(credentials) \ .with_endpoint(endpoint) \ .build() else: raise HwcModuleException( 'Getting CCE client failed: "project_id" is required for CCE client' ) return self._cce_client
def get_iot_client(): config = HttpConfig.get_default_config() credentials = BasicCredentials(HwyunSettings.access_key_id, HwyunSettings.access_key_secret, HwyunSettings.project_id, HwyunSettings.domain_id) builder: ClientBuilder = IoTDAClient().new_builder(IoTDAClient) client: IoTDAClient = builder.with_http_config(config) \ .with_credentials(credentials) \ .with_endpoint(HwyunSettings.endpoint) \ .with_stream_log(log_level=logging.INFO) \ .build() return client
def test_project_id_with_region(mocker): mock_credential = BasicCredentials("ak", "sk").with_iam_endpoint(MOCK_ENDPOINT) client = Client().with_credentials(mock_credential).with_config( HttpConfig.get_default_config()) client.init_http_client() http_client = client.get_http_client() mocker.patch.object(http_client, 'do_request_sync', return_value=mock_keystone_list_projects(REGION_ID)) result_credential = mock_credential.process_auth_params( http_client, ServiceRegion.CN_NORTH_7.id) assert result_credential.project_id == "123456789"
def client(): ak = "my ak" sk = "my sk" project_id = "my project_id" endpoint = "https://vpc.cn-north-x.myhuaweicloud.com" config = HttpConfig.get_default_config() config.ignore_ssl_verification = True credentials = BasicCredentials(ak, sk, project_id) client = ClientBuilder(Client) \ .with_http_config(config) \ .with_credentials(credentials) \ .with_endpoint(endpoint) \ .build() yield client
def build(self): if self._config is None: self._config = HttpConfig.get_default_config() if self._credentials is None: self._credentials = EnvCredentials.load_credential_from_env( self._credential_type[0]) if self._credentials is None: raise ValueError( "credential can not be None, %s credential objects are required" % ",".join(self._credential_type)) if self._credentials.__class__.__name__ not in self._credential_type: raise TypeError( "credential type error, supported credential type is %s" % ",".join(self._credential_type)) client = self._client_type() \ .with_credentials(self._credentials) \ .with_config(self._config) \ .with_http_handler(self._http_handler) client.init_http_client() if self._region is not None: self._endpoint = self._region.endpoint self._credentials = self._credentials.process_auth_params( client.get_http_client(), self._region.id) if not self._endpoint.startswith(self._http_scheme): self._endpoint = self._https_scheme + "://" + self._endpoint client.with_endpoint(self._endpoint) \ .with_credentials(self._credentials) if self._file_logger_handler is not None: client.add_file_logger(**self._file_logger_handler) if self._stream_logger_handler is not None: client.add_stream_logger(**self._stream_logger_handler) return client
specific language governing permissions and limitations under the LICENSE. """ import pytest from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.http.http_config import HttpConfig from huaweicloudsdkcore.region.region import Region from tests.data.project_response_mocker import MOCK_ENDPOINT from tests.model.service.service_client import ServiceClient from tests.model.service.service_region import ServiceRegion OVERRIDE_ENDPOINT = "https://service.cn-north-x.myhuaweicloud.com" config = HttpConfig.get_default_config() config.ignore_ssl_verification = True credentials = BasicCredentials("ak", "sk", "project_id") def test_endpoint_with_region_1(): client = ServiceClient.new_builder() \ .with_http_config(config) \ .with_credentials(credentials) \ .with_region(ServiceRegion.CN_NORTH_7) \ .build() assert hasattr(client, "_endpoint") is True assert getattr(client, "_endpoint") == MOCK_ENDPOINT