예제 #1
0
def _get_instance(spanner_client: client.Client) -> instance.Instance:
    """Returns a spanner instance from the client.

  First, checks if there is an existing instance that can be re-used, returning
  it if one exists. Otherwise, create a new instance, waits for it to be created
  and then returns it.

  Args:
    spanner_client: An initialized spanner client.
  """
    existing_instances = list(spanner_client.list_instances())
    if existing_instances:
        return existing_instances[0]

    # The emulator has one default config.
    config = list(spanner_client.list_instance_configs())[0]
    inst = spanner_client.instance('spanner-instance-name',
                                   configuration_name=config.name)
    inst.create().result()
    return inst
예제 #2
0
    def _get_client(self, project_id: str) -> Client:
        """
        Provides a client for interacting with the Cloud Spanner API.

        :param project_id: The ID of the  GCP project.
        :type project_id: str
        :return: Client
        :rtype: google.cloud.spanner_v1.client.Client
        """
        if not self._client:
            self._client = Client(project=project_id, credentials=self._get_credentials())
        return self._client
예제 #3
0
    def _get_client(self, project_id):
        """
        Provides a client for interacting with the Cloud Spanner API.

        :param project_id: The ID of the  GCP project.
        :type project_id: str
        :return: Client for interacting with the Cloud Spanner API. See:
            https://googleapis.github.io/google-cloud-python/latest/spanner/client-api.html#google.cloud.spanner_v1.client.Client
        :rtype: object
        """
        if not self._client:
            self._client = Client(project=project_id, credentials=self._get_credentials())
        return self._client
예제 #4
0
    def get_client(self, project_id):
        # type: (str) -> Client
        """
        Provides a client for interacting with Cloud Spanner API.

        :param project_id: The ID of the project which owns the instances, tables and data.
        :type project_id: str
        :return: Client for interacting with Cloud Spanner API. See:
            https://googleapis.github.io/google-cloud-python/latest/spanner/client-api.html#google.cloud.spanner_v1.client.Client
        :rtype: object
        """
        if not self._client:
            self._client = Client(project=project_id,
                                  credentials=self._get_credentials())
        return self._client