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

feat(bigtable): Managed Backups wrappers #57

Merged
merged 27 commits into from Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
69cde27
[new] managed backup wrappers + unit tests
mf2199 Jun 18, 2020
1452ab6
feat: managed backups wrappers
mf2199 Jun 18, 2020
84390ed
Merge branch 'managed-backups' of https://github.com/q-logic/python-b…
mf2199 Jun 18, 2020
b019cbe
fix: docstrings + blacken
mf2199 Jun 18, 2020
303a202
fix: cleanup
mf2199 Jun 22, 2020
e7edc3d
refactor: ``backup``, ``list_backups`` and ``retore_table`` methods m…
mf2199 Jun 23, 2020
c7f5a25
feat: `reaload` and `is_ready` methods removed
mf2199 Jun 23, 2020
5cfb1fc
refactor: `re` parser made local
mf2199 Jun 23, 2020
d3a121a
feat: integration test
mf2199 Jun 24, 2020
e58a8e1
refactor: cleanup
mf2199 Jun 24, 2020
3a00c77
fix: format
mf2199 Jun 25, 2020
d472fc6
refactor: `name`, `cluster` property getters & `table_list_backups`
mf2199 Jun 28, 2020
38c6798
refactor: using `BigtableTableAdminClient.table_path` in lieu of `for…
mf2199 Jun 29, 2020
b25ac9b
fix: `from_pb2` method to include all `backup_pb` fields
mf2199 Jun 29, 2020
ecf35d7
refactor: cleanup
mf2199 Jun 29, 2020
d638a09
format: blacken
mf2199 Jun 30, 2020
db2421b
Merge branch 'master' into managed-backups
mf2199 Jul 2, 2020
8404c89
feat: reinstated `Backup.reload` + test method
mf2199 Jul 8, 2020
6f64861
fix: docstring typos
mf2199 Jul 14, 2020
c291d8c
cleanup: minor cleanup
mf2199 Jul 17, 2020
69c4d4f
cleanup: minor cleanup
mf2199 Jul 20, 2020
0a015d1
fix: ASCII encoding
mf2199 Jul 20, 2020
f76e17a
fix: Python 2 compatibility issue
mf2199 Jul 21, 2020
2376562
fix: SphinxWarning [possible cause]
mf2199 Jul 21, 2020
48c933f
fix: lint errors
mf2199 Jul 21, 2020
1607448
Merge branch 'master' into managed-backups
mf2199 Jul 21, 2020
ea43ae0
Merge branch 'master' into managed-backups
kolea2 Jul 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
392 changes: 392 additions & 0 deletions google/cloud/bigtable/backup.py
@@ -0,0 +1,392 @@
# Copyright 2020 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.

"""A user-friendly container for Cloud Bigtable Backup."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure container is the correct word here? wrapper?. Also a Cloud Bigtable Backup or Cloud Bigtable Backups perhaps?

And it seems we mix google cloud bigtable and cloud bigtable. Maybe best to be consistent?

Copy link
Contributor Author

@mf2199 mf2199 Jul 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed about the wrapper vs container as well as Google Cloud Bigtable. Not sure about the article though, since it implies a definition of notion here, rather than "a" (i.e. particular) Backup class. I've put it in for now, just must say that it's debatable.


import re

from google.cloud._helpers import _datetime_to_pb_timestamp
from google.cloud.bigtable_admin_v2.gapic.bigtable_table_admin_client import (
BigtableTableAdminClient,
)
from google.cloud.bigtable_admin_v2.types import table_pb2
from google.cloud.exceptions import NotFound
from google.protobuf import field_mask_pb2

_BACKUP_NAME_RE = re.compile(
mf2199 marked this conversation as resolved.
Show resolved Hide resolved
r"^projects/(?P<project>[^/]+)/"
r"instances/(?P<instance_id>[a-z][-a-z0-9]*)/"
r"clusters/(?P<cluster_id>[a-z][-a-z0-9]*)/"
r"backups/(?P<backup_id>[a-z][a-z0-9_\-]*[a-z0-9])$"
)


class Backup(object):
"""Representation of a Google Cloud Bigtable Backup.

A :class: `Backup` can be used to:

* :meth:`create` the backup
* :meth:`update` the backup
* :meth:`delete` the backup

:type backup_id: str
:param backup_id: The ID of the backup.

:type instance: :class:`~google.cloud.spanner_v1.instance.Instance`
mf2199 marked this conversation as resolved.
Show resolved Hide resolved
:param instance: The Instance that owns this Backup.

:type cluster_id: str
:param cluster_id: (Optional) The ID of the Cluster that contains this Backup.
Required for calling 'delete', 'exists' etc. methods.

:type table_id: str
:param table_id: (Optional) The ID of the Table that the Backup is for.
Required if the 'create' method will be called.

:type expire_time: :class:`datetime.datetime`
:param expire_time: (Optional) The expiration time after which the Backup
will be automatically deleted. Required if the `create`
method will be called.
"""

def __init__(
self, backup_id, instance, cluster_id=None, table_id=None, expire_time=None
):
self.backup_id = backup_id
self._instance = instance
self._cluster = cluster_id
self.table_id = table_id
self._expire_time = expire_time

self._parent = None
self._source_table = None
self._start_time = None
self._end_time = None
self._size_bytes = None
self._state = None

@property
def name(self):
"""Backup name used in requests.

The Backup name is of the form

``"projects/../instances/../clusters/../backups/{backup_id}"``

:rtype: str
:returns: The Backup name.

:raises: ValueError: If the 'cluster' has not been set.
"""
if not self._cluster:
raise ValueError('"cluster" parameter must be set')

return BigtableTableAdminClient.backup_path(
project=self._instance._client.project,
instance=self._instance.instance_id,
cluster=self._cluster,
backup=self.backup_id,
)

@property
def cluster(self):
"""The ID of the [parent] cluster used in requests.

:rtype: str
:returns: The ID of the cluster containing the Backup.
"""
return self._cluster

@cluster.setter
def cluster(self, cluster_id):
self._cluster = cluster_id

@property
def parent(self):
"""Name of the parent cluster used in requests.

.. note::
This property will return None if ``cluster`` is not set.

The parent name is of the form

``"projects/{project}/instances/{instance_id}/clusters/{cluster}"``

:rtype: str
:returns: A full path to the parent cluster.
"""
if not self._parent and self._cluster:
self._parent = BigtableTableAdminClient.cluster_path(
project=self._instance._client.project,
instance=self._instance.instance_id,
cluster=self._cluster,
)
return self._parent

@property
def source_table(self):
"""The full name of the Table from which this Backup is created.

.. note::
This property will return None if ``table_id`` is not set.

The table name is of the form

``"projects/../instances/../tables/{source_table}"``

:rtype: str
:returns: The Table name.
"""
if not self._source_table and self.table_id:
self._source_table = BigtableTableAdminClient.table_path(
project=self._instance._client.project,
instance=self._instance.instance_id,
table=self.table_id,
)
return self._source_table

@property
def expire_time(self):
"""Expiration time used in the creation requests.

:rtype: :class:`datetime.datetime`
:returns: A 'datetime' object representing the expiration time of
this Backup.
"""
return self._expire_time

@expire_time.setter
def expire_time(self, new_expire_time):
self._expire_time = new_expire_time

@property
def start_time(self):
"""The time this Backup was started.

:rtype: :class:`datetime.datetime`
:returns: A 'datetime' object representing the time when the creation
of this Backup had started.
"""
return self._start_time

@property
def end_time(self):
"""The time this Backup was finished.

:rtype: :class:`datetime.datetime`
:returns: A 'datetime' object representing the time when the creation
of this Backup was finished.
"""
return self._end_time

@property
def size_bytes(self):
"""The size of this Backup, in bytes.

:rtype: int
:returns: The size of this Backup, in bytes.
"""
return self._size_bytes

@property
def state(self):
""" The current state of this Backup.

:rtype: :class:`~google.cloud.bigtable_admin_v2.gapic.enums.Backup.State`
:returns: The current state of this Backup.
"""
return self._state

@classmethod
def from_pb(cls, backup_pb, instance):
"""Creates a Backup instance from a protobuf message.

:type backup_pb: :class:`table_pb2.Backup`
:param backup_pb: A Backup protobuf object.

:type instance: :class:`Instance <google.cloud.bigtable.instance.Instance>`
:param instance: The Instance that owns the Backup.

:rtype: :class:`~google.cloud.bigtable.backup.Backup`
:returns: The backup parsed from the protobuf response.
:raises: ValueError: If the backup name does not match the expected
format or the parsed project ID does not match the
project ID on the Instance's client, or if the
parsed instance ID does not match the Instance ID.
"""
match = _BACKUP_NAME_RE.match(backup_pb.name)
if match is None:
raise ValueError(
"Backup protobuf name was not in the expected format.", backup_pb.name
)
if match.group("project") != instance._client.project:
raise ValueError(
"Project ID of the Backup does not match the Project ID "
"of the instance's client"
)

instance_id = match.group("instance_id")
if instance_id != instance.instance_id:
raise ValueError(
"Instance ID of the Backup does not match the Instance ID "
"of the instance"
)
backup_id = match.group("backup_id")
cluster_id = match.group("cluster_id")

match = re.compile(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a const regex above. Should this be done similarly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. We couldn't decide whether to declare re as constants, hence the two ended up in different representations) It's fixed now.

r"^projects/(?P<project>[^/]+)/"
r"instances/(?P<instance_id>[a-z][-a-z0-9]*)/"
r"tables/(?P<table_id>[_a-zA-Z0-9][-_.a-zA-Z0-9]*)$"
).match(backup_pb.source_table)
table_id = match.group("table_id") if match else None

expire_time = backup_pb.expire_time

backup = cls(
backup_id,
instance,
cluster_id=cluster_id,
table_id=table_id,
expire_time=expire_time,
)
backup._start_time = backup_pb.start_time
backup._end_time = backup_pb.end_time
backup._size_bytes = backup_pb.size_bytes
backup._state = backup_pb.state

return backup

def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return other.backup_id == self.backup_id and other._instance == self._instance

def __ne__(self, other):
return not self == other

def create(self, cluster_id=None):
"""Creates this backup within its instance.

:type cluster_id: str
:param cluster_id: (Optional) The ID of the Cluster for the newly
created Backup.

:rtype: :class:`~google.api_core.operation.Operation`
:returns: :class:`~google.cloud.bigtable_admin_v2.types._OperationFuture`
instance, to be used to poll the status of the 'create' request
:raises Conflict: if the Backup already exists
:raises NotFound: if the Instance owning the Backup does not exist
:raises BadRequest: if the `table` or `expire_time` values are invalid,
or `expire_time` is not set
"""
if not self._expire_time:
raise ValueError('"expire_time" parameter must be set')
# TODO: Consider implementing a method that sets a default value of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we create an issue to track this as a feature request?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly, that's the idea. I'd just wait until the initial release is merged, but please let know if you'd rather see it sooner.

# `expire_time`, e.g. 1 week from the creation of the Backup.
if not self.table_id:
raise ValueError('"table" parameter must be set')

if cluster_id:
self._cluster = cluster_id

if not self._cluster:
raise ValueError('"cluster" parameter must be set')

backup = table_pb2.Backup(
source_table=self.source_table,
expire_time=_datetime_to_pb_timestamp(self.expire_time),
)

api = self._instance._client.table_admin_client
return api.create_backup(self.parent, self.backup_id, backup)

def get(self):
"""Retrieves metadata of a pending or completed Backup.

:returns: An instance of
:class:`~google.cloud.bigtable_admin_v2.types.Backup`

:raises google.api_core.exceptions.GoogleAPICallError: If the request
failed for any reason.
:raises google.api_core.exceptions.RetryError: If the request failed
due to a retryable error and retry attempts failed.
:raises ValueError: If the parameters are invalid.
"""
api = self._instance._client.table_admin_client
try:
return api.get_backup(self.name)
except NotFound:
return None

def reload(self):
"""Refreshes the stored backup properties."""
backup = self.get()
self._source_table = backup.source_table
self._expire_time = backup.expire_time
self._start_time = backup.start_time
self._end_time = backup.end_time
self._size_bytes = backup.size_bytes
self._state = backup.state

def exists(self):
"""Tests whether this Backup exists.

:rtype: bool
:returns: True if the Backup exists, else False.
"""
return self.get() is not None

def update_expire_time(self, new_expire_time):
"""Update the expire time of this Backup.

:type new_expire_time: :class:`datetime.datetime`
:param new_expire_time: the new expiration time timestamp
"""
backup_update = table_pb2.Backup(
name=self.name, expire_time=_datetime_to_pb_timestamp(new_expire_time),
)
# update_mask = {"paths": ["expire_time"]}
mf2199 marked this conversation as resolved.
Show resolved Hide resolved
update_mask = field_mask_pb2.FieldMask(paths=["expire_time"])
api = self._instance._client.table_admin_client
api.update_backup(backup_update, update_mask)
self._expire_time = new_expire_time

def delete(self):
"""Delete this Backup."""
self._instance._client.table_admin_client.delete_backup(self.name)

def restore(self, table_id):
"""Creates a new Table by restoring from this Backup. The new Table
must be in the same Instance as the Instance containing the Backup.
The returned Table ``long-running operation`` can be used to track the
progress of the operation and to cancel it. The ``response`` type is
``Table``, if successful.

:param table_id: The ID of the Table to create and restore to.
This Table must not already exist.
:returns: An instance of
:class:`~google.cloud.bigtable_admin_v2.types._OperationFuture`.

:raises: google.api_core.exceptions.AlreadyExists: If the table
already exists.
:raises: google.api_core.exceptions.GoogleAPICallError: If the request
failed for any reason.
:raises: google.api_core.exceptions.RetryError: If the request failed
due to a retryable error and retry attempts failed.
:raises: ValueError: If the parameters are invalid.
"""
api = self._instance._client.table_admin_client
return api.restore_table(self._instance.name, table_id, self.name)