def test_get_known_clouds(self): cli = DummyCli() with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]): # Check that we can get all the known clouds without any exceptions for kc in KNOWN_CLOUDS: get_cloud(cli, kc.name)
def test_get_clouds_concurrent(self): with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as config_file: pool_size = 100 p = multiprocessing.Pool(pool_size) p.map(_helper_get_clouds, range(pool_size)) p.close() p.join() # Check we can read the file with no exceptions config = get_config_parser() config.read(config_file) for kc in KNOWN_CLOUDS: get_cloud(kc.name)
def test_get_clouds_concurrent(self): with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as config_file: pool_size = 100 p = multiprocessing.Pool(pool_size) p.map(_helper_get_clouds, range(pool_size)) p.close() p.join() # Check we can read the file with no exceptions cli = TestCli() config = cli.config config.config_parser.read(config_file) for kc in KNOWN_CLOUDS: get_cloud(cli, kc.name)
def test_init_known_clouds_force_concurrent(self): # Support multiple concurrent calls to clouds init method with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as config_file: pool_size = 100 p = multiprocessing.Pool(pool_size) p.map(init_known_clouds, [True] * pool_size) p.close() p.join() # Check we can read the file with no exceptions config = get_config_parser() config.read(config_file) # Check that we can get all the known clouds without any exceptions for kc in KNOWN_CLOUDS: get_cloud(kc.name)
def show_cloud(cloud_name=None): if not cloud_name: cloud_name = get_active_cloud_name() try: return get_cloud(cloud_name) except CloudNotRegisteredException as e: raise CLIError(e)
def test_modify_known_cloud(self): with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as config_file: cloud_name = AZURE_PUBLIC_CLOUD.name cloud = get_cloud(cloud_name) self.assertEqual(cloud.name, cloud_name) mcloud = Cloud(cloud_name) mcloud.endpoints.gallery = 'https://mynewcustomgallery.azure.com' update_cloud(mcloud) cloud = get_cloud(cloud_name) self.assertEqual(cloud.endpoints.gallery, 'https://mynewcustomgallery.azure.com') # Check that the config file only has what we changed, not the full cloud info. config = get_config_parser() config.read(config_file) items = config.items(cloud_name) self.assertEqual(len(items), 1) self.assertEqual(items[0], ('endpoint_gallery', 'https://mynewcustomgallery.azure.com'))
def test_known_cloud_missing_endpoint(self): # New endpoints in cloud config should be saved in config for the known clouds with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as\ config_file: # Save the clouds to config to get started init_known_clouds() cloud = get_cloud(AZURE_PUBLIC_CLOUD.name) self.assertEqual(cloud.endpoints.batch_resource_id, AZURE_PUBLIC_CLOUD.endpoints.batch_resource_id) # Remove an endpoint from the cloud config (leaving other config values as is) config = get_config_parser() config.read(config_file) config.remove_option(AZURE_PUBLIC_CLOUD.name, 'endpoint_batch_resource_id') with open(config_file, 'w') as cf: config.write(cf) # Verify that it was removed config.read(config_file) self.assertFalse( config.has_option(AZURE_PUBLIC_CLOUD.name, 'endpoint_batch_resource_id')) # Init the known clouds again (this should add the missing endpoint) init_known_clouds(force=True) config.read(config_file) # The missing endpoint should have been added by init_known_clouds as 'force' was used. self.assertTrue( config.has_option(AZURE_PUBLIC_CLOUD.name, 'endpoint_batch_resource_id'), 'Expected the missing endpoint to be added but it was not.') actual_val = config.get(AZURE_PUBLIC_CLOUD.name, 'endpoint_batch_resource_id') expected_val = AZURE_PUBLIC_CLOUD.endpoints.batch_resource_id self.assertEqual(actual_val, expected_val)
def show_cloud(cmd, cloud_name=None): if not cloud_name: cloud_name = cmd.cli_ctx.cloud.name try: return get_cloud(cmd.cli_ctx, cloud_name) except CloudNotRegisteredException as e: raise CLIError(e)
def show_cloud(cloud_name=None): if not cloud_name: cloud_name = get_active_cloud_name() try: return get_cloud(cloud_name) except CloudNotRegisteredException as e: raise CLIError(e)
def show_cloud(cmd, cloud_name=None): if not cloud_name: cloud_name = cmd.cli_ctx.cloud.name try: return get_cloud(cmd.cli_ctx, cloud_name) except CloudNotRegisteredException as e: raise CLIError(e)
def test_get_clouds_concurrent(self): with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as config_file: # Max pool_size is 61, otherwise exception will be thrown on Python 3.8 Windows: # File "...Python38\lib\multiprocessing\connection.py", line 810, in _exhaustive_wait # res = _winapi.WaitForMultipleObjects(L, False, timeout) # ValueError: need at most 63 handles, got a sequence of length 102 pool_size = 20 p = multiprocessing.Pool(pool_size) p.map(_helper_get_clouds, range(pool_size)) p.close() p.join() # Check we can read the file with no exceptions cli = DummyCli() configparser.ConfigParser().read(config_file) for kc in KNOWN_CLOUDS: get_cloud(cli, kc.name)
def test_modify_known_cloud(self): with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as config_file: cli = TestCli() config = cli.config.config_parser cloud_name = AZURE_PUBLIC_CLOUD.name cloud = get_cloud(cli, cloud_name) self.assertEqual(cloud.name, cloud_name) mcloud = Cloud(cloud_name) mcloud.endpoints.gallery = 'https://mynewcustomgallery.azure.com' update_cloud(cli, mcloud) cloud = get_cloud(cli, cloud_name) self.assertEqual(cloud.endpoints.gallery, 'https://mynewcustomgallery.azure.com') # Check that the config file only has what we changed, not the full cloud info. config.read(config_file) items = config.items(cloud_name) self.assertEqual(len(items), 1) self.assertEqual(items[0], ('endpoint_gallery', 'https://mynewcustomgallery.azure.com'))
def create_context(context_name, cloud_name, skip_exists_check=False): from azure.cli.core.cloud import get_cloud if not skip_exists_check and context_exists(context_name): raise ContextExistsException(context_name) cloud = get_cloud(cloud_name) context_file_path = get_context_file_path(context_name) context_config = configparser.SafeConfigParser() context_config.read(context_file_path) try: context_config.add_section('context') except configparser.DuplicateSectionError: pass context_config.set('context', 'cloud', cloud.name) if not os.path.isdir(CONTEXT_CONFIG_DIR): os.makedirs(CONTEXT_CONFIG_DIR) with open(context_file_path, 'w') as configfile: context_config.write(configfile)
def create_context(context_name, cloud_name, skip_exists_check=False): from azure.cli.core.cloud import get_cloud if not skip_exists_check and context_exists(context_name): raise ContextExistsException(context_name) cloud = get_cloud(cloud_name) context_file_path = get_context_file_path(context_name) context_config = configparser.SafeConfigParser() context_config.read(context_file_path) try: context_config.add_section('context') except configparser.DuplicateSectionError: pass context_config.set('context', 'cloud', cloud.name) if not os.path.isdir(CONTEXT_CONFIG_DIR): os.makedirs(CONTEXT_CONFIG_DIR) with open(context_file_path, 'w') as configfile: context_config.write(configfile)
def modify_context(context_name, cloud_name=None, default_subscription=None): from azure.cli.core.cloud import get_cloud if not context_exists(context_name): raise ContextNotFoundException(context_name) if cloud_name or default_subscription: context_file_path = get_context_file_path(context_name) context_config = configparser.SafeConfigParser() context_config.read(context_file_path) if cloud_name: cloud = get_cloud(cloud_name) context_config.set('context', 'cloud', cloud.name) if default_subscription is not None: if default_subscription: context_config.set('context', 'default_subscription', default_subscription) else: context_config.remove_option('context', 'default_subscription') with open(context_file_path, 'w') as configfile: context_config.write(configfile) active_context = get_active_context() if context_name == active_context['name']: # User has changed the cloud or default subscription for the current context # so update the active subscription we use _set_active_subscription(active_context)
def modify_context(context_name, cloud_name=None, default_subscription=None): from azure.cli.core.cloud import get_cloud if not context_exists(context_name): raise ContextNotFoundException(context_name) if cloud_name or default_subscription: context_file_path = get_context_file_path(context_name) context_config = configparser.SafeConfigParser() context_config.read(context_file_path) if cloud_name: cloud = get_cloud(cloud_name) context_config.set('context', 'cloud', cloud.name) if default_subscription is not None: if default_subscription: context_config.set('context', 'default_subscription', default_subscription) else: context_config.remove_option('context', 'default_subscription') with open(context_file_path, 'w') as configfile: context_config.write(configfile) active_context = get_active_context() if context_name == active_context['name']: # User has changed the cloud or default subscription for the current context # so update the active subscription we use _set_active_subscription(active_context)
_TOKEN_ENTRY_TOKEN_TYPE = 'tokenType' #This could mean either real access token, or client secret of a service principal #This naming is no good, but can't change because xplat-cli does so. _ACCESS_TOKEN = 'accessToken' TOKEN_FIELDS_EXCLUDED_FROM_PERSISTENCE = [ 'familyName', 'givenName', 'isUserIdDisplayable', 'tenantId' ] _CLIENT_ID = '04b07795-8ddb-461a-bbee-02f9e1bf7b46' _COMMON_TENANT = 'common' _AUTH_CTX_FACTORY = lambda authority, cache: adal.AuthenticationContext( authority, cache=cache, api_version=None) CLOUD = get_cloud(get_active_context()['cloud']) def get_authority_url(tenant=None): return CLOUD.endpoints.active_directory + '/' + (tenant or _COMMON_TENANT) def _load_tokens_from_file(file_path): all_entries = [] if os.path.isfile(file_path): all_entries = get_file_json(file_path, throw_on_empty=False) or [] return all_entries def _delete_file(file_path): try:
def test_get_known_clouds(self): with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]): # Check that we can get all the known clouds without any exceptions for kc in KNOWN_CLOUDS: get_cloud(kc.name)
def show_cloud(cloud_name): try: return get_cloud(cloud_name) except CloudNotRegisteredException as e: raise CLIError(e)
TOKEN_FIELDS_EXCLUDED_FROM_PERSISTENCE = ['familyName', 'givenName', 'isUserIdDisplayable', 'tenantId'] _CLIENT_ID = '04b07795-8ddb-461a-bbee-02f9e1bf7b46' _COMMON_TENANT = 'common' def _authentication_context_factory(authority, cache): return adal.AuthenticationContext(authority, cache=cache, api_version=None) _AUTH_CTX_FACTORY = _authentication_context_factory CLOUD = get_cloud(get_active_context()['cloud']) def get_authority_url(tenant=None): return CLOUD.endpoints.active_directory + '/' + (tenant or _COMMON_TENANT) def _load_tokens_from_file(file_path): all_entries = [] if os.path.isfile(file_path): all_entries = get_file_json(file_path, throw_on_empty=False) or [] return all_entries def _delete_file(file_path): try:
def show_cloud(cloud_name): try: return get_cloud(cloud_name) except CloudNotRegisteredException as e: raise CLIError(e)