def __init__(self, username, region, apikey=None, password=None):
        assert apikey or password

        resp = None
        # If apikey and password are given, give priority to
        # authentication via API key.
        if apikey:
            resp = auth.authenticate(username, apikey=apikey)
        else:
            resp = auth.authenticate(username, password=password)

        self._token = resp['access']['token']['id']
        endpoints = resp['access']['serviceCatalog']

        self._endpoint = _find_backup_endpoint(endpoints, region)
        if not self._endpoint:
            raise exceptions.NoEndpointFound(username, region)

        self._username = username
        self._tenant = resp['access']['token']['tenant']['id']
        self._expiry = resp['access']['token']['expires']
        self._session = requests
Beispiel #2
0
    def __init__(self, username, region, apikey=None, password=None):
        assert apikey or password

        resp = None
        # If apikey and password are given, give priority to
        # authentication via API key.
        if apikey:
            resp = auth.authenticate(username, apikey=apikey)
        else:
            resp = auth.authenticate(username, password=password)

        self._token = resp['access']['token']['id']
        endpoints = resp['access']['serviceCatalog']

        self._endpoint = _find_backup_endpoint(endpoints, region)
        if not self._endpoint:
            raise exceptions.NoEndpointFound(username, region)

        self._username = username
        self._tenant = resp['access']['token']['tenant']['id']
        self._expiry = resp['access']['token']['expires']
        self._session = requests.session()
 def test_auth_returns_catalog_on_success(self):
     _prepare_mock_post(status=200, body='{"yay": 1}')
     catalog = auth.authenticate(username='******', password='******')
     self.assertEqual(catalog, {'yay': 1})
 def test_auth_raises_if_neither_apikey_or_password_provided(self):
     _prepare_mock_post(status=200)
     with self.assertRaises(AssertionError):
         auth.authenticate(username='******')
 def test_auth_by_apikey_raises_if_invalid_creds(self):
     _prepare_mock_post(status=403)
     with self.assertRaises(HTTPError):
         auth.authenticate(apikey='bad', username='******')
 def test_auth_returns_catalog_on_success(self):
     _prepare_mock_post(status=200, body='{"yay": 1}')
     catalog = auth.authenticate(username='******', password='******')
     self.assertEqual(catalog, {'yay': 1})
 def test_auth_raises_if_neither_apikey_or_password_provided(self):
     _prepare_mock_post(status=200)
     with self.assertRaises(AssertionError):
         auth.authenticate(username='******')
 def test_auth_by_apikey_raises_if_invalid_creds(self):
     _prepare_mock_post(status=403)
     with self.assertRaises(HTTPError):
         auth.authenticate(apikey='bad', username='******')