Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bigtable appprofile snippets #7033

Merged
merged 11 commits into from
Jul 1, 2019
81 changes: 73 additions & 8 deletions bigtable/docs/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
UNIQUE_SUFFIX = unique_resource_id("-")
INSTANCE_ID = "snippet-tests" + UNIQUE_SUFFIX
CLUSTER_ID = "clus-1-" + UNIQUE_SUFFIX
APP_PROFILE_ID = "app-prof" + UNIQUE_SUFFIX
ROUTING_POLICY_TYPE = enums.RoutingPolicyType.ANY
LOCATION_ID = "us-central1-f"
ALT_LOCATION_ID = "us-central1-a"
PRODUCTION = enums.Instance.Type.PRODUCTION
Expand Down Expand Up @@ -167,19 +169,22 @@ def test_bigtable_create_additional_cluster():
retry_429(cluster.delete)()


def test_bigtable_create_app_profile():
def test_bigtable_create_reload_delete_app_profile():
import re
sangramql marked this conversation as resolved.
Show resolved Hide resolved

# [START bigtable_create_app_profile]
from google.cloud.bigtable import Client
from google.cloud.bigtable import enums

routing_policy_type = enums.RoutingPolicyType.ANY

client = Client(admin=True)
instance = client.instance(INSTANCE_ID)

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

app_profile = instance.app_profile(
app_profile_id=app_profile_id,
app_profile_id=APP_PROFILE_ID,
routing_policy_type=routing_policy_type,
description=description,
sangramql marked this conversation as resolved.
Show resolved Hide resolved
cluster_id=CLUSTER_ID,
Expand All @@ -188,10 +193,70 @@ def test_bigtable_create_app_profile():
app_profile = app_profile.create(ignore_warnings=True)
# [END bigtable_create_app_profile]

try:
assert app_profile.exists()
finally:
retry_429(app_profile.delete)(ignore_warnings=True)
# [START bigtable_app_profile_name]
from google.cloud.bigtable import Client

client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
app_profile = instance.app_profile(APP_PROFILE_ID)

app_profile_name = app_profile.name
# [END bigtable_app_profile_name]
_profile_name_re = re.compile(
r"^projects/(?P<project>[^/]+)/"
r"instances/(?P<instance>[^/]+)/"
r"appProfiles/(?P<appprofile_id>"
r"[_a-zA-Z0-9][-_.a-zA-Z0-9]*)$"
)
assert _profile_name_re.match(app_profile_name)

# [START bigtable_app_profile_exists]
from google.cloud.bigtable import Client

client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
app_profile = instance.app_profile(APP_PROFILE_ID)

app_profile_exists = app_profile.exists()
# [END bigtable_app_profile_exists]
assert app_profile_exists

# [START bigtable_reload_app_profile]
from google.cloud.bigtable import Client

client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
app_profile = instance.app_profile(APP_PROFILE_ID)

app_profile.reload()
# [END bigtable_reload_app_profile]
assert app_profile.routing_policy_type == ROUTING_POLICY_TYPE

# [START bigtable_update_app_profile]
from google.cloud.bigtable import Client

client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
app_profile = instance.app_profile(APP_PROFILE_ID)
app_profile.reload()

description = "My new app profile"
app_profile.description = description
app_profile.update()
# [END bigtable_update_app_profile]
assert app_profile.description == description

# [START bigtable_delete_app_profile]
from google.cloud.bigtable import Client

client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
app_profile = instance.app_profile(APP_PROFILE_ID)
app_profile.reload()

app_profile.delete(ignore_warnings=True)
# [END bigtable_delete_app_profile]
assert not app_profile.exists()


def test_bigtable_list_instances():
Expand Down
46 changes: 35 additions & 11 deletions bigtable/google/cloud/bigtable/app_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def name(self):
This property will not change if ``app_profile_id`` does not, but
the return value is not cached.

For example:

.. literalinclude:: snippets.py
:start-after: [START bigtable_app_profile_name]
:end-before: [END bigtable_app_profile_name]

The AppProfile name is of the form
``"projects/../instances/../app_profile/{app_profile_id}"``

Expand Down Expand Up @@ -225,7 +231,14 @@ def _to_pb(self):
return app_profile_pb

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_app_profile]
:end-before: [END bigtable_reload_app_profile]
"""

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

Expand All @@ -236,6 +249,12 @@ def reload(self):
def exists(self):
"""Check whether the AppProfile already exists.

For example:

.. literalinclude:: snippets.py
:start-after: [START bigtable_app_profile_exists]
:end-before: [END bigtable_app_profile_exists]

:rtype: bool
:returns: True if the AppProfile exists, else False.
"""
Expand All @@ -256,17 +275,11 @@ def create(self, ignore_warnings=None):
``description``, ``cluster_id`` and ``allow_transactional_writes``.
To change them before creating, reset the values via

.. code:: python

app_profile.app_profile_id = 'i-changed-my-mind'
app_profile.routing_policy_type = (
google.cloud.bigtable.enums.RoutingPolicyType.SINGLE
)
app_profile.description = 'new-description'
app-profile.cluster_id = 'other-cluster-id'
app-profile.allow_transactional_writes = True
For example:

before calling :meth:`create`.
.. literalinclude:: snippets.py
:start-after: [START bigtable_create_app_profile]
:end-before: [END bigtable_create_app_profile]

:type: ignore_warnings: bool
:param: ignore_warnings: (Optional) If true, ignore safety checks when
Expand All @@ -293,6 +306,11 @@ def update(self, ignore_warnings=None):
``cluster_id``
``allow_transactional_writes``

For example:

.. literalinclude:: snippets.py
:start-after: [START bigtable_update_app_profile]
:end-before: [END bigtable_update_app_profile]
"""
update_mask_pb = field_mask_pb2.FieldMask()

Expand All @@ -313,6 +331,12 @@ def update(self, ignore_warnings=None):
def delete(self, ignore_warnings=None):
"""Delete this AppProfile.

For example:

.. literalinclude:: snippets.py
:start-after: [START bigtable_delete_app_profile]
:end-before: [END bigtable_delete_app_profile]

:type: ignore_warnings: bool
:param: ignore_warnings: If true, ignore safety checks when deleting
the AppProfile.
Expand Down