Esempio n. 1
0
def create_dbaas_client(user):
    """Creates a rich client for the RedDwarf API using the test config."""
    auth_strategy = None

    kwargs = {
        'service_type': 'reddwarf',
        'insecure': test_config.values['reddwarf_client_insecure'],
    }

    def set_optional(kwargs_name, test_conf_name):
        value = test_config.values.get(test_conf_name, None)
        if value is not None:
            kwargs[kwargs_name] = value

    force_url = 'override_reddwarf_api_url' in test_config.values
    set_optional('auth_strategy', 'auth_strategy')
    set_optional('region_name', 'reddwarf_client_region_name')
    set_optional('service_url', 'override_reddwarf_api_url')

    dbaas = Dbaas(user.auth_user, user.auth_key, user.tenant,
                  test_config.reddwarf_auth_url, **kwargs)
    dbaas.authenticate()
    with Check() as check:
        check.is_not_none(dbaas.client.auth_token, "Auth token not set!")
        if not force_url and user.requirements.is_admin:
            expected_prefix = test_config.dbaas_url
            actual = dbaas.client.management_url
            msg = "Dbaas management url was expected to start with %s, but " \
                  "was %s." % (expected_prefix, actual)
            check.true(actual.startswith(expected_prefix), msg)
    return TestClient(dbaas)
def create_client(cls=ReddwarfHTTPClient):
    client = Dbaas(conf.username,
                   conf.password,
                   tenant=conf.tenant,
                   auth_url="blah/",
                   auth_strategy='fake',
                   insecure=True,
                   service_type='reddwarf',
                   service_url=conf.dbaas_url,
                   client_cls=cls)
    return client
def create_dbaas_client(user):
    """Creates a rich client for the RedDwarf API using the test config."""
    auth_strategy = None

    kwargs = {
        'service_type': 'reddwarf',
        'insecure': test_config.values['reddwarf_client_insecure'],
    }

    def set_optional(kwargs_name, test_conf_name):
        value = test_config.values.get(test_conf_name, None)
        if value is not None:
            kwargs[kwargs_name] = value

    force_url = 'override_reddwarf_api_url' in test_config.values

    service_url = test_config.get('override_reddwarf_api_url', None)
    if user.requirements.is_admin:
        service_url = test_config.get('override_admin_reddwarf_api_url',
                                      service_url)
    if service_url:
        kwargs['service_url'] = service_url

    auth_strategy = None
    if user.requirements.is_admin:
        auth_strategy = test_config.get('admin_auth_strategy',
                                        test_config.auth_strategy)
    else:
        auth_strategy = test_config.auth_strategy
    set_optional('region_name', 'reddwarf_client_region_name')
    if test_config.values.get('override_reddwarf_api_url_append_tenant',
                              False):
        kwargs['service_url'] += "/" + user.tenant

    if auth_strategy == 'fake':
        from reddwarfclient import auth

        class FakeAuth(auth.Authenticator):
            def authenticate(self):
                class FakeCatalog(object):
                    def __init__(self, auth):
                        self.auth = auth

                    def get_public_url(self):
                        return "%s/%s" % (test_config.dbaas_url,
                                          self.auth.tenant)

                    def get_token(self):
                        return self.auth.tenant

                return FakeCatalog(self)

        auth_strategy = FakeAuth

    if auth_strategy:
        kwargs['auth_strategy'] = auth_strategy

    if not user.requirements.is_admin:
        auth_url = test_config.reddwarf_auth_url
    else:
        auth_url = test_config.values.get('reddwarf_admin_auth_url',
                                          test_config.reddwarf_auth_url)

    dbaas = Dbaas(user.auth_user,
                  user.auth_key,
                  tenant=user.tenant,
                  auth_url=auth_url,
                  **kwargs)
    dbaas.authenticate()
    with Check() as check:
        check.is_not_none(dbaas.client.auth_token, "Auth token not set!")
        if not force_url and user.requirements.is_admin:
            expected_prefix = test_config.dbaas_url
            actual = dbaas.client.service_url
            msg = "Dbaas management url was expected to start with %s, but " \
                  "was %s." % (expected_prefix, actual)
            check.true(actual.startswith(expected_prefix), msg)
    return TestClient(dbaas)