def _build_cloud(cli_ctx, cloud_name, cloud_config=None, cloud_args=None): from msrestazure.azure_cloud import _populate_from_metadata_endpoint, MetadataEndpointError if cloud_config: # Using JSON format so convert the keys to snake case for key in cloud_config: cloud_config[to_snake_case(key)] = cloud_config.pop(key) cloud_args = cloud_config c = Cloud(cloud_name) c.profile = cloud_args.get('profile', None) for arg in cloud_args: if arg.startswith('endpoint_') and cloud_args[arg] is not None: setattr(c.endpoints, arg.replace('endpoint_', ''), cloud_args[arg]) elif arg.startswith('suffix_') and cloud_args[arg] is not None: setattr(c.suffixes, arg.replace('suffix_', ''), cloud_args[arg]) arm_endpoint = cloud_args.get('endpoint_resource_manager', None) try: _populate_from_metadata_endpoint(c, arm_endpoint) except MetadataEndpointError as err: raise CLIError(err) required_endpoints = {'resource_manager': '--endpoint-resource-manager', 'active_directory': '--endpoint-active-directory', 'active_directory_resource_id': '--endpoint-active-directory-resource-id', 'active_directory_graph_resource_id': '--endpoint-active-directory-graph-resource-id'} missing_endpoints = [e_param for e_name, e_param in required_endpoints.items() if not c.endpoints.has_endpoint_set(e_name)] if missing_endpoints and not cloud_is_registered(cli_ctx, cloud_name): raise CLIError("The following endpoints are required for the CLI to function and were not specified on the " "command line, in the cloud config or could not be autodetected.\n" "Specify them on the command line or through the cloud config file:\n" "{}".format(', '.join(missing_endpoints))) return c
def _build_cloud(cloud_name, cloud_config=None, cloud_args=None): if cloud_config: # Using JSON format so convert the keys to snake case for key in cloud_config: cloud_config[to_snake_case(key)] = cloud_config.pop(key) cloud_args = cloud_config c = Cloud(cloud_name) c.profile = cloud_args.get('profile', None) for arg in cloud_args: if arg.startswith('endpoint_') and cloud_args[arg] is not None: setattr(c.endpoints, arg.replace('endpoint_', ''), cloud_args[arg]) elif arg.startswith('suffix_') and cloud_args[arg] is not None: setattr(c.suffixes, arg.replace('suffix_', ''), cloud_args[arg]) return c
def test_add_get_delete_custom_cloud(self): endpoints = CloudEndpoints(management='http://management.contoso.com') suffixes = CloudSuffixes(storage_endpoint='core.contoso.com') c = Cloud('MyOwnCloud', endpoints=endpoints, suffixes=suffixes) expected_config_file_result = '[MyOwnCloud]\nendpoint_management = ' \ 'http://management.contoso.com\nsuffix_storage_endpoint = ' \ 'core.contoso.com\n\n' with mock.patch('azure.cli.core.cloud.CUSTOM_CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as\ config_file: with mock.patch('azure.cli.core.cloud.get_custom_clouds', lambda: []): add_cloud(c) with open(config_file, 'r') as cf: self.assertEqual(cf.read(), expected_config_file_result) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 1) self.assertEqual(custom_clouds[0].name, c.name) self.assertEqual(custom_clouds[0].endpoints.management, c.endpoints.management) self.assertEqual(custom_clouds[0].suffixes.storage_endpoint, c.suffixes.storage_endpoint) with mock.patch('azure.cli.core.cloud._get_cloud', lambda _: c): remove_cloud(c.name) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 0)
def test_get_api_version_semver(self): # Can get correct resource type API version if semver used cli = DummyCli() cli.cloud = Cloud('TestCloud', profile='2017-01-01-profile') test_profile = { '2017-01-01-profile': { ResourceType.MGMT_KEYVAULT: '7.0' } } with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile): self.assertEqual(get_api_version(cli, ResourceType.MGMT_KEYVAULT), '7.0')
def test_supported_api_version_max_constraint_not_supported(self): cli = DummyCli() cli.cloud = Cloud('TestCloud', profile='2017-01-01-profile') test_profile = { '2017-01-01-profile': { ResourceType.MGMT_STORAGE: '2020-10-10' } } with mock.patch('azure.cli.core.profiles._shared.AZURE_API_PROFILES', test_profile): self.assertFalse( supported_api_version(cli, ResourceType.MGMT_STORAGE, max_api='2019-01-01'))
def test_custom_cloud_management_endpoint_set(self): ''' We have set management endpoint so don't override it ''' endpoint_rm = 'http://management.contoso.com' endpoint_mgmt = 'http://management.core.contoso.com' endpoints = CloudEndpoints(resource_manager=endpoint_rm, management=endpoint_mgmt) profile = '2017-03-09-profile-preview' c = Cloud('MyOwnCloud', endpoints=endpoints, profile=profile) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]): add_cloud(c) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 1) self.assertEqual(custom_clouds[0].endpoints.resource_manager, c.endpoints.resource_manager) # CLI logic should keep our set management endpoint self.assertEqual(custom_clouds[0].endpoints.management, c.endpoints.management)
class TestCloud(unittest.TestCase): @mock.patch('azure.cli.core._profile.CLOUD', Cloud('AzureCloud')) def test_endpoint_none(self): with self.assertRaises(CloudEndpointNotSetException): profile = Profile() profile.get_login_credentials() @mock.patch('azure.cli.core.cloud.get_custom_clouds', lambda: []) def test_add_get_delete_custom_cloud(self): endpoint_rm = 'http://management.contoso.com' suffix_storage = 'core.contoso.com' endpoints = CloudEndpoints(resource_manager=endpoint_rm) suffixes = CloudSuffixes(storage_endpoint=suffix_storage) c = Cloud('MyOwnCloud', endpoints=endpoints, suffixes=suffixes) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as\ config_file: with mock.patch('azure.cli.core.cloud.get_custom_clouds', lambda: []): add_cloud(c) config = configparser.SafeConfigParser() config.read(config_file) self.assertTrue(c.name in config.sections()) self.assertEqual( config.get(c.name, 'endpoint_resource_manager'), endpoint_rm) self.assertEqual(config.get(c.name, 'suffix_storage_endpoint'), suffix_storage) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 1) self.assertEqual(custom_clouds[0].name, c.name) self.assertEqual(custom_clouds[0].endpoints.resource_manager, c.endpoints.resource_manager) self.assertEqual(custom_clouds[0].suffixes.storage_endpoint, c.suffixes.storage_endpoint) with mock.patch('azure.cli.core.cloud._get_cloud', lambda _: c): remove_cloud(c.name) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 0) def test_get_active_cloud_name_default(self): expected = AZURE_PUBLIC_CLOUD.name actual = get_active_cloud_name() self.assertEqual(expected, actual)
def test_supported_api_profile_min_constraint_not_supported(self): cli = DummyCli() cli.cloud = Cloud('TestCloud', profile='2000-01-01-profile-preview') self.assertFalse( supported_api_version(cli, PROFILE_TYPE, min_api='2000-01-02'))
class TestCloud(unittest.TestCase): @mock.patch('azure.cli.core._profile.CLOUD', Cloud('AzureCloud')) def test_endpoint_none(self): with self.assertRaises(CloudEndpointNotSetException): profile = Profile() profile.get_login_credentials() @mock.patch('azure.cli.core.cloud.get_custom_clouds', lambda: []) def test_add_get_delete_custom_cloud(self): endpoint_rm = 'http://management.contoso.com' suffix_storage = 'core.contoso.com' endpoints = CloudEndpoints(resource_manager=endpoint_rm) suffixes = CloudSuffixes(storage_endpoint=suffix_storage) c = Cloud('MyOwnCloud', endpoints=endpoints, suffixes=suffixes) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as\ config_file: with mock.patch('azure.cli.core.cloud.get_custom_clouds', lambda: []): add_cloud(c) config = get_config_parser() config.read(config_file) self.assertTrue(c.name in config.sections()) self.assertEqual(config.get(c.name, 'endpoint_resource_manager'), endpoint_rm) self.assertEqual(config.get(c.name, 'suffix_storage_endpoint'), suffix_storage) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 1) self.assertEqual(custom_clouds[0].name, c.name) self.assertEqual(custom_clouds[0].endpoints.resource_manager, c.endpoints.resource_manager) self.assertEqual(custom_clouds[0].suffixes.storage_endpoint, c.suffixes.storage_endpoint) with mock.patch('azure.cli.core.cloud._get_cloud', lambda _: c): remove_cloud(c.name) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 0) def test_add_get_cloud_with_profile(self): endpoint_rm = 'http://management.contoso.com' endpoints = CloudEndpoints(resource_manager=endpoint_rm) profile = '2017-03-09-profile-preview' c = Cloud('MyOwnCloud', endpoints=endpoints, profile=profile) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as\ config_file: add_cloud(c) config = get_config_parser() config.read(config_file) self.assertTrue(c.name in config.sections()) self.assertEqual(config.get(c.name, 'endpoint_resource_manager'), endpoint_rm) self.assertEqual(config.get(c.name, 'profile'), profile) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 1) self.assertEqual(custom_clouds[0].name, c.name) self.assertEqual(custom_clouds[0].endpoints.resource_manager, c.endpoints.resource_manager) self.assertEqual(custom_clouds[0].profile, c.profile) def test_add_get_cloud_with_invalid_profile(self): ''' Cloud has profile that doesn't exist so an exception should be raised ''' profile = 'none-existent-profile' c = Cloud('MyOwnCloud', profile=profile) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as\ config_file: add_cloud(c) config = get_config_parser() config.read(config_file) self.assertTrue(c.name in config.sections()) self.assertEqual(config.get(c.name, 'profile'), profile) with self.assertRaises(CLIError): get_custom_clouds() def test_get_default_latest_profile(self): with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]): clouds = get_clouds() for c in clouds: self.assertEqual(c.profile, 'latest') def test_custom_cloud_management_endpoint_set(self): ''' We have set management endpoint so don't override it ''' endpoint_rm = 'http://management.contoso.com' endpoint_mgmt = 'http://management.core.contoso.com' endpoints = CloudEndpoints(resource_manager=endpoint_rm, management=endpoint_mgmt) profile = '2017-03-09-profile-preview' c = Cloud('MyOwnCloud', endpoints=endpoints, profile=profile) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]): add_cloud(c) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 1) self.assertEqual(custom_clouds[0].endpoints.resource_manager, c.endpoints.resource_manager) # CLI logic should keep our set management endpoint self.assertEqual(custom_clouds[0].endpoints.management, c.endpoints.management) def test_custom_cloud_no_management_endpoint_set(self): ''' Use ARM 'resource manager' endpoint as 'management' (old ASM) endpoint if only ARM endpoint is set ''' endpoint_rm = 'http://management.contoso.com' endpoints = CloudEndpoints(resource_manager=endpoint_rm) profile = '2017-03-09-profile-preview' c = Cloud('MyOwnCloud', endpoints=endpoints, profile=profile) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]): add_cloud(c) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 1) self.assertEqual(custom_clouds[0].endpoints.resource_manager, c.endpoints.resource_manager) # CLI logic should add management endpoint to equal resource_manager as we didn't set it self.assertEqual(custom_clouds[0].endpoints.management, c.endpoints.resource_manager) def test_get_active_cloud_name_default(self): expected = AZURE_PUBLIC_CLOUD.name actual = get_active_cloud_name() self.assertEqual(expected, actual)
class TestCloud(unittest.TestCase): @mock.patch('azure.cli.core._profile.CLOUD', Cloud('AzureCloud')) def test_endpoint_none(self): with self.assertRaises(CloudEndpointNotSetException): profile = Profile() profile.get_login_credentials() @mock.patch('azure.cli.core.cloud.get_custom_clouds', lambda: []) def test_add_get_delete_custom_cloud(self): endpoint_rm = 'http://management.contoso.com' suffix_storage = 'core.contoso.com' endpoints = CloudEndpoints(resource_manager=endpoint_rm) suffixes = CloudSuffixes(storage_endpoint=suffix_storage) c = Cloud('MyOwnCloud', endpoints=endpoints, suffixes=suffixes) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as\ config_file: with mock.patch('azure.cli.core.cloud.get_custom_clouds', lambda: []): add_cloud(c) config = get_config_parser() config.read(config_file) self.assertTrue(c.name in config.sections()) self.assertEqual( config.get(c.name, 'endpoint_resource_manager'), endpoint_rm) self.assertEqual(config.get(c.name, 'suffix_storage_endpoint'), suffix_storage) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 1) self.assertEqual(custom_clouds[0].name, c.name) self.assertEqual(custom_clouds[0].endpoints.resource_manager, c.endpoints.resource_manager) self.assertEqual(custom_clouds[0].suffixes.storage_endpoint, c.suffixes.storage_endpoint) with mock.patch('azure.cli.core.cloud._get_cloud', lambda _: c): remove_cloud(c.name) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 0) def test_add_get_cloud_with_profile(self): endpoint_rm = 'http://management.contoso.com' endpoints = CloudEndpoints(resource_manager=endpoint_rm) profile = '2017-03-09-profile' c = Cloud('MyOwnCloud', endpoints=endpoints, profile=profile) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as\ config_file: add_cloud(c) config = get_config_parser() config.read(config_file) self.assertTrue(c.name in config.sections()) self.assertEqual(config.get(c.name, 'endpoint_resource_manager'), endpoint_rm) self.assertEqual(config.get(c.name, 'profile'), profile) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 1) self.assertEqual(custom_clouds[0].name, c.name) self.assertEqual(custom_clouds[0].endpoints.resource_manager, c.endpoints.resource_manager) self.assertEqual(custom_clouds[0].profile, c.profile) def test_add_get_cloud_with_invalid_profile(self): # Cloud has profile that doesn't exist so an exception should be raised profile = 'none-existent-profile' c = Cloud('MyOwnCloud', profile=profile) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]) as\ config_file: add_cloud(c) config = get_config_parser() config.read(config_file) self.assertTrue(c.name in config.sections()) self.assertEqual(config.get(c.name, 'profile'), profile) with self.assertRaises(CLIError): get_custom_clouds() def test_get_default_latest_profile(self): with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]): clouds = get_clouds() for c in clouds: self.assertEqual(c.profile, 'latest') def test_custom_cloud_management_endpoint_set(self): # We have set management endpoint so don't override it endpoint_rm = 'http://management.contoso.com' endpoint_mgmt = 'http://management.core.contoso.com' endpoints = CloudEndpoints(resource_manager=endpoint_rm, management=endpoint_mgmt) profile = '2017-03-09-profile' c = Cloud('MyOwnCloud', endpoints=endpoints, profile=profile) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]): add_cloud(c) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 1) self.assertEqual(custom_clouds[0].endpoints.resource_manager, c.endpoints.resource_manager) # CLI logic should keep our set management endpoint self.assertEqual(custom_clouds[0].endpoints.management, c.endpoints.management) def test_custom_cloud_no_management_endpoint_set(self): # Use ARM 'resource manager' endpoint as 'management' (old ASM) endpoint if only ARM endpoint is set endpoint_rm = 'http://management.contoso.com' endpoints = CloudEndpoints(resource_manager=endpoint_rm) profile = '2017-03-09-profile' c = Cloud('MyOwnCloud', endpoints=endpoints, profile=profile) with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]): add_cloud(c) custom_clouds = get_custom_clouds() self.assertEqual(len(custom_clouds), 1) self.assertEqual(custom_clouds[0].endpoints.resource_manager, c.endpoints.resource_manager) # CLI logic should add management endpoint to equal resource_manager as we didn't set it self.assertEqual(custom_clouds[0].endpoints.management, c.endpoints.resource_manager) def test_get_active_cloud_name_default(self): expected = AZURE_PUBLIC_CLOUD.name actual = get_active_cloud_name() self.assertEqual(expected, actual) 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 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_remove_known_cloud(self): with mock.patch('azure.cli.core.cloud.CLOUD_CONFIG_FILE', tempfile.mkstemp()[1]): with self.assertRaises(CannotUnregisterCloudException): remove_cloud(AZURE_PUBLIC_CLOUD.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)