def register_cloud(cloud_name, cloud_config=None, endpoint_management=None, endpoint_resource_manager=None, endpoint_sql_management=None, endpoint_gallery=None, endpoint_active_directory=None, endpoint_active_directory_resource_id=None, endpoint_active_directory_graph_resource_id=None, suffix_sql_server_hostname=None, suffix_storage_endpoint=None, suffix_keyvault_dns=None, suffix_azure_datalake_store_file_system_endpoint=None, suffix_azure_datalake_analytics_catalog_and_job_endpoint=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 else: cloud_args = locals() c = Cloud(cloud_name) for arg in cloud_args: if arg.startswith('endpoint_') and cloud_args[arg]: setattr(c.endpoints, arg.replace('endpoint_', ''), cloud_args[arg]) elif arg.startswith('suffix_') and cloud_args[arg]: setattr(c.suffixes, arg.replace('suffix_', ''), cloud_args[arg]) try: add_cloud(c) except CloudAlreadyRegisteredException as e: raise CLIError(e)
def register_cloud( cloud_name, cloud_config=None, profile=None, endpoint_management=None, endpoint_resource_manager=None, endpoint_sql_management=None, endpoint_gallery=None, endpoint_active_directory=None, endpoint_active_directory_resource_id=None, endpoint_active_directory_graph_resource_id=None, endpoint_active_directory_data_lake_resource_id=None, endpoint_vm_image_alias_doc=None, suffix_sql_server_hostname=None, suffix_storage_endpoint=None, suffix_keyvault_dns=None, suffix_azure_datalake_store_file_system_endpoint=None, suffix_azure_datalake_analytics_catalog_and_job_endpoint=None): c = _build_cloud(cloud_name, cloud_config=cloud_config, cloud_args=locals()) try: add_cloud(c) except CloudAlreadyRegisteredException as e: raise CLIError(e)
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_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 register_cloud(cmd, cloud_name, cloud_config=None, profile=None, endpoint_management=None, endpoint_resource_manager=None, endpoint_sql_management=None, endpoint_gallery=None, endpoint_active_directory=None, endpoint_active_directory_resource_id=None, endpoint_active_directory_graph_resource_id=None, endpoint_active_directory_data_lake_resource_id=None, endpoint_vm_image_alias_doc=None, suffix_sql_server_hostname=None, suffix_storage_endpoint=None, suffix_keyvault_dns=None, suffix_azure_datalake_store_file_system_endpoint=None, suffix_azure_datalake_analytics_catalog_and_job_endpoint=None, suffix_acr_login_server_endpoint=None): c = _build_cloud(cmd.cli_ctx, cloud_name, cloud_config=cloud_config, cloud_args=locals()) try: add_cloud(cmd.cli_ctx, c) except CloudAlreadyRegisteredException as e: raise CLIError(e)
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_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_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_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-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_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_add_get_cloud_with_profile(self): cli = TestCli() 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(cli, c) config = cli.config.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(cli) 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_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)