Skip to content

Commit fc8fd4e

Browse files
sangramqlcrwilcox
authored andcommitted
Bigtable appprofile snippets (#7033)
* add conditional row snippets * add snippets * add app profile snippets * Implement pending review comments
1 parent 8a20190 commit fc8fd4e

2 files changed

Lines changed: 108 additions & 19 deletions

File tree

docs/snippets.py

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
UNIQUE_SUFFIX = unique_resource_id("-")
4545
INSTANCE_ID = "snippet-tests" + UNIQUE_SUFFIX
4646
CLUSTER_ID = "clus-1-" + UNIQUE_SUFFIX
47+
APP_PROFILE_ID = "app-prof" + UNIQUE_SUFFIX
48+
ROUTING_POLICY_TYPE = enums.RoutingPolicyType.ANY
4749
LOCATION_ID = "us-central1-f"
4850
ALT_LOCATION_ID = "us-central1-a"
4951
PRODUCTION = enums.Instance.Type.PRODUCTION
@@ -167,19 +169,22 @@ def test_bigtable_create_additional_cluster():
167169
retry_429(cluster.delete)()
168170

169171

170-
def test_bigtable_create_app_profile():
172+
def test_bigtable_create_reload_delete_app_profile():
173+
import re
174+
171175
# [START bigtable_create_app_profile]
172176
from google.cloud.bigtable import Client
177+
from google.cloud.bigtable import enums
178+
179+
routing_policy_type = enums.RoutingPolicyType.ANY
173180

174181
client = Client(admin=True)
175182
instance = client.instance(INSTANCE_ID)
176183

177-
app_profile_id = "app-prof-" + UNIQUE_SUFFIX
178184
description = "routing policy-multy"
179-
routing_policy_type = enums.RoutingPolicyType.ANY
180185

181186
app_profile = instance.app_profile(
182-
app_profile_id=app_profile_id,
187+
app_profile_id=APP_PROFILE_ID,
183188
routing_policy_type=routing_policy_type,
184189
description=description,
185190
cluster_id=CLUSTER_ID,
@@ -188,10 +193,70 @@ def test_bigtable_create_app_profile():
188193
app_profile = app_profile.create(ignore_warnings=True)
189194
# [END bigtable_create_app_profile]
190195

191-
try:
192-
assert app_profile.exists()
193-
finally:
194-
retry_429(app_profile.delete)(ignore_warnings=True)
196+
# [START bigtable_app_profile_name]
197+
from google.cloud.bigtable import Client
198+
199+
client = Client(admin=True)
200+
instance = client.instance(INSTANCE_ID)
201+
app_profile = instance.app_profile(APP_PROFILE_ID)
202+
203+
app_profile_name = app_profile.name
204+
# [END bigtable_app_profile_name]
205+
_profile_name_re = re.compile(
206+
r"^projects/(?P<project>[^/]+)/"
207+
r"instances/(?P<instance>[^/]+)/"
208+
r"appProfiles/(?P<appprofile_id>"
209+
r"[_a-zA-Z0-9][-_.a-zA-Z0-9]*)$"
210+
)
211+
assert _profile_name_re.match(app_profile_name)
212+
213+
# [START bigtable_app_profile_exists]
214+
from google.cloud.bigtable import Client
215+
216+
client = Client(admin=True)
217+
instance = client.instance(INSTANCE_ID)
218+
app_profile = instance.app_profile(APP_PROFILE_ID)
219+
220+
app_profile_exists = app_profile.exists()
221+
# [END bigtable_app_profile_exists]
222+
assert app_profile_exists
223+
224+
# [START bigtable_reload_app_profile]
225+
from google.cloud.bigtable import Client
226+
227+
client = Client(admin=True)
228+
instance = client.instance(INSTANCE_ID)
229+
app_profile = instance.app_profile(APP_PROFILE_ID)
230+
231+
app_profile.reload()
232+
# [END bigtable_reload_app_profile]
233+
assert app_profile.routing_policy_type == ROUTING_POLICY_TYPE
234+
235+
# [START bigtable_update_app_profile]
236+
from google.cloud.bigtable import Client
237+
238+
client = Client(admin=True)
239+
instance = client.instance(INSTANCE_ID)
240+
app_profile = instance.app_profile(APP_PROFILE_ID)
241+
app_profile.reload()
242+
243+
description = "My new app profile"
244+
app_profile.description = description
245+
app_profile.update()
246+
# [END bigtable_update_app_profile]
247+
assert app_profile.description == description
248+
249+
# [START bigtable_delete_app_profile]
250+
from google.cloud.bigtable import Client
251+
252+
client = Client(admin=True)
253+
instance = client.instance(INSTANCE_ID)
254+
app_profile = instance.app_profile(APP_PROFILE_ID)
255+
app_profile.reload()
256+
257+
app_profile.delete(ignore_warnings=True)
258+
# [END bigtable_delete_app_profile]
259+
assert not app_profile.exists()
195260

196261

197262
def test_bigtable_list_instances():

google/cloud/bigtable/app_profile.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ def name(self):
9090
This property will not change if ``app_profile_id`` does not, but
9191
the return value is not cached.
9292
93+
For example:
94+
95+
.. literalinclude:: snippets.py
96+
:start-after: [START bigtable_app_profile_name]
97+
:end-before: [END bigtable_app_profile_name]
98+
9399
The AppProfile name is of the form
94100
``"projects/../instances/../app_profile/{app_profile_id}"``
95101
@@ -225,7 +231,14 @@ def _to_pb(self):
225231
return app_profile_pb
226232

227233
def reload(self):
228-
"""Reload the metadata for this cluster"""
234+
"""Reload the metadata for this cluster
235+
236+
For example:
237+
238+
.. literalinclude:: snippets.py
239+
:start-after: [START bigtable_reload_app_profile]
240+
:end-before: [END bigtable_reload_app_profile]
241+
"""
229242

230243
app_profile_pb = self.instance_admin_client.get_app_profile(self.name)
231244

@@ -236,6 +249,12 @@ def reload(self):
236249
def exists(self):
237250
"""Check whether the AppProfile already exists.
238251
252+
For example:
253+
254+
.. literalinclude:: snippets.py
255+
:start-after: [START bigtable_app_profile_exists]
256+
:end-before: [END bigtable_app_profile_exists]
257+
239258
:rtype: bool
240259
:returns: True if the AppProfile exists, else False.
241260
"""
@@ -256,17 +275,11 @@ def create(self, ignore_warnings=None):
256275
``description``, ``cluster_id`` and ``allow_transactional_writes``.
257276
To change them before creating, reset the values via
258277
259-
.. code:: python
260-
261-
app_profile.app_profile_id = 'i-changed-my-mind'
262-
app_profile.routing_policy_type = (
263-
google.cloud.bigtable.enums.RoutingPolicyType.SINGLE
264-
)
265-
app_profile.description = 'new-description'
266-
app-profile.cluster_id = 'other-cluster-id'
267-
app-profile.allow_transactional_writes = True
278+
For example:
268279
269-
before calling :meth:`create`.
280+
.. literalinclude:: snippets.py
281+
:start-after: [START bigtable_create_app_profile]
282+
:end-before: [END bigtable_create_app_profile]
270283
271284
:type: ignore_warnings: bool
272285
:param: ignore_warnings: (Optional) If true, ignore safety checks when
@@ -293,6 +306,11 @@ def update(self, ignore_warnings=None):
293306
``cluster_id``
294307
``allow_transactional_writes``
295308
309+
For example:
310+
311+
.. literalinclude:: snippets.py
312+
:start-after: [START bigtable_update_app_profile]
313+
:end-before: [END bigtable_update_app_profile]
296314
"""
297315
update_mask_pb = field_mask_pb2.FieldMask()
298316

@@ -313,6 +331,12 @@ def update(self, ignore_warnings=None):
313331
def delete(self, ignore_warnings=None):
314332
"""Delete this AppProfile.
315333
334+
For example:
335+
336+
.. literalinclude:: snippets.py
337+
:start-after: [START bigtable_delete_app_profile]
338+
:end-before: [END bigtable_delete_app_profile]
339+
316340
:type: ignore_warnings: bool
317341
:param: ignore_warnings: If true, ignore safety checks when deleting
318342
the AppProfile.

0 commit comments

Comments
 (0)