def test_create_session(self): auth = mock.Mock() prof = profile.Profile() conn = connection.Connection(authenticator=auth, profile=prof) self.assertEqual(auth, conn.authenticator) self.assertEqual(prof, conn.profile) self.assertEqual('ecl.baremetal.v2._proxy', conn.baremetal.__class__.__module__) self.assertEqual('ecl.compute.v2._proxy', conn.compute.__class__.__module__) self.assertEqual('ecl.connectivity.v1._proxy', conn.connectivity.__class__.__module__) self.assertEqual('ecl.dedicated_hypervisor.v1._proxy', conn.dedicated_hypervisor.__class__.__module__) self.assertEqual('ecl.identity.v3._proxy', conn.identity.__class__.__module__) self.assertEqual('ecl.image.v2._proxy', conn.image.__class__.__module__) self.assertEqual('ecl.network.v2._proxy', conn.network.__class__.__module__) self.assertEqual('ecl.rca.v1._proxy', conn.rca.__class__.__module__) self.assertEqual('ecl.orchestration.v1._proxy', conn.orchestration.__class__.__module__) self.assertEqual('ecl.telemetry.v2._proxy', conn.telemetry.__class__.__module__) self.assertEqual('ecl.sss.v1._proxy', conn.sss.__class__.__module__)
def test_set(self): prof = profile.Profile() prof.set_version('compute', 'v2') self.assertEqual('v2', prof.get_filter('compute').version) prof.set_version('baremetal-server', 'v1') self.assertEqual('v1', prof.get_filter('baremetal-server').version) prof.set_version('dedicated-hypervisor', 'v3') self.assertEqual('v3', prof.get_filter('dedicated-hypervisor').version) prof.set_version('identity', 'v4') self.assertEqual('v4', prof.get_filter('identity').version) prof.set_version('image', 'v5') self.assertEqual('v5', prof.get_filter('image').version) prof.set_version('interconnectivity', 'v9') self.assertEqual('v9', prof.get_filter('interconnectivity').version) prof.set_version('metering', 'v6') self.assertEqual('v6', prof.get_filter('metering').version) prof.set_version('network', 'v7') self.assertEqual('v7', prof.get_filter('network').version) prof.set_version('orchestration', 'v8') self.assertEqual('v8', prof.get_filter('orchestration').version) prof.set_version('rca', 'v9') self.assertEqual('v9', prof.get_filter('rca').version) prof.set_version('sss', 'v10') self.assertEqual('v10', prof.get_filter('sss').version) prof.set_version('storage', 'v11') self.assertEqual('v11', prof.get_filter('storage').version) prof.set_version('volumev2', 'v12') self.assertEqual('v12', prof.get_filter('volumev2').version)
def test_set_all(self): prof = profile.Profile() prof.set_name(prof.ALL, 'fee') prof.set_region(prof.ALL, 'fie') prof.set_interface(prof.ALL, 'public') for service in prof.service_keys: self.assertEqual('fee', prof.get_filter(service).service_name) self.assertEqual('fie', prof.get_filter(service).region) self.assertEqual('public', prof.get_filter(service).interface)
def test_set_api_version(self): # This tests that api_version is effective after explicit setting, or # else it defaults to None. prof = profile.Profile() prof.set_api_version('storage', '1.2') svc = prof.get_filter('storage') self.assertEqual('1.2', svc.api_version) svc = prof.get_filter('compute') self.assertIsNone(svc.api_version)
def test_init_with_single_api_request(self): prof = profile.Profile() prof.set_api_version('storage', '1.2') sot = session.Session(prof) # The assertion acutally tests the property assigned in parent class self.assertEqual({'ecl-api-version': 'storage 1.2'}, sot.additional_headers)
def test_init_with_multi_api_requests(self): prof = profile.Profile() prof.set_api_version('storage', '1.2') prof.set_api_version('compute', '2.15') sot = session.Session(prof) versions = sot.additional_headers['ecl-api-version'] requests = [req.strip() for req in versions.split(',')] self.assertIn('storage 1.2', requests) self.assertIn('compute 2.15', requests)
def test_default_versions(self): prof = profile.Profile() self.assertEqual('v2', prof.get_filter('baremetal-server').version) self.assertEqual('v2', prof.get_filter('compute').version) self.assertEqual('v1', prof.get_filter('dedicated-hypervisor').version) self.assertEqual('v3', prof.get_filter('identity').version) self.assertEqual('v2', prof.get_filter('image').version) self.assertEqual('v1', prof.get_filter('interconnectivity').version) self.assertEqual('v2', prof.get_filter('metering').version) self.assertEqual('v2', prof.get_filter('network').version) self.assertEqual('v1', prof.get_filter('orchestration').version) self.assertEqual('v1', prof.get_filter('rca').version) self.assertEqual('v1', prof.get_filter('sss').version) self.assertEqual('v1', prof.get_filter('storage').version) self.assertEqual('v2', prof.get_filter('volumev2').version)
def test_init(self): prof = profile.Profile() expected = [ 'baremetal-server', 'compute', 'dedicated-hypervisor', 'identity', 'image', 'interconnectivity', 'metering', 'network', 'orchestration', 'rca', 'sss', 'storage', 'volumev2', ] self.assertEqual(expected, prof.service_keys)
def from_config(cloud_name=None, cloud_config=None, options=None): """Create a Connection using os-client-config :param str cloud_name: Use the `cloud_name` configuration details when creating the Connection instance. :param cloud_config: An instance of `os_client_config.config.OpenStackConfig` as returned from the os-client-config library. If no `config` is provided, `os_client_config.OpenStackConfig` will be called, and the provided `cloud_name` will be used in determining which cloud's configuration details will be used in creation of the `Connection` instance. :param options: An argparse Namespace object; allows direct passing in of argparse options to be added to the cloud config. This value is passed to the `argparse` argument of `os_client_config.config.OpenStackConfig.get_one_cloud`. :rtype: :class:`~ecl.connection.Connection` """ # TODO(thowe): I proposed that service name defaults to None in OCC defaults = {} prof = _profile.Profile() services = [service.service_type for service in prof.get_services()] for service in services: defaults[service + '_service_name'] = None # TODO(thowe): default is 2 which turns into v2 which doesn't work # this stuff needs to be fixed where we keep version and path separated. defaults['network_api_version'] = 'v2.0' if cloud_config is None: occ = os_client_config.OpenStackConfig(override_defaults=defaults) cloud_config = occ.get_one_cloud(cloud=cloud_name, argparse=options) if cloud_config.debug: utils.enable_logging(True, stream=sys.stdout) # TODO(mordred) we need to add service_type setting to eclsdk. # Some clouds have type overridden as well as name. services = [service.service_type for service in prof.get_services()] for service in cloud_config.get_services(): if service in services: version = cloud_config.get_api_version(service) if version: version = str(version) if not version.startswith("v"): version = "v" + version prof.set_version(service, version) name = cloud_config.get_service_name(service) if name: prof.set_name(service, name) interface = cloud_config.get_interface(service) if interface: prof.set_interface(service, interface) region = cloud_config.get_region_name(service) if region: for service in services: prof.set_region(service, region) # Auth auth = cloud_config.config['auth'] # TODO(thowe) We should be using auth_type auth['auth_plugin'] = cloud_config.config['auth_type'] if 'cacert' in auth: auth['verify'] = auth.pop('cacert') if 'cacert' in cloud_config.config: auth['verify'] = cloud_config.config['cacert'] insecure = cloud_config.config.get('insecure', False) if insecure: auth['verify'] = False cert = cloud_config.config.get('cert') if cert: key = cloud_config.config.get('key') auth['cert'] = (cert, key) if key else cert return Connection(profile=prof, **auth)
def __init__(self, session=None, authenticator=None, profile=None, verify=True, cert=None, user_agent=None, auth_plugin="password", timeout=None, **auth_args): """Create a context for a connection to a cloud provider. A connection needs a transport and an authenticator. The user may pass in a transport and authenticator they want to use or they may pass in the parameters to create a transport and authenticator. The connection creates a :class:`~ecl.session.Session` which uses the profile and authenticator to perform HTTP requests. :param session: A session object compatible with :class:`~ecl.session.Session`. :type session: :class:`~ecl.session.Session` :param authenticator: An authenticator derived from the base authenticator plugin that was previously created. Two common authentication identity plugins are :class:`identity_v2 <ecl.auth.identity.v2.Auth>` and :class:`identity_v3 <ecl.auth.identity.v3.Auth>`. If this parameter is not passed in, the connection will create an authenticator. :type authenticator: :class:`~ecl.auth.base.BaseAuthPlugin` :param profile: If the user has any special profiles such as the service name, region, version or interface, they may be provided in the profile object. If no profiles are provided, the services that appear first in the service catalog will be used. :type profile: :class:`~ecl.profile.Profile` :param bool verify: If a transport is not provided to the connection, this parameter will be used to create a transport. If ``verify`` is set to true, which is the default, the SSL cert will be verified. It can also be set to a CA_BUNDLE path. :param cert: If a transport is not provided to the connection then this parameter will be used to create a transport. `cert` allows to provide a client certificate file path or a tuple with client certificate and key paths. :type cert: str or tuple :param str user_agent: If a transport is not provided to the connection, this parameter will be used when creating a transport. The value given here will be prepended to the default, which is specified in :attr:`~ecl.transport.USER_AGENT`. The resulting ``user_agent`` value is used for the ``User-Agent`` HTTP header. :param str auth_plugin: The name of authentication plugin to use. The default value is ``password``. :param float timeout: A timeout to pass to requests. This should be a numerical value indicating some amount (or fraction) of seconds or 0 for no timeout. (optional, defaults to 0) :param auth_args: The rest of the parameters provided are assumed to be authentication arguments that are used by the authentication plugin. """ self.authenticator = self._create_authenticator( authenticator, auth_plugin, **auth_args) self.profile = profile if profile else _profile.Profile() self.session = session if session else _session.Session( self.profile, auth=self.authenticator, verify=verify, cert=cert, user_agent=user_agent, timeout=timeout) self._open()
def test_set_version_bad_service(self): prof = profile.Profile() self.assertRaises(exceptions.SDKException, prof.set_version, 'bogus', 'v2')
def test_init_with_no_api_requests(self): prof = profile.Profile() sot = session.Session(prof) self.assertEqual({}, sot.additional_headers)