def __init__(self, project_id=None, credentials=None, project_endpoint=None):
    """Datastore client connection constructor.

    Args:
      project_id: the Cloud project to use. Exactly one of
          project_endpoint and project_id must be set.
      credentials: oauth2client.Credentials to authorize the
          connection, default to no credentials.
      project_endpoint: the Datastore endpoint to use. Exactly one of
          project_endpoint and project_id must be set.

    Usage: demos/trivial.py for example usages.

    Raises:
      TypeError: when neither or both of project_endpoint and project_id
      are set.
    """
    self._http = httplib2.Http()
    if not project_endpoint and not project_id:
      raise TypeError('project_endpoint or project_id argument is required.')
    if project_endpoint and project_id:
      raise TypeError('only one of project_endpoint or project_id argument '
                      'is allowed.')

    self._url = (project_endpoint
                 or helper.get_project_endpoint_from_env(project_id=project_id))

    if credentials:
      self._credentials = credentials
      credentials.authorize(self._http)
    else:
      logging.warning('no datastore credentials')
    def __init__(self,
                 project_id=None,
                 credentials=None,
                 project_endpoint=None,
                 host=None):
        """Datastore client connection constructor.

    Args:
      project_id: the Cloud project to use. Exactly one of
          project_endpoint and project_id must be set.
      credentials: oauth2client.Credentials to authorize the
          connection, default to no credentials.
      project_endpoint: the Cloud Datastore API project endpoint to use. Exactly one of
          project_endpoint and project_id must be set. Must not be set if
          host is also set.
      host: the Cloud Datastore API host to use. Must not be set if project_endpoint
         is also set.

    Usage: demos/trivial.py for example usages.

    Raises:
      TypeError: when neither or both of project_endpoint and project_id
      are set or when both project_endpoint and host are set.
    """
        self._http = httplib2.Http()
        if not project_endpoint and not project_id:
            raise TypeError(
                'project_endpoint or project_id argument is required.')
        if project_endpoint and project_id:
            raise TypeError(
                'only one of project_endpoint and project_id argument '
                'is allowed.')
        if project_endpoint and host:
            raise TypeError(
                'only one of project_endpoint and host is allowed.')

        self._url = (project_endpoint or helper.get_project_endpoint_from_env(
            project_id=project_id, host=host))

        if credentials:
            self._credentials = credentials
            credentials.authorize(self._http)
        else:
            logging.warning('no datastore credentials')
Beispiel #3
0
    def testSetOptions(self):
        other_thread_conn = []
        lock1 = threading.Lock()
        lock2 = threading.Lock()
        lock1.acquire()
        lock2.acquire()

        def target():
            # Grab two connections
            other_thread_conn.append(datastore.get_default_connection())
            other_thread_conn.append(datastore.get_default_connection())
            lock1.release(
            )  # Notify that we have grabbed the first 2 connections.
            lock2.acquire()  # Wait for the signal to grab the 3rd.
            other_thread_conn.append(datastore.get_default_connection())

        other_thread = threading.Thread(target=target)

        # Resetting options and state.
        datastore._options = {}
        datastore.set_options(project_id='foo')

        self.mox.StubOutWithMock(helper, 'get_credentials_from_env')
        self.mox.StubOutWithMock(helper, 'get_project_endpoint_from_env')
        endpoint = 'http://localhost:8080/datastore/v1/projects/%s'
        helper.get_project_endpoint_from_env(
            project_id='foo', host=None).AndReturn(endpoint % 'foo')
        helper.get_project_endpoint_from_env(
            project_id='foo', host=None).AndReturn(endpoint % 'foo')
        helper.get_project_endpoint_from_env(
            project_id='bar', host=None).AndReturn(endpoint % 'bar')
        helper.get_project_endpoint_from_env(
            project_id='bar', host=None).AndReturn(endpoint % 'bar')

        helper.get_credentials_from_env().AndReturn(FakeCredentialsFromEnv())
        self.mox.ReplayAll()

        # Start the thread and wait for the first lock.
        other_thread.start()
        lock1.acquire()

        t1_conn1 = datastore.get_default_connection()
        t2_conn1, t2_conn1b = other_thread_conn
        other_thread_conn = []
        # The two threads get different connections.
        self.assertIsNot(t1_conn1, t2_conn1)
        # Multiple calls on the same thread get the same connection.
        self.assertIs(t1_conn1, datastore.get_default_connection())
        self.assertIs(t2_conn1, t2_conn1b)

        # Change the global options and grab the connections again.
        datastore.set_options(project_id='bar')
        lock2.release()
        other_thread.join()
        t1_conn2 = datastore.get_default_connection()
        t2_conn2 = other_thread_conn[0]

        # Changing the options causes all threads to create new connections.
        self.assertIsNot(t1_conn1, t1_conn2)
        self.assertIsNot(t2_conn1, t2_conn2)
        # The new connections are still different for each thread.
        self.assertIsNot(t1_conn2, t2_conn2)
        # The old connections has the old settings.
        self.assertEqual('http://localhost:8080/datastore/v1/projects/foo',
                         t1_conn1._url)
        self.assertEqual('http://localhost:8080/datastore/v1/projects/foo',
                         t2_conn1._url)
        # The new connections has the new settings.
        self.assertEqual('http://localhost:8080/datastore/v1/projects/bar',
                         t1_conn2._url)
        self.assertEqual('http://localhost:8080/datastore/v1/projects/bar',
                         t2_conn2._url)
        self.assertEqual(FakeCredentialsFromEnv, type(t1_conn2._credentials))
        self.assertEqual(FakeCredentialsFromEnv, type(t2_conn2._credentials))
        self.mox.VerifyAll()
    def testSetOptions(self):
        other_thread_conn = []
        lock1 = threading.Lock()
        lock2 = threading.Lock()
        lock1.acquire()
        lock2.acquire()

        def target():
            # Grab two connections
            other_thread_conn.append(datastore.get_default_connection())
            other_thread_conn.append(datastore.get_default_connection())
            lock1.release()  # Notify that we have grabbed the first 2 connections.
            lock2.acquire()  # Wait for the signal to grab the 3rd.
            other_thread_conn.append(datastore.get_default_connection())

        other_thread = threading.Thread(target=target)

        # Resetting options and state.
        datastore._options = {}
        datastore.set_options(project_id="foo")

        self.mox.StubOutWithMock(helper, "get_credentials_from_env")
        self.mox.StubOutWithMock(helper, "get_project_endpoint_from_env")
        endpoint = "http://localhost:8080/datastore/v1/projects/%s"
        helper.get_project_endpoint_from_env(project_id="foo").AndReturn(endpoint % "foo")
        helper.get_project_endpoint_from_env(project_id="foo").AndReturn(endpoint % "foo")
        helper.get_project_endpoint_from_env(project_id="bar").AndReturn(endpoint % "bar")
        helper.get_project_endpoint_from_env(project_id="bar").AndReturn(endpoint % "bar")

        helper.get_credentials_from_env().AndReturn(FakeCredentialsFromEnv())
        self.mox.ReplayAll()

        # Start the thread and wait for the first lock.
        other_thread.start()
        lock1.acquire()

        t1_conn1 = datastore.get_default_connection()
        t2_conn1, t2_conn1b = other_thread_conn
        other_thread_conn = []
        # The two threads get different connections.
        self.assertIsNot(t1_conn1, t2_conn1)
        # Multiple calls on the same thread get the same connection.
        self.assertIs(t1_conn1, datastore.get_default_connection())
        self.assertIs(t2_conn1, t2_conn1b)

        # Change the global options and grab the connections again.
        datastore.set_options(project_id="bar")
        lock2.release()
        other_thread.join()
        t1_conn2 = datastore.get_default_connection()
        t2_conn2 = other_thread_conn[0]

        # Changing the options causes all threads to create new connections.
        self.assertIsNot(t1_conn1, t1_conn2)
        self.assertIsNot(t2_conn1, t2_conn2)
        # The new connections are still different for each thread.
        self.assertIsNot(t1_conn2, t2_conn2)
        # The old connections has the old settings.
        self.assertEqual("http://localhost:8080/datastore/v1/projects/foo", t1_conn1._url)
        self.assertEqual("http://localhost:8080/datastore/v1/projects/foo", t2_conn1._url)
        # The new connections has the new settings.
        self.assertEqual("http://localhost:8080/datastore/v1/projects/bar", t1_conn2._url)
        self.assertEqual("http://localhost:8080/datastore/v1/projects/bar", t2_conn2._url)
        self.assertEqual(FakeCredentialsFromEnv, type(t1_conn2._credentials))
        self.assertEqual(FakeCredentialsFromEnv, type(t2_conn2._credentials))
        self.mox.VerifyAll()