Skip to content

Commit f242cc1

Browse files
sangramqlcrwilcox
authored andcommitted
Bigtable Batcher, RowData, Row Operations, AppendRow snippets (googleapis#7019)
1 parent 806c6df commit f242cc1

File tree

8 files changed

+1008
-57
lines changed

8 files changed

+1008
-57
lines changed

bigtable/docs/snippets.py

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,5 +470,208 @@ def test_bigtable_instance_admin_client():
470470
assert "BigtableInstanceAdmin" in str(instance_admin_client)
471471

472472

473+
def test_bigtable_admins_policy():
474+
# [START bigtable_admins_policy]
475+
from google.cloud.bigtable import Client
476+
from google.cloud.bigtable.policy import Policy
477+
from google.cloud.bigtable.policy import BIGTABLE_ADMIN_ROLE
478+
479+
# [END bigtable_admins_policy]
480+
481+
service_account_email = Config.CLIENT._credentials.service_account_email
482+
483+
# [START bigtable_admins_policy]
484+
client = Client(admin=True)
485+
instance = client.instance(INSTANCE_ID)
486+
instance.reload()
487+
new_policy = Policy()
488+
new_policy[BIGTABLE_ADMIN_ROLE] = [Policy.service_account(service_account_email)]
489+
490+
policy_latest = instance.set_iam_policy(new_policy)
491+
policy = policy_latest.bigtable_admins
492+
# [END bigtable_admins_policy]
493+
494+
assert len(policy) > 0
495+
496+
497+
def test_bigtable_readers_policy():
498+
# [START bigtable_readers_policy]
499+
from google.cloud.bigtable import Client
500+
from google.cloud.bigtable.policy import Policy
501+
from google.cloud.bigtable.policy import BIGTABLE_READER_ROLE
502+
503+
# [END bigtable_readers_policy]
504+
505+
service_account_email = Config.CLIENT._credentials.service_account_email
506+
507+
# [START bigtable_readers_policy]
508+
client = Client(admin=True)
509+
instance = client.instance(INSTANCE_ID)
510+
instance.reload()
511+
new_policy = Policy()
512+
new_policy[BIGTABLE_READER_ROLE] = [Policy.service_account(service_account_email)]
513+
514+
policy_latest = instance.set_iam_policy(new_policy)
515+
policy = policy_latest.bigtable_readers
516+
# [END bigtable_readers_policy]
517+
518+
assert len(policy) > 0
519+
520+
521+
def test_bigtable_users_policy():
522+
# [START bigtable_users_policy]
523+
from google.cloud.bigtable import Client
524+
from google.cloud.bigtable.policy import Policy
525+
from google.cloud.bigtable.policy import BIGTABLE_USER_ROLE
526+
527+
# [END bigtable_users_policy]
528+
529+
service_account_email = Config.CLIENT._credentials.service_account_email
530+
531+
# [START bigtable_users_policy]
532+
client = Client(admin=True)
533+
instance = client.instance(INSTANCE_ID)
534+
instance.reload()
535+
new_policy = Policy()
536+
new_policy[BIGTABLE_USER_ROLE] = [Policy.service_account(service_account_email)]
537+
538+
policy_latest = instance.set_iam_policy(new_policy)
539+
policy = policy_latest.bigtable_users
540+
# [END bigtable_users_policy]
541+
542+
assert len(policy) > 0
543+
544+
545+
def test_bigtable_viewers_policy():
546+
# [START bigtable_viewers_policy]
547+
from google.cloud.bigtable import Client
548+
from google.cloud.bigtable.policy import Policy
549+
from google.cloud.bigtable.policy import BIGTABLE_VIEWER_ROLE
550+
551+
# [END bigtable_viewers_policy]
552+
553+
service_account_email = Config.CLIENT._credentials.service_account_email
554+
555+
# [START bigtable_viewers_policy]
556+
client = Client(admin=True)
557+
instance = client.instance(INSTANCE_ID)
558+
instance.reload()
559+
new_policy = Policy()
560+
new_policy[BIGTABLE_VIEWER_ROLE] = [Policy.service_account(service_account_email)]
561+
562+
policy_latest = instance.set_iam_policy(new_policy)
563+
policy = policy_latest.bigtable_viewers
564+
# [END bigtable_viewers_policy]
565+
566+
assert len(policy) > 0
567+
568+
569+
def test_bigtable_instance_name():
570+
import re
571+
572+
# [START bigtable_instance_name]
573+
from google.cloud.bigtable import Client
574+
575+
client = Client(admin=True)
576+
instance = client.instance(INSTANCE_ID)
577+
instance_name = instance.name
578+
# [END bigtable_instance_name]
579+
580+
_instance_name_re = re.compile(
581+
r"^projects/(?P<project>[^/]+)/"
582+
r"instances/(?P<instance_id>"
583+
r"[a-z][-a-z0-9]*)$"
584+
)
585+
assert _instance_name_re.match(instance_name)
586+
587+
588+
def test_bigtable_cluster_name():
589+
import re
590+
591+
# [START bigtable_cluster_name]
592+
from google.cloud.bigtable import Client
593+
594+
client = Client(admin=True)
595+
instance = client.instance(INSTANCE_ID)
596+
cluster = instance.cluster(CLUSTER_ID)
597+
cluster_name = cluster.name
598+
# [END bigtable_cluster_name]
599+
600+
_cluster_name_re = re.compile(
601+
r"^projects/(?P<project>[^/]+)/"
602+
r"instances/(?P<instance>[^/]+)/"
603+
r"clusters/(?P<cluster_id>"
604+
r"[_a-zA-Z0-9][-_.a-zA-Z0-9]*)$"
605+
)
606+
607+
assert _cluster_name_re.match(cluster_name)
608+
609+
610+
def test_bigtable_instance_from_pb():
611+
# [START bigtable_instance_from_pb]
612+
from google.cloud.bigtable import Client
613+
from google.cloud.bigtable_admin_v2.types import instance_pb2
614+
615+
client = Client(admin=True)
616+
instance = client.instance(INSTANCE_ID)
617+
618+
name = instance.name
619+
instance_pb = instance_pb2.Instance(
620+
name=name, display_name=INSTANCE_ID, type=PRODUCTION, labels=LABELS
621+
)
622+
623+
instance2 = instance.from_pb(instance_pb, client)
624+
# [END bigtable_instance_from_pb]
625+
assert instance2.name == instance.name
626+
627+
628+
def test_bigtable_cluster_from_pb():
629+
# [START bigtable_cluster_from_pb]
630+
from google.cloud.bigtable import Client
631+
from google.cloud.bigtable_admin_v2.types import instance_pb2
632+
633+
client = Client(admin=True)
634+
instance = client.instance(INSTANCE_ID)
635+
cluster = instance.cluster(CLUSTER_ID)
636+
637+
name = cluster.name
638+
cluster_state = cluster.state
639+
cluster_pb = instance_pb2.Cluster(
640+
name=name,
641+
location=LOCATION_ID,
642+
state=cluster_state,
643+
serve_nodes=SERVER_NODES,
644+
default_storage_type=STORAGE_TYPE,
645+
)
646+
647+
cluster2 = cluster.from_pb(cluster_pb, instance)
648+
# [END bigtable_cluster_from_pb]
649+
assert cluster2.name == cluster.name
650+
651+
652+
def test_bigtable_instance_state():
653+
# [START bigtable_instance_state]
654+
from google.cloud.bigtable import Client
655+
656+
client = Client(admin=True)
657+
instance = client.instance(INSTANCE_ID)
658+
instance_state = instance.state
659+
# [END bigtable_instance_state]
660+
assert not instance_state
661+
662+
663+
def test_bigtable_cluster_state():
664+
# [START bigtable_cluster_state]
665+
from google.cloud.bigtable import Client
666+
667+
client = Client(admin=True)
668+
instance = client.instance(INSTANCE_ID)
669+
cluster = instance.cluster(CLUSTER_ID)
670+
cluster_state = cluster.state
671+
# [END bigtable_cluster_state]
672+
673+
assert not cluster_state
674+
675+
473676
if __name__ == "__main__":
474677
pytest.main()

0 commit comments

Comments
 (0)