Ejemplo n.º 1
0
    def test_empty(self):
        from six.moves.urllib.parse import parse_qs
        from six.moves.urllib.parse import urlparse
        from gcloud.storage.connection import Connection
        PROJECT = 'project'
        conn = Connection()
        EXPECTED_QUERY = {
            'project': [PROJECT],
            'projection': ['noAcl'],
        }
        http = conn._http = Http(
            {
                'status': '200',
                'content-type': 'application/json'
            },
            b'{}',
        )
        buckets = list(self._callFUT(project=PROJECT, connection=conn))
        self.assertEqual(len(buckets), 0)
        self.assertEqual(http._called_with['method'], 'GET')
        self.assertEqual(http._called_with['body'], None)

        BASE_URI = '/'.join([
            conn.API_BASE_URL,
            'storage',
            conn.API_VERSION,
            'b',
        ])
        URI = http._called_with['uri']
        self.assertTrue(URI.startswith(BASE_URI))
        uri_parts = urlparse(URI)
        self.assertEqual(parse_qs(uri_parts.query), EXPECTED_QUERY)
Ejemplo n.º 2
0
def get_connection(project, client_email, private_key_path):
    """Shortcut method to establish a connection to Cloud Storage.

  Use this if you are going to access several buckets
  with the same set of credentials:

  >>> from gcloud import storage
  >>> connection = storage.get_connection(project, email, key_path)
  >>> bucket1 = connection.get_bucket('bucket1')
  >>> bucket2 = connection.get_bucket('bucket2')

  :type project: string
  :param project: The name of the project to connect to.

  :type client_email: string
  :param client_email: The e-mail attached to the service account.

  :type private_key_path: string
  :param private_key_path: The path to a private key file (this file was
                           given to you when you created the service
                           account).

  :rtype: :class:`gcloud.storage.connection.Connection`
  :returns: A connection defined with the proper credentials.
  """

    from gcloud.credentials import Credentials
    from gcloud.storage.connection import Connection

    credentials = Credentials.get_for_service_account(client_email,
                                                      private_key_path,
                                                      scope=SCOPE)
    return Connection(project=project, credentials=credentials)
Ejemplo n.º 3
0
    def _lookup_bucket_hit_helper(self, use_default=False):
        from gcloud.storage._testing import _monkey_defaults
        from gcloud.storage.bucket import Bucket
        from gcloud.storage.connection import Connection
        BLOB_NAME = 'blob-name'
        conn = Connection()
        URI = '/'.join([
            conn.API_BASE_URL,
            'storage',
            conn.API_VERSION,
            'b',
            '%s?projection=noAcl' % (BLOB_NAME, ),
        ])
        http = conn._http = Http(
            {
                'status': '200',
                'content-type': 'application/json'
            },
            '{{"name": "{0}"}}'.format(BLOB_NAME).encode('utf-8'),
        )

        if use_default:
            with _monkey_defaults(connection=conn):
                bucket = self._callFUT(BLOB_NAME)
        else:
            bucket = self._callFUT(BLOB_NAME, connection=conn)

        self.assertTrue(isinstance(bucket, Bucket))
        self.assertTrue(bucket.connection is conn)
        self.assertEqual(bucket.name, BLOB_NAME)
        self.assertEqual(http._called_with['method'], 'GET')
        self.assertEqual(http._called_with['uri'], URI)
Ejemplo n.º 4
0
 def setUp(self):
     # Mock Connection.api_request with a method that just stores the HTTP
     # method, path and query params in an instance variable for later
     # inspection.
     # TODO: It'd be better to make the Connection talk to a local HTTP server
     # that we can inspect, but a simple test using a mock is certainly better
     # than no tests.
     self.connection = Connection('project-name')
     self.connection.api_request = self.mock_api_request
     self.api_request_calls = []
Ejemplo n.º 5
0
    def test_all_arguments(self):
        from six.moves.urllib.parse import parse_qs
        from six.moves.urllib.parse import urlparse
        from gcloud.storage.connection import Connection
        PROJECT = 'foo-bar'
        MAX_RESULTS = 10
        PAGE_TOKEN = 'ABCD'
        PREFIX = 'subfolder'
        PROJECTION = 'full'
        FIELDS = 'items/id,nextPageToken'
        EXPECTED_QUERY = {
            'project': [PROJECT],
            'maxResults': [str(MAX_RESULTS)],
            'pageToken': [PAGE_TOKEN],
            'prefix': [PREFIX],
            'projection': [PROJECTION],
            'fields': [FIELDS],
        }
        CONNECTION = Connection()
        http = CONNECTION._http = Http(
            {
                'status': '200',
                'content-type': 'application/json'
            },
            '{"items": []}',
        )
        iterator = self._callFUT(
            project=PROJECT,
            max_results=MAX_RESULTS,
            page_token=PAGE_TOKEN,
            prefix=PREFIX,
            projection=PROJECTION,
            fields=FIELDS,
            connection=CONNECTION,
        )
        buckets = list(iterator)
        self.assertEqual(buckets, [])
        self.assertEqual(http._called_with['method'], 'GET')
        self.assertEqual(http._called_with['body'], None)

        BASE_URI = '/'.join(
            [CONNECTION.API_BASE_URL, 'storage', CONNECTION.API_VERSION, 'b'])
        URI = http._called_with['uri']
        self.assertTrue(URI.startswith(BASE_URI))
        uri_parts = urlparse(URI)
        self.assertEqual(parse_qs(uri_parts.query), EXPECTED_QUERY)
Ejemplo n.º 6
0
def get_connection(project):
    """Shortcut method to establish a connection to Cloud Storage.

    Use this if you are going to access several buckets with the same
    set of credentials:

    >>> from gcloud import storage
    >>> connection = storage.get_connection(project)
    >>> bucket1 = connection.get_bucket('bucket1')
    >>> bucket2 = connection.get_bucket('bucket2')

    :type project: string
    :param project: The name of the project to connect to.

    :rtype: :class:`gcloud.storage.connection.Connection`
    :returns: A connection defined with the proper credentials.
    """
    implicit_credentials = credentials.get_credentials()
    scoped_credentials = implicit_credentials.create_scoped(SCOPE)
    return Connection(project=project, credentials=scoped_credentials)
Ejemplo n.º 7
0
 def test_miss(self):
     from gcloud.exceptions import NotFound
     from gcloud.storage.connection import Connection
     NONESUCH = 'nonesuch'
     conn = Connection()
     URI = '/'.join([
         conn.API_BASE_URL,
         'storage',
         conn.API_VERSION,
         'b',
         'nonesuch?projection=noAcl',
     ])
     http = conn._http = Http(
         {
             'status': '404',
             'content-type': 'application/json'
         },
         b'{}',
     )
     self.assertRaises(NotFound, self._callFUT, NONESUCH, connection=conn)
     self.assertEqual(http._called_with['method'], 'GET')
     self.assertEqual(http._called_with['uri'], URI)
Ejemplo n.º 8
0
    def _list_buckets_non_empty_helper(self, project, use_default=False):
        from six.moves.urllib.parse import parse_qs
        from six.moves.urllib.parse import urlencode
        from six.moves.urllib.parse import urlparse
        from gcloud._testing import _monkey_defaults as _base_monkey_defaults
        from gcloud.storage._testing import _monkey_defaults
        from gcloud.storage.connection import Connection
        BUCKET_NAME = 'bucket-name'
        conn = Connection()
        query_params = urlencode({'project': project, 'projection': 'noAcl'})
        BASE_URI = '/'.join([
            conn.API_BASE_URL,
            'storage',
            conn.API_VERSION,
        ])
        URI = '/'.join([BASE_URI, 'b?%s' % (query_params, )])
        http = conn._http = Http(
            {
                'status': '200',
                'content-type': 'application/json'
            },
            '{{"items": [{{"name": "{0}"}}]}}'.format(BUCKET_NAME).encode(
                'utf-8'),
        )

        if use_default:
            with _base_monkey_defaults(project=project):
                with _monkey_defaults(connection=conn):
                    buckets = list(self._callFUT())
        else:
            buckets = list(self._callFUT(project=project, connection=conn))

        self.assertEqual(len(buckets), 1)
        self.assertEqual(buckets[0].name, BUCKET_NAME)
        self.assertEqual(http._called_with['method'], 'GET')
        self.assertTrue(http._called_with['uri'].startswith(BASE_URI))
        self.assertEqual(parse_qs(urlparse(http._called_with['uri']).query),
                         parse_qs(urlparse(URI).query))
Ejemplo n.º 9
0
 def test_init(self):
     connection = Connection('project-name')
     self.assertEqual('project-name', connection.project)