Esempio n. 1
0
def get_connection(client_email, private_key_path):
    """Shortcut method to establish a connection to the Cloud Datastore.

  Use this if you are going to access several datasets
  with the same set of credentials (unlikely):

  >>> from gcloud import datastore
  >>> connection = datastore.get_connection(email, key_path)
  >>> dataset1 = connection.dataset('dataset1')
  >>> dataset2 = connection.dataset('dataset2')

  :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.datastore.connection.Connection`
  :returns: A connection defined with the proper credentials.
  """
    from gcloud.credentials import Credentials
    from gcloud.datastore.connection import Connection

    credentials = Credentials.get_for_service_account(client_email,
                                                      private_key_path,
                                                      scope=SCOPE)
    return Connection(credentials=credentials)
Esempio n. 2
0
 def __init__(self, dataset_id=None, namespace=None, connection=None):
     dataset_id = _determine_default_dataset_id(dataset_id)
     if dataset_id is None:
         raise EnvironmentError('Dataset ID could not be inferred.')
     self.dataset_id = dataset_id
     if connection is None:
         connection = Connection.from_environment()
     self.connection = connection
     self._batch_stack = _LocalStack()
     self.namespace = namespace
Esempio n. 3
0
 def __init__(self, dataset_id=None, namespace=None, connection=None):
     dataset_id = _determine_default_dataset_id(dataset_id)
     if dataset_id is None:
         raise EnvironmentError('Dataset ID could not be inferred.')
     self.dataset_id = dataset_id
     if connection is None:
         connection = Connection.from_environment()
     self.connection = connection
     self._batch_stack = _LocalStack()
     self.namespace = namespace
Esempio n. 4
0
def get_connection():
    """Shortcut method to establish a connection to the Cloud Datastore.

    Use this if you are going to access several datasets
    with the same set of credentials (unlikely):

    >>> from gcloud import datastore
    >>> connection = datastore.get_connection()
    >>> dataset1 = connection.dataset('dataset1')
    >>> dataset2 = connection.dataset('dataset2')

    :rtype: :class:`gcloud.datastore.connection.Connection`
    :returns: A connection defined with the proper credentials.
    """
    implicit_credentials = credentials.get_credentials()
    scoped_credentials = implicit_credentials.create_scoped(SCOPE)
    return Connection(credentials=scoped_credentials)
def get_connection():
    """Shortcut method to establish a connection to the Cloud Datastore.

    Use this if you are going to access several datasets
    with the same set of credentials (unlikely):

    >>> from gcloud import datastore

    >>> connection = datastore.get_connection()
    >>> key1 = datastore.Key('Kind', 1234, dataset_id='dataset1')
    >>> key2 = datastore.Key('Kind', 1234, dataset_id='dataset2')
    >>> entity1 = datastore.get(key1, connection=connection)
    >>> entity2 = datastore.get(key2, connection=connection)

    :rtype: :class:`gcloud.datastore.connection.Connection`
    :returns: A connection defined with the proper credentials.
    """
    return Connection.from_environment()
Esempio n. 6
0
    def test_get_multi_w_deferred_from_backend_but_not_passed(self):
        from gcloud.datastore import _datastore_v1_pb2 as datastore_pb
        from gcloud.datastore.connection import Connection
        from gcloud.datastore.key import Key
        from gcloud.datastore import test_connection

        # Shortening name, import line above was too long.
        cmp_key_after_req = test_connection._compare_key_pb_after_request

        key1 = Key('Kind', dataset_id=self.DATASET_ID)
        key2 = Key('Kind', 2345, dataset_id=self.DATASET_ID)
        key_pb1 = key1.to_protobuf()
        key_pb2 = key2.to_protobuf()

        # Build mock first response.
        rsp_pb1 = datastore_pb.LookupResponse()
        entity1 = datastore_pb.Entity()
        entity1.key.CopyFrom(key_pb1)
        # Add the entity to the "found" part of the response.
        rsp_pb1.found.add(entity=entity1)
        # Add the second key to the "deferred" part of the response.
        rsp_pb1.deferred.add().CopyFrom(key_pb2)

        # Build mock second response.
        rsp_pb2 = datastore_pb.LookupResponse()
        # Add in entity that was deferred.
        entity2 = datastore_pb.Entity()
        entity2.key.CopyFrom(key_pb2)
        rsp_pb2.found.add(entity=entity2)

        connection = Connection()
        client = self._makeOne(connection=connection)
        # Add mock http object to connection with response from above.
        http = connection._http = _HttpMultiple(
            ({'status': '200'}, rsp_pb1.SerializeToString()),
            ({'status': '200'}, rsp_pb2.SerializeToString()),
        )

        missing = []
        found = client.get_multi([key1, key2], missing=missing)
        self.assertEqual(len(found), 2)
        self.assertEqual(len(missing), 0)

        # Check the actual contents on the response.
        self.assertEqual(found[0].key.path, key1.path)
        self.assertEqual(found[0].key.dataset_id, key1.dataset_id)
        self.assertEqual(found[1].key.path, key2.path)
        self.assertEqual(found[1].key.dataset_id, key2.dataset_id)

        # Check that our http object was called correctly.
        cw = http._called_with
        rq_class = datastore_pb.LookupRequest
        request = rq_class()
        self.assertEqual(len(cw), 2)

        # Make URI to check for requests.
        URI = '/'.join([
            connection.api_base_url,
            'datastore',
            connection.API_VERSION,
            'datasets',
            self.DATASET_ID,
            'lookup',
        ])

        # Make sure the first called with argument checks out.
        self._verifyProtobufCall(cw[0], URI, connection)
        request.ParseFromString(cw[0]['body'])
        keys = list(request.key)
        self.assertEqual(len(keys), 2)
        cmp_key_after_req(self, key_pb1, keys[0])
        cmp_key_after_req(self, key_pb2, keys[1])

        # Make sure the second called with argument checks out.
        self._verifyProtobufCall(cw[1], URI, connection)
        request.ParseFromString(cw[1]['body'])
        keys = list(request.key)
        self.assertEqual(len(keys), 1)
        cmp_key_after_req(self, key_pb2, keys[0])