Пример #1
0
def get_cloud_subscription(cloud_name):
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    try:
        return config.get(cloud_name, 'subscription')
    except (configparser.NoOptionError, configparser.NoSectionError):
        return None
Пример #2
0
def get_clouds():
    clouds = []
    # load the config again as it may have changed
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    for section in config.sections():
        c = Cloud(section)
        for option in config.options(section):
            if option == 'profile':
                c.profile = config.get(section, option)
            if option.startswith('endpoint_'):
                setattr(c.endpoints, option.replace('endpoint_', ''), config.get(section, option))
            elif option.startswith('suffix_'):
                setattr(c.suffixes, option.replace('suffix_', ''), config.get(section, option))
        if c.profile is None:
            # If profile isn't set, use latest
            setattr(c, 'profile', 'latest')
        if c.profile not in API_PROFILES:
            raise CLIError('Profile {} does not exist or is not supported.'.format(c.profile))
        if not c.endpoints.has_endpoint_set('management') and \
                c.endpoints.has_endpoint_set('resource_manager'):
            # If management endpoint not set, use resource manager endpoint
            c.endpoints.management = c.endpoints.resource_manager
        clouds.append(c)
    active_cloud_name = get_active_cloud_name()
    for c in clouds:
        if c.name == active_cloud_name:
            c.is_active = True
            break
    return clouds
Пример #3
0
 def test_set_config_value(self):
     with mock.patch('azure.cli.core._config.GLOBAL_CONFIG_DIR', self.config_dir), \
     mock.patch('azure.cli.core._config.GLOBAL_CONFIG_PATH', self.config_path):
         set_global_config_value('test_section', 'test_option', 'a_value')
         config = get_config_parser()
         config.read(os.path.join(self.config_dir, CONFIG_FILE_NAME))
         self.assertEqual(config.get('test_section', 'test_option'), 'a_value')
Пример #4
0
def get_cloud_subscription(cloud_name):
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    try:
        return config.get(cloud_name, 'subscription')
    except (configparser.NoOptionError, configparser.NoSectionError):
        return None
Пример #5
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)
Пример #6
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)
Пример #7
0
def _init_known_clouds():
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    stored_cloud_names = config.sections()
    for c in KNOWN_CLOUDS:
        if c.name not in stored_cloud_names:
            _save_cloud(c)
Пример #8
0
 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)
Пример #9
0
def init_known_clouds(force=False):
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    stored_cloud_names = config.sections()
    for c in KNOWN_CLOUDS:
        if force or c.name not in stored_cloud_names:
            _save_cloud(c, overwrite=force)
Пример #10
0
def get_active_cloud_name():
    global_config = get_config_parser()
    global_config.read(GLOBAL_CONFIG_PATH)
    try:
        return global_config.get('cloud', 'name')
    except (configparser.NoOptionError, configparser.NoSectionError):
        _set_active_cloud(AZURE_PUBLIC_CLOUD.name)
        return AZURE_PUBLIC_CLOUD.name
Пример #11
0
def _save_cloud(cloud, overwrite=False):
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    _config_add_cloud(config, cloud, overwrite=overwrite)
    if not os.path.isdir(GLOBAL_CONFIG_DIR):
        os.makedirs(GLOBAL_CONFIG_DIR)
    with open(CLOUD_CONFIG_FILE, 'w') as configfile:
        config.write(configfile)
Пример #12
0
def get_active_cloud_name():
    global_config = get_config_parser()
    global_config.read(GLOBAL_CONFIG_PATH)
    try:
        return global_config.get('cloud', 'name')
    except (configparser.NoOptionError, configparser.NoSectionError):
        _set_active_cloud(AZURE_PUBLIC_CLOUD.name)
        return AZURE_PUBLIC_CLOUD.name
Пример #13
0
 def test_set_config_value(self):
     with mock.patch('azure.cli.core._config.GLOBAL_CONFIG_DIR', self.config_dir), \
     mock.patch('azure.cli.core._config.GLOBAL_CONFIG_PATH', self.config_path):
         set_global_config_value('test_section', 'test_option', 'a_value')
         config = get_config_parser()
         config.read(os.path.join(self.config_dir, CONFIG_FILE_NAME))
         self.assertEqual(config.get('test_section', 'test_option'),
                          'a_value')
Пример #14
0
def _handle_global_configuration():
    # print location of global configuration
    print(MSG_GLOBAL_SETTINGS_LOCATION.format(GLOBAL_CONFIG_PATH))
    # set up the config parsers
    file_config = get_config_parser()
    config_exists = file_config.read([GLOBAL_CONFIG_PATH])
    global_config = get_config_parser()
    global_config.read(GLOBAL_CONFIG_PATH)
    should_modify_global_config = False
    if config_exists:
        # print current config and prompt to allow global config modification
        _print_cur_configuration(file_config)
        should_modify_global_config = prompt_y_n(MSG_PROMPT_MANAGE_GLOBAL,
                                                 default='n')
        answers['modify_global_prompt'] = should_modify_global_config
    if not config_exists or should_modify_global_config:
        # no config exists yet so configure global config or user wants to modify global config
        output_index = prompt_choice_list(MSG_PROMPT_GLOBAL_OUTPUT,
                                          OUTPUT_LIST,
                                          default=get_default_from_config(
                                              global_config, 'core', 'output',
                                              OUTPUT_LIST))
        answers['output_type_prompt'] = output_index
        answers['output_type_options'] = str(OUTPUT_LIST)
        enable_file_logging = prompt_y_n(MSG_PROMPT_FILE_LOGGING, default='n')
        allow_telemetry = prompt_y_n(MSG_PROMPT_TELEMETRY, default='y')
        answers['telemetry_prompt'] = allow_telemetry
        # save the global config
        try:
            global_config.add_section('core')
        except configparser.DuplicateSectionError:
            pass
        try:
            global_config.add_section('logging')
        except configparser.DuplicateSectionError:
            pass
        global_config.set('core', 'output', OUTPUT_LIST[output_index]['name'])
        global_config.set('core', 'collect_telemetry',
                          'yes' if allow_telemetry else 'no')
        global_config.set('logging', 'enable_log_file',
                          'yes' if enable_file_logging else 'no')
        set_global_config(global_config)
Пример #15
0
def init_known_clouds(force=False):
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    stored_cloud_names = config.sections()
    for c in KNOWN_CLOUDS:
        if force or c.name not in stored_cloud_names:
            _config_add_cloud(config, c, overwrite=force)
    if not os.path.isdir(GLOBAL_CONFIG_DIR):
        os.makedirs(GLOBAL_CONFIG_DIR)
    with open(CLOUD_CONFIG_FILE, 'w') as configfile:
        config.write(configfile)
Пример #16
0
 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
         config = get_config_parser()
         config.read(config_file)
         for kc in KNOWN_CLOUDS:
             get_cloud(kc.name)
Пример #18
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()
Пример #19
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()
Пример #20
0
def set_cloud_subscription(cloud_name, subscription):
    if not _get_cloud(cloud_name):
        raise CloudNotRegisteredException(cloud_name)
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    if subscription:
        config.set(cloud_name, 'subscription', subscription)
    else:
        config.remove_option(cloud_name, 'subscription')
    if not os.path.isdir(GLOBAL_CONFIG_DIR):
        os.makedirs(GLOBAL_CONFIG_DIR)
    with open(CLOUD_CONFIG_FILE, 'w') as configfile:
        config.write(configfile)
Пример #21
0
def set_cloud_subscription(cloud_name, subscription):
    if not _get_cloud(cloud_name):
        raise CloudNotRegisteredException(cloud_name)
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    if subscription:
        config.set(cloud_name, 'subscription', subscription)
    else:
        config.remove_option(cloud_name, 'subscription')
    if not os.path.isdir(GLOBAL_CONFIG_DIR):
        os.makedirs(GLOBAL_CONFIG_DIR)
    with open(CLOUD_CONFIG_FILE, 'w') as configfile:
        config.write(configfile)
Пример #22
0
def _handle_global_configuration():
    # print location of global configuration
    print(MSG_GLOBAL_SETTINGS_LOCATION.format(GLOBAL_CONFIG_PATH))
    # set up the config parsers
    file_config = get_config_parser()
    config_exists = file_config.read([GLOBAL_CONFIG_PATH])
    global_config = get_config_parser()
    global_config.read(GLOBAL_CONFIG_PATH)
    should_modify_global_config = False
    if config_exists:
        # print current config and prompt to allow global config modification
        _print_cur_configuration(file_config)
        should_modify_global_config = prompt_y_n(MSG_PROMPT_MANAGE_GLOBAL, default='n')
        answers['modify_global_prompt'] = should_modify_global_config
    if not config_exists or should_modify_global_config:
        # no config exists yet so configure global config or user wants to modify global config
        output_index = prompt_choice_list(MSG_PROMPT_GLOBAL_OUTPUT, OUTPUT_LIST,
                                          default=get_default_from_config(global_config,
                                                                          'core', 'output',
                                                                          OUTPUT_LIST))
        answers['output_type_prompt'] = output_index
        answers['output_type_options'] = str(OUTPUT_LIST)
        enable_file_logging = prompt_y_n(MSG_PROMPT_FILE_LOGGING, default='n')
        allow_telemetry = prompt_y_n(MSG_PROMPT_TELEMETRY, default='y')
        answers['telemetry_prompt'] = allow_telemetry
        # save the global config
        try:
            global_config.add_section('core')
        except configparser.DuplicateSectionError:
            pass
        try:
            global_config.add_section('logging')
        except configparser.DuplicateSectionError:
            pass
        global_config.set('core', 'output', OUTPUT_LIST[output_index]['name'])
        global_config.set('core', 'collect_telemetry', 'yes' if allow_telemetry else 'no')
        global_config.set('logging', 'enable_log_file', 'yes' if enable_file_logging else 'no')
        set_global_config(global_config)
Пример #23
0
 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)
Пример #24
0
def remove_cloud(cloud_name):
    if not _get_cloud(cloud_name):
        raise CloudNotRegisteredException(cloud_name)
    if cloud_name == get_active_cloud_name():
        raise CannotUnregisterCloudException("The cloud '{}' cannot be unregistered "
                                             "as it's currently active.".format(cloud_name))
    is_known_cloud = next((x for x in KNOWN_CLOUDS if x.name == cloud_name), None)
    if is_known_cloud:
        raise CannotUnregisterCloudException("The cloud '{}' cannot be unregistered "
                                             "as it's not a custom cloud.".format(cloud_name))
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    config.remove_section(cloud_name)
    with open(CLOUD_CONFIG_FILE, 'w') as configfile:
        config.write(configfile)
Пример #25
0
 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'))
Пример #26
0
def _save_cloud(cloud, overwrite=False):
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    try:
        config.add_section(cloud.name)
    except configparser.DuplicateSectionError:
        if not overwrite:
            raise CloudAlreadyRegisteredException(cloud.name)
    for k, v in cloud.endpoints.__dict__.items():
        if v is not None:
            config.set(cloud.name, 'endpoint_{}'.format(k), v)
    for k, v in cloud.suffixes.__dict__.items():
        if v is not None:
            config.set(cloud.name, 'suffix_{}'.format(k), v)
    if not os.path.isdir(GLOBAL_CONFIG_DIR):
        os.makedirs(GLOBAL_CONFIG_DIR)
    with open(CLOUD_CONFIG_FILE, 'w') as configfile:
        config.write(configfile)
 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'))
Пример #28
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)
Пример #29
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)
Пример #30
0
def get_clouds():
    # ensure the known clouds are always in cloud config
    _init_known_clouds()
    clouds = []
    # load the config again as it may have changed
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    for section in config.sections():
        c = Cloud(section)
        for option in config.options(section):
            if option.startswith('endpoint_'):
                setattr(c.endpoints, option.replace('endpoint_', ''), config.get(section, option))
            elif option.startswith('suffix_'):
                setattr(c.suffixes, option.replace('suffix_', ''), config.get(section, option))
        clouds.append(c)
    active_cloud_name = get_active_cloud_name()
    for c in clouds:
        if c.name == active_cloud_name:
            c.is_active = True
            break
    return clouds