-
Notifications
You must be signed in to change notification settings - Fork 0
/
samples.py
312 lines (244 loc) · 9.98 KB
/
samples.py
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""examples of how to use the CloudSqlAdmin wrapper class
"""
from pprint import pprint
from time import sleep
from timeit import default_timer
import uuid # used to generate a random password for user_insert
from gcsql_admin import CloudSqlAdmin
# For convenience during dev/test, config.py contains some default values.
from config import MY_PROJECT, MY_INSTANCE
def databases_delete(project: str, instance: str, database: str):
"""Deletes a database from a Cloud SQL instance.
Demonstrates use of CloudSqlAdmin.databases.delete() method.
Args:
project: name of the Cloud SQL project
instance: name of the Cloud SQL instance
database: database name
Returns:
None. Database is deleted, and a summary is printed to the console.
"""
sql_admin = CloudSqlAdmin()
if sql_admin.databases.delete(project, instance, database):
print(f"database {database} deleted, status = {sql_admin.response['status']}")
else:
print(f"ERROR deleting database {database}!")
print(sql_admin.response["error"])
def databases_get(project: str, instance: str, database: str):
"""gets metadata for database in a Cloud SQL instance.
Demonstrates use of CloudSqlAdmin.databases.get() method.
Args:
project: name of the Cloud SQL project
instance: name of the Cloud SQL instance
database: database name
Returns:
None. Prints the database metadata to the console.
"""
sql_admin = CloudSqlAdmin()
metadata = sql_admin.databases.get(project, instance, database)
print(f"metadata for project {project}, instance {instance}, database {database}:")
pprint(metadata)
def databases_insert(project: str, instance: str, database: str):
"""Inserts a new database in a Cloud SQL instance.
Demonstrates use of CloudSqlAdmin.databases.insert() method.
Args:
project: name of the Cloud SQL project
instance: name of the Cloud SQL instance
database: name of the database
Returns:
None. database is created, and a summary is printed to the console.
"""
sql_admin = CloudSqlAdmin()
if sql_admin.databases.insert(project, instance, database):
print(f"Database created: {database}, status = {sql_admin.response['status']}")
else:
print(f"ERROR creating database: {sql_admin.response}")
def databases_insert_delete():
"""Inserts a new database, then deletes the database.
"""
databases_insert(MY_PROJECT, MY_INSTANCE, "testdb")
databases_get(MY_PROJECT, MY_INSTANCE, "testdb")
databases_delete(MY_PROJECT, MY_INSTANCE, "testdb")
databases_get(MY_PROJECT, MY_INSTANCE, "testdb")
def databases_list(project: str, instance: str):
"""Prints a summary of the databases in a Cloud SQL instance.
Demonstrates use of CloudSqlAdmin.databases.list() method.
Args:
project: name of the Cloud SQL project
instance: name of the Cloud SQL instance
Returns:
None - prints output to console.
"""
print(f"PROJECT/INSTANCE: {project} / {instance}")
sql_admin = CloudSqlAdmin()
for database in sql_admin.databases.list(project, instance):
print(f" Database: {database['name']}")
def instances_delete(project: str, instance: str):
"""Deletes a Cloud SQL instance.
Demonstrates use of CloudSqlAdmin.instances.delete() method.
Args:
project: name of the Cloud SQL project
instance: name of the Cloud SQL instance
Returns:
None. Instance is deleted, and a summary is printed to the console.
"""
sql_admin = CloudSqlAdmin()
if sql_admin.instances.delete(project, instance):
print(f"instance {instance} deleted, status = {sql_admin.response['status']}")
else:
print(f"ERROR deleting instance {instance}!")
print(sql_admin.response["error"])
def instances_get(project: str, instance: str):
"""gets metadata for a Cloud SQL instance.
Demonstrates use of CloudSqlAdmin.instances.get() method.
Args:
project: name of the Cloud SQL project
instance: name of the Cloud SQL instance
Returns:
None. Prints the instance metadata to the console.
"""
sql_admin = CloudSqlAdmin()
metadata = sql_admin.instances.get(project, instance)
print(f"metadata for project {project}, instance {instance}:")
pprint(metadata)
def instances_insert(
project: str, instance_name: str, root_password: str, database_type: str = "MySQL"
):
"""Creates a new Cloud SQL instance.
Demonstrates use of CloudSqlAdmin.instances.insert() method.
Args:
project: name of the Cloud SQL project
instance_name: name of the Cloud SQL instance to be created
root_password: password for root user
database_type: the type of database; must be "MySQL" or "PostgreSQL"
Returns:
None. instance is created, and a summary is printed to the console.
"""
sql_admin = CloudSqlAdmin()
if sql_admin.instances.insert(
project=project,
instance_name=instance_name,
root_password=root_password,
database_type=database_type,
):
print(
f"Cloud SQL instance {instance_name} created, status = {sql_admin.response['status']}"
)
else:
print(f"ERROR creating instance {instance_name}: {sql_admin.response}")
def instances_list(project: str):
"""Prints a summary of the list of Cloud SQL instances in a project.
Demonstrates use of CloudSqlAdmin.instances.list() method.
Args:
project: name of the Cloud SQL project
Returns:
None - prints output to console.
"""
print(f"PROJECT NAME: {project}")
sql_admin = CloudSqlAdmin()
for instance in sql_admin.instances.list(project):
print(f"Instance -->: {instance['name']}")
print(f" DB Version: {instance['databaseVersion']}")
print(f" Tier: {instance['settings']['tier']}")
print(f" Pricing: {instance['settings']['pricingPlan']}")
print(f" State: {instance['state']}")
def instance_state_polling(project: str, instance: str):
"""Prints state of an instance to the console every 5 seconds.
"""
start_time = default_timer()
sql_admin = CloudSqlAdmin()
while True:
metadata = sql_admin.instances.get(project, instance)
if "state" in metadata:
state = metadata["state"]
else:
state = "not found"
print(
(
f"{default_timer() - start_time:9.4} seconds elapsed - "
f"project: {project}, instance: {instance}, state: {state}"
)
)
sleep(5)
def tiers_list(project: str):
"""Prints out the available Cloud SQL tiers (machine types) for a project.
This sample demonstrates how to use the CloudSqlAdmin.service resource to
make an API call that has not been implemented in the entity-specific
classes contained in CloudSqlAdmin.
Documentation for the API used:
https://developers.google.com/resources/api-libraries/documentation/sqladmin/v1beta4/python/latest/sqladmin_v1beta4.tiers.html
"""
sql_admin = CloudSqlAdmin()
request = sql_admin.service.tiers().list(project=project)
response: dict = request.execute()
print(response)
def users_delete(project: str, instance: str, host: str, username: str):
"""Deletes a user from a Cloud SQL instance.
Demonstrates use of CloudSqlAdmin.users.delete() method.
Args:
project: name of the Cloud SQL project
instance: name of the Cloud SQL instance
host: the user's host IP address
username: user name
Returns:
None. User is deleted, and a summary is printed to the console.
"""
sql_admin = CloudSqlAdmin()
if sql_admin.users.delete(project, instance, host, username):
print(f"user {username} deleted, status = {sql_admin.response['status']}")
else:
print(f"ERROR deleting user {username}!")
print(sql_admin.response["error"])
def users_insert(project: str, instance: str, host: str, username: str, password: str):
"""Inserts a new user in a Cloud SQL instance.
Demonstrates use of CloudSqlAdmin.users.insert() method.
Args:
project: name of the Cloud SQL project
instance: name of the Cloud SQL instance
host: the user's host IP address
username: user name
password: password for the new user
Returns:
None. User is created, and a summary is printed to the console.
"""
sql_admin = CloudSqlAdmin()
if sql_admin.users.insert(project, instance, host, username, password):
print(f"User created: {username}")
else:
print(f"ERROR inserting user: {sql_admin.response}")
def users_insert_delete():
"""Inserts a new user, then deletes the user.
"""
users_insert(
project=MY_PROJECT,
instance=MY_INSTANCE,
host="localhost",
username="testuser",
password=str(uuid.uuid4()),
)
users_list(MY_PROJECT, MY_INSTANCE) # this list will include testuser
users_delete(
project=MY_PROJECT, instance=MY_INSTANCE, host="localhost", username="testuser"
)
users_list(MY_PROJECT, MY_INSTANCE) # this list will not include testuser
def users_list(project: str, instance: str):
"""Prints a summary of the users in a Cloud SQL instance.
Demonstrates use of CloudSqlAdmin.users.list() method.
Args:
project: name of the Cloud SQL project
instance: name of the Cloud SQL instance
Returns:
None - prints output to console.
"""
print(f"PROJECT/INSTANCE: {project} / {instance}")
sql_admin = CloudSqlAdmin()
for user in sql_admin.users.list(project, instance):
print(f" User Name: {user['name']}")
if __name__ == "__main__":
# typical examples of running the samples:
# databases_insert_delete()
databases_list(MY_PROJECT, MY_INSTANCE)
# instances_get(MY_PROJECT, MY_INSTANCE)
instances_list(MY_PROJECT)
tiers_list(MY_PROJECT)
# users_insert_delete()
pass