diff --git a/bigtable/instanceadmin/README.rst b/bigtable/instanceadmin/README.rst new file mode 100644 index 00000000000..16f176a6099 --- /dev/null +++ b/bigtable/instanceadmin/README.rst @@ -0,0 +1,120 @@ +.. This file is automatically generated. Do not edit this file directly. + + +Google Cloud Bigtable table creation +=============================================================================== + +https://cloud.google.com/bigtable/docs/quickstart-cbt + +This page explains how to use the cbt command to connect to a Cloud Bigtable instance, perform basic administrative tasks, and read and write data in a table. + +Google Cloud Bigtable Python Samples +=============================================================================== + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=bigtable/hello/README.rst + + +This directory contains samples for Google Cloud Bigtable. `Google Cloud Bigtable`_ is Google's NoSQL Big Data database service. It's the same database that powers many core Google services, including Search, Analytics, Maps, and Gmail. + + + + +.. _Google Cloud Bigtable: https://cloud.google.com/bigtable/docs + +Setup +------------------------------------------------------------------------------- + + +Authentication +++++++++++++++ + +This sample requires you to have authentication setup. Refer to the +`Authentication Getting Started Guide`_ for instructions on setting up +credentials for applications. + +.. _Authentication Getting Started Guide: + https://cloud.google.com/docs/authentication/getting-started + +Install Dependencies +++++++++++++++++++++ + +#. Clone python-docs-samples and change directory to the sample directory you want to use. + + .. code-block:: bash + + $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git + +#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions. + + .. _Python Development Environment Setup Guide: + https://cloud.google.com/python/setup + +#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. + + .. code-block:: bash + + $ virtualenv env + $ source env/bin/activate + +#. Install the dependencies needed to run the samples. + + .. code-block:: bash + + $ pip install -r requirements.txt + +.. _pip: https://pip.pypa.io/ +.. _virtualenv: https://virtualenv.pypa.io/ + +Samples +------------------------------------------------------------------------------- + +Basic example ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=bigtable/instanceadmin.py,bigtable/instanceadmin/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python instanceadmin.py + + usage: instanceadmin.py [-h] [run] [dev-instance] [del-instance] [add-cluster] [del-cluster] project_id instance_id cluster_id + + Demonstrates how to connect to Cloud Bigtable and run some basic operations + to create instance, create cluster, delete instance and delete cluster. + Prerequisites: - Create a Cloud Bigtable cluster. + https://cloud.google.com/bigtable/docs/creating-cluster - Set your Google + Application Default Credentials. + https://developers.google.com/identity/protocols/application-default- + credentials + + positional arguments: + project_id Your Cloud Platform project ID. + instance_id ID of the Cloud Bigtable instance to connect to. + cluster_id ID of the Cloud Bigtable cluster to connect to. + + optional arguments: + -h, --help show this help message and exit + +The client library +------------------------------------------------------------------------------- + +This sample uses the `Google Cloud Client Library for Python`_. +You can read the documentation for more details on API usage and use GitHub +to `browse the source`_ and `report issues`_. + +.. _Google Cloud Client Library for Python: + https://googlecloudplatform.github.io/google-cloud-python/ +.. _browse the source: + https://github.com/GoogleCloudPlatform/google-cloud-python +.. _report issues: + https://github.com/GoogleCloudPlatform/google-cloud-python/issues + + +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/bigtable/instanceadmin/README.rst.in b/bigtable/instanceadmin/README.rst.in new file mode 100644 index 00000000000..c085e40a627 --- /dev/null +++ b/bigtable/instanceadmin/README.rst.in @@ -0,0 +1,23 @@ +# This file is used to generate README.rst + +product: + name: Google Cloud Bigtable and run some basic operations. + short_name: Cloud Bigtable + url: https://cloud.google.com/bigtable/docs + description: > + `Google Cloud Bigtable`_ is Google's NoSQL Big Data database service. It's + the same database that powers many core Google services, including Search, + Analytics, Maps, and Gmail. + +setup: +- auth +- install_deps + +samples: +- name: Basic example with Bigtable Column family and GC rules. + file: instanceadmin.py + show_help: true + +cloud_client_library: true + +folder: bigtable/instanceadmin \ No newline at end of file diff --git a/bigtable/instanceadmin/instanceadmin.py b/bigtable/instanceadmin/instanceadmin.py new file mode 100644 index 00000000000..55a18567c76 --- /dev/null +++ b/bigtable/instanceadmin/instanceadmin.py @@ -0,0 +1,257 @@ +#!/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. + +"""Demonstrates how to connect to Cloud Bigtable and run some basic operations. +# http://www.apache.org/licenses/LICENSE-2.0 +Prerequisites: +- Create a Cloud Bigtable project. + https://cloud.google.com/bigtable/docs/ +- Set your Google Application Default Credentials. + https://developers.google.com/identity/protocols/application-default-credentials + +Operations performed: +- Create a Cloud Bigtable Instance. +- List Instance for a Cloud Bigtable. +- Delete a Cloud Bigtable Instance. +- Create a Cloud Bigtable Cluster. +- List Cloud Bigtable Clusters. +- Delete a Cloud Bigtable Cluster. +""" + +import argparse + +from google.cloud import bigtable +from google.cloud.bigtable import enums + + +def run_instance_operations(project_id, instance_id): + ''' Check Instance exists. + Creates a Production instance with default Cluster. + List instances in a project. + List clusters in an instance. + + :type project_id: str + :param project_id: Project id of the client. + + :type instance_id: str + :param instance_id: Instance of the client. + ''' + client = bigtable.Client(project=project_id, admin=True) + 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) + + # [START bigtable_check_instance_exists] + if not instance.exists(): + print 'Instance {} does not exists.'.format(instance_id) + else: + print 'Instance {} already exists.'.format(instance_id) + # [END bigtable_check_instance_exists] + + # [START bigtable_create_prod_instance] + cluster = instance.cluster("ssd-cluster1", location_id=location_id, + serve_nodes=serve_nodes, + default_storage_type=storage_type) + if not instance.exists(): + print '\nCreating an Instance' + # Create instance with given options + instance.create(clusters=[cluster]) + print '\nCreated instance: {}'.format(instance_id) + # [END bigtable_create_prod_instance] + + # [START bigtable_list_instances] + print '\nListing Instances:' + for instance_local in client.list_instances()[0]: + print instance_local.instance_id + # [END bigtable_list_instances] + + # [START bigtable_get_instance] + print '\nName of instance:{}\nLabels:{}'.format(instance.display_name, + instance.labels) + # [END bigtable_get_instance] + + # [START bigtable_get_clusters] + print '\nListing Clusters...' + for cluster in instance.list_clusters()[0]: + print cluster.cluster_id + # [END bigtable_get_clusters] + + +def create_dev_instance(project_id, instance_id, cluster_id): + ''' Creates a Development instance with the name "hdd-instance" + location us-central1-f + Cluster nodes should not be set while creating Development + Instance + + :type project_id: str + :param project_id: Project id of the client. + + :type instance_id: str + :param instance_id: Instance of the client. + ''' + + client = bigtable.Client(project=project_id, admin=True) + + # [START bigtable_create_dev_instance] + print '\nCreating a DEVELOPMENT Instance' + # Set options to create an Instance + location_id = 'us-central1-f' + development = enums.Instance.Type.DEVELOPMENT + storage_type = enums.StorageType.HDD + labels = {'dev-label': 'dev-label'} + + # Create instance with given options + instance = client.instance(instance_id, instance_type=development, + labels=labels) + cluster = instance.cluster(cluster_id, location_id=location_id, + default_storage_type=storage_type) + + # Create development instance with given options + if not instance.exists(): + instance.create(clusters=[cluster]) + print 'Created development instance: {}'.format(instance_id) + else: + print 'Instance {} already exists.'.format(instance_id) + + # [END bigtable_create_dev_instance] + + +def delete_instance(project_id, instance_id): + ''' Delete the Instance + + :type project_id: str + :param project_id: Project id of the client. + + :type instance_id: str + :param instance_id: Instance of the client. + ''' + + client = bigtable.Client(project=project_id, admin=True) + instance = client.instance(instance_id) + # [START bigtable_delete_instance] + 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] + + +def add_cluster(project_id, instance_id, cluster_id): + ''' Add Cluster + + :type project_id: str + :param project_id: Project id of the client. + + :type instance_id: str + :param instance_id: Instance of the client. + + :type cluster_id: str + :param cluster_id: Cluster id. + ''' + client = bigtable.Client(project=project_id, admin=True) + instance = client.instance(instance_id) + + location_id = 'us-central1-a' + serve_nodes = 3 + storage_type = enums.StorageType.SSD + + if not instance.exists(): + print 'Instance {} does not exists.'.format(instance_id) + else: + print '\nAdding Cluster to Instance {}'.format(instance_id) + # [START bigtable_create_cluster] + print '\nListing Clusters...' + for cluster in instance.list_clusters()[0]: + print cluster.cluster_id + cluster = instance.cluster(cluster_id, location_id=location_id, + serve_nodes=serve_nodes, + default_storage_type=storage_type) + if cluster.exists(): + print '\nCluster not created, as {} already exists.'.\ + format(cluster_id) + else: + cluster.create() + print '\nCluster created: {}'.format(cluster_id) + # [END bigtable_create_cluster] + + +def delete_cluster(project_id, instance_id, cluster_id): + ''' Delete the cluster + + :type project_id: str + :param project_id: Project id of the client. + + :type instance_id: str + :param instance_id: Instance of the client. + + :type cluster_id: str + :param cluster_id: Cluster id. + ''' + + client = bigtable.Client(project=project_id, admin=True) + instance = client.instance(instance_id) + cluster = instance.cluster(cluster_id) + + # [START bigtable_delete_cluster] + print '\nDeleting Cluster' + if cluster.exists(): + cluster.delete() + print 'Cluster deleted: {}'.format(cluster_id) + else: + print '\nCluster {} does not exist.'.format(cluster_id) + + # [END bigtable_delete_cluster] + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + parser.add_argument('command', + help='run, dev-instance, del-instance, \ + add-cluster or del-cluster. \ + Operation to perform on Instance.') + parser.add_argument('project_id', + help='Your Cloud Platform project ID.') + parser.add_argument('instance_id', + help='ID of the Cloud Bigtable instance to \ + connect to.') + parser.add_argument('cluster_id', + help='ID of the Cloud Bigtable cluster to \ + connect to.') + + args = parser.parse_args() + + if args.command.lower() == 'run': + run_instance_operations(args.project_id, args.instance_id) + elif args.command.lower() == 'dev-instance': + create_dev_instance(args.project_id, args.instance_id, + args.cluster_id) + elif args.command.lower() == 'del-instance': + delete_instance(args.project_id, args.instance_id) + elif args.command.lower() == 'add-cluster': + add_cluster(args.project_id, args.instance_id, args.cluster_id) + elif args.command.lower() == 'del-cluster': + delete_cluster(args.project_id, args.instance_id, args.cluster_id) + else: + print 'Command should be either run \n Use argument -h, \ + --help to show help and exit.' diff --git a/bigtable/instanceadmin/requirements.txt b/bigtable/instanceadmin/requirements.txt new file mode 100755 index 00000000000..8f77d301825 --- /dev/null +++ b/bigtable/instanceadmin/requirements.txt @@ -0,0 +1 @@ +google-cloud-bigtable==0.30.0