Пример #1
0
def set_default(key, value):
    warnings.warn(
        "Use of set_default() is deprecated. Defaults should be set with the "
        "`override_defaults` parameter of OpenStackConfig."
    )
    defaults.get_defaults()  # make sure the dict is initialized
    defaults._defaults[key] = value
def set_default(key, value):
    warnings.warn(
        "Use of set_default() is deprecated. Defaults should be set with the "
        "`override_defaults` parameter of OpenStackConfig."
    )
    defaults.get_defaults()  # make sure the dict is initialized
    defaults._defaults[key] = value
Пример #3
0
 def test_legacy_client_object_store_password_v2(self,
                                                 mock_get_session_endpoint,
                                                 mock_get_auth_args):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://swift.example.com'
     mock_get_auth_args.return_value = dict(
         username='******',
         password='******',
         project_name='testproject',
         auth_url='http://example.com',
     )
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_legacy_client('object-store', mock_client)
     mock_client.assert_called_with(session=mock.ANY,
                                    os_options={
                                        'region_name': 'region-al',
                                        'service_type': 'object-store',
                                        'object_storage_url': None,
                                        'endpoint_type': 'public',
                                    })
Пример #4
0
 def test_get_session_no_auth(self):
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1", "region-al", config_dict)
     self.assertRaises(
         exceptions.OpenStackConfigException,
         cc.get_session)
Пример #5
0
 def test_legacy_client_object_store_endpoint(
         self, mock_get_auth_args):
     mock_client = mock.Mock()
     mock_get_auth_args.return_value = {}
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     config_dict['object_store_endpoint'] = 'http://example.com/swift'
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_legacy_client('object-store', mock_client)
     mock_client.assert_called_with(
         preauthtoken=mock.ANY,
         auth_version=u'2.0',
         authurl=None,
         key=None,
         os_options={
             'auth_token': mock.ANY,
             'region_name': 'region-al',
             'object_storage_url': 'http://example.com/swift',
             'user_id': None,
             'user_domain_name': None,
             'tenant_name': None,
             'project_domain_name': None,
             'project_domain_id': None,
             'tenant_id': None,
             'service_type': 'object-store',
             'endpoint_type': 'public',
             'user_domain_id': None
         },
         preauthurl='http://example.com/swift',
         user=None)
 def test_get_session_no_auth(self):
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1", "region-al", config_dict)
     self.assertRaises(
         exceptions.OpenStackConfigException,
         cc.get_session)
Пример #7
0
 def test_legacy_client_object_store_endpoint(self, mock_get_auth_args):
     mock_client = mock.Mock()
     mock_get_auth_args.return_value = {}
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     config_dict['object_store_endpoint'] = 'http://example.com/swift'
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_legacy_client('object-store', mock_client)
     mock_client.assert_called_with(preauthtoken=mock.ANY,
                                    auth_version=u'2.0',
                                    authurl=None,
                                    key=None,
                                    os_options={
                                        'auth_token': mock.ANY,
                                        'region_name': 'region-al',
                                        'object_storage_url':
                                        'http://example.com/swift',
                                        'user_id': None,
                                        'user_domain_name': None,
                                        'tenant_name': None,
                                        'project_domain_name': None,
                                        'project_domain_id': None,
                                        'tenant_id': None,
                                        'service_type': 'object-store',
                                        'endpoint_type': 'public',
                                        'user_domain_id': None
                                    },
                                    preauthurl='http://example.com/swift',
                                    user=None)
 def test_legacy_client_object_store_password(
         self,
         mock_get_session_endpoint,
         mock_get_auth_args,
         mock_get_api_version):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://swift.example.com'
     mock_get_api_version.return_value = '3'
     mock_get_auth_args.return_value = dict(
         username='******',
         password='******',
         project_name='testproject',
         auth_url='http://example.com',
     )
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_legacy_client('object-store', mock_client)
     mock_client.assert_called_with(
         session=mock.ANY,
         os_options={
             'region_name': 'region-al',
             'service_type': 'object-store',
             'object_storage_url': None,
             'endpoint_type': 'public',
         })
Пример #9
0
 def test_override_session_endpoint(self, mock_session):
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     self.assertEqual(
         cc.get_session_endpoint('telemetry'),
         fake_services_dict['telemetry_endpoint'])
 def test_override_session_endpoint(self, mock_session):
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     self.assertEqual(
         cc.get_session_endpoint('compute'),
         fake_services_dict['compute_endpoint'])
 def test_get_session(self, mock_session):
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_session()
     mock_session.assert_called_with(
         auth=mock.ANY,
         verify=True, cert=None, timeout=None)
 def test_session_endpoint_identity(self, mock_get_session):
     mock_session = mock.Mock()
     mock_get_session.return_value = mock_session
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_session_endpoint('identity')
     mock_session.get_endpoint.assert_called_with(
         interface=ksa_plugin.AUTH_INTERFACE)
 def test_session_endpoint_identity(self, mock_get_session):
     mock_session = mock.Mock()
     mock_get_session.return_value = mock_session
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_session_endpoint('identity')
     mock_session.get_endpoint.assert_called_with(
         interface=ksa_plugin.AUTH_INTERFACE)
 def test_get_session(self, mock_session):
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_session()
     mock_session.assert_called_with(auth=mock.ANY,
                                     verify=True,
                                     cert=None,
                                     timeout=None)
Пример #15
0
 def test_session_endpoint(self, mock_get_session):
     mock_session = mock.Mock()
     mock_get_session.return_value = mock_session
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_session_endpoint('orchestration')
     mock_session.get_endpoint.assert_called_with(
         interface='public',
         service_name=None,
         region_name='region-al',
         service_type='orchestration')
Пример #16
0
def _get_os_environ():
    ret = defaults.get_defaults()
    environkeys = [k for k in os.environ.keys()
                   if k.startswith('OS_')
                   and not k.startswith('OS_TEST')  # infra CI var
                   and not k.startswith('OS_STD')   # infra CI var
                   ]
    if not environkeys:
        return None
    for k in environkeys:
        newkey = k[3:].lower()
        ret[newkey] = os.environ[k]
    return ret
 def test_session_endpoint(self, mock_get_session):
     mock_session = mock.Mock()
     mock_get_session.return_value = mock_session
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_session_endpoint('orchestration')
     mock_session.get_endpoint.assert_called_with(
         interface='public',
         service_name=None,
         region_name='region-al',
         service_type='orchestration')
 def test_legacy_client_compute(self, mock_get_session_endpoint):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://example.com/v2'
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_legacy_client('compute', mock_client)
     mock_client.assert_called_with(
         '2',
         endpoint_type='public',
         region_name='region-al',
         service_type='compute',
         session=mock.ANY,
         service_name=None)
Пример #19
0
 def test_get_session(self, mock_session):
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     fake_session = mock.Mock()
     fake_session.additional_user_agent = []
     mock_session.return_value = fake_session
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_session()
     mock_session.assert_called_with(
         auth=mock.ANY,
         verify=True, cert=None, timeout=None)
     self.assertEqual(
         fake_session.additional_user_agent,
         [('os-client-config', '1.2.3')])
Пример #20
0
 def test_legacy_client_compute(self, mock_get_session_endpoint):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://example.com/v2'
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_legacy_client('compute', mock_client)
     mock_client.assert_called_with(
         version='2',
         endpoint_type='public',
         endpoint_override='http://compute.example.com',
         region_name='region-al',
         service_type='compute',
         session=mock.ANY,
         service_name=None)
Пример #21
0
def _get_os_environ():
    ret = defaults.get_defaults()
    environkeys = [k for k in os.environ.keys()
                   if k.startswith('OS_')
                   and not k.startswith('OS_TEST')  # infra CI var
                   and not k.startswith('OS_STD')   # infra CI var
                   ]
    # If the only environ key is region name, don't make a cloud, because
    # it's being used as a cloud selector
    if not environkeys or (
            len(environkeys) == 1 and 'OS_REGION_NAME' in environkeys):
        return None
    for k in environkeys:
        newkey = k[3:].lower()
        ret[newkey] = os.environ[k]
    return ret
 def test_legacy_client_identity_v3(self, mock_get_session_endpoint):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://example.com'
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     config_dict['identity_api_version'] = '3'
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_legacy_client('identity', mock_client)
     mock_client.assert_called_with(
         version=('3', '0'),
         endpoint='http://example.com',
         endpoint_type='admin',
         region_name='region-al',
         service_type='identity',
         session=mock.ANY,
         service_name='locks')
Пример #23
0
 def test_get_session(self, mock_session):
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     fake_session = mock.Mock()
     fake_session.additional_user_agent = []
     mock_session.return_value = fake_session
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_session()
     mock_session.assert_called_with(auth=mock.ANY,
                                     verify=True,
                                     cert=None,
                                     timeout=None)
     self.assertEqual(fake_session.additional_user_agent,
                      [('os-client-config', '1.2.3')])
 def test_legacy_client_object_store_endpoint(self):
     mock_client = mock.Mock()
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     config_dict['object_store_endpoint'] = 'http://example.com/v2'
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_legacy_client('object-store', mock_client)
     mock_client.assert_called_with(
         preauthtoken=mock.ANY,
         os_options={
             'auth_token': mock.ANY,
             'region_name': 'region-al',
             'object_storage_url': 'http://example.com/v2'
         },
         preauthurl='http://example.com/v2',
         auth_version='2.0')
 def test_legacy_client_object_store_endpoint(
         self, mock_get_auth_args):
     mock_client = mock.Mock()
     mock_get_auth_args.return_value = {}
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     config_dict['object_store_endpoint'] = 'http://example.com/swift'
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_legacy_client('object-store', mock_client)
     mock_client.assert_called_with(
         session=mock.ANY,
         os_options={
             'region_name': 'region-al',
             'service_type': 'object-store',
             'object_storage_url': 'http://example.com/swift',
             'endpoint_type': 'public',
         })
 def test_legacy_client_image(self, mock_get_session_endpoint):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://example.com/v2'
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_legacy_client('image', mock_client)
     mock_client.assert_called_with(
         '2',
         service_name=None,
         endpoint='http://example.com',
         region_name='region-al',
         interface='public',
         session=mock.ANY,
         # Not a typo - the config dict above overrides this
         service_type='mage'
     )
Пример #27
0
 def test_legacy_client_object_store_endpoint(
         self, mock_get_auth_args):
     mock_client = mock.Mock()
     mock_get_auth_args.return_value = {}
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     config_dict['object_store_endpoint'] = 'http://example.com/swift'
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_legacy_client('object-store', mock_client)
     mock_client.assert_called_with(
         session=mock.ANY,
         os_options={
             'region_name': 'region-al',
             'service_type': 'object-store',
             'object_storage_url': 'http://example.com/swift',
             'endpoint_type': 'public',
         })
Пример #28
0
 def test_legacy_client_image(self, mock_get_session_endpoint):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://example.com/v2'
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig(
         "test1", "region-al", config_dict, auth_plugin=mock.Mock())
     cc.get_legacy_client('image', mock_client)
     mock_client.assert_called_with(
         version=2.0,
         service_name=None,
         endpoint_override='http://example.com',
         region_name='region-al',
         interface='public',
         session=mock.ANY,
         # Not a typo - the config dict above overrides this
         service_type='mage'
     )
Пример #29
0
 def test_legacy_client_identity(self, mock_get_session_endpoint):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://example.com/v2'
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_legacy_client('identity', mock_client)
     mock_client.assert_called_with(version='2.0',
                                    endpoint='http://example.com/v2',
                                    interface='admin',
                                    endpoint_override=None,
                                    region_name='region-al',
                                    service_type='identity',
                                    session=mock.ANY,
                                    service_name='locks')
def _get_os_environ(envvar_prefix=None):
    ret = defaults.get_defaults()
    if not envvar_prefix:
        # This makes the or below be OS_ or OS_ which is a no-op
        envvar_prefix = 'OS_'
    environkeys = [k for k in os.environ.keys()
                   if (k.startswith('OS_') or k.startswith(envvar_prefix))
                   and not k.startswith('OS_TEST')  # infra CI var
                   and not k.startswith('OS_STD')   # infra CI var
                   ]
    for k in environkeys:
        newkey = k.split('_', 1)[-1].lower()
        ret[newkey] = os.environ[k]
    # If the only environ key is region name, don't make a cloud, because
    # it's being used as a cloud selector
    if not environkeys or (
            len(environkeys) == 1 and 'region_name' in ret):
        return None
    return ret
Пример #31
0
def _get_os_environ(envvar_prefix=None):
    ret = defaults.get_defaults()
    if not envvar_prefix:
        # This makes the or below be OS_ or OS_ which is a no-op
        envvar_prefix = 'OS_'
    environkeys = [
        k for k in os.environ.keys()
        if (k.startswith('OS_') or k.startswith(envvar_prefix))
        and not k.startswith('OS_TEST')  # infra CI var
        and not k.startswith('OS_STD')  # infra CI var
    ]
    for k in environkeys:
        newkey = k.split('_', 1)[-1].lower()
        ret[newkey] = os.environ[k]
    # If the only environ keys are cloud and region_name, don't return anything
    # because they are cloud selectors
    if set(environkeys) - set(['OS_CLOUD', 'OS_REGION_NAME']):
        return ret
    return None
Пример #32
0
def _get_os_environ(envvar_prefix=None):
    ret = defaults.get_defaults()
    if not envvar_prefix:
        # This makes the or below be OS_ or OS_ which is a no-op
        envvar_prefix = 'OS_'
    environkeys = [k for k in os.environ.keys()
                   if (k.startswith('OS_') or k.startswith(envvar_prefix))
                   and not k.startswith('OS_TEST')  # infra CI var
                   and not k.startswith('OS_STD')   # infra CI var
                   ]
    for k in environkeys:
        newkey = k.split('_', 1)[-1].lower()
        ret[newkey] = os.environ[k]
    # If the only environ key is region name, don't make a cloud, because
    # it's being used as a cloud selector
    if not environkeys or (
            len(environkeys) == 1 and 'region_name' in ret):
        return None
    return ret
Пример #33
0
def _get_os_environ(envvar_prefix=None):
    ret = defaults.get_defaults()
    if not envvar_prefix:
        # This makes the or below be OS_ or OS_ which is a no-op
        envvar_prefix = 'OS_'
    environkeys = [k for k in os.environ.keys()
                   if (k.startswith('OS_') or k.startswith(envvar_prefix))
                   and not k.startswith('OS_TEST')  # infra CI var
                   and not k.startswith('OS_STD')   # infra CI var
                   ]
    for k in environkeys:
        newkey = k.split('_', 1)[-1].lower()
        ret[newkey] = os.environ[k]
    # If the only environ keys are selectors or behavior modification, don't
    # return anything
    selectors = set([
        'OS_CLOUD', 'OS_REGION_NAME',
        'OS_CLIENT_CONFIG_FILE', 'OS_CLIENT_SECURE_FILE', 'OS_CLOUD_NAME'])
    if set(environkeys) - selectors:
        return ret
    return None
Пример #34
0
def _get_os_environ(envvar_prefix=None):
    ret = defaults.get_defaults()
    if not envvar_prefix:
        # This makes the or below be OS_ or OS_ which is a no-op
        envvar_prefix = 'OS_'
    environkeys = [k for k in os.environ.keys()
                   if (k.startswith('OS_') or k.startswith(envvar_prefix))
                   and not k.startswith('OS_TEST')  # infra CI var
                   and not k.startswith('OS_STD')   # infra CI var
                   ]
    for k in environkeys:
        newkey = k.split('_', 1)[-1].lower()
        ret[newkey] = os.environ[k]
    # If the only environ keys are selectors or behavior modification, don't
    # return anything
    selectors = set([
        'OS_CLOUD', 'OS_REGION_NAME',
        'OS_CLIENT_CONFIG_FILE', 'OS_CLIENT_SECURE_FILE', 'OS_CLOUD_NAME'])
    if set(environkeys) - selectors:
        return ret
    return None
 def test_legacy_client_object_store_endpoint(self):
     mock_client = mock.Mock()
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     config_dict['object_store_endpoint'] = 'http://example.com/v2'
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_legacy_client('object-store', mock_client)
     mock_client.assert_called_with(preauthtoken=mock.ANY,
                                    os_options={
                                        'auth_token':
                                        mock.ANY,
                                        'region_name':
                                        'region-al',
                                        'object_storage_url':
                                        'http://example.com/v2'
                                    },
                                    preauthurl='http://example.com/v2',
                                    auth_version='2.0')
Пример #36
0
    def __init__(self,
                 config_files=None,
                 vendor_files=None,
                 override_defaults=None,
                 force_ipv4=None,
                 envvar_prefix=None,
                 secure_files=None,
                 pw_func=None,
                 session_constructor=None,
                 app_name=None,
                 app_version=None,
                 load_yaml_config=True):
        self.log = _log.setup_logging(__name__)
        self._session_constructor = session_constructor
        self._app_name = app_name
        self._app_version = app_version

        if load_yaml_config:
            self._config_files = config_files or CONFIG_FILES
            self._secure_files = secure_files or SECURE_FILES
            self._vendor_files = vendor_files or VENDOR_FILES
        else:
            self._config_files = []
            self._secure_files = []
            self._vendor_files = []

        config_file_override = os.environ.pop('OS_CLIENT_CONFIG_FILE', None)
        if config_file_override:
            self._config_files.insert(0, config_file_override)

        secure_file_override = os.environ.pop('OS_CLIENT_SECURE_FILE', None)
        if secure_file_override:
            self._secure_files.insert(0, secure_file_override)

        self.defaults = defaults.get_defaults()
        if override_defaults:
            self.defaults.update(override_defaults)

        # First, use a config file if it exists where expected
        self.config_filename, self.cloud_config = self._load_config_file()
        _, secure_config = self._load_secure_file()
        if secure_config:
            self.cloud_config = _merge_clouds(self.cloud_config, secure_config)

        if not self.cloud_config:
            self.cloud_config = {'clouds': {}}
        if 'clouds' not in self.cloud_config:
            self.cloud_config['clouds'] = {}

        # Grab ipv6 preference settings from env
        client_config = self.cloud_config.get('client', {})

        if force_ipv4 is not None:
            # If it's passed in to the constructor, honor it.
            self.force_ipv4 = force_ipv4
        else:
            # Get the backwards compat value
            prefer_ipv6 = get_boolean(
                os.environ.pop(
                    'OS_PREFER_IPV6',
                    client_config.get('prefer_ipv6',
                                      client_config.get('prefer-ipv6', True))))
            force_ipv4 = get_boolean(
                os.environ.pop(
                    'OS_FORCE_IPV4',
                    client_config.get('force_ipv4',
                                      client_config.get('broken-ipv6',
                                                        False))))

            self.force_ipv4 = force_ipv4
            if not prefer_ipv6:
                # this will only be false if someone set it explicitly
                # honor their wishes
                self.force_ipv4 = True

        # Next, process environment variables and add them to the mix
        self.envvar_key = os.environ.pop('OS_CLOUD_NAME', 'envvars')
        if self.envvar_key in self.cloud_config['clouds']:
            raise exceptions.OpenStackConfigException(
                '"{0}" defines a cloud named "{1}", but'
                ' OS_CLOUD_NAME is also set to "{1}". Please rename'
                ' either your environment based cloud, or one of your'
                ' file-based clouds.'.format(self.config_filename,
                                             self.envvar_key))
        # Pull out OS_CLOUD so that if it's the only thing set, do not
        # make an envvars cloud
        self.default_cloud = os.environ.pop('OS_CLOUD', None)

        envvars = _get_os_environ(envvar_prefix=envvar_prefix)
        if envvars:
            self.cloud_config['clouds'][self.envvar_key] = envvars
            if not self.default_cloud:
                self.default_cloud = self.envvar_key

        # Finally, fall through and make a cloud that starts with defaults
        # because we need somewhere to put arguments, and there are neither
        # config files or env vars
        if not self.cloud_config['clouds']:
            self.cloud_config = dict(clouds=dict(defaults=dict(self.defaults)))
            self.default_cloud = 'defaults'

        self._cache_expiration_time = 0
        self._cache_path = CACHE_PATH
        self._cache_class = 'dogpile.cache.null'
        self._cache_arguments = {}
        self._cache_expiration = {}
        if 'cache' in self.cloud_config:
            cache_settings = self._normalize_keys(self.cloud_config['cache'])

            # expiration_time used to be 'max_age' but the dogpile setting
            # is expiration_time. Support max_age for backwards compat.
            self._cache_expiration_time = cache_settings.get(
                'expiration_time',
                cache_settings.get('max_age', self._cache_expiration_time))

            # If cache class is given, use that. If not, but if cache time
            # is given, default to memory. Otherwise, default to nothing.
            # to memory.
            if self._cache_expiration_time:
                self._cache_class = 'dogpile.cache.memory'
            self._cache_class = self.cloud_config['cache'].get(
                'class', self._cache_class)

            self._cache_path = os.path.expanduser(
                cache_settings.get('path', self._cache_path))
            self._cache_arguments = cache_settings.get('arguments',
                                                       self._cache_arguments)
            self._cache_expiration = cache_settings.get(
                'expiration', self._cache_expiration)

        # Flag location to hold the peeked value of an argparse timeout value
        self._argv_timeout = False

        # Save the password callback
        # password = self._pw_callback(prompt="Password: ")
        self._pw_callback = pw_func
Пример #37
0
    def __init__(self, config_files=None, vendor_files=None,
                 override_defaults=None, force_ipv4=None,
                 envvar_prefix=None, secure_files=None,
                 pw_func=None, session_constructor=None,
                 app_name=None, app_version=None,
                 load_yaml_config=True):
        self.log = _log.setup_logging(__name__)
        self._session_constructor = session_constructor
        self._app_name = app_name
        self._app_version = app_version

        if load_yaml_config:
            self._config_files = config_files or CONFIG_FILES
            self._secure_files = secure_files or SECURE_FILES
            self._vendor_files = vendor_files or VENDOR_FILES
        else:
            self._config_files = []
            self._secure_files = []
            self._vendor_files = []

        config_file_override = os.environ.get('OS_CLIENT_CONFIG_FILE')
        if config_file_override:
            self._config_files.insert(0, config_file_override)

        secure_file_override = os.environ.get('OS_CLIENT_SECURE_FILE')
        if secure_file_override:
            self._secure_files.insert(0, secure_file_override)

        self.defaults = defaults.get_defaults()
        if override_defaults:
            self.defaults.update(override_defaults)

        # First, use a config file if it exists where expected
        self.config_filename, self.cloud_config = self._load_config_file()
        _, secure_config = self._load_secure_file()
        if secure_config:
            self.cloud_config = _merge_clouds(
                self.cloud_config, secure_config)

        if not self.cloud_config:
            self.cloud_config = {'clouds': {}}
        if 'clouds' not in self.cloud_config:
            self.cloud_config['clouds'] = {}

        # Grab ipv6 preference settings from env
        client_config = self.cloud_config.get('client', {})

        if force_ipv4 is not None:
            # If it's passed in to the constructor, honor it.
            self.force_ipv4 = force_ipv4
        else:
            # Get the backwards compat value
            prefer_ipv6 = get_boolean(
                os.environ.get(
                    'OS_PREFER_IPV6', client_config.get(
                        'prefer_ipv6', client_config.get(
                            'prefer-ipv6', True))))
            force_ipv4 = get_boolean(
                os.environ.get(
                    'OS_FORCE_IPV4', client_config.get(
                        'force_ipv4', client_config.get(
                            'broken-ipv6', False))))

            self.force_ipv4 = force_ipv4
            if not prefer_ipv6:
                # this will only be false if someone set it explicitly
                # honor their wishes
                self.force_ipv4 = True

        # Next, process environment variables and add them to the mix
        self.envvar_key = os.environ.get('OS_CLOUD_NAME', 'envvars')
        if self.envvar_key in self.cloud_config['clouds']:
            raise exceptions.OpenStackConfigException(
                '"{0}" defines a cloud named "{1}", but'
                ' OS_CLOUD_NAME is also set to "{1}". Please rename'
                ' either your environment based cloud, or one of your'
                ' file-based clouds.'.format(self.config_filename,
                                             self.envvar_key))

        self.default_cloud = os.environ.get('OS_CLOUD')

        envvars = _get_os_environ(envvar_prefix=envvar_prefix)
        if envvars:
            self.cloud_config['clouds'][self.envvar_key] = envvars
            if not self.default_cloud:
                self.default_cloud = self.envvar_key

        if not self.default_cloud and self.cloud_config['clouds']:
            if len(self.cloud_config['clouds'].keys()) == 1:
                # If there is only one cloud just use it. This matches envvars
                # behavior and allows for much less typing.
                # TODO(mordred) allow someone to mark a cloud as "default" in
                # clouds.yaml.
                # The next/iter thing is for python3 compat where dict.keys
                # returns an iterator but in python2 it's a list.
                self.default_cloud = next(iter(
                    self.cloud_config['clouds'].keys()))

        # Finally, fall through and make a cloud that starts with defaults
        # because we need somewhere to put arguments, and there are neither
        # config files or env vars
        if not self.cloud_config['clouds']:
            self.cloud_config = dict(
                clouds=dict(defaults=dict(self.defaults)))
            self.default_cloud = 'defaults'

        self._cache_expiration_time = 0
        self._cache_path = CACHE_PATH
        self._cache_class = 'dogpile.cache.null'
        self._cache_arguments = {}
        self._cache_expiration = {}
        if 'cache' in self.cloud_config:
            cache_settings = self._normalize_keys(self.cloud_config['cache'])

            # expiration_time used to be 'max_age' but the dogpile setting
            # is expiration_time. Support max_age for backwards compat.
            self._cache_expiration_time = cache_settings.get(
                'expiration_time', cache_settings.get(
                    'max_age', self._cache_expiration_time))

            # If cache class is given, use that. If not, but if cache time
            # is given, default to memory. Otherwise, default to nothing.
            # to memory.
            if self._cache_expiration_time:
                self._cache_class = 'dogpile.cache.memory'
            self._cache_class = self.cloud_config['cache'].get(
                'class', self._cache_class)

            self._cache_path = os.path.expanduser(
                cache_settings.get('path', self._cache_path))
            self._cache_arguments = cache_settings.get(
                'arguments', self._cache_arguments)
            self._cache_expiration = cache_settings.get(
                'expiration', self._cache_expiration)

        # Flag location to hold the peeked value of an argparse timeout value
        self._argv_timeout = False

        # Save the password callback
        # password = self._pw_callback(prompt="Password: ")
        self._pw_callback = pw_func
Пример #38
0
    def __init__(self, config_files=None, vendor_files=None, override_defaults=None, force_ipv4=None):
        self._config_files = config_files or CONFIG_FILES
        self._vendor_files = vendor_files or VENDOR_FILES

        config_file_override = os.environ.pop("OS_CLIENT_CONFIG_FILE", None)
        if config_file_override:
            self._config_files.insert(0, config_file_override)

        self.defaults = defaults.get_defaults()
        if override_defaults:
            self.defaults.update(override_defaults)

        # First, use a config file if it exists where expected
        self.config_filename, self.cloud_config = self._load_config_file()

        if not self.cloud_config:
            self.cloud_config = {"clouds": {}}
        if "clouds" not in self.cloud_config:
            self.cloud_config["clouds"] = {}

        # Grab ipv6 preference settings from env
        client_config = self.cloud_config.get("client", {})

        if force_ipv4 is not None:
            # If it's passed in to the constructor, honor it.
            self.force_ipv4 = force_ipv4
        else:
            # Get the backwards compat value
            prefer_ipv6 = get_boolean(
                os.environ.pop(
                    "OS_PREFER_IPV6", client_config.get("prefer_ipv6", client_config.get("prefer-ipv6", True))
                )
            )
            force_ipv4 = get_boolean(
                os.environ.pop(
                    "OS_FORCE_IPV4", client_config.get("force_ipv4", client_config.get("broken-ipv6", False))
                )
            )

            self.force_ipv4 = force_ipv4
            if not prefer_ipv6:
                # this will only be false if someone set it explicitly
                # honor their wishes
                self.force_ipv4 = True

        # Next, process environment variables and add them to the mix
        self.envvar_key = os.environ.pop("OS_CLOUD_NAME", "envvars")
        if self.envvar_key in self.cloud_config["clouds"]:
            raise exceptions.OpenStackConfigException(
                '"{0}" defines a cloud named "{1}", but'
                ' OS_CLOUD_NAME is also set to "{1}". Please rename'
                " either your environment based cloud, or one of your"
                " file-based clouds.".format(self.config_filename, self.envvar_key)
            )
        # Pull out OS_CLOUD so that if it's the only thing set, do not
        # make an envvars cloud
        self.default_cloud = os.environ.pop("OS_CLOUD", None)

        envvars = _get_os_environ()
        if envvars:
            self.cloud_config["clouds"][self.envvar_key] = envvars

        # Finally, fall through and make a cloud that starts with defaults
        # because we need somewhere to put arguments, and there are neither
        # config files or env vars
        if not self.cloud_config["clouds"]:
            self.cloud_config = dict(clouds=dict(defaults=dict(self.defaults)))

        self._cache_expiration_time = 0
        self._cache_path = CACHE_PATH
        self._cache_class = "dogpile.cache.null"
        self._cache_arguments = {}
        self._cache_expiration = {}
        if "cache" in self.cloud_config:
            cache_settings = self._normalize_keys(self.cloud_config["cache"])

            # expiration_time used to be 'max_age' but the dogpile setting
            # is expiration_time. Support max_age for backwards compat.
            self._cache_expiration_time = cache_settings.get(
                "expiration_time", cache_settings.get("max_age", self._cache_expiration_time)
            )

            # If cache class is given, use that. If not, but if cache time
            # is given, default to memory. Otherwise, default to nothing.
            # to memory.
            if self._cache_expiration_time:
                self._cache_class = "dogpile.cache.memory"
            self._cache_class = self.cloud_config["cache"].get("class", self._cache_class)

            self._cache_path = os.path.expanduser(cache_settings.get("path", self._cache_path))
            self._cache_arguments = cache_settings.get("arguments", self._cache_arguments)
            self._cache_expiration = cache_settings.get("expiration", self._cache_expiration)
Пример #39
0
    def __init__(self, config_files=None, vendor_files=None,
                 override_defaults=None):
        self._config_files = config_files or CONFIG_FILES
        self._vendor_files = vendor_files or VENDOR_FILES

        config_file_override = os.environ.pop('OS_CLIENT_CONFIG_FILE', None)
        if config_file_override:
            self._config_files.insert(0, config_file_override)

        self.defaults = defaults.get_defaults()
        if override_defaults:
            self.defaults.update(override_defaults)

        # First, use a config file if it exists where expected
        self.config_filename, self.cloud_config = self._load_config_file()

        if not self.cloud_config:
            self.cloud_config = {'clouds': {}}
        if 'clouds' not in self.cloud_config:
            self.cloud_config['clouds'] = {}

        # Grab ipv6 preference settings from env
        client_config = self.cloud_config.get('client', {})
        self.prefer_ipv6 = get_boolean(
            os.environ.pop(
                'OS_PREFER_IPV6', client_config.get(
                    'prefer_ipv6', client_config.get(
                        'prefer-ipv6', False))))

        # Next, process environment variables and add them to the mix
        self.envvar_key = os.environ.pop('OS_CLOUD_NAME', 'envvars')
        if self.envvar_key in self.cloud_config['clouds']:
            raise exceptions.OpenStackConfigException(
                '"{0}" defines a cloud named "{1}", but'
                ' OS_CLOUD_NAME is also set to "{1}". Please rename'
                ' either your environment based cloud, or one of your'
                ' file-based clouds.'.format(self.config_filename,
                                             self.envvar_key))

        envvars = _get_os_environ()
        if envvars:
            self.cloud_config['clouds'][self.envvar_key] = envvars

        # Finally, fall through and make a cloud that starts with defaults
        # because we need somewhere to put arguments, and there are neither
        # config files or env vars
        if not self.cloud_config['clouds']:
            self.cloud_config = dict(
                clouds=dict(defaults=dict(self.defaults)))

        self._cache_max_age = 0
        self._cache_path = CACHE_PATH
        self._cache_class = 'dogpile.cache.null'
        self._cache_arguments = {}
        if 'cache' in self.cloud_config:
            self._cache_max_age = self.cloud_config['cache'].get(
                'max_age', self._cache_max_age)
            if self._cache_max_age:
                self._cache_class = 'dogpile.cache.memory'
            self._cache_path = os.path.expanduser(
                self.cloud_config['cache'].get('path', self._cache_path))
            self._cache_class = self.cloud_config['cache'].get(
                'class', self._cache_class)
            self._cache_arguments = self.cloud_config['cache'].get(
                'arguments', self._cache_arguments)
    def __init__(self, config_files=None, vendor_files=None,
                 override_defaults=None, force_ipv4=None,
                 envvar_prefix=None, secure_files=None):
        self._config_files = config_files or CONFIG_FILES
        self._secure_files = secure_files or SECURE_FILES
        self._vendor_files = vendor_files or VENDOR_FILES

        config_file_override = os.environ.pop('OS_CLIENT_CONFIG_FILE', None)
        if config_file_override:
            self._config_files.insert(0, config_file_override)

        secure_file_override = os.environ.pop('OS_CLIENT_SECURE_FILE', None)
        if secure_file_override:
            self._secure_files.insert(0, secure_file_override)

        self.defaults = defaults.get_defaults()
        if override_defaults:
            self.defaults.update(override_defaults)

        # First, use a config file if it exists where expected
        self.config_filename, self.cloud_config = self._load_config_file()
        _, secure_config = self._load_secure_file()
        if secure_config:
            self.cloud_config = _merge_clouds(
                self.cloud_config, secure_config)

        if not self.cloud_config:
            self.cloud_config = {'clouds': {}}
        if 'clouds' not in self.cloud_config:
            self.cloud_config['clouds'] = {}

        # Grab ipv6 preference settings from env
        client_config = self.cloud_config.get('client', {})

        if force_ipv4 is not None:
            # If it's passed in to the constructor, honor it.
            self.force_ipv4 = force_ipv4
        else:
            # Get the backwards compat value
            prefer_ipv6 = get_boolean(
                os.environ.pop(
                    'OS_PREFER_IPV6', client_config.get(
                        'prefer_ipv6', client_config.get(
                            'prefer-ipv6', True))))
            force_ipv4 = get_boolean(
                os.environ.pop(
                    'OS_FORCE_IPV4', client_config.get(
                        'force_ipv4', client_config.get(
                            'broken-ipv6', False))))

            self.force_ipv4 = force_ipv4
            if not prefer_ipv6:
                # this will only be false if someone set it explicitly
                # honor their wishes
                self.force_ipv4 = True

        # Next, process environment variables and add them to the mix
        self.envvar_key = os.environ.pop('OS_CLOUD_NAME', 'envvars')
        if self.envvar_key in self.cloud_config['clouds']:
            raise exceptions.OpenStackConfigException(
                '"{0}" defines a cloud named "{1}", but'
                ' OS_CLOUD_NAME is also set to "{1}". Please rename'
                ' either your environment based cloud, or one of your'
                ' file-based clouds.'.format(self.config_filename,
                                             self.envvar_key))
        # Pull out OS_CLOUD so that if it's the only thing set, do not
        # make an envvars cloud
        self.default_cloud = os.environ.pop('OS_CLOUD', None)

        envvars = _get_os_environ(envvar_prefix=envvar_prefix)
        if envvars:
            self.cloud_config['clouds'][self.envvar_key] = envvars
            if not self.default_cloud:
                self.default_cloud = self.envvar_key

        # Finally, fall through and make a cloud that starts with defaults
        # because we need somewhere to put arguments, and there are neither
        # config files or env vars
        if not self.cloud_config['clouds']:
            self.cloud_config = dict(
                clouds=dict(defaults=dict(self.defaults)))
            self.default_cloud = 'defaults'

        self._cache_expiration_time = 0
        self._cache_path = CACHE_PATH
        self._cache_class = 'dogpile.cache.null'
        self._cache_arguments = {}
        self._cache_expiration = {}
        if 'cache' in self.cloud_config:
            cache_settings = self._normalize_keys(self.cloud_config['cache'])

            # expiration_time used to be 'max_age' but the dogpile setting
            # is expiration_time. Support max_age for backwards compat.
            self._cache_expiration_time = cache_settings.get(
                'expiration_time', cache_settings.get(
                    'max_age', self._cache_expiration_time))

            # If cache class is given, use that. If not, but if cache time
            # is given, default to memory. Otherwise, default to nothing.
            # to memory.
            if self._cache_expiration_time:
                self._cache_class = 'dogpile.cache.memory'
            self._cache_class = self.cloud_config['cache'].get(
                'class', self._cache_class)

            self._cache_path = os.path.expanduser(
                cache_settings.get('path', self._cache_path))
            self._cache_arguments = cache_settings.get(
                'arguments', self._cache_arguments)
            self._cache_expiration = cache_settings.get(
                'expiration', self._cache_expiration)

        # Flag location to hold the peeked value of an argparse timeout value
        self._argv_timeout = False