-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathinstanceadmin.py
More file actions
232 lines (193 loc) · 7.33 KB
/
instanceadmin.py
File metadata and controls
232 lines (193 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/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, cluster_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 = 1
storage_type = enums.StorageType.SSD
labels = {"prod-label": "prod-label"}
instance = client.instance(instance_id, labels=labels)
# [START bigtable_check_instance_exists]
if not instance.exists():
print("Instance {} does not exist.".format(instance_id))
else:
print("Instance {} already exists.".format(instance_id))
# [END bigtable_check_instance_exists]
# [START bigtable_create_prod_instance]
cluster = instance.cluster(
cluster_id,
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
operation = instance.create(clusters=[cluster])
# Ensure the operation completes.
operation.result(timeout=480)
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 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 exist.".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 = 1
storage_type = enums.StorageType.SSD
if not instance.exists():
print("Instance {} does not exist.".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:
operation = cluster.create()
# Ensure the operation completes.
operation.result(timeout=480)
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, 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, 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."
)