Exemple #1
0
def _make_cloud_datastore_context(app_id, external_app_ids=()):
    """Creates a new context to connect to a remote Cloud Datastore instance.

  This should only be used outside of Google App Engine.

  Args:
    app_id: The application id to connect to. This differs from the project
      id as it may have an additional prefix, e.g. "s~" or "e~".
    external_app_ids: A list of apps that may be referenced by data in your
      application. For example, if you are connected to s~my-app and store keys
      for s~my-other-app, you should include s~my-other-app in the external_apps
      list.
  Returns:
    An ndb.Context that can connect to a Remote Cloud Datastore. You can use
    this context by passing it to ndb.set_context.
  """
    from . import model  # Late import to deal with circular imports.
    # Late import since it might not exist.
    if not datastore_pbs._CLOUD_DATASTORE_ENABLED:
        raise datastore_errors.BadArgumentError(
            datastore_pbs.MISSING_CLOUD_DATASTORE_MESSAGE)
    import googledatastore
    try:
        from google.appengine.datastore import cloud_datastore_v1_remote_stub
    except ImportError:
        from google3.apphosting.datastore import cloud_datastore_v1_remote_stub

    current_app_id = os.environ.get('APPLICATION_ID', None)
    if current_app_id and current_app_id != app_id:
        # TODO(pcostello): We should support this so users can connect to different
        # applications.
        raise ValueError(
            'Cannot create a Cloud Datastore context that connects '
            'to an application (%s) that differs from the application '
            'already connected to (%s).' % (app_id, current_app_id))
    os.environ['APPLICATION_ID'] = app_id

    id_resolver = datastore_pbs.IdResolver((app_id, ) +
                                           tuple(external_app_ids))
    project_id = id_resolver.resolve_project_id(app_id)
    endpoint = googledatastore.helper.get_project_endpoint_from_env(project_id)
    datastore = googledatastore.Datastore(
        project_endpoint=endpoint,
        credentials=googledatastore.helper.get_credentials_from_env())

    conn = model.make_connection(
        _api_version=datastore_rpc._CLOUD_DATASTORE_V1,
        _id_resolver=id_resolver)

    # If necessary, install the stubs
    try:
        stub = cloud_datastore_v1_remote_stub.CloudDatastoreV1RemoteStub(
            datastore)
        apiproxy_stub_map.apiproxy.RegisterStub(
            datastore_rpc._CLOUD_DATASTORE_V1, stub)
    except:
        pass  # The stub is already installed.
    # TODO(pcostello): Ensure the current stub is connected to the right project.
    return make_context(conn=conn)
Exemple #2
0
    def testDefaultBaseUrl(self):
        self.conn = datastore.Datastore(project_id='foo')
        request = self.makeLookupRequest()
        payload = request.SerializeToString()
        response = self.makeLookupResponse()
        self.expectRequest(
            'https://datastore.googleapis.com/v1/projects/foo:lookup',
            method='POST',
            body=payload,
            headers=self.makeExpectedHeaders(payload)).AndReturn(
                (TestResponse(status=200,
                              reason='Found'), response.SerializeToString()))
        self.mox.ReplayAll()

        resp = self.conn.lookup(request)
        self.assertEqual(response, resp)
        self.mox.VerifyAll()
Exemple #3
0
  def testDefaultBaseUrl(self):
    self.conn = datastore.Datastore(
        dataset='foo')
    request = self.makeLookupRequest()
    payload = request.SerializeToString()
    response = self.makeLookupResponse()
    self.expectRequest(
        'https://www.googleapis.com/datastore/v1beta1/datasets/foo/lookup',
        method='POST', body=payload,
        headers={'Content-Type': 'application/x-protobuf',
                 'Content-Length': str(len(payload))}).AndReturn(
                     (TestResponse(status=200, reason='Found'),
                      response.SerializeToString()))
    self.mox.ReplayAll()

    resp = self.conn.lookup(request)
    self.assertEqual(response, resp)
    self.mox.VerifyAll()
Exemple #4
0
  def test_functions(self):
    datastore._conn = datastore.Datastore(dataset='foo')
    def caml(s): return ''.join(p[0].upper()+p[1:] for p in s.split('_'))
    rpcs = ['lookup', 'blind_write', 'run_query', 'begin_transaction',
            'commit', 'rollback', 'allocate_ids']
    methods = [(r, getattr(datastore, caml(r)+'Request'),
                getattr(datastore, caml(r)+'Response'))
               for r in rpcs]
    for m, req_class, resp_class in methods:
      self.mox.StubOutWithMock(datastore._conn, m)
      method = getattr(datastore._conn, m)
      method(mox.IsA(req_class)).AndReturn(resp_class())
    self.mox.ReplayAll()

    for m, req_class, resp_class in methods:
      method = getattr(datastore, m)
      result = method(req_class())
      self.assertEqual(resp_class, type(result))
    self.mox.VerifyAll()
    def testDefaultBaseUrl(self):
        self.conn = datastore.Datastore(project_id='foo')
        request = self.makeLookupRequest()
        payload = request.SerializeToString()
        proto_response = self.makeLookupResponse()
        response = httplib2.Response({
            'status': 200,
            'content-type': 'application/x-protobuf',
        })

        self.expectRequest(
            'https://datastore.googleapis.com/v1/projects/foo:lookup',
            method='POST',
            body=payload,
            headers=self.makeExpectedHeaders(payload)).AndReturn(
                (response, proto_response.SerializeToString()))
        self.mox.ReplayAll()

        resp = self.conn.lookup(request)
        self.assertEqual(proto_response, resp)
        self.mox.VerifyAll()
 def setUp(self):
     self.mox = mox.Mox()
     self.conn = datastore.Datastore('foo', host='https://example.com')
Exemple #7
0
 def setUp(self):
     self.mox = mox.Mox()
     self.conn = datastore.Datastore(
         project_endpoint='https://example.com/datastore/v1/projects/foo')