def get_mysql_flexible_management_client(cli_ctx, **_): from os import getenv from azure.mgmt.rdbms.mysql_flexibleservers import MySQLManagementClient # Allow overriding resource manager URI using environment variable # for testing purposes. Subscription id is also determined by environment # variable. rm_uri_override = getenv(RM_URI_OVERRIDE) if rm_uri_override: client_id = getenv(CLIENT_ID) if client_id: from azure.identity import ClientSecretCredential credentials = ClientSecretCredential( client_id=client_id, client_secret=getenv(CLIENT_SECRET), tenant_id=getenv(TENANT_ID)) else: from msrest.authentication import Authentication # pylint: disable=import-error credentials = Authentication() return MySQLManagementClient(subscription_id=getenv(SUB_ID_OVERRIDE), base_url=rm_uri_override, credential=credentials) # Normal production scenario. return get_mgmt_service_client(cli_ctx, MySQLManagementClient)
def credentials(self): if self.token: return BasicAuthentication("", self.token) else: warnings.warn( "No token available. No modifications will be possible!") return Authentication()
def get_postgresql_management_client(cli_ctx, **_): from os import getenv from azure.mgmt.rdbms.postgresql import PostgreSQLManagementClient # Allow overriding resource manager URI using environment variable # for testing purposes. Subscription id is also determined by environment # variable. rm_uri_override = getenv(RM_URI_OVERRIDE) if rm_uri_override: client_id = getenv(CLIENT_ID) if client_id: from azure.common.credentials import ServicePrincipalCredentials credentials = ServicePrincipalCredentials( client_id=client_id, secret=getenv(CLIENT_SECRET), tenant=getenv(TENANT_ID)) else: from msrest.authentication import Authentication # pylint: disable=import-error credentials = Authentication() return PostgreSQLManagementClient( subscription_id=getenv(SUB_ID_OVERRIDE), base_url=rm_uri_override, credentials=credentials) else: # Normal production scenario. return get_mgmt_service_client(cli_ctx, PostgreSQLManagementClient)
def test_request_proxy(self): cfg = Configuration("http://my_service.com") cfg.proxies.add("http://my_service.com", 'http://localhost:57979') creds = Authentication() def hook(adptr, request, *args, **kwargs): self.assertEqual(kwargs.get('proxies'), {"http://my_service.com":'http://localhost:57979'}) kwargs['result']._content_consumed = True kwargs['result'].status_code = 200 return kwargs['result'] client = ServiceClient(creds, cfg) client.add_hook('request', hook, precall=False, overwrite=True) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) os.environ['HTTPS_PROXY'] = "http://localhost:1987" def hook2(adptr, request, *args, **kwargs): self.assertEqual(kwargs.get('proxies')['https'], "http://localhost:1987") kwargs['result']._content_consumed = True kwargs['result'].status_code = 200 return kwargs['result'] cfg = Configuration("http://my_service.com") client = ServiceClient(creds, cfg) client.add_hook('request', hook2, precall=False, overwrite=True) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) del os.environ['HTTPS_PROXY']
def test_request_proxy(self): # Note that this test requires requests >= 2.8.0 to accept host on proxy cfg = Configuration("http://my_service.com") cfg.proxies.add("http://my_service.com", 'http://localhost:57979') cfg.credentials = Authentication() httpretty.register_uri(httpretty.GET, "http://localhost:57979/get_endpoint?check=True", body='"Mocked body"', content_type="application/json", status=200) client = ServiceClient(None, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) assert response.json() == "Mocked body" with mock.patch.dict('os.environ', {'HTTP_PROXY': "http://localhost:1987"}): httpretty.register_uri(httpretty.GET, "http://localhost:1987/get_endpoint?check=True", body='"Mocked body"', content_type="application/json", status=200) cfg = Configuration("http://my_service.com") client = ServiceClient(None, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) assert response.json() == "Mocked body"
def setUp(self): cfg = Configuration("https://my_service.com") cfg.retry_policy.backoff_factor = 0 creds = Authentication() self.client = ServiceClient(creds, cfg) self.request = self.client.get("/get_endpoint", {'check': True}) return super(TestRuntimeRetry, self).setUp()
def setUp(self): cfg = Configuration("https://my_service.com") cfg.retry_policy.backoff_factor=0 cfg.redirect_policy.max_redirects=2 creds = Authentication() self.client = ServiceClient(creds, cfg) return super(TestRedirect, self).setUp()
def test_deprecated_creds(self): """Test that creds parameters gets populated correctly. https://github.com/Azure/msrest-for-python/issues/135 """ cfg = Configuration("http://127.0.0.1/") assert cfg.credentials is None creds = Authentication() client = SDKClient(creds, cfg) assert cfg.credentials is creds
def test_request_fail(self, mock_requests): mock_requests.return_value.request.return_value = mock.Mock(text="text") cfg = Configuration("https://my_service.com") cfg.credentials = Authentication() client = ServiceClient(None, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) assert response.text == "text" mock_requests.return_value.request.side_effect = requests.RequestException with self.assertRaises(ClientRequestError): client.send(request)
def get_sql_management_client(cli_ctx): from azure.cli.core.commands.client_factory import get_mgmt_service_client from azure.mgmt.sql import SqlManagementClient from msrest.authentication import Authentication from os import getenv # Allow overriding SQL resource manager URI using environment variable # for testing purposes. Subscription id is also determined by environment # variable. sql_rm_override = getenv('_AZURE_CLI_SQL_RM_URI') if sql_rm_override: return SqlManagementClient( subscription_id=getenv('_AZURE_CLI_SQL_SUB_ID'), base_url=sql_rm_override, credentials=Authentication()) # No authentication # Normal production scenario. return get_mgmt_service_client(cli_ctx, SqlManagementClient)
def test_request_fail(self, mock_requests): mock_requests.return_value.request.return_value = "test" cfg = Configuration("https://my_service.com") creds = Authentication() client = ServiceClient(creds, cfg) request = client.get("/get_endpoint", {'check': True}) response = client.send(request) check = httpretty.last_request() self.assertEqual(response, "test") mock_requests.return_value.request.side_effect = requests.RequestException with self.assertRaises(ClientRequestError): client.send(request)
def test_request_fail(self, mock_requests): mock_requests.return_value.request.return_value = mock.Mock(_content_consumed=True) cfg = Configuration("https://my_service.com") creds = Authentication() client = ServiceClient(creds, cfg) url = client.format_url("/get_endpoint") request = client.get(url, {'check':True}) response = client.send(request) check = httpretty.last_request() self.assertTrue(response._content_consumed) mock_requests.return_value.request.side_effect = requests.RequestException with self.assertRaises(ClientRequestError): client.send(request)
def get_mariadb_management_client(cli_ctx, **_): from os import getenv from azure.mgmt.rdbms.mariadb import MariaDBManagementClient # Allow overriding resource manager URI using environment variable # for testing purposes. Subscription id is also determined by environment # variable. rm_uri_override = getenv(RM_URI_OVERRIDE) if rm_uri_override: client_id = getenv(AZURE_CLIENT_ID) if client_id: credentials = get_environment_credential() else: from msrest.authentication import Authentication # pylint: disable=import-error credentials = Authentication() return MariaDBManagementClient(subscription_id=getenv(SUB_ID_OVERRIDE), base_url=rm_uri_override, credential=credentials) # Normal production scenario. return get_mgmt_service_client(cli_ctx, MariaDBManagementClient)
def __init__(self, creds, config): super(ServiceClient, self).__init__(creds, config) self.config = config self.creds = creds if creds else Authentication() self._headers = {}
def client(): # This is the same client of the "vanilla" one, generated with "azure" because it's the # only test that use client level method, and I want to test Azure works too on that. return AutoRestResourceFlatteningTestService(Authentication(), base_url="http://localhost:3000")