Beispiel #1
0
    def setUp(self):
        self.token_file_contents = '123TOKEN'
        self.response = MagicMock()
        self.session_get = 'viperpy.util.token_request.requests.Session.get'

        self.token_request = TokenRequest(username='******',
                                          password='******',
                                          vipr_endpoint='https://localhost',
                                          token_endpoint='https://localhost',
                                          verify_ssl=False,
                                          token_filename='viperpy.tkn',
                                          token_location='/tmp',
                                          request_timeout=5.0,
                                          cache_token=True)
Beispiel #2
0
class WhenTestingTokenRequest(unittest.TestCase):

    def setUp(self):
        self.token_file_contents = '123TOKEN'
        self.response = MagicMock()
        self.session_get = 'viperpy.util.token_request.requests.Session.get'

        self.token_request = TokenRequest(username='******',
                                          password='******',
                                          vipr_endpoint='https://localhost',
                                          token_endpoint='https://localhost',
                                          verify_ssl=False,
                                          token_filename='viperpy.tkn',
                                          token_location='/tmp',
                                          request_timeout=5.0,
                                          cache_token=True)

    def test_should_get_existing_token(self):
        with patch('os.path.isfile', return_value=True),\
            patch('__builtin__.open', mock_open(read_data='123TOKEN'),
                  create=True):
            self.assertEqual(self.token_request._get_existing_token(),
                             self.token_file_contents)

    def test_should_not_get_existing_token(self):
        with patch('os.path.isfile', return_value=False):
            self.assertEqual(self.token_request._get_existing_token(), None)

    def test_get_new_token_should_throw_viperpyexception_500(self):
        self.response.status_code = httplib.INTERNAL_SERVER_ERROR
        self.requests = MagicMock(return_value=self.response)
        self.requests.get.side_effect = [self.response]

        with patch(self.session_get, self.requests):
            with self.assertRaises(ViperpyException):
                self.token_request.get_new_token()

    def test_get_new_token_should_throw_viperpyexception_401(self):
        self.response.status_code = httplib.UNAUTHORIZED
        self.requests = MagicMock(return_value=self.response)
        self.requests.get.side_effect = [self.response]

        with patch(self.session_get, self.requests):
            with self.assertRaises(ViperpyException):
                self.token_request.get_new_token()
Beispiel #3
0
    def __init__(self, username=None, password=None, token=None,
                 vipr_endpoint=None, token_endpoint=None, verify_ssl=False,
                 token_filename='viperpy.tkn', token_location='/tmp',
                 request_timeout=15.0, cache_token=True):
        """
        Creates the ViperPy class that the client will directly work with

        :param username: The username to fetch a token
        :param password: The password to fetch a token
        :param token: Supply a valid token to use instead of username/password
        :param vipr_endpoint: The URL where ViPR is located
        :param token_endpoint: The URL where the ViPR login is located
        :param verify_ssl: Verify SSL certificates
        :param token_filename: The name of the cached token filename
        :param token_location: By default this is stored in /tmp
        :param request_timeout: How long to wait for ViPR to respond
        :param cache_token: Whether to cache the token, by default this is true
        you should only switch this to false when you want to directly fetch
        a token for a user
        """

        self.username = username
        self.password = password
        self.token = token
        self.vipr_endpoint = vipr_endpoint.rstrip('/')
        self.token_endpoint = token_endpoint.rstrip('/')
        self.verify_ssl = verify_ssl
        self.token_filename = token_filename
        self.token_location = token_location
        self.request_timeout = request_timeout
        self.cache_token = cache_token
        self._session = requests.Session()
        self._token_request = TokenRequest(
            username=self.username,
            password=self.password,
            vipr_endpoint=self.vipr_endpoint,
            token_endpoint=self.token_endpoint,
            verify_ssl=self.verify_ssl,
            token_filename=self.token_filename,
            token_location=self.token_location,
            request_timeout=self.request_timeout,
            cache_token=self.cache_token)
        self.token_file = os.path.join(
            self.token_location, self.token_filename)

        # API -> Authentication
        self.authentication = Authentication(self)

        # API -> Fabric
        self.fabric_capacity = FabricCapacity(self)
        self.disk = Disk(self)
        self.health = Health(self)
        self.node = Node(self)
        self.services = Services(self)

        # API -> Object Data Control
        self.billing = Billing(self)
        self.bucket = Bucket(self)
        self.object_data_control_capacity = ObjectDataControlCapacity(self)
        self.namespace = Namespace(self)
        self.user_management = UserManagement(self)
        self.user_secret_key = UserSecretKey(self)
        self.object_soft_quota = ObjectSoftQuota(self)

        # API -> Other
        self.audit = Audit(self)
        self.user_info = UserInfo(self)

        # API -> System Management
        self.configuration = Configuration(self)
        self.health_monitor = HealthMonitor(self)
        self.upgrade = Upgrade(self)

        # API -> Tenant
        self.tenants = Tenants(self)
        self.projects = Projects(self)

        # API -> Block Volumes
        self.block_volumes = Volumes(self)

        # API -> License
        self.license = License(self)
Beispiel #4
0
class Viperpy(object):

    def __init__(self, username=None, password=None, token=None,
                 vipr_endpoint=None, token_endpoint=None, verify_ssl=False,
                 token_filename='viperpy.tkn', token_location='/tmp',
                 request_timeout=15.0, cache_token=True):
        """
        Creates the ViperPy class that the client will directly work with

        :param username: The username to fetch a token
        :param password: The password to fetch a token
        :param token: Supply a valid token to use instead of username/password
        :param vipr_endpoint: The URL where ViPR is located
        :param token_endpoint: The URL where the ViPR login is located
        :param verify_ssl: Verify SSL certificates
        :param token_filename: The name of the cached token filename
        :param token_location: By default this is stored in /tmp
        :param request_timeout: How long to wait for ViPR to respond
        :param cache_token: Whether to cache the token, by default this is true
        you should only switch this to false when you want to directly fetch
        a token for a user
        """

        self.username = username
        self.password = password
        self.token = token
        self.vipr_endpoint = vipr_endpoint.rstrip('/')
        self.token_endpoint = token_endpoint.rstrip('/')
        self.verify_ssl = verify_ssl
        self.token_filename = token_filename
        self.token_location = token_location
        self.request_timeout = request_timeout
        self.cache_token = cache_token
        self._session = requests.Session()
        self._token_request = TokenRequest(
            username=self.username,
            password=self.password,
            vipr_endpoint=self.vipr_endpoint,
            token_endpoint=self.token_endpoint,
            verify_ssl=self.verify_ssl,
            token_filename=self.token_filename,
            token_location=self.token_location,
            request_timeout=self.request_timeout,
            cache_token=self.cache_token)
        self.token_file = os.path.join(
            self.token_location, self.token_filename)

        # API -> Authentication
        self.authentication = Authentication(self)

        # API -> Fabric
        self.fabric_capacity = FabricCapacity(self)
        self.disk = Disk(self)
        self.health = Health(self)
        self.node = Node(self)
        self.services = Services(self)

        # API -> Object Data Control
        self.billing = Billing(self)
        self.bucket = Bucket(self)
        self.object_data_control_capacity = ObjectDataControlCapacity(self)
        self.namespace = Namespace(self)
        self.user_management = UserManagement(self)
        self.user_secret_key = UserSecretKey(self)
        self.object_soft_quota = ObjectSoftQuota(self)

        # API -> Other
        self.audit = Audit(self)
        self.user_info = UserInfo(self)

        # API -> System Management
        self.configuration = Configuration(self)
        self.health_monitor = HealthMonitor(self)
        self.upgrade = Upgrade(self)

        # API -> Tenant
        self.tenants = Tenants(self)
        self.projects = Projects(self)

        # API -> Block Volumes
        self.block_volumes = Volumes(self)

        # API -> License
        self.license = License(self)

    def get_token(self):
        """
        Get a token directly back, typically you want to set the cache_token
        param for ViperPy to false for this call.

        :return: A valid token or an ViperPy exception
        """
        return self._token_request.get_new_token()

    def remove_cached_token(self):
        """
        Remove the cached token file, this is useful if you switch users
        and want to use a different token
        """
        if os.path.isfile(self.token_file):
            os.remove(self.token_file)

    def _fetch_headers(self):
        if self.token:
            return {'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    'x-sds-auth-token': self.token}
        else:
            return {'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    'x-sds-auth-token': self._token_request.get_token()}

    def _construct_url(self, url):
        return '{0}/{1}'.format(self.vipr_endpoint, url)

    def get(self, url, params=None):
        return self._request(url, params=params)

    def post(self, url, json_payload='{}'):
        return self._request(url, json_payload, http_verb='POST')

    def put(self, url, json_payload='{}'):
        return self._request(url, json_payload, http_verb='PUT')

    def _request(self, url, json_payload='{}', http_verb='GET', params=None):
        json_payload = json.dumps(json_payload)

        try:
            if http_verb == "PUT":
                req = self._session.put(
                    self._construct_url(url),
                    verify=self.verify_ssl,
                    headers=self._fetch_headers(),
                    timeout=self.request_timeout,
                    data=json_payload)
            elif http_verb == 'POST':
                req = self._session.post(
                    self._construct_url(url),
                    verify=self.verify_ssl,
                    headers=self._fetch_headers(),
                    timeout=self.request_timeout,
                    data=json_payload)
            else:  # Default to GET
                req = self._session.get(
                    self._construct_url(url),
                    verify=self.verify_ssl,
                    headers=self._fetch_headers(),
                    timeout=self.request_timeout,
                    params=params)

            if req.status_code != 200:
                raise ViperpyException(
                    http_status_code=req.status_code,
                    vipr_message=req.text)
            return req.json()

        except requests.ConnectionError as conn_err:
            raise ViperpyException(message=conn_err.message)
        except requests.HTTPError as http_err:
            raise ViperpyException(message=http_err.message)
        except requests.RequestException as req_err:
            raise ViperpyException(message=req_err.message)
        except ValueError:
            return
Beispiel #5
0
    def __init__(self,
                 username=None,
                 password=None,
                 token=None,
                 vipr_endpoint=None,
                 token_endpoint=None,
                 verify_ssl=False,
                 token_filename='viperpy.tkn',
                 token_location='/tmp',
                 request_timeout=15.0,
                 cache_token=True):
        """
        Creates the ViperPy class that the client will directly work with

        :param username: The username to fetch a token
        :param password: The password to fetch a token
        :param token: Supply a valid token to use instead of username/password
        :param vipr_endpoint: The URL where ViPR is located
        :param token_endpoint: The URL where the ViPR login is located
        :param verify_ssl: Verify SSL certificates
        :param token_filename: The name of the cached token filename
        :param token_location: By default this is stored in /tmp
        :param request_timeout: How long to wait for ViPR to respond
        :param cache_token: Whether to cache the token, by default this is true
        you should only switch this to false when you want to directly fetch
        a token for a user
        """

        self.username = username
        self.password = password
        self.token = token
        self.vipr_endpoint = vipr_endpoint.rstrip('/')
        self.token_endpoint = token_endpoint.rstrip('/')
        self.verify_ssl = verify_ssl
        self.token_filename = token_filename
        self.token_location = token_location
        self.request_timeout = request_timeout
        self.cache_token = cache_token
        self._session = requests.Session()
        self._token_request = TokenRequest(
            username=self.username,
            password=self.password,
            vipr_endpoint=self.vipr_endpoint,
            token_endpoint=self.token_endpoint,
            verify_ssl=self.verify_ssl,
            token_filename=self.token_filename,
            token_location=self.token_location,
            request_timeout=self.request_timeout,
            cache_token=self.cache_token)
        self.token_file = os.path.join(self.token_location,
                                       self.token_filename)

        # API -> Authentication
        self.authentication = Authentication(self)

        # API -> Fabric
        self.fabric_capacity = FabricCapacity(self)
        self.disk = Disk(self)
        self.health = Health(self)
        self.node = Node(self)
        self.services = Services(self)

        # API -> Object Data Control
        self.billing = Billing(self)
        self.bucket = Bucket(self)
        self.object_data_control_capacity = ObjectDataControlCapacity(self)
        self.namespace = Namespace(self)
        self.user_management = UserManagement(self)
        self.user_secret_key = UserSecretKey(self)
        self.object_soft_quota = ObjectSoftQuota(self)

        # API -> Other
        self.audit = Audit(self)
        self.user_info = UserInfo(self)

        # API -> System Management
        self.configuration = Configuration(self)
        self.health_monitor = HealthMonitor(self)
        self.upgrade = Upgrade(self)

        # API -> Tenant
        self.tenants = Tenants(self)
Beispiel #6
0
class Viperpy(object):
    def __init__(self,
                 username=None,
                 password=None,
                 token=None,
                 vipr_endpoint=None,
                 token_endpoint=None,
                 verify_ssl=False,
                 token_filename='viperpy.tkn',
                 token_location='/tmp',
                 request_timeout=15.0,
                 cache_token=True):
        """
        Creates the ViperPy class that the client will directly work with

        :param username: The username to fetch a token
        :param password: The password to fetch a token
        :param token: Supply a valid token to use instead of username/password
        :param vipr_endpoint: The URL where ViPR is located
        :param token_endpoint: The URL where the ViPR login is located
        :param verify_ssl: Verify SSL certificates
        :param token_filename: The name of the cached token filename
        :param token_location: By default this is stored in /tmp
        :param request_timeout: How long to wait for ViPR to respond
        :param cache_token: Whether to cache the token, by default this is true
        you should only switch this to false when you want to directly fetch
        a token for a user
        """

        self.username = username
        self.password = password
        self.token = token
        self.vipr_endpoint = vipr_endpoint.rstrip('/')
        self.token_endpoint = token_endpoint.rstrip('/')
        self.verify_ssl = verify_ssl
        self.token_filename = token_filename
        self.token_location = token_location
        self.request_timeout = request_timeout
        self.cache_token = cache_token
        self._session = requests.Session()
        self._token_request = TokenRequest(
            username=self.username,
            password=self.password,
            vipr_endpoint=self.vipr_endpoint,
            token_endpoint=self.token_endpoint,
            verify_ssl=self.verify_ssl,
            token_filename=self.token_filename,
            token_location=self.token_location,
            request_timeout=self.request_timeout,
            cache_token=self.cache_token)
        self.token_file = os.path.join(self.token_location,
                                       self.token_filename)

        # API -> Authentication
        self.authentication = Authentication(self)

        # API -> Fabric
        self.fabric_capacity = FabricCapacity(self)
        self.disk = Disk(self)
        self.health = Health(self)
        self.node = Node(self)
        self.services = Services(self)

        # API -> Object Data Control
        self.billing = Billing(self)
        self.bucket = Bucket(self)
        self.object_data_control_capacity = ObjectDataControlCapacity(self)
        self.namespace = Namespace(self)
        self.user_management = UserManagement(self)
        self.user_secret_key = UserSecretKey(self)
        self.object_soft_quota = ObjectSoftQuota(self)

        # API -> Other
        self.audit = Audit(self)
        self.user_info = UserInfo(self)

        # API -> System Management
        self.configuration = Configuration(self)
        self.health_monitor = HealthMonitor(self)
        self.upgrade = Upgrade(self)

        # API -> Tenant
        self.tenants = Tenants(self)

    def get_token(self):
        """
        Get a token directly back, typically you want to set the cache_token
        param for ViperPy to false for this call.

        :return: A valid token or an ViperPy exception
        """
        return self._token_request.get_new_token()

    def remove_cached_token(self):
        """
        Remove the cached token file, this is useful if you switch users
        and want to use a different token
        """
        if os.path.isfile(self.token_file):
            os.remove(self.token_file)

    def _fetch_headers(self):
        if self.token:
            return {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'x-sds-auth-token': self.token
            }
        else:
            return {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'x-sds-auth-token': self._token_request.get_token()
            }

    def _construct_url(self, url):
        return '{0}/{1}'.format(self.vipr_endpoint, url)

    def get(self, url, params=None):
        return self._request(url, params=params)

    def post(self, url, json_payload='{}'):
        return self._request(url, json_payload, http_verb='POST')

    def put(self, url, json_payload='{}'):
        return self._request(url, json_payload, http_verb='PUT')

    def _request(self, url, json_payload='{}', http_verb='GET', params=None):
        json_payload = json.dumps(json_payload)

        try:
            if http_verb == "PUT":
                req = self._session.put(self._construct_url(url),
                                        verify=self.verify_ssl,
                                        headers=self._fetch_headers(),
                                        timeout=self.request_timeout,
                                        data=json_payload)
            elif http_verb == 'POST':
                req = self._session.post(self._construct_url(url),
                                         verify=self.verify_ssl,
                                         headers=self._fetch_headers(),
                                         timeout=self.request_timeout,
                                         data=json_payload)
            else:  # Default to GET
                req = self._session.get(self._construct_url(url),
                                        verify=self.verify_ssl,
                                        headers=self._fetch_headers(),
                                        timeout=self.request_timeout,
                                        params=params)

            if req.status_code != 200:
                raise ViperpyException(http_status_code=req.status_code,
                                       vipr_message=req.text)
            return req.json()

        except requests.ConnectionError as conn_err:
            raise ViperpyException(message=conn_err.message)
        except requests.HTTPError as http_err:
            raise ViperpyException(message=http_err.message)
        except requests.RequestException as req_err:
            raise ViperpyException(message=req_err.message)
        except ValueError:
            return