Skip to content

Commit

Permalink
Adding display name and serve nodes to second Cluster class.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Jul 25, 2015
1 parent 93d02ad commit 2aba071
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
27 changes: 25 additions & 2 deletions gcloud_bigtable/cluster_standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import re

from gcloud_bigtable.cluster import _require_pb_property


_CLUSTER_NAME_RE = re.compile(r'^projects/(?P<project_id>[^/]+)/'
r'zones/(?P<zone>[^/]+)/clusters/'
Expand All @@ -35,11 +37,24 @@ class Cluster(object):
:type client: :class:`.client.Client`
:param client: The client that owns the cluster. Provides
authorization and a project ID.
:type display_name: string
:param display_name: (Optional) The display name for the cluster in the
Cloud Console UI. (Must be between 4 and 30
characters.) If this value is not set in the
constructor, will fall back to the cluster ID.
:type serve_nodes: integer
:param serve_nodes: (Optional) The number of nodes in the cluster.
Defaults to 3.
"""

def __init__(self, zone, cluster_id, client):
def __init__(self, zone, cluster_id, client,
display_name=None, serve_nodes=3):
self.zone = zone
self.cluster_id = cluster_id
self.display_name = display_name or cluster_id
self.serve_nodes = serve_nodes
self._client = client

@classmethod
Expand All @@ -66,7 +81,10 @@ def from_pb(cls, cluster_pb, client):
raise ValueError('Project ID on cluster does not match the '
'project ID on the client')

return cls(match.group('zone'), match.group('cluster_id'), client)
display_name = _require_pb_property(cluster_pb, 'display_name', None)
serve_nodes = _require_pb_property(cluster_pb, 'serve_nodes', None)
return cls(match.group('zone'), match.group('cluster_id'), client,
display_name=display_name, serve_nodes=serve_nodes)

@property
def client(self):
Expand Down Expand Up @@ -106,6 +124,11 @@ def name(self):
def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
# NOTE: This does not compare the configuration values, such as
# the serve_nodes or display_name. This is intentional, since
# the same cluster can be in different states if not
# synchronized. This suggests we should use `project_id`
# instead of `client` for the third comparison.
return (other.zone == self.zone and
other.cluster_id == self.cluster_id and
other.client == self.client)
Expand Down
12 changes: 10 additions & 2 deletions gcloud_bigtable/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,16 @@ def test_list_clusters(self):
data_pb2.Zone(display_name=failed_zone),
],
clusters=[
data_pb2.Cluster(name=cluster_name1),
data_pb2.Cluster(name=cluster_name2),
data_pb2.Cluster(
name=cluster_name1,
display_name=cluster_name1,
serve_nodes=3,
),
data_pb2.Cluster(
name=cluster_name2,
display_name=cluster_name2,
serve_nodes=3,
),
],
)

Expand Down
6 changes: 5 additions & 1 deletion gcloud_bigtable/test_cluster_standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ def test_from_pb_success(self):

cluster_name = ('projects/' + PROJECT_ID + '/zones/' + ZONE +
'/clusters/' + CLUSTER_ID)
cluster_pb = data_pb2.Cluster(name=cluster_name)
cluster_pb = data_pb2.Cluster(
name=cluster_name,
display_name=CLUSTER_ID,
serve_nodes=3,
)

klass = self._getTargetClass()
cluster = klass.from_pb(cluster_pb, client)
Expand Down

0 comments on commit 2aba071

Please sign in to comment.