Skip to content

Commit

Permalink
added instance.exists(), system and unit tests (#5802)
Browse files Browse the repository at this point in the history
  • Loading branch information
AVaksman authored and sduskis committed Aug 16, 2018
1 parent 3541f26 commit b2261bd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
14 changes: 14 additions & 0 deletions bigtable/google/cloud/bigtable/instance.py
Expand Up @@ -25,6 +25,7 @@
from google.cloud.bigtable_admin_v2.types import instance_pb2

from google.cloud.bigtable.enums import RoutingPolicyType
from google.api_core.exceptions import NotFound


_EXISTING_INSTANCE_LOCATION_ID = 'see-existing-cluster'
Expand Down Expand Up @@ -192,6 +193,19 @@ def reload(self):
# instance ID on the response match the request.
self._update_from_pb(instance_pb)

def exists(self):
"""Check whether the instance already exists.
:rtype: bool
:returns: True if the table exists, else False.
"""
try:
self._client.instance_admin_client.get_instance(name=self.name)
return True
# NOTE: There could be other exceptions that are returned to the user.
except NotFound:
return False

def create(self, location_id=None,
serve_nodes=None,
default_storage_type=None, clusters=None):
Expand Down
7 changes: 7 additions & 0 deletions bigtable/tests/system.py
Expand Up @@ -211,6 +211,13 @@ def test_cluster_exists(self):
self.assertTrue(cluster.exists())
self.assertFalse(alt_cluster.exists())

def test_instance_exists(self):
NONEXISTING_INSTANCE_ID = 'instancer-id'

alt_instance = Config.CLIENT.instance(NONEXISTING_INSTANCE_ID)
self.assertTrue(Config.INSTANCE.exists())
self.assertFalse(alt_instance.exists())

def test_create_instance_w_two_clusters(self):
from google.cloud.bigtable import enums
_PRODUCTION = enums.Instance.Type.PRODUCTION
Expand Down
38 changes: 38 additions & 0 deletions bigtable/tests/unit/test_instance.py
Expand Up @@ -394,6 +394,44 @@ def test_reload(self):
# Check Instance optional config values before.
self.assertEqual(instance.display_name, DISPLAY_NAME)

def test_exists(self):
from google.cloud.bigtable_admin_v2.gapic import (
bigtable_instance_admin_client)
from google.cloud.bigtable_admin_v2.proto import (
instance_pb2 as data_v2_pb2)
from google.api_core import exceptions

api = (
bigtable_instance_admin_client.BigtableInstanceAdminClient(
mock.Mock()))
credentials = _make_credentials()
client = self._make_client(project=self.PROJECT,
credentials=credentials, admin=True)

# Create response_pb
instance_name = client.instance_admin_client.instance_path(
self.PROJECT, self.INSTANCE_ID)
response_pb = data_v2_pb2.Instance(name=instance_name)

# Patch the stub used by the API method.
client._instance_admin_client = api
instance_admin_client = client._instance_admin_client
instance_stub = instance_admin_client.bigtable_instance_admin_stub
instance_stub.GetCluster.side_effect = [
response_pb,
exceptions.NotFound('testing'),
exceptions.BadRequest('testing')
]

# Perform the method and check the result.
non_existing_instance_id = 'instance-id-2'
alt_instance_1 = self._make_one(self.INSTANCE_ID, client)
alt_instance_2 = self._make_one(non_existing_instance_id, client)
self.assertTrue(alt_instance_1.exists())
self.assertFalse(alt_instance_2.exists())
with self.assertRaises(exceptions.BadRequest):
alt_instance_2.exists()

def test_create_check_conflicts(self):
instance = self._make_one(self.INSTANCE_ID, None)
with self.assertRaises(ValueError):
Expand Down

0 comments on commit b2261bd

Please sign in to comment.