From e8b7e722a20b84da15c6d46771755ec7677723ea Mon Sep 17 00:00:00 2001 From: Sangram Date: Thu, 13 Sep 2018 22:11:14 +0530 Subject: [PATCH 01/16] Adding snippets examples --- bigtable/google/cloud/bigtable/client.py | 12 ++ bigtable/google/cloud/bigtable/cluster.py | 18 +++ bigtable/google/cloud/bigtable/instance.py | 24 +++ docs/bigtable/snippets.py | 180 +++++++++++++++++++++ 4 files changed, 234 insertions(+) create mode 100644 docs/bigtable/snippets.py diff --git a/bigtable/google/cloud/bigtable/client.py b/bigtable/google/cloud/bigtable/client.py index 1ef9e072199c..b212d4aba656 100644 --- a/bigtable/google/cloud/bigtable/client.py +++ b/bigtable/google/cloud/bigtable/client.py @@ -202,6 +202,12 @@ def instance_admin_client(self): def instance(self, instance_id, display_name=None, instance_type=None, labels=None): """Factory to create a instance associated with this client. + + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_create_prod_instance] + :end-before: [END bigtable_create_prod_instance] :type instance_id: str :param instance_id: The ID of the instance. @@ -240,6 +246,12 @@ def instance(self, instance_id, display_name=None, def list_instances(self): """List instances owned by the project. + + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_list_instances] + :end-before: [END bigtable_list_instances] :rtype: tuple :returns: diff --git a/bigtable/google/cloud/bigtable/cluster.py b/bigtable/google/cloud/bigtable/cluster.py index b5032f805f10..d11e964095dc 100644 --- a/bigtable/google/cloud/bigtable/cluster.py +++ b/bigtable/google/cloud/bigtable/cluster.py @@ -182,6 +182,12 @@ def reload(self): def exists(self): """Check whether the cluster already exists. + + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_check_cluster_exists] + :end-before: [END bigtable_check_cluster_exists] :rtype: bool :returns: True if the table exists, else False. @@ -197,6 +203,12 @@ def exists(self): def create(self): """Create this cluster. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_create_cluster] + :end-before: [END bigtable_create_cluster] + .. note:: Uses the ``project``, ``instance`` and ``cluster_id`` on the @@ -259,6 +271,12 @@ def update(self): def delete(self): """Delete this cluster. + + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_delete_cluster] + :end-before: [END bigtable_delete_cluster] Marks a cluster and all of its tables for permanent deletion in 7 days. diff --git a/bigtable/google/cloud/bigtable/instance.py b/bigtable/google/cloud/bigtable/instance.py index b09470dc60b7..2b059f2c9674 100644 --- a/bigtable/google/cloud/bigtable/instance.py +++ b/bigtable/google/cloud/bigtable/instance.py @@ -198,6 +198,12 @@ def reload(self): def exists(self): """Check whether the instance already exists. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_check_instance_exists] + :end-before: [END bigtable_check_instance_exists] + :rtype: bool :returns: True if the table exists, else False. """ @@ -213,6 +219,12 @@ def create(self, location_id=None, default_storage_type=None, clusters=None): """Create this instance. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_create_prod_instance] + :end-before: [END bigtable_create_prod_instance] + .. note:: Uses the ``project`` and ``instance_id`` on the current @@ -324,6 +336,12 @@ def update(self): def delete(self): """Delete this instance. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_delete_instance] + :end-before: [END bigtable_delete_instance] + Marks an instance and all of its tables for permanent deletion in 7 days. @@ -348,6 +366,12 @@ def delete(self): def cluster(self, cluster_id, location_id=None, serve_nodes=None, default_storage_type=None): """Factory to create a cluster associated with this instance. + + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_create_cluster] + :end-before: [END bigtable_create_cluster] :type cluster_id: str :param cluster_id: The ID of the cluster. diff --git a/docs/bigtable/snippets.py b/docs/bigtable/snippets.py new file mode 100644 index 000000000000..01b29f91001a --- /dev/null +++ b/docs/bigtable/snippets.py @@ -0,0 +1,180 @@ +#!/usr/bin/env python + +# Copyright 2018, Google LLC +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Testable usage examples for Google Cloud Bigtable API wrapper + +Each example function takes a ``client`` argument (which must be an instance +of :class:`google.cloud.bigtable.client.Client`) and uses it to perform a task +with the API. + +To facilitate running the examples as system tests, each example is also passed +a ``to_delete`` list; the function adds to the list any objects created which +need to be deleted during teardown. +""" + +from google.cloud import bigtable + + +def snippet(func): + """Mark ``func`` as a snippet example function.""" + func._snippet = True + return func + + +@snippet +def bigtable_create_instance(client, to_delete): + # [START bigtable_create_prod_instance] + from google.cloud.bigtable import enums + + location_id = 'us-central1-f' + serve_nodes = 3 + storage_type = enums.StorageType.SSD + production = enums.Instance.Type.PRODUCTION + labels = {'prod-label': 'prod-label'} + + instance = client.instance("instance_my1", instance_type=production, + labels=labels) + cluster = instance.cluster("ssd-cluster1", location_id=location_id, + serve_nodes=serve_nodes, + default_storage_type=storage_type) + instance.create(clusters=[cluster]) + # [END bigtable_create_prod_instance] + + to_delete.append(instance) + + +@snippet +def bigtable_create_cluster(client): + # [START bigtable_create_cluster] + instance = client.instance("instance_my1") + location_id = 'us-central1-a' + serve_nodes = 3 + storage_type = enums.StorageType.SSD + + cluster = instance.cluster("cluster_my2", location_id=location_id, + serve_nodes=serve_nodes, + default_storage_type=storage_type) + cluster.create() + # [END bigtable_create_cluster] + + +@snippet +def bigtable_list_instances(client): + # [START bigtable_list_instances] + print '\nListing Instances:' + for instance_local in client.list_instances()[0]: + print instance_local.instance_id + # [END bigtable_list_instances] + + +@snippet +def bigtable_list_clusters(client): + # [START bigtable_list_clusters] + from google.cloud.bigtable import enums + + production = enums.Instance.Type.PRODUCTION + labels = {'prod-label': 'prod-label'} + instance = client.instance("instance_my1", instance_type=production, + labels=labels) + + for cluster in instance.list_clusters()[0]: + print cluster.cluster_id + # [END bigtable_list_clusters] + + +@snippet +def bigtable_instance_exists(client): + # [START bigtable_check_instance_exists] + from google.cloud.bigtable import enums + instance = client.instance("instance_my1") + if instance.exists(): + print 'Instance {} exists.'.format(instance_id) + # [END bigtable_check_instance_exists] + + +@snippet +def bigtable_cluster_exists(client): + from google.cloud.bigtable import enums + instance = client.instance("instance_my1") + + # [START bigtable_check_cluster_exists] + location_id = 'us-central1-a' + serve_nodes = 3 + storage_type = enums.StorageType.SSD + cluster = instance.cluster("ssd-cluster1", location_id=location_id, + serve_nodes=serve_nodes, + default_storage_type=storage_type) + if cluster.exists(): + print '\nCluster {} already exists.'.format(cluster_id) + # [END bigtable_check_cluster_exists] + + +@snippet +def bigtable_delete_instance(client): + # [START bigtable_delete_instance] + instance = client.instance("instance_my1") + print '\nDeleting Instance' + if not instance.exists(): + print 'Instance {} does not exists.'.format(instance_id) + else: + instance.delete() + print 'Deleted Instance: {}'.format(instance_id) + # [END bigtable_delete_instance] + + +@snippet +def bigtable_delete_cluster(client): + instance = client.instance("instance_my1") + + # [START bigtable_delete_cluster] + cluster = instance.cluster("ssd-cluster1") + if cluster.exists(): + cluster.delete() + # [END bigtable_delete_cluster] + + +def _line_no(func): + code = getattr(func, '__code__', None) or getattr(func, 'func_code') + return code.co_firstlineno + + +def _find_examples(): + funcs = [obj for obj in globals().values() + if getattr(obj, '_snippet', False)] + for func in sorted(funcs, key=_line_no): + yield func + + +def _name_and_doc(func): + return func.__name__, func.__doc__ + + +def main(): + client = bigtable.Client(project='my-project', admin=True) + for example in _find_examples(): + to_delete = [] + print('%-25s: %s' % _name_and_doc(example)) + try: + example(client, to_delete) + except AssertionError as failure: + print(' FAIL: %s' % (failure,)) + except Exception as error: # pylint: disable=broad-except + print(' ERROR: %r' % (error,)) + for item in to_delete: + item.delete() + + +if __name__ == '__main__': + main() From 97fe27db19879aa2b7e99c09dad069cd20fda091 Mon Sep 17 00:00:00 2001 From: Sangram Date: Mon, 17 Sep 2018 08:53:55 +0530 Subject: [PATCH 02/16] update snippets.py --- bigtable/google/cloud/bigtable/client.py | 6 +++ bigtable/google/cloud/bigtable/instance.py | 20 ++++++++- docs/bigtable/snippets.py | 52 ++++++++++++++-------- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/bigtable/google/cloud/bigtable/client.py b/bigtable/google/cloud/bigtable/client.py index b212d4aba656..a7f705b2c4ff 100644 --- a/bigtable/google/cloud/bigtable/client.py +++ b/bigtable/google/cloud/bigtable/client.py @@ -268,6 +268,12 @@ def list_instances(self): def list_clusters(self): """List the clusters in the project. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_list_clusters] + :end-before: [END bigtable_list_clusters] + :rtype: tuple :returns: (clusters, failed_locations), where 'clusters' is list of diff --git a/bigtable/google/cloud/bigtable/instance.py b/bigtable/google/cloud/bigtable/instance.py index 2b059f2c9674..267c2b5013d5 100644 --- a/bigtable/google/cloud/bigtable/instance.py +++ b/bigtable/google/cloud/bigtable/instance.py @@ -366,7 +366,7 @@ def delete(self): def cluster(self, cluster_id, location_id=None, serve_nodes=None, default_storage_type=None): """Factory to create a cluster associated with this instance. - + For example: .. literalinclude:: snippets.py @@ -409,6 +409,12 @@ def cluster(self, cluster_id, location_id=None, def list_clusters(self): """List the clusters in this instance. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_list_clusters] + :end-before: [END bigtable_list_clusters] + :rtype: tuple :returns: (clusters, failed_locations), where 'clusters' is list of @@ -424,6 +430,12 @@ def list_clusters(self): def table(self, table_id, app_profile_id=None): """Factory to create a table associated with this instance. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_create_table] + :end-before: [END bigtable_create_table] + :type table_id: str :param table_id: The ID of the table. @@ -438,6 +450,12 @@ def table(self, table_id, app_profile_id=None): def list_tables(self): """List the tables in this instance. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_list_tables] + :end-before: [END bigtable_list_tables] + :rtype: list of :class:`Table ` :returns: The list of tables owned by the instance. :raises: :class:`ValueError ` if one of the diff --git a/docs/bigtable/snippets.py b/docs/bigtable/snippets.py index 01b29f91001a..80eb03c4599c 100644 --- a/docs/bigtable/snippets.py +++ b/docs/bigtable/snippets.py @@ -37,7 +37,7 @@ def snippet(func): def bigtable_create_instance(client, to_delete): # [START bigtable_create_prod_instance] from google.cloud.bigtable import enums - + location_id = 'us-central1-f' serve_nodes = 3 storage_type = enums.StorageType.SSD @@ -58,11 +58,13 @@ def bigtable_create_instance(client, to_delete): @snippet def bigtable_create_cluster(client): # [START bigtable_create_cluster] + from google.cloud.bigtable import enums + instance = client.instance("instance_my1") location_id = 'us-central1-a' serve_nodes = 3 storage_type = enums.StorageType.SSD - + cluster = instance.cluster("cluster_my2", location_id=location_id, serve_nodes=serve_nodes, default_storage_type=storage_type) @@ -73,7 +75,6 @@ def bigtable_create_cluster(client): @snippet def bigtable_list_instances(client): # [START bigtable_list_instances] - print '\nListing Instances:' for instance_local in client.list_instances()[0]: print instance_local.instance_id # [END bigtable_list_instances] @@ -88,7 +89,7 @@ def bigtable_list_clusters(client): labels = {'prod-label': 'prod-label'} instance = client.instance("instance_my1", instance_type=production, labels=labels) - + for cluster in instance.list_clusters()[0]: print cluster.cluster_id # [END bigtable_list_clusters] @@ -97,10 +98,9 @@ def bigtable_list_clusters(client): @snippet def bigtable_instance_exists(client): # [START bigtable_check_instance_exists] - from google.cloud.bigtable import enums instance = client.instance("instance_my1") if instance.exists(): - print 'Instance {} exists.'.format(instance_id) + print 'Instance {} exists.'.format("instance_my1") # [END bigtable_check_instance_exists] @@ -108,16 +108,16 @@ def bigtable_instance_exists(client): def bigtable_cluster_exists(client): from google.cloud.bigtable import enums instance = client.instance("instance_my1") - + # [START bigtable_check_cluster_exists] location_id = 'us-central1-a' serve_nodes = 3 - storage_type = enums.StorageType.SSD + storage_type = enums.StorageType.SSD cluster = instance.cluster("ssd-cluster1", location_id=location_id, serve_nodes=serve_nodes, default_storage_type=storage_type) if cluster.exists(): - print '\nCluster {} already exists.'.format(cluster_id) + print '\nCluster {} already exists.'.format("ssd-cluster1") # [END bigtable_check_cluster_exists] @@ -125,12 +125,7 @@ def bigtable_cluster_exists(client): def bigtable_delete_instance(client): # [START bigtable_delete_instance] instance = client.instance("instance_my1") - print '\nDeleting Instance' - if not instance.exists(): - print 'Instance {} does not exists.'.format(instance_id) - else: - instance.delete() - print 'Deleted Instance: {}'.format(instance_id) + instance.delete() # [END bigtable_delete_instance] @@ -145,6 +140,25 @@ def bigtable_delete_cluster(client): # [END bigtable_delete_cluster] +@snippet +def bigtable_create_table(client): + # [START bigtable_create_table] + instance = client.instance("instance_my1") + table = instance.table("table_my") + table.create() + # [END bigtable_create_table] + + +@snippet +def bigtable_list_tables(client): + # [START bigtable_list_tables] + instance = client.instance("instance_my1") + tables = instance.list_tables() + for tbl in tables: + print tbl.table_id + # [END bigtable_list_tables] + + def _line_no(func): code = getattr(func, '__code__', None) or getattr(func, 'func_code') return code.co_firstlineno @@ -164,14 +178,14 @@ def _name_and_doc(func): def main(): client = bigtable.Client(project='my-project', admin=True) for example in _find_examples(): - to_delete = [] - print('%-25s: %s' % _name_and_doc(example)) + to_delete = [] + print '%-25s: %s' % _name_and_doc(example) try: example(client, to_delete) except AssertionError as failure: - print(' FAIL: %s' % (failure,)) + print ' FAIL: %s' % (failure,) except Exception as error: # pylint: disable=broad-except - print(' ERROR: %r' % (error,)) + print ' ERROR: %r' % (error,) for item in to_delete: item.delete() From 44fc05cd8cc3f9ca89730d9ae29ab506ca5b6fea Mon Sep 17 00:00:00 2001 From: Sangram Date: Wed, 19 Sep 2018 02:41:34 +0530 Subject: [PATCH 03/16] Update snippets.py --- bigtable/google/cloud/bigtable/cluster.py | 17 +++++- docs/bigtable/snippets.py | 64 +++++++++++------------ 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/bigtable/google/cloud/bigtable/cluster.py b/bigtable/google/cloud/bigtable/cluster.py index d11e964095dc..14cbc69f1e9f 100644 --- a/bigtable/google/cloud/bigtable/cluster.py +++ b/bigtable/google/cloud/bigtable/cluster.py @@ -172,7 +172,14 @@ def __ne__(self, other): return not self == other def reload(self): - """Reload the metadata for this cluster.""" + """Reload the metadata for this cluster. + + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_reload_cluster] + :end-before: [END bigtable_reload_cluster] + """ cluster_pb = self._instance._client.instance_admin_client.get_cluster( self.name) @@ -182,7 +189,7 @@ def reload(self): def exists(self): """Check whether the cluster already exists. - + For example: .. literalinclude:: snippets.py @@ -235,6 +242,12 @@ def create(self): def update(self): """Update this cluster. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_update_cluster] + :end-before: [END bigtable_update_cluster] + .. note:: Updates the ``serve_nodes``. If you'd like to diff --git a/docs/bigtable/snippets.py b/docs/bigtable/snippets.py index 80eb03c4599c..d7ceb7ebec44 100644 --- a/docs/bigtable/snippets.py +++ b/docs/bigtable/snippets.py @@ -75,23 +75,15 @@ def bigtable_create_cluster(client): @snippet def bigtable_list_instances(client): # [START bigtable_list_instances] - for instance_local in client.list_instances()[0]: - print instance_local.instance_id + (instances_list, failed_locations_list) = client.list_instances() # [END bigtable_list_instances] @snippet def bigtable_list_clusters(client): # [START bigtable_list_clusters] - from google.cloud.bigtable import enums - - production = enums.Instance.Type.PRODUCTION - labels = {'prod-label': 'prod-label'} - instance = client.instance("instance_my1", instance_type=production, - labels=labels) - - for cluster in instance.list_clusters()[0]: - print cluster.cluster_id + instance = client.instance("instance_my1") + (clusters_list, failed_locations_list) = instance.list_clusters() # [END bigtable_list_clusters] @@ -99,25 +91,16 @@ def bigtable_list_clusters(client): def bigtable_instance_exists(client): # [START bigtable_check_instance_exists] instance = client.instance("instance_my1") - if instance.exists(): - print 'Instance {} exists.'.format("instance_my1") + instance_exists = instance.exists() # [END bigtable_check_instance_exists] @snippet def bigtable_cluster_exists(client): - from google.cloud.bigtable import enums - instance = client.instance("instance_my1") - # [START bigtable_check_cluster_exists] - location_id = 'us-central1-a' - serve_nodes = 3 - storage_type = enums.StorageType.SSD - cluster = instance.cluster("ssd-cluster1", location_id=location_id, - serve_nodes=serve_nodes, - default_storage_type=storage_type) - if cluster.exists(): - print '\nCluster {} already exists.'.format("ssd-cluster1") + instance = client.instance("instance_my1") + cluster = instance.cluster("ssd-cluster1") + cluster_exists = cluster.exists() # [END bigtable_check_cluster_exists] @@ -131,21 +114,40 @@ def bigtable_delete_instance(client): @snippet def bigtable_delete_cluster(client): - instance = client.instance("instance_my1") - # [START bigtable_delete_cluster] + instance = client.instance("instance_my1") cluster = instance.cluster("ssd-cluster1") - if cluster.exists(): - cluster.delete() + cluster.delete() # [END bigtable_delete_cluster] +def bigtable_reload_cluster(client): + # [START bigtable_reload_cluster] + instance = client.instance("instance_my1") + cluster = instance.cluster("ssd-cluster1") + cluster.reload() + # [END bigtable_reload_cluster] + +def bigtable_update_cluster(client): + # [START bigtable_update_cluster] + instance = client.instance("instance_my1") + cluster = instance.cluster("ssd-cluster1") + cluster.serve_nodes = 8 + cluster.update() + # [END bigtable_update_cluster] + + @snippet def bigtable_create_table(client): # [START bigtable_create_table] + from google.cloud.bigtable import column_family + instance = client.instance("instance_my1") table = instance.table("table_my") - table.create() + # Define the GC policy to retain only the most recent 2 versions. + max_versions_rule = column_family.MaxVersionsGCRule(2) + column_families = {'cf1': max_versions_rule} + table.create(column_families=column_families) # [END bigtable_create_table] @@ -153,9 +155,7 @@ def bigtable_create_table(client): def bigtable_list_tables(client): # [START bigtable_list_tables] instance = client.instance("instance_my1") - tables = instance.list_tables() - for tbl in tables: - print tbl.table_id + tables_list = instance.list_tables() # [END bigtable_list_tables] From c00c76ad275007d078e187ea0c28fb2a0957ada8 Mon Sep 17 00:00:00 2001 From: Sangram Date: Wed, 19 Sep 2018 02:47:43 +0530 Subject: [PATCH 04/16] Update snippets.py --- docs/bigtable/snippets.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/bigtable/snippets.py b/docs/bigtable/snippets.py index d7ceb7ebec44..caf47f833d2f 100644 --- a/docs/bigtable/snippets.py +++ b/docs/bigtable/snippets.py @@ -22,6 +22,11 @@ To facilitate running the examples as system tests, each example is also passed a ``to_delete`` list; the function adds to the list any objects created which need to be deleted during teardown. + +.. note:: + This file is under progress and will be updated with more guidance from + the team. Unit tests will be added with guidance from the team. + """ from google.cloud import bigtable From eaf24da66af302d984919b2ec2965b59ac18f339 Mon Sep 17 00:00:00 2001 From: Sangram Date: Wed, 19 Sep 2018 03:07:55 +0530 Subject: [PATCH 05/16] Update snippets.py --- bigtable/google/cloud/bigtable/client.py | 4 ++-- bigtable/google/cloud/bigtable/cluster.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bigtable/google/cloud/bigtable/client.py b/bigtable/google/cloud/bigtable/client.py index a7f705b2c4ff..1456e925072b 100644 --- a/bigtable/google/cloud/bigtable/client.py +++ b/bigtable/google/cloud/bigtable/client.py @@ -202,7 +202,7 @@ def instance_admin_client(self): def instance(self, instance_id, display_name=None, instance_type=None, labels=None): """Factory to create a instance associated with this client. - + For example: .. literalinclude:: snippets.py @@ -246,7 +246,7 @@ def instance(self, instance_id, display_name=None, def list_instances(self): """List instances owned by the project. - + For example: .. literalinclude:: snippets.py diff --git a/bigtable/google/cloud/bigtable/cluster.py b/bigtable/google/cloud/bigtable/cluster.py index 14cbc69f1e9f..1b3fe559c3c7 100644 --- a/bigtable/google/cloud/bigtable/cluster.py +++ b/bigtable/google/cloud/bigtable/cluster.py @@ -284,7 +284,7 @@ def update(self): def delete(self): """Delete this cluster. - + For example: .. literalinclude:: snippets.py From 30b76409145eb10ba683a523b19d75162ea6279a Mon Sep 17 00:00:00 2001 From: Sangram Date: Wed, 26 Sep 2018 21:33:56 +0530 Subject: [PATCH 06/16] add support for testing the snippets. --- bigtable/nox.py | 41 +++++++++++++++++++++++++++++++++------ docs/bigtable/snippets.py | 25 +++++++++++++----------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/bigtable/nox.py b/bigtable/nox.py index 1e8ce157ee81..eb3cd9fb5eb6 100644 --- a/bigtable/nox.py +++ b/bigtable/nox.py @@ -97,11 +97,40 @@ def system(session, py): # Run py.test against the system tests. session.run('py.test', '--quiet', 'tests/system.py', *session.posargs) +#'py', ['2.7', '3.6'] + +@nox.session +@nox.parametrize('py', ['2.7']) +def snippets(session, py): + """Run the system test suite.""" + + # Sanity check: Only run system tests if the environment variable is set. + if not os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', ''): + session.skip('Credentials must be set via environment variable.') + + # Run the system tests against latest Python 2 and Python 3 only. + session.interpreter = 'python{}'.format(py) + + # Set the virtualenv dirname. + session.virtualenv_dirname = 'snip-' + py + + # Install all test dependencies, then install local packages in place. + session.install('mock', 'pytest') + for local_dep in LOCAL_DEPS: + session.install('-e', local_dep) + session.install('-e', os.path.join('..', 'bigtable')) + session.install('-e', '.') + + # Run py.test against the system tests. + session.run('py.test', os.path.join('../docs/bigtable', \ + 'snippets.py'), *session.posargs) + + @nox.session def lint(session): """Run linters. - + Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ @@ -109,16 +138,16 @@ def lint(session): session.install('flake8', *LOCAL_DEPS) session.install('.') session.run('flake8', 'google', 'tests') - - + + @nox.session def lint_setup_py(session): """Verify that setup.py is valid (including RST check).""" session.interpreter = 'python3.6' - + # Set the virtualenv dirname. session.virtualenv_dirname = 'setup' - + session.install('docutils', 'Pygments') session.run( 'python', 'setup.py', 'check', '--restructuredtext', '--strict') @@ -127,7 +156,7 @@ def lint_setup_py(session): @nox.session def cover(session): """Run the final coverage report. - + This outputs the coverage report aggregating coverage from the unit test runs (not system test runs), and then erases coverage data. """ diff --git a/docs/bigtable/snippets.py b/docs/bigtable/snippets.py index caf47f833d2f..b8d4aecb1c79 100644 --- a/docs/bigtable/snippets.py +++ b/docs/bigtable/snippets.py @@ -61,7 +61,7 @@ def bigtable_create_instance(client, to_delete): @snippet -def bigtable_create_cluster(client): +def bigtable_create_cluster(client, to_delete): # [START bigtable_create_cluster] from google.cloud.bigtable import enums @@ -78,14 +78,14 @@ def bigtable_create_cluster(client): @snippet -def bigtable_list_instances(client): +def bigtable_list_instances(client, to_delete): # [START bigtable_list_instances] (instances_list, failed_locations_list) = client.list_instances() # [END bigtable_list_instances] @snippet -def bigtable_list_clusters(client): +def bigtable_list_clusters(client, to_delete): # [START bigtable_list_clusters] instance = client.instance("instance_my1") (clusters_list, failed_locations_list) = instance.list_clusters() @@ -93,7 +93,7 @@ def bigtable_list_clusters(client): @snippet -def bigtable_instance_exists(client): +def bigtable_instance_exists(client, to_delete): # [START bigtable_check_instance_exists] instance = client.instance("instance_my1") instance_exists = instance.exists() @@ -101,7 +101,7 @@ def bigtable_instance_exists(client): @snippet -def bigtable_cluster_exists(client): +def bigtable_cluster_exists(client, to_delete): # [START bigtable_check_cluster_exists] instance = client.instance("instance_my1") cluster = instance.cluster("ssd-cluster1") @@ -110,7 +110,7 @@ def bigtable_cluster_exists(client): @snippet -def bigtable_delete_instance(client): +def bigtable_delete_instance(client, to_delete): # [START bigtable_delete_instance] instance = client.instance("instance_my1") instance.delete() @@ -118,7 +118,7 @@ def bigtable_delete_instance(client): @snippet -def bigtable_delete_cluster(client): +def bigtable_delete_cluster(client, to_delete): # [START bigtable_delete_cluster] instance = client.instance("instance_my1") cluster = instance.cluster("ssd-cluster1") @@ -126,14 +126,17 @@ def bigtable_delete_cluster(client): # [END bigtable_delete_cluster] -def bigtable_reload_cluster(client): +@snippet +def bigtable_reload_cluster(client, to_delete): # [START bigtable_reload_cluster] instance = client.instance("instance_my1") cluster = instance.cluster("ssd-cluster1") cluster.reload() # [END bigtable_reload_cluster] -def bigtable_update_cluster(client): + +@snippet +def bigtable_update_cluster(client, to_delete): # [START bigtable_update_cluster] instance = client.instance("instance_my1") cluster = instance.cluster("ssd-cluster1") @@ -143,7 +146,7 @@ def bigtable_update_cluster(client): @snippet -def bigtable_create_table(client): +def bigtable_create_table(client, to_delete): # [START bigtable_create_table] from google.cloud.bigtable import column_family @@ -157,7 +160,7 @@ def bigtable_create_table(client): @snippet -def bigtable_list_tables(client): +def bigtable_list_tables(client, to_delete): # [START bigtable_list_tables] instance = client.instance("instance_my1") tables_list = instance.list_tables() From 558b48b09dadfa602b6b94b7b5ac85e9fb400256 Mon Sep 17 00:00:00 2001 From: Sangram Date: Wed, 26 Sep 2018 21:35:29 +0530 Subject: [PATCH 07/16] add support for testing the snippets. --- bigtable/nox.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bigtable/nox.py b/bigtable/nox.py index eb3cd9fb5eb6..eb24988dca80 100644 --- a/bigtable/nox.py +++ b/bigtable/nox.py @@ -97,10 +97,9 @@ def system(session, py): # Run py.test against the system tests. session.run('py.test', '--quiet', 'tests/system.py', *session.posargs) -#'py', ['2.7', '3.6'] @nox.session -@nox.parametrize('py', ['2.7']) +@nox.parametrize('py', ['2.7', '3.6']) def snippets(session, py): """Run the system test suite.""" From 9bc57fa916973538e742cb708f05daa45aa200a6 Mon Sep 17 00:00:00 2001 From: Sangram Date: Tue, 2 Oct 2018 17:46:04 +0530 Subject: [PATCH 08/16] Update snippets.py to test using pytest --- bigtable/nox.py | 12 ++--- docs/bigtable/snippets.py | 104 +++++++++++++++----------------------- 2 files changed, 48 insertions(+), 68 deletions(-) diff --git a/bigtable/nox.py b/bigtable/nox.py index eb24988dca80..88596666948c 100644 --- a/bigtable/nox.py +++ b/bigtable/nox.py @@ -129,7 +129,7 @@ def snippets(session, py): @nox.session def lint(session): """Run linters. - + Returns a failure if the linters find linting errors or sufficiently serious code quality issues. """ @@ -137,16 +137,16 @@ def lint(session): session.install('flake8', *LOCAL_DEPS) session.install('.') session.run('flake8', 'google', 'tests') - - + + @nox.session def lint_setup_py(session): """Verify that setup.py is valid (including RST check).""" session.interpreter = 'python3.6' - + # Set the virtualenv dirname. session.virtualenv_dirname = 'setup' - + session.install('docutils', 'Pygments') session.run( 'python', 'setup.py', 'check', '--restructuredtext', '--strict') @@ -155,7 +155,7 @@ def lint_setup_py(session): @nox.session def cover(session): """Run the final coverage report. - + This outputs the coverage report aggregating coverage from the unit test runs (not system test runs), and then erases coverage data. """ diff --git a/docs/bigtable/snippets.py b/docs/bigtable/snippets.py index b8d4aecb1c79..8f979c64e639 100644 --- a/docs/bigtable/snippets.py +++ b/docs/bigtable/snippets.py @@ -29,17 +29,25 @@ """ +import pytest + from google.cloud import bigtable -def snippet(func): - """Mark ``func`` as a snippet example function.""" - func._snippet = True - return func +@pytest.fixture(scope='module') +def client(): + return bigtable.Client(project='my-project', admin=True) + +@pytest.fixture +def to_delete(): + doomed = [] + yield doomed + for item in doomed: + item.delete() -@snippet -def bigtable_create_instance(client, to_delete): + +def test_bigtable_create_instance(client, to_delete): # [START bigtable_create_prod_instance] from google.cloud.bigtable import enums @@ -57,11 +65,11 @@ def bigtable_create_instance(client, to_delete): instance.create(clusters=[cluster]) # [END bigtable_create_prod_instance] + assert instance is not None to_delete.append(instance) -@snippet -def bigtable_create_cluster(client, to_delete): +def test_bigtable_create_cluster(client): # [START bigtable_create_cluster] from google.cloud.bigtable import enums @@ -75,81 +83,83 @@ def bigtable_create_cluster(client, to_delete): default_storage_type=storage_type) cluster.create() # [END bigtable_create_cluster] + assert cluster is not None -@snippet -def bigtable_list_instances(client, to_delete): +def test_bigtable_list_instances(client): # [START bigtable_list_instances] (instances_list, failed_locations_list) = client.list_instances() # [END bigtable_list_instances] + assert instances_list.__len__() is not 0 + -@snippet -def bigtable_list_clusters(client, to_delete): +def test_bigtable_list_clusters(client): # [START bigtable_list_clusters] instance = client.instance("instance_my1") (clusters_list, failed_locations_list) = instance.list_clusters() # [END bigtable_list_clusters] + assert clusters_list.__len__() is not 0 -@snippet -def bigtable_instance_exists(client, to_delete): + +def test_bigtable_instance_exists(client): # [START bigtable_check_instance_exists] instance = client.instance("instance_my1") - instance_exists = instance.exists() + instance_exists = instance.exists() # [END bigtable_check_instance_exists] + assert instance_exists -@snippet -def bigtable_cluster_exists(client, to_delete): +def test_bigtable_cluster_exists(client): # [START bigtable_check_cluster_exists] instance = client.instance("instance_my1") cluster = instance.cluster("ssd-cluster1") cluster_exists = cluster.exists() # [END bigtable_check_cluster_exists] + assert cluster_exists -@snippet -def bigtable_delete_instance(client, to_delete): +def test_bigtable_delete_instance(client): # [START bigtable_delete_instance] instance = client.instance("instance_my1") instance.delete() # [END bigtable_delete_instance] + assert instance is None -@snippet -def bigtable_delete_cluster(client, to_delete): +def test_bigtable_delete_cluster(client): # [START bigtable_delete_cluster] instance = client.instance("instance_my1") cluster = instance.cluster("ssd-cluster1") cluster.delete() # [END bigtable_delete_cluster] + assert cluster is None -@snippet -def bigtable_reload_cluster(client, to_delete): +def test_bigtable_reload_cluster(client): # [START bigtable_reload_cluster] instance = client.instance("instance_my1") cluster = instance.cluster("ssd-cluster1") cluster.reload() # [END bigtable_reload_cluster] + assert cluster is not None -@snippet -def bigtable_update_cluster(client, to_delete): +def test_bigtable_update_cluster(client): # [START bigtable_update_cluster] instance = client.instance("instance_my1") cluster = instance.cluster("ssd-cluster1") cluster.serve_nodes = 8 cluster.update() # [END bigtable_update_cluster] + assert cluster is not None -@snippet -def bigtable_create_table(client, to_delete): +def test_bigtable_create_table(client): # [START bigtable_create_table] from google.cloud.bigtable import column_family - + instance = client.instance("instance_my1") table = instance.table("table_my") # Define the GC policy to retain only the most recent 2 versions. @@ -157,46 +167,16 @@ def bigtable_create_table(client, to_delete): column_families = {'cf1': max_versions_rule} table.create(column_families=column_families) # [END bigtable_create_table] + assert table is not None -@snippet -def bigtable_list_tables(client, to_delete): +def test_bigtable_list_tables(client): # [START bigtable_list_tables] instance = client.instance("instance_my1") tables_list = instance.list_tables() # [END bigtable_list_tables] - - -def _line_no(func): - code = getattr(func, '__code__', None) or getattr(func, 'func_code') - return code.co_firstlineno - - -def _find_examples(): - funcs = [obj for obj in globals().values() - if getattr(obj, '_snippet', False)] - for func in sorted(funcs, key=_line_no): - yield func - - -def _name_and_doc(func): - return func.__name__, func.__doc__ - - -def main(): - client = bigtable.Client(project='my-project', admin=True) - for example in _find_examples(): - to_delete = [] - print '%-25s: %s' % _name_and_doc(example) - try: - example(client, to_delete) - except AssertionError as failure: - print ' FAIL: %s' % (failure,) - except Exception as error: # pylint: disable=broad-except - print ' ERROR: %r' % (error,) - for item in to_delete: - item.delete() + assert tables_list.__len__() is not 0 if __name__ == '__main__': - main() + pytest.main() From 31eb41497dd88f2db7fe3d8cb4066123d2e95687 Mon Sep 17 00:00:00 2001 From: Sangram Date: Thu, 18 Oct 2018 22:54:59 +0530 Subject: [PATCH 09/16] updating snippets --- bigtable/nox.py | 3 +- docs/bigtable/snippets.py | 80 +++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/bigtable/nox.py b/bigtable/nox.py index 88596666948c..413b23d1e364 100644 --- a/bigtable/nox.py +++ b/bigtable/nox.py @@ -97,9 +97,10 @@ def system(session, py): # Run py.test against the system tests. session.run('py.test', '--quiet', 'tests/system.py', *session.posargs) +#@nox.parametrize('py', ['2.7', '3.6']) @nox.session -@nox.parametrize('py', ['2.7', '3.6']) +@nox.parametrize('py', ['2.7']) def snippets(session, py): """Run the system test suite.""" diff --git a/docs/bigtable/snippets.py b/docs/bigtable/snippets.py index 8f979c64e639..f05638c3ffd1 100644 --- a/docs/bigtable/snippets.py +++ b/docs/bigtable/snippets.py @@ -33,10 +33,14 @@ from google.cloud import bigtable +INSTANCE_ID = "instance-snippet" +CLUSTER_ID = "cluster-snippet" +CLUSTER_ID1 = "ssd-cluster-1" + @pytest.fixture(scope='module') def client(): - return bigtable.Client(project='my-project', admin=True) + return bigtable.Client(project='grass-clump-479', admin=True) @pytest.fixture @@ -47,6 +51,7 @@ def to_delete(): item.delete() +@pytest.mark.order1 def test_bigtable_create_instance(client, to_delete): # [START bigtable_create_prod_instance] from google.cloud.bigtable import enums @@ -57,9 +62,9 @@ def test_bigtable_create_instance(client, to_delete): production = enums.Instance.Type.PRODUCTION labels = {'prod-label': 'prod-label'} - instance = client.instance("instance_my1", instance_type=production, + instance = client.instance(INSTANCE_ID, instance_type=production, labels=labels) - cluster = instance.cluster("ssd-cluster1", location_id=location_id, + cluster = instance.cluster(CLUSTER_ID1, location_id=location_id, serve_nodes=serve_nodes, default_storage_type=storage_type) instance.create(clusters=[cluster]) @@ -69,16 +74,17 @@ def test_bigtable_create_instance(client, to_delete): to_delete.append(instance) +@pytest.mark.order2 def test_bigtable_create_cluster(client): # [START bigtable_create_cluster] from google.cloud.bigtable import enums - instance = client.instance("instance_my1") + instance = client.instance(INSTANCE_ID) location_id = 'us-central1-a' serve_nodes = 3 storage_type = enums.StorageType.SSD - cluster = instance.cluster("cluster_my2", location_id=location_id, + cluster = instance.cluster(CLUSTER_ID, location_id=location_id, serve_nodes=serve_nodes, default_storage_type=storage_type) cluster.create() @@ -86,6 +92,7 @@ def test_bigtable_create_cluster(client): assert cluster is not None +@pytest.mark.order3 def test_bigtable_list_instances(client): # [START bigtable_list_instances] (instances_list, failed_locations_list) = client.list_instances() @@ -94,73 +101,62 @@ def test_bigtable_list_instances(client): assert instances_list.__len__() is not 0 +@pytest.mark.order4 def test_bigtable_list_clusters(client): # [START bigtable_list_clusters] - instance = client.instance("instance_my1") + instance = client.instance(INSTANCE_ID) (clusters_list, failed_locations_list) = instance.list_clusters() # [END bigtable_list_clusters] assert clusters_list.__len__() is not 0 +@pytest.mark.order5 def test_bigtable_instance_exists(client): # [START bigtable_check_instance_exists] - instance = client.instance("instance_my1") + instance = client.instance(INSTANCE_ID) instance_exists = instance.exists() # [END bigtable_check_instance_exists] assert instance_exists +@pytest.mark.order6 def test_bigtable_cluster_exists(client): # [START bigtable_check_cluster_exists] - instance = client.instance("instance_my1") - cluster = instance.cluster("ssd-cluster1") + instance = client.instance(INSTANCE_ID) + cluster = instance.cluster(CLUSTER_ID1) cluster_exists = cluster.exists() # [END bigtable_check_cluster_exists] assert cluster_exists -def test_bigtable_delete_instance(client): - # [START bigtable_delete_instance] - instance = client.instance("instance_my1") - instance.delete() - # [END bigtable_delete_instance] - assert instance is None - - -def test_bigtable_delete_cluster(client): - # [START bigtable_delete_cluster] - instance = client.instance("instance_my1") - cluster = instance.cluster("ssd-cluster1") - cluster.delete() - # [END bigtable_delete_cluster] - assert cluster is None - - +@pytest.mark.order7 def test_bigtable_reload_cluster(client): # [START bigtable_reload_cluster] - instance = client.instance("instance_my1") - cluster = instance.cluster("ssd-cluster1") + instance = client.instance(INSTANCE_ID) + cluster = instance.cluster(CLUSTER_ID1) cluster.reload() # [END bigtable_reload_cluster] assert cluster is not None +@pytest.mark.order8 def test_bigtable_update_cluster(client): # [START bigtable_update_cluster] - instance = client.instance("instance_my1") - cluster = instance.cluster("ssd-cluster1") + instance = client.instance(INSTANCE_ID) + cluster = instance.cluster(CLUSTER_ID1) cluster.serve_nodes = 8 cluster.update() # [END bigtable_update_cluster] assert cluster is not None +@pytest.mark.order9 def test_bigtable_create_table(client): # [START bigtable_create_table] from google.cloud.bigtable import column_family - instance = client.instance("instance_my1") + instance = client.instance(INSTANCE_ID) table = instance.table("table_my") # Define the GC policy to retain only the most recent 2 versions. max_versions_rule = column_family.MaxVersionsGCRule(2) @@ -170,13 +166,33 @@ def test_bigtable_create_table(client): assert table is not None +@pytest.mark.order10 def test_bigtable_list_tables(client): # [START bigtable_list_tables] - instance = client.instance("instance_my1") + instance = client.instance(INSTANCE_ID) tables_list = instance.list_tables() # [END bigtable_list_tables] assert tables_list.__len__() is not 0 +@pytest.mark.order11 +def test_bigtable_delete_cluster(client): + # [START bigtable_delete_cluster] + instance = client.instance(INSTANCE_ID) + cluster = instance.cluster(CLUSTER_ID1) + cluster.delete() + # [END bigtable_delete_cluster] + assert cluster is None + + +@pytest.mark.order12 +def test_bigtable_delete_instance(client): + # [START bigtable_delete_instance] + instance = client.instance(INSTANCE_ID) + instance.delete() + # [END bigtable_delete_instance] + assert instance is None + + if __name__ == '__main__': pytest.main() From 6e4f1e0ec44cd70aebd3f19e8010fe3572e83c2b Mon Sep 17 00:00:00 2001 From: Sangram Date: Wed, 24 Oct 2018 20:00:35 +0530 Subject: [PATCH 10/16] Update snippets to fix tests --- bigtable/docs/snippets.py | 293 ++++++++++++++++++++++++++++++++++++++ bigtable/noxfile.py | 49 +++---- docs/bigtable/snippets.py | 198 -------------------------- 3 files changed, 312 insertions(+), 228 deletions(-) create mode 100644 bigtable/docs/snippets.py delete mode 100644 docs/bigtable/snippets.py diff --git a/bigtable/docs/snippets.py b/bigtable/docs/snippets.py new file mode 100644 index 000000000000..b9c0438fc3de --- /dev/null +++ b/bigtable/docs/snippets.py @@ -0,0 +1,293 @@ +#!/usr/bin/env python + +# Copyright 2018, Google LLC +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Testable usage examples for Google Cloud Bigtable API wrapper + +Each example function takes a ``client`` argument (which must be an instance +of :class:`google.cloud.bigtable.client.Client`) and uses it to perform a task +with the API. + +To facilitate running the examples as system tests, each example is also passed +a ``to_delete`` list; the function adds to the list any objects created which +need to be deleted during teardown. + +.. note:: + This file is under progress and will be updated with more guidance from + the team. Unit tests will be added with guidance from the team. + +""" + +import datetime +import pytest + +from test_utils.system import unique_resource_id +from google.cloud._helpers import UTC +from google.cloud.bigtable import Client +from google.cloud.bigtable import enums + + +INSTANCE_ID = "snippet-" + unique_resource_id('-') +CLUSTER_ID = "clus-1-" + unique_resource_id('-') +LOCATION_ID = 'us-central1-f' +ALT_LOCATION_ID = 'us-central1-a' +PRODUCTION = enums.Instance.Type.PRODUCTION +SERVER_NODES = 3 +STORAGE_TYPE = enums.StorageType.SSD +LABEL_KEY = u'python-system' +label_stamp = datetime.datetime.utcnow() \ + .replace(microsecond=0, tzinfo=UTC,) \ + .strftime("%Y-%m-%dt%H-%M-%S") +LABELS = {LABEL_KEY: str(label_stamp)} + + +class Config(object): + """Run-time configuration to be modified at set-up. + + This is a mutable stand-in to allow test set-up to modify + global state. + """ + INSTANCE = None + + +def setup_module(): + client = Client(admin=True) + Config.INSTANCE = client.instance(INSTANCE_ID, + instance_type=PRODUCTION, + labels=LABELS) + cluster = Config.INSTANCE.cluster(CLUSTER_ID, + location_id=LOCATION_ID, + serve_nodes=SERVER_NODES, + default_storage_type=STORAGE_TYPE) + operation = Config.INSTANCE.create(clusters=[cluster]) + # We want to make sure the operation completes. + operation.result(timeout=100) + + +def teardown_module(): + Config.INSTANCE.delete() + + +def test_bigtable_create_instance(): + # [START bigtable_create_prod_instance] + from google.cloud.bigtable import Client + from google.cloud.bigtable import enums + + my_instance_id = "inst-my-" + unique_resource_id('-') + my_cluster_id = "clus-my-" + unique_resource_id('-') + location_id = 'us-central1-f' + serve_nodes = 3 + storage_type = enums.StorageType.SSD + production = enums.Instance.Type.PRODUCTION + labels = {'prod-label': 'prod-label'} + + client = Client(admin=True) + instance = client.instance(my_instance_id, instance_type=production, + labels=labels) + cluster = instance.cluster(my_cluster_id, location_id=location_id, + serve_nodes=serve_nodes, + default_storage_type=storage_type) + operation = instance.create(clusters=[cluster]) + # We want to make sure the operation completes. + operation.result(timeout=100) + # [END bigtable_create_prod_instance] + + assert instance.exists() + instance.delete() + + +def test_bigtable_create_additional_cluster(): + # [START bigtable_create_cluster] + from google.cloud.bigtable import Client + from google.cloud.bigtable import enums + + # Assuming that there is an existing instance with `INSTANCE_ID` + # on the server already. + # to create an instance see 'link' + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + + cluster_id = "clus-my-" + unique_resource_id('-') + location_id = 'us-central1-a' + serve_nodes = 3 + storage_type = enums.StorageType.SSD + + cluster = instance.cluster(cluster_id, location_id=location_id, + serve_nodes=serve_nodes, + default_storage_type=storage_type) + operation = cluster.create() + # We want to make sure the operation completes. + operation.result(timeout=100) + # [END bigtable_create_cluster] + cluster2 = instance.cluster(cluster_id) + assert cluster2.exists() + + cluster.delete() + + +def test_bigtable_list_instances(): + # [START bigtable_list_instances] + from google.cloud.bigtable import Client + + client = Client(admin=True) + (instances_list, failed_locations_list) = client.list_instances() + # [END bigtable_list_instances] + + assert instances_list.__len__() is not 0 + + +def test_bigtable_list_clusters(): + # [START bigtable_list_clusters] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + (clusters_list, failed_locations_list) = instance.list_clusters() + # [END bigtable_list_clusters] + + assert clusters_list.__len__() is not 0 + + +def test_bigtable_instance_exists(): + # [START bigtable_check_instance_exists] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + instance_exists = instance.exists() + # [END bigtable_check_instance_exists] + assert instance_exists + + +def test_bigtable_cluster_exists(): + # [START bigtable_check_cluster_exists] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + cluster = instance.cluster(CLUSTER_ID) + cluster_exists = cluster.exists() + # [END bigtable_check_cluster_exists] + assert cluster_exists + + +def test_bigtable_reload_cluster(): + # [START bigtable_reload_cluster] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + cluster = instance.cluster(CLUSTER_ID) + cluster.reload() + # [END bigtable_reload_cluster] + + assert cluster.exists() + + +def test_bigtable_update_cluster(): + # [START bigtable_update_cluster] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + cluster = instance.cluster(CLUSTER_ID) + cluster.serve_nodes = 8 + cluster.update() + # [END bigtable_update_cluster] + assert cluster.exists() + + +def test_bigtable_create_table(): + # [START bigtable_create_table] + from google.cloud.bigtable import Client + from google.cloud.bigtable import column_family + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + table = instance.table("table_my") + # Define the GC policy to retain only the most recent 2 versions. + max_versions_rule = column_family.MaxVersionsGCRule(2) + column_families = {'cf1': max_versions_rule} + table.create(column_families=column_families) + # [END bigtable_create_table] + + assert table.exists() + + +def test_bigtable_list_tables(): + # [START bigtable_list_tables] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + tables_list = instance.list_tables() + # [END bigtable_list_tables] + + assert tables_list.__len__() is not 0 + + +def test_bigtable_delete_cluster(): + # [START bigtable_delete_cluster] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + cluster_id = "clus-my-" + unique_resource_id('-') + # [END bigtable_delete_cluster] + + cluster = instance.cluster(cluster_id, location_id=ALT_LOCATION_ID, + serve_nodes=SERVER_NODES, + default_storage_type=STORAGE_TYPE) + operation = cluster.create() + # We want to make sure the operation completes. + operation.result(timeout=1000) + + # [START bigtable_delete_cluster] + cluster_to_delete = instance.cluster(cluster_id) + cluster_to_delete.delete() + # [END bigtable_delete_cluster] + + assert not cluster_to_delete.exists() + + +def test_bigtable_delete_instance(): + # [START bigtable_delete_instance] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance_id_to_delete = "inst-my-" + unique_resource_id('-') + cluster_id = "clus-my-" + unique_resource_id('-') + # [END bigtable_delete_instance] + + instance = client.instance(instance_id_to_delete, + instance_type=PRODUCTION, + labels=LABELS) + cluster = instance.cluster(cluster_id, + location_id=ALT_LOCATION_ID, + serve_nodes=SERVER_NODES, + default_storage_type=STORAGE_TYPE) + operation = instance.create(clusters=[cluster]) + # We want to make sure the operation completes. + operation.result(timeout=100) + + # [START bigtable_delete_instance] + instance_to_delete = client.instance(instance_id_to_delete) + instance_to_delete.delete() + # [END bigtable_delete_instance] + + assert not instance_to_delete.exists() + + +if __name__ == '__main__': + pytest.main() diff --git a/bigtable/noxfile.py b/bigtable/noxfile.py index 24ba37f0cc82..a4f18e586262 100644 --- a/bigtable/noxfile.py +++ b/bigtable/noxfile.py @@ -81,35 +81,6 @@ def system(session): # Run py.test against the system tests. session.run('py.test', '--quiet', 'tests/system.py', *session.posargs) -#@nox.parametrize('py', ['2.7', '3.6']) - -@nox.session -@nox.parametrize('py', ['2.7']) -def snippets(session, py): - """Run the system test suite.""" - - # Sanity check: Only run system tests if the environment variable is set. - if not os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', ''): - session.skip('Credentials must be set via environment variable.') - - # Run the system tests against latest Python 2 and Python 3 only. - session.interpreter = 'python{}'.format(py) - - # Set the virtualenv dirname. - session.virtualenv_dirname = 'snip-' + py - - # Install all test dependencies, then install local packages in place. - session.install('mock', 'pytest') - for local_dep in LOCAL_DEPS: - session.install('-e', local_dep) - session.install('-e', os.path.join('..', 'bigtable')) - session.install('-e', '.') - - # Run py.test against the system tests. - session.run('py.test', os.path.join('../docs/bigtable', \ - 'snippets.py'), *session.posargs) - - @nox.session(python='3.6') def lint(session): @@ -120,7 +91,7 @@ def lint(session): """ session.install('flake8', *LOCAL_DEPS) session.install('.') - session.run('flake8', 'google', 'tests') + session.run('flake8', 'google', 'tests', 'docs') @nox.session(python='3.6') @@ -142,3 +113,21 @@ def cover(session): session.install('coverage', 'pytest-cov') session.run('coverage', 'report', '--show-missing', '--fail-under=100') session.run('coverage', 'erase') + + +@nox.session(python='3.7') +def snippets(session): + """Run the system test suite.""" + # Sanity check: Only run system tests if the environment variable is set. + if not os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', ''): + session.skip('Credentials must be set via environment variable.') + + # Install all test dependencies, then install local packages in place. + session.install('mock', 'pytest') + for local_dep in LOCAL_DEPS: + session.install('-e', local_dep) + session.install('-e', os.path.join('..', 'bigtable')) + session.install('-e', '../test_utils/') + session.install('-e', '.') + session.run('py.test', os.path.join('docs', \ + 'snippets.py'), *session.posargs) diff --git a/docs/bigtable/snippets.py b/docs/bigtable/snippets.py deleted file mode 100644 index f05638c3ffd1..000000000000 --- a/docs/bigtable/snippets.py +++ /dev/null @@ -1,198 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2018, Google LLC -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Testable usage examples for Google Cloud Bigtable API wrapper - -Each example function takes a ``client`` argument (which must be an instance -of :class:`google.cloud.bigtable.client.Client`) and uses it to perform a task -with the API. - -To facilitate running the examples as system tests, each example is also passed -a ``to_delete`` list; the function adds to the list any objects created which -need to be deleted during teardown. - -.. note:: - This file is under progress and will be updated with more guidance from - the team. Unit tests will be added with guidance from the team. - -""" - -import pytest - -from google.cloud import bigtable - -INSTANCE_ID = "instance-snippet" -CLUSTER_ID = "cluster-snippet" -CLUSTER_ID1 = "ssd-cluster-1" - - -@pytest.fixture(scope='module') -def client(): - return bigtable.Client(project='grass-clump-479', admin=True) - - -@pytest.fixture -def to_delete(): - doomed = [] - yield doomed - for item in doomed: - item.delete() - - -@pytest.mark.order1 -def test_bigtable_create_instance(client, to_delete): - # [START bigtable_create_prod_instance] - from google.cloud.bigtable import enums - - location_id = 'us-central1-f' - serve_nodes = 3 - storage_type = enums.StorageType.SSD - production = enums.Instance.Type.PRODUCTION - labels = {'prod-label': 'prod-label'} - - instance = client.instance(INSTANCE_ID, instance_type=production, - labels=labels) - cluster = instance.cluster(CLUSTER_ID1, location_id=location_id, - serve_nodes=serve_nodes, - default_storage_type=storage_type) - instance.create(clusters=[cluster]) - # [END bigtable_create_prod_instance] - - assert instance is not None - to_delete.append(instance) - - -@pytest.mark.order2 -def test_bigtable_create_cluster(client): - # [START bigtable_create_cluster] - from google.cloud.bigtable import enums - - instance = client.instance(INSTANCE_ID) - location_id = 'us-central1-a' - serve_nodes = 3 - storage_type = enums.StorageType.SSD - - cluster = instance.cluster(CLUSTER_ID, location_id=location_id, - serve_nodes=serve_nodes, - default_storage_type=storage_type) - cluster.create() - # [END bigtable_create_cluster] - assert cluster is not None - - -@pytest.mark.order3 -def test_bigtable_list_instances(client): - # [START bigtable_list_instances] - (instances_list, failed_locations_list) = client.list_instances() - # [END bigtable_list_instances] - - assert instances_list.__len__() is not 0 - - -@pytest.mark.order4 -def test_bigtable_list_clusters(client): - # [START bigtable_list_clusters] - instance = client.instance(INSTANCE_ID) - (clusters_list, failed_locations_list) = instance.list_clusters() - # [END bigtable_list_clusters] - - assert clusters_list.__len__() is not 0 - - -@pytest.mark.order5 -def test_bigtable_instance_exists(client): - # [START bigtable_check_instance_exists] - instance = client.instance(INSTANCE_ID) - instance_exists = instance.exists() - # [END bigtable_check_instance_exists] - assert instance_exists - - -@pytest.mark.order6 -def test_bigtable_cluster_exists(client): - # [START bigtable_check_cluster_exists] - instance = client.instance(INSTANCE_ID) - cluster = instance.cluster(CLUSTER_ID1) - cluster_exists = cluster.exists() - # [END bigtable_check_cluster_exists] - assert cluster_exists - - -@pytest.mark.order7 -def test_bigtable_reload_cluster(client): - # [START bigtable_reload_cluster] - instance = client.instance(INSTANCE_ID) - cluster = instance.cluster(CLUSTER_ID1) - cluster.reload() - # [END bigtable_reload_cluster] - assert cluster is not None - - -@pytest.mark.order8 -def test_bigtable_update_cluster(client): - # [START bigtable_update_cluster] - instance = client.instance(INSTANCE_ID) - cluster = instance.cluster(CLUSTER_ID1) - cluster.serve_nodes = 8 - cluster.update() - # [END bigtable_update_cluster] - assert cluster is not None - - -@pytest.mark.order9 -def test_bigtable_create_table(client): - # [START bigtable_create_table] - from google.cloud.bigtable import column_family - - instance = client.instance(INSTANCE_ID) - table = instance.table("table_my") - # Define the GC policy to retain only the most recent 2 versions. - max_versions_rule = column_family.MaxVersionsGCRule(2) - column_families = {'cf1': max_versions_rule} - table.create(column_families=column_families) - # [END bigtable_create_table] - assert table is not None - - -@pytest.mark.order10 -def test_bigtable_list_tables(client): - # [START bigtable_list_tables] - instance = client.instance(INSTANCE_ID) - tables_list = instance.list_tables() - # [END bigtable_list_tables] - assert tables_list.__len__() is not 0 - - -@pytest.mark.order11 -def test_bigtable_delete_cluster(client): - # [START bigtable_delete_cluster] - instance = client.instance(INSTANCE_ID) - cluster = instance.cluster(CLUSTER_ID1) - cluster.delete() - # [END bigtable_delete_cluster] - assert cluster is None - - -@pytest.mark.order12 -def test_bigtable_delete_instance(client): - # [START bigtable_delete_instance] - instance = client.instance(INSTANCE_ID) - instance.delete() - # [END bigtable_delete_instance] - assert instance is None - - -if __name__ == '__main__': - pytest.main() From 473fab4670b199fd4f3a61f9ecf5b420e4891276 Mon Sep 17 00:00:00 2001 From: Sangram Date: Thu, 25 Oct 2018 21:42:42 +0530 Subject: [PATCH 11/16] add deleted symlink --- bigtable/docs/bigtable | 1 + 1 file changed, 1 insertion(+) create mode 120000 bigtable/docs/bigtable diff --git a/bigtable/docs/bigtable b/bigtable/docs/bigtable new file mode 120000 index 000000000000..f5652544d9a8 --- /dev/null +++ b/bigtable/docs/bigtable @@ -0,0 +1 @@ +bigtable \ No newline at end of file From c4d4da595782f2e26de05590cf430c88479d8375 Mon Sep 17 00:00:00 2001 From: Sangram Date: Thu, 25 Oct 2018 21:51:38 +0530 Subject: [PATCH 12/16] fixed the symlink again --- bigtable/docs/bigtable | 1 - docs/bigtable | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 120000 bigtable/docs/bigtable create mode 120000 docs/bigtable diff --git a/bigtable/docs/bigtable b/bigtable/docs/bigtable deleted file mode 120000 index f5652544d9a8..000000000000 --- a/bigtable/docs/bigtable +++ /dev/null @@ -1 +0,0 @@ -bigtable \ No newline at end of file diff --git a/docs/bigtable b/docs/bigtable new file mode 120000 index 000000000000..27a5bfe5866c --- /dev/null +++ b/docs/bigtable @@ -0,0 +1 @@ +../bigtable/docs/ \ No newline at end of file From 45a37a2eb1026f0e3412c436bfc9fc0f8b51fe58 Mon Sep 17 00:00:00 2001 From: Sangram Date: Sat, 27 Oct 2018 03:59:58 +0530 Subject: [PATCH 13/16] Fix the code as per review for snippets --- bigtable/docs/snippets.py | 127 +++++++++++++++++---- bigtable/google/cloud/bigtable/client.py | 4 +- bigtable/google/cloud/bigtable/instance.py | 44 +++++-- 3 files changed, 138 insertions(+), 37 deletions(-) diff --git a/bigtable/docs/snippets.py b/bigtable/docs/snippets.py index b9c0438fc3de..8ad119e1d670 100644 --- a/bigtable/docs/snippets.py +++ b/bigtable/docs/snippets.py @@ -45,11 +45,11 @@ PRODUCTION = enums.Instance.Type.PRODUCTION SERVER_NODES = 3 STORAGE_TYPE = enums.StorageType.SSD -LABEL_KEY = u'python-system' -label_stamp = datetime.datetime.utcnow() \ +LABEL_KEY = u'python-snippet' +LABEL_STAMP = datetime.datetime.utcnow() \ .replace(microsecond=0, tzinfo=UTC,) \ .strftime("%Y-%m-%dt%H-%M-%S") -LABELS = {LABEL_KEY: str(label_stamp)} +LABELS = {LABEL_KEY: str(LABEL_STAMP)} class Config(object): @@ -102,7 +102,6 @@ def test_bigtable_create_instance(): # We want to make sure the operation completes. operation.result(timeout=100) # [END bigtable_create_prod_instance] - assert instance.exists() instance.delete() @@ -114,7 +113,9 @@ def test_bigtable_create_additional_cluster(): # Assuming that there is an existing instance with `INSTANCE_ID` # on the server already. - # to create an instance see 'link' + # to create an instance see + # 'https://cloud.google.com/bigtable/docs/creating-instance' + client = Client(admin=True) instance = client.instance(INSTANCE_ID) @@ -130,12 +131,34 @@ def test_bigtable_create_additional_cluster(): # We want to make sure the operation completes. operation.result(timeout=100) # [END bigtable_create_cluster] - cluster2 = instance.cluster(cluster_id) - assert cluster2.exists() + assert cluster.exists() cluster.delete() +def test_bigtable_create_app_profile(): + # [START bigtable_create_app_profile] + from google.cloud.bigtable import Client + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + + app_profile_id = "app-prof-" + unique_resource_id('-') + description = 'routing policy-multy' + routing_policy_type = enums.RoutingPolicyType.ANY + + app_profile = instance.app_profile( + app_profile_id=app_profile_id, + routing_policy_type=routing_policy_type, + description=description, + cluster_id=CLUSTER_ID) + + app_profile = app_profile.create(ignore_warnings=True) + # [END bigtable_create_app_profile] + assert app_profile.exists() + + app_profile.delete(ignore_warnings=True) + + def test_bigtable_list_instances(): # [START bigtable_list_instances] from google.cloud.bigtable import Client @@ -143,20 +166,47 @@ def test_bigtable_list_instances(): client = Client(admin=True) (instances_list, failed_locations_list) = client.list_instances() # [END bigtable_list_instances] - - assert instances_list.__len__() is not 0 + assert len(instances_list) is not 0 -def test_bigtable_list_clusters(): - # [START bigtable_list_clusters] +def test_bigtable_list_clusters_on_instance(): + # [START bigtable_list_clusters_on_instance] from google.cloud.bigtable import Client client = Client(admin=True) instance = client.instance(INSTANCE_ID) (clusters_list, failed_locations_list) = instance.list_clusters() - # [END bigtable_list_clusters] + # [END bigtable_list_clusters_on_instance] + assert len(clusters_list) is not 0 + + +def test_bigtable_list_clusters_in_project(): + # [START bigtable_list_clusters_in_project] + from google.cloud.bigtable import Client + + client = Client(admin=True) + (clusters_list, failed_locations_list) = client.list_clusters() + # [END bigtable_list_clusters_in_project] + assert len(clusters_list) is not 0 + + +def test_bigtable_list_app_profiles(): + # [START bigtable_list_app_profiles] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + # [END bigtable_list_app_profiles] + + app_profile = instance.app_profile( + app_profile_id="app-prof-" + unique_resource_id('-'), + routing_policy_type=enums.RoutingPolicyType.ANY) + app_profile = app_profile.create(ignore_warnings=True) - assert clusters_list.__len__() is not 0 + # [START bigtable_list_app_profiles] + app_profiles_list = instance.list_app_profiles() + # [END bigtable_list_app_profiles] + assert len(app_profiles_list) is not 0 def test_bigtable_instance_exists(): @@ -182,6 +232,17 @@ def test_bigtable_cluster_exists(): assert cluster_exists +def test_bigtable_reload_instance(): + # [START bigtable_reload_instance] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + instance.reload() + # [END bigtable_reload_instance] + assert instance.type_ is PRODUCTION.value + + def test_bigtable_reload_cluster(): # [START bigtable_reload_cluster] from google.cloud.bigtable import Client @@ -191,8 +252,20 @@ def test_bigtable_reload_cluster(): cluster = instance.cluster(CLUSTER_ID) cluster.reload() # [END bigtable_reload_cluster] + assert cluster.serve_nodes is SERVER_NODES - assert cluster.exists() + +def test_bigtable_update_instance(): + # [START bigtable_update_instance] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + display_name = "My new instance" + instance.display_name = display_name + instance.update() + # [END bigtable_update_instance] + assert instance.display_name is display_name def test_bigtable_update_cluster(): @@ -205,7 +278,7 @@ def test_bigtable_update_cluster(): cluster.serve_nodes = 8 cluster.update() # [END bigtable_update_cluster] - assert cluster.exists() + assert cluster.serve_nodes is 8 def test_bigtable_create_table(): @@ -218,10 +291,8 @@ def test_bigtable_create_table(): table = instance.table("table_my") # Define the GC policy to retain only the most recent 2 versions. max_versions_rule = column_family.MaxVersionsGCRule(2) - column_families = {'cf1': max_versions_rule} - table.create(column_families=column_families) + table.create(column_families={'cf1': max_versions_rule}) # [END bigtable_create_table] - assert table.exists() @@ -233,8 +304,7 @@ def test_bigtable_list_tables(): instance = client.instance(INSTANCE_ID) tables_list = instance.list_tables() # [END bigtable_list_tables] - - assert tables_list.__len__() is not 0 + assert len(tables_list) is not 0 def test_bigtable_delete_cluster(): @@ -257,7 +327,6 @@ def test_bigtable_delete_cluster(): cluster_to_delete = instance.cluster(cluster_id) cluster_to_delete.delete() # [END bigtable_delete_cluster] - assert not cluster_to_delete.exists() @@ -267,8 +336,8 @@ def test_bigtable_delete_instance(): client = Client(admin=True) instance_id_to_delete = "inst-my-" + unique_resource_id('-') - cluster_id = "clus-my-" + unique_resource_id('-') # [END bigtable_delete_instance] + cluster_id = "clus-my-" + unique_resource_id('-') instance = client.instance(instance_id_to_delete, instance_type=PRODUCTION, @@ -285,9 +354,21 @@ def test_bigtable_delete_instance(): instance_to_delete = client.instance(instance_id_to_delete) instance_to_delete.delete() # [END bigtable_delete_instance] - assert not instance_to_delete.exists() +def test_bigtable_test_iam_permissions(): + # [START bigtable_test_iam_permissions] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + instance.reload() + permissions = ["bigtable.clusters.create", "bigtable.tables.create"] + permissions_allowed = instance.test_iam_permissions(permissions) + # [END bigtable_test_iam_permissions] + assert permissions_allowed == permissions + + if __name__ == '__main__': pytest.main() diff --git a/bigtable/google/cloud/bigtable/client.py b/bigtable/google/cloud/bigtable/client.py index 1456e925072b..76bb3706190a 100644 --- a/bigtable/google/cloud/bigtable/client.py +++ b/bigtable/google/cloud/bigtable/client.py @@ -271,8 +271,8 @@ def list_clusters(self): For example: .. literalinclude:: snippets.py - :start-after: [START bigtable_list_clusters] - :end-before: [END bigtable_list_clusters] + :start-after: [START bigtable_list_clusters_in_project] + :end-before: [END bigtable_list_clusters_in_project] :rtype: tuple :returns: diff --git a/bigtable/google/cloud/bigtable/instance.py b/bigtable/google/cloud/bigtable/instance.py index 267c2b5013d5..3e387a492449 100644 --- a/bigtable/google/cloud/bigtable/instance.py +++ b/bigtable/google/cloud/bigtable/instance.py @@ -187,7 +187,14 @@ def __ne__(self, other): return not self == other def reload(self): - """Reload the metadata for this instance.""" + """Reload the metadata for this instance. + + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_reload_instance] + :end-before: [END bigtable_reload_instance] + """ instance_pb = self._client.instance_admin_client.get_instance( self.name) @@ -299,6 +306,12 @@ def create(self, location_id=None, def update(self): """Updates an instance within a project. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_update_instance] + :end-before: [END bigtable_update_instance] + .. note:: Updates any or all of the following values: @@ -412,8 +425,8 @@ def list_clusters(self): For example: .. literalinclude:: snippets.py - :start-after: [START bigtable_list_clusters] - :end-before: [END bigtable_list_clusters] + :start-after: [START bigtable_list_clusters_on_instance] + :end-before: [END bigtable_list_clusters_on_instance] :rtype: tuple :returns: @@ -481,6 +494,12 @@ def app_profile(self, app_profile_id, allow_transactional_writes=None): """Factory to create AppProfile associated with this instance. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_create_app_profile] + :end-before: [END bigtable_create_app_profile] + :type app_profile_id: str :param app_profile_id: The ID of the AppProfile. Must be of the form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``. @@ -517,6 +536,12 @@ def app_profile(self, app_profile_id, def list_app_profiles(self): """Lists information about AppProfiles in an instance. + For example: + + .. literalinclude:: snippets.py + :start-after: [START bigtable_list_app_profiles] + :end-before: [END bigtable_list_app_profiles] + :rtype: :list:[`~google.cloud.bigtable.app_profile.AppProfile`] :returns: A :list:[`~google.cloud.bigtable.app_profile.AppProfile`]. By default, this is a list of @@ -585,16 +610,11 @@ def test_iam_permissions(self, permissions): """Returns permissions that the caller has on the specified instance resource. - .. code-block:: python - - from google.cloud.bigtable.client import Client + For example: - client = Client(admin=True) - instance = client.instance('[INSTANCE_ID]') - permissions = ["bigtable.tables.create", - "bigtable.clusters.create"] - permissions_allowed = instance.test_iam_permissions(permissions) - print (permissions_allowed) + .. literalinclude:: snippets.py + :start-after: [START bigtable_test_iam_permissions] + :end-before: [END bigtable_test_iam_permissions] :type permissions: list :param permissions: The set of permissions to check for From b8079ab06a33c3dff070da8708b6993952043510 Mon Sep 17 00:00:00 2001 From: Sangram Date: Sat, 27 Oct 2018 19:55:43 +0530 Subject: [PATCH 14/16] Added quite flag while running tests --- bigtable/noxfile.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bigtable/noxfile.py b/bigtable/noxfile.py index a4f18e586262..dfef10881d03 100644 --- a/bigtable/noxfile.py +++ b/bigtable/noxfile.py @@ -129,5 +129,6 @@ def snippets(session): session.install('-e', os.path.join('..', 'bigtable')) session.install('-e', '../test_utils/') session.install('-e', '.') - session.run('py.test', os.path.join('docs', \ - 'snippets.py'), *session.posargs) + session.run('py.test', '--quiet', \ + os.path.join('docs', 'snippets.py'), \ + *session.posargs) From 100f51880e2abebb895be707072040237be542aa Mon Sep 17 00:00:00 2001 From: Sangram Date: Mon, 29 Oct 2018 23:16:55 +0530 Subject: [PATCH 15/16] add get and set iam policy --- bigtable/docs/snippets.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/bigtable/docs/snippets.py b/bigtable/docs/snippets.py index 8ad119e1d670..01564fca50b5 100644 --- a/bigtable/docs/snippets.py +++ b/bigtable/docs/snippets.py @@ -370,5 +370,37 @@ def test_bigtable_test_iam_permissions(): assert permissions_allowed == permissions +def test_bigtable_get_iam_policy(): + # [START bigtable_get_iam_policy] + from google.cloud.bigtable import Client + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + policy_latest = instance.get_iam_policy() + # [END bigtable_get_iam_policy] + + assert len(policy_latest.bigtable_viewers) is not 0 + + +def test_bigtable_set_iam_policy(): + # [START bigtable_set_iam_policy] + from google.cloud.bigtable import Client + from google.cloud.bigtable.policy import Policy + from google.cloud.bigtable.policy import BIGTABLE_ADMIN_ROLE + + client = Client(admin=True) + instance = client.instance(INSTANCE_ID) + instance.reload() + ins_policy = Policy() + ins_policy[BIGTABLE_ADMIN_ROLE] = [ + Policy.user("test_iam@test.com"), + Policy.service_account("sv_account@gmail.com")] + + policy_latest = instance.set_iam_policy(ins_policy) + # [END bigtable_set_iam_policy] + + assert len(policy_latest.bigtable_admins) is not 0 + + if __name__ == '__main__': pytest.main() From 64a8aa4a67048812fd9d9fb141bf764027ee1da2 Mon Sep 17 00:00:00 2001 From: Sangram Date: Mon, 29 Oct 2018 23:29:44 +0530 Subject: [PATCH 16/16] add snippet example link in instance.py for get and set iam policy --- bigtable/google/cloud/bigtable/instance.py | 30 ++++++---------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/bigtable/google/cloud/bigtable/instance.py b/bigtable/google/cloud/bigtable/instance.py index 3e387a492449..17e373673f64 100644 --- a/bigtable/google/cloud/bigtable/instance.py +++ b/bigtable/google/cloud/bigtable/instance.py @@ -554,15 +554,11 @@ def list_app_profiles(self): def get_iam_policy(self): """Gets the access control policy for an instance resource. - .. code-block:: python - - from google.cloud.bigtable.client import Client - from google.cloud.bigtable.policy import Policy + For example: - client = Client(admin=True) - instance = client.instance('[INSTANCE_ID]') - policy_latest = instance.get_iam_policy() - print (policy_latest.bigtable_viewers) + .. literalinclude:: snippets.py + :start-after: [START bigtable_get_iam_policy] + :end-before: [END bigtable_get_iam_policy] :rtype: :class:`google.cloud.bigtable.policy.Policy` :returns: The current IAM policy of this instance @@ -578,21 +574,11 @@ def set_iam_policy(self, policy): For more information about policy, please see documentation of class `google.cloud.bigtable.policy.Policy` - .. code-block:: python - - from google.cloud.bigtable.client import Client - from google.cloud.bigtable.policy import Policy - from google.cloud.bigtable.policy import BIGTABLE_ADMIN_ROLE - - client = Client(admin=True) - instance = client.instance('[INSTANCE_ID]') - ins_policy = instance.get_iam_policy() - ins_policy[BIGTABLE_ADMIN_ROLE] = [ - Policy.user("test_iam@test.com"), - Policy.service_account("sv_account@gmail.com")] + For example: - policy_latest = instance.set_iam_policy() - print (policy_latest.bigtable_admins) + .. literalinclude:: snippets.py + :start-after: [START bigtable_set_iam_policy] + :end-before: [END bigtable_set_iam_policy] :type policy: :class:`google.cloud.bigtable.policy.Policy` :param policy: A new IAM policy to replace the current IAM policy