Ejemplo n.º 1
0
    def setUp(self):
        self.token_file_contents = '123TOKEN'
        self.response = MagicMock()
        self.session_get = 'ecsminion.util.token_request.requests.Session.get'

        self.token_request = TokenRequest(username='******',
                                          password='******',
                                          ecs_endpoint='https://localhost',
                                          token_endpoint='https://localhost',
                                          verify_ssl=False,
                                          token_filename='ecstoken.tkn',
                                          token_location='/tmp',
                                          request_timeout=5.0,
                                          cache_token=True)
Ejemplo n.º 2
0
class WhenTestingTokenRequest(unittest.TestCase):

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

        self.token_request = TokenRequest(username='******',
                                          password='******',
                                          ecs_endpoint='https://localhost',
                                          token_endpoint='https://localhost',
                                          verify_ssl=False,
                                          token_filename='ecstoken.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_ecsminionexception_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(ECSMinionException):
                self.token_request.get_new_token()

    def test_get_new_token_should_throw_ecsminionexception_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(ECSMinionException):
                self.token_request.get_new_token()
Ejemplo n.º 3
0
    def __init__(self, username=None, password=None, token=None,
                 ecs_endpoint=None, token_endpoint=None, verify_ssl=False,
                 token_filename='ecsminion.tkn', token_location='/tmp',
                 request_timeout=15.0, cache_token=True):
        """
        Creates the ECSMinion 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 ecs_endpoint: The URL where ECS is located
        :param token_endpoint: The URL where the ECS 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 ECS 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.ecs_endpoint = ecs_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,
            ecs_endpoint=self.ecs_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 -> Billing
        self.billing = Billing(self)

        # API -> Configuration
        self.certificate = Certificate(self)
        self.configuration_properties = ConfigurationProperties(self)
        self.licensing = Licensing(self)

        # API -> Geo Replication
        self.replication_group = ReplicationGroup(self)
        self.temp_failed_zone = TemporaryFailedZone(self)

        # API -> Monitoring
        self.capacity = Capacity(self)
        self.dashboard = Dashboard(self)
        self.events = Events(self)

        # API -> Monitoring
        self.namespace = Namespace(self)

        # API -> Provisioning
        self.base_url = BaseUrl(self)
        self.bucket = Bucket(self)
        self.data_store = DataStore(self)
        self.node = Node(self)
        self.storage_pool = StoragePool(self)
        self.virtual_data_center = VirtualDataCenter(self)

        # API -> Support
        self.call_home = CallHome(self)

        # API -> User Management
        self.authentication_provider = AuthenticationProvider(self)
        self.secret_key = SecretKey(self)
        self.secret_key_self_service = SecretKeySelfService(self)
        self.management_object = ManagementUser(self)
        self.user_object = ObjectUser(self)

        # API -> Undocumented
        self.user_info = UserInfo(self)
Ejemplo n.º 4
0
class ECSMinion(object):

    def __init__(self, username=None, password=None, token=None,
                 ecs_endpoint=None, token_endpoint=None, verify_ssl=False,
                 token_filename='ecsminion.tkn', token_location='/tmp',
                 request_timeout=15.0, cache_token=True):
        """
        Creates the ECSMinion 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 ecs_endpoint: The URL where ECS is located
        :param token_endpoint: The URL where the ECS 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 ECS 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.ecs_endpoint = ecs_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,
            ecs_endpoint=self.ecs_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 -> Billing
        self.billing = Billing(self)

        # API -> Configuration
        self.certificate = Certificate(self)
        self.configuration_properties = ConfigurationProperties(self)
        self.licensing = Licensing(self)

        # API -> Geo Replication
        self.replication_group = ReplicationGroup(self)
        self.temp_failed_zone = TemporaryFailedZone(self)

        # API -> Monitoring
        self.capacity = Capacity(self)
        self.dashboard = Dashboard(self)
        self.events = Events(self)

        # API -> Monitoring
        self.namespace = Namespace(self)

        # API -> Provisioning
        self.base_url = BaseUrl(self)
        self.bucket = Bucket(self)
        self.data_store = DataStore(self)
        self.node = Node(self)
        self.storage_pool = StoragePool(self)
        self.virtual_data_center = VirtualDataCenter(self)

        # API -> Support
        self.call_home = CallHome(self)

        # API -> User Management
        self.authentication_provider = AuthenticationProvider(self)
        self.secret_key = SecretKey(self)
        self.secret_key_self_service = SecretKeySelfService(self)
        self.management_object = ManagementUser(self)
        self.user_object = ObjectUser(self)

        # API -> Undocumented
        self.user_info = UserInfo(self)

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

        :return: A valid token or an ecsminion 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):
            log.debug("Removing cached token '{0}'".format(self.token_file))
            os.remove(self.token_file)

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

    def _construct_url(self, path):
        url = '{0}/{1}'.format(self.ecs_endpoint, path)
        log.debug('Constructed URL as: {0}'.format(url))
        return 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 delete(self, url, params=None):
        return self._request(url, params=params, http_verb='DELETE')

    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)
            elif http_verb == 'DELETE':
                # Need to follow up - if 'accept' is in the headers
                # delete calls are not working because ECS 2.0 is returning
                # XML even is JSON is specified
                headers = self._fetch_headers()
                del headers['Accept']

                req = self._session.delete(
                    self._construct_url(url),
                    verify=self.verify_ssl,
                    headers=headers,
                    timeout=self.request_timeout,
                    params=params)
            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:
                log.error("Status code NOT OK")
                raise ECSMinionException(
                    http_status_code=req.status_code,
                    ecs_message=req.text)
            return req.json()

        except requests.ConnectionError as conn_err:
            msg = 'Connection error: {0}'.format(conn_err.args)
            log.error(msg)
            raise ECSMinionException(message=msg)
        except requests.HTTPError as http_err:
            msg = 'HTTP error: {0}'.format(http_err.args)
            log.error(msg)
            raise ECSMinionException(message=msg)
        except requests.RequestException as req_err:
            msg = 'Request error: {0}'.format(req_err.args)
            log.error(msg)
            raise ECSMinionException(message=msg)
        except ValueError:
            return
Ejemplo n.º 5
0
    def __init__(self,
                 username=None,
                 password=None,
                 token=None,
                 ecs_endpoint=None,
                 token_endpoint=None,
                 verify_ssl=False,
                 token_filename='ecsminion.tkn',
                 token_location='/tmp',
                 request_timeout=15.0,
                 cache_token=True):
        """
        Creates the ECSMinion 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 ecs_endpoint: The URL where ECS is located
        :param token_endpoint: The URL where the ECS 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 ECS 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.ecs_endpoint = ecs_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,
            ecs_endpoint=self.ecs_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 -> Billing
        self.billing = Billing(self)

        # API -> Configuration
        self.certificate = Certificate(self)
        self.configuration_properties = ConfigurationProperties(self)
        self.licensing = Licensing(self)

        # API -> Geo Replication
        self.replication_group = ReplicationGroup(self)
        self.temp_failed_zone = TemporaryFailedZone(self)

        # API -> Monitoring
        self.capacity = Capacity(self)
        self.dashboard = Dashboard(self)
        self.events = Events(self)

        # API -> Monitoring
        self.namespace = Namespace(self)

        # API -> Provisioning
        self.base_url = BaseUrl(self)
        self.bucket = Bucket(self)
        self.data_store = DataStore(self)
        self.node = Node(self)
        self.storage_pool = StoragePool(self)
        self.virtual_data_center = VirtualDataCenter(self)

        # API -> Support
        self.call_home = CallHome(self)

        # API -> User Management
        self.authentication_provider = AuthenticationProvider(self)
        self.secret_key = SecretKey(self)
        self.secret_key_self_service = SecretKeySelfService(self)
        self.management_object = ManagementUser(self)
        self.user_object = ObjectUser(self)

        # API -> Undocumented
        self.user_info = UserInfo(self)
Ejemplo n.º 6
0
class ECSMinion(object):
    def __init__(self,
                 username=None,
                 password=None,
                 token=None,
                 ecs_endpoint=None,
                 token_endpoint=None,
                 verify_ssl=False,
                 token_filename='ecsminion.tkn',
                 token_location='/tmp',
                 request_timeout=15.0,
                 cache_token=True):
        """
        Creates the ECSMinion 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 ecs_endpoint: The URL where ECS is located
        :param token_endpoint: The URL where the ECS 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 ECS 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.ecs_endpoint = ecs_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,
            ecs_endpoint=self.ecs_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 -> Billing
        self.billing = Billing(self)

        # API -> Configuration
        self.certificate = Certificate(self)
        self.configuration_properties = ConfigurationProperties(self)
        self.licensing = Licensing(self)

        # API -> Geo Replication
        self.replication_group = ReplicationGroup(self)
        self.temp_failed_zone = TemporaryFailedZone(self)

        # API -> Monitoring
        self.capacity = Capacity(self)
        self.dashboard = Dashboard(self)
        self.events = Events(self)

        # API -> Monitoring
        self.namespace = Namespace(self)

        # API -> Provisioning
        self.base_url = BaseUrl(self)
        self.bucket = Bucket(self)
        self.data_store = DataStore(self)
        self.node = Node(self)
        self.storage_pool = StoragePool(self)
        self.virtual_data_center = VirtualDataCenter(self)

        # API -> Support
        self.call_home = CallHome(self)

        # API -> User Management
        self.authentication_provider = AuthenticationProvider(self)
        self.secret_key = SecretKey(self)
        self.secret_key_self_service = SecretKeySelfService(self)
        self.management_object = ManagementUser(self)
        self.user_object = ObjectUser(self)

        # API -> Undocumented
        self.user_info = UserInfo(self)

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

        :return: A valid token or an ecsminion 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):
            log.debug("Removing cached token '{0}'".format(self.token_file))
            os.remove(self.token_file)

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

    def _construct_url(self, path):
        url = '{0}/{1}'.format(self.ecs_endpoint, path)
        log.debug('Constructed URL as: {0}'.format(url))
        return 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 delete(self, url, params=None):
        return self._request(url, params=params, http_verb='DELETE')

    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)
            elif http_verb == 'DELETE':
                # Need to follow up - if 'accept' is in the headers
                # delete calls are not working because ECS 2.0 is returning
                # XML even is JSON is specified
                headers = self._fetch_headers()
                del headers['Accept']

                req = self._session.delete(self._construct_url(url),
                                           verify=self.verify_ssl,
                                           headers=headers,
                                           timeout=self.request_timeout,
                                           params=params)
            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:
                log.error("Status code NOT OK")
                raise ECSMinionException(http_status_code=req.status_code,
                                         ecs_message=req.text)
            return req.json()

        except requests.ConnectionError as conn_err:
            msg = 'Connection error: {0}'.format(conn_err.args)
            log.error(msg)
            raise ECSMinionException(message=msg)
        except requests.HTTPError as http_err:
            msg = 'HTTP error: {0}'.format(http_err.args)
            log.error(msg)
            raise ECSMinionException(message=msg)
        except requests.RequestException as req_err:
            msg = 'Request error: {0}'.format(req_err.args)
            log.error(msg)
            raise ECSMinionException(message=msg)
        except ValueError:
            return