Skip to content

Commit 43d53a8

Browse files
Bigtable: wrap proto enums (#3659)
1 parent 6f1a105 commit 43d53a8

File tree

12 files changed

+529
-116
lines changed

12 files changed

+529
-116
lines changed

google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
package com.google.cloud.bigtable.admin.v2.models;
1717

1818
import com.google.api.core.InternalApi;
19-
import com.google.bigtable.admin.v2.Cluster.State;
2019
import com.google.bigtable.admin.v2.ClusterName;
2120
import com.google.bigtable.admin.v2.LocationName;
22-
import com.google.bigtable.admin.v2.StorageType;
2321
import com.google.common.base.Objects;
2422
import com.google.common.base.Preconditions;
2523
import com.google.common.base.Verify;
@@ -32,8 +30,63 @@
3230
* in the instance.
3331
*/
3432
public class Cluster {
33+
public enum State {
34+
/** The state of the cluster could not be determined. */
35+
NOT_KNOWN(com.google.bigtable.admin.v2.Cluster.State.STATE_NOT_KNOWN),
36+
/** The cluster has been successfully created and is ready to serve requests. */
37+
READY(com.google.bigtable.admin.v2.Cluster.State.READY),
38+
/**
39+
* The cluster is currently being created, and may be destroyed if the creation process
40+
* encounters an error. A cluster may not be able to serve requests while being created.
41+
*/
42+
CREATING(com.google.bigtable.admin.v2.Cluster.State.CREATING),
43+
/**
44+
* The cluster is currently being resized, and may revert to its previous node count if the
45+
* process encounters an error. A cluster is still capable of serving requests while being
46+
* resized, but may exhibit performance as if its number of allocated nodes is between the
47+
* starting and requested states.
48+
*/
49+
RESIZING(com.google.bigtable.admin.v2.Cluster.State.RESIZING),
50+
/**
51+
* The cluster has no backing nodes. The data (tables) still exist, but no operations can be
52+
* performed on the cluster.
53+
*/
54+
DISABLED(com.google.bigtable.admin.v2.Cluster.State.DISABLED),
55+
/** The state of the cluster is not known by this client. Please upgrade your client. */
56+
UNRECOGNIZED(com.google.bigtable.admin.v2.Cluster.State.UNRECOGNIZED);
57+
58+
private final com.google.bigtable.admin.v2.Cluster.State proto;
59+
60+
/**
61+
* Wraps the protobuf. This method is considered an internal implementation detail and not meant
62+
* to be used by applications.
63+
*/
64+
@InternalApi
65+
public static State fromProto(com.google.bigtable.admin.v2.Cluster.State proto) {
66+
for (State state : values()) {
67+
if (state.proto.equals(proto)) {
68+
return state;
69+
}
70+
}
71+
return UNRECOGNIZED;
72+
}
73+
74+
State(com.google.bigtable.admin.v2.Cluster.State proto) {
75+
this.proto = proto;
76+
}
77+
78+
/**
79+
* Creates the request protobuf. This method is considered an internal implementation detail and
80+
* not meant to be used by applications.
81+
*/
82+
@InternalApi
83+
public com.google.bigtable.admin.v2.Cluster.State toProto() {
84+
return proto;
85+
}
86+
}
87+
3588
@Nonnull
36-
private final com.google.bigtable.admin.v2.Cluster proto;
89+
private final com.google.bigtable.admin.v2.Cluster stateProto;
3790

3891
/**
3992
* Wraps a protobuf response.
@@ -49,7 +102,7 @@ public static Cluster fromProto(com.google.bigtable.admin.v2.Cluster proto) {
49102
private Cluster(@Nonnull com.google.bigtable.admin.v2.Cluster proto) {
50103
Preconditions.checkNotNull(proto);
51104
Preconditions.checkArgument(!proto.getName().isEmpty(), "Name must be set");
52-
this.proto = proto;
105+
this.stateProto = proto;
53106
}
54107

55108

@@ -58,7 +111,7 @@ private Cluster(@Nonnull com.google.bigtable.admin.v2.Cluster proto) {
58111
public String getId() {
59112
// Constructor ensures that name is not null
60113
ClusterName fullName = Verify.verifyNotNull(
61-
ClusterName.parse(proto.getName()),
114+
ClusterName.parse(stateProto.getName()),
62115
"Name can never be null");
63116
//noinspection ConstantConditions
64117
return fullName.getCluster();
@@ -69,7 +122,7 @@ public String getId() {
69122
public String getInstanceId() {
70123
// Constructor ensures that name is not null
71124
ClusterName fullName = Verify.verifyNotNull(
72-
ClusterName.parse(proto.getName()),
125+
ClusterName.parse(stateProto.getName()),
73126
"Name can never be null");
74127
//noinspection ConstantConditions
75128
return fullName.getInstance();
@@ -80,16 +133,15 @@ public String getInstanceId() {
80133
/** Get the zone where this cluster is located. */
81134
@SuppressWarnings("WeakerAccess")
82135
public String getZone() {
83-
LocationName location = Verify.verifyNotNull(LocationName.parse(proto.getLocation()));
136+
LocationName location = Verify.verifyNotNull(LocationName.parse(stateProto.getLocation()));
84137
//noinspection ConstantConditions
85138
return location.getLocation();
86139
}
87140

88141
/** Gets the current state of the cluster */
89-
// TODO(igorbernstein2): try to avoid exposing proto enums
90142
@SuppressWarnings("WeakerAccess")
91143
public State getState() {
92-
return proto.getState();
144+
return State.fromProto(stateProto.getState());
93145
}
94146

95147
/**
@@ -98,17 +150,16 @@ public State getState() {
98150
*/
99151
@SuppressWarnings("WeakerAccess")
100152
public int getServeNodes() {
101-
return proto.getServeNodes();
153+
return stateProto.getServeNodes();
102154
}
103155

104156
/**
105157
* The type of storage used by this cluster to serve its parent instance's tables, unless
106158
* explicitly overridden.
107159
*/
108-
// TODO(igorbernstein2): try to avoid exposing proto enums
109160
@SuppressWarnings("WeakerAccess")
110161
public StorageType getStorageType() {
111-
return proto.getDefaultStorageType();
162+
return StorageType.fromProto(stateProto.getDefaultStorageType());
112163
}
113164

114165
@Override
@@ -120,11 +171,11 @@ public boolean equals(Object o) {
120171
return false;
121172
}
122173
Cluster cluster = (Cluster) o;
123-
return Objects.equal(proto, cluster.proto);
174+
return Objects.equal(stateProto, cluster.stateProto);
124175
}
125176

126177
@Override
127178
public int hashCode() {
128-
return Objects.hashCode(proto);
179+
return Objects.hashCode(stateProto);
129180
}
130181
}

google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,27 @@
1515
*/
1616
package com.google.cloud.bigtable.admin.v2.models;
1717

18+
import static com.google.cloud.bigtable.admin.v2.models.StorageType.SSD;
19+
1820
import com.google.api.core.InternalApi;
1921
import com.google.bigtable.admin.v2.InstanceName;
2022
import com.google.bigtable.admin.v2.LocationName;
2123
import com.google.bigtable.admin.v2.ProjectName;
22-
import com.google.bigtable.admin.v2.StorageType;
24+
import com.google.common.base.Preconditions;
25+
import javax.annotation.Nonnull;
2326

2427
/**
2528
* Parameters for creating a new Bigtable cluster.
2629
*
27-
* <p>A cluster represents the actual Cloud Bigtable service. Each cluster belongs to a single Cloud
28-
* Bigtable instance. When your application sends requests to a Cloud Bigtable instance, those
30+
* <p>A cluster represents the actual Cloud Bigtable service. Each cluster belongs to a single
31+
* Cloud Bigtable instance. When your application sends requests to a Cloud Bigtable instance, those
2932
* requests are actually handled by one of the clusters in the instance.
3033
*
3134
* <p>Each cluster is located in a single zone. An instance's clusters must be in unique zones that
3235
* are within the same region. For example, if the first cluster is in us-east1-b, then us-east1-c
3336
* is a valid zone for the second cluster. For a list of zones and regions where Cloud Bigtable is
34-
* available, see <a href="https://cloud.google.com/bigtable/docs/locations">Cloud Bigtable Locations</a>.
37+
* available, see <a href="https://cloud.google.com/bigtable/docs/locations">Cloud Bigtable
38+
* Locations</a>.
3539
*
3640
*
3741
* Examples:
@@ -44,7 +48,8 @@
4448
* .setStorageType(StorageType.SSD);
4549
* }</pre>
4650
*
47-
* @see <a href="https://cloud.google.com/bigtable/docs/instances-clusters-nodes#clusters">For more details</a>
51+
* @see <a href="https://cloud.google.com/bigtable/docs/instances-clusters-nodes#clusters">For more
52+
* details</a>
4853
*/
4954
public final class CreateClusterRequest {
5055
private final com.google.bigtable.admin.v2.CreateClusterRequest.Builder proto = com.google.bigtable.admin.v2.CreateClusterRequest
@@ -57,15 +62,16 @@ public final class CreateClusterRequest {
5762

5863
/**
5964
* Builds a new request to create a new cluster to the specified instance with the specified
60-
* cluster id. */
65+
* cluster id.
66+
*/
6167
public static CreateClusterRequest of(String instanceId, String clusterId) {
6268
return new CreateClusterRequest(instanceId, clusterId);
6369
}
6470

6571
private CreateClusterRequest(String instanceId, String clusterId) {
6672
this.instanceId = instanceId;
6773
proto.setClusterId(clusterId);
68-
proto.getClusterBuilder().setDefaultStorageType(StorageType.SSD);
74+
proto.getClusterBuilder().setDefaultStorageType(SSD.toProto());
6975
}
7076

7177
/**
@@ -78,22 +84,27 @@ public CreateClusterRequest setZone(String zone) {
7884
return this;
7985
}
8086

81-
/** Sets the number of nodes allocated to this cluster. More nodes enable higher throughput and
82-
* more consistent performance. */
87+
/**
88+
* Sets the number of nodes allocated to this cluster. More nodes enable higher throughput and
89+
* more consistent performance.
90+
*/
8391
@SuppressWarnings("WeakerAccess")
8492
public CreateClusterRequest setServeNodes(int numNodes) {
8593
proto.getClusterBuilder().setServeNodes(numNodes);
8694
return this;
8795
}
8896

8997
/**
90-
* Sets the type of storage used by this cluster to serve its parent instance's tables.
91-
* Defaults to {@code SSD}.
98+
* Sets the type of storage used by this cluster to serve its parent instance's tables. Defaults
99+
* to {@code SSD}.
92100
*/
93-
// TODO(igorbernstein2): try to avoid leaking protobuf generated enums
94101
@SuppressWarnings("WeakerAccess")
95-
public CreateClusterRequest setStorageType(StorageType storageType) {
96-
proto.getClusterBuilder().setDefaultStorageType(storageType);
102+
public CreateClusterRequest setStorageType(@Nonnull StorageType storageType) {
103+
Preconditions.checkNotNull(storageType);
104+
Preconditions.checkArgument(storageType != StorageType.UNRECOGNIZED,
105+
"StorageType can't be UNRECOGNIZED");
106+
107+
proto.getClusterBuilder().setDefaultStorageType(storageType.toProto());
97108
return this;
98109
}
99110

@@ -104,7 +115,8 @@ public CreateClusterRequest setStorageType(StorageType storageType) {
104115
@InternalApi
105116
public com.google.bigtable.admin.v2.CreateClusterRequest toProto(ProjectName projectName) {
106117
proto.setParent(InstanceName.of(projectName.getProject(), instanceId).toString());
107-
proto.getClusterBuilder().setLocation(LocationName.of(projectName.getProject(), zone).toString());
118+
proto.getClusterBuilder()
119+
.setLocation(LocationName.of(projectName.getProject(), zone).toString());
108120

109121
return proto.build();
110122
}
@@ -130,7 +142,8 @@ String getClusterId() {
130142
*/
131143
@InternalApi
132144
com.google.bigtable.admin.v2.Cluster toEmbeddedProto(ProjectName projectName) {
133-
proto.getClusterBuilder().setLocation(LocationName.of(projectName.getProject(), zone).toString());
145+
proto.getClusterBuilder()
146+
.setLocation(LocationName.of(projectName.getProject(), zone).toString());
134147

135148
return proto.getClusterBuilder().build();
136149
}

google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.api.core.InternalApi;
1919
import com.google.bigtable.admin.v2.Instance.Type;
2020
import com.google.bigtable.admin.v2.ProjectName;
21-
import com.google.bigtable.admin.v2.StorageType;
2221
import com.google.common.base.Preconditions;
2322
import com.google.common.collect.Lists;
2423
import java.util.List;
@@ -31,13 +30,13 @@
3130
* which do all of the real work. Instances come in 2 flavors:
3231
*
3332
* <dl>
34-
* <dt>Production
35-
* <dd>A standard instance with either 1 or 2 clusters, as well as 3 or more nodes in each cluster.
36-
* You cannot downgrade a production instance to a development instance.
33+
* <dt>Production
34+
* <dd>A standard instance with either 1 or 2 clusters, as well as 3 or more nodes in each cluster.
35+
* You cannot downgrade a production instance to a development instance.
3736
*
38-
* <dt>Development
39-
* <dd>A low-cost instance for development and testing, with performance limited to the equivalent
40-
* of a 1-node cluster. Development instances only support a single 1 node cluster.
37+
* <dt>Development
38+
* <dd>A low-cost instance for development and testing, with performance limited to the equivalent
39+
* of a 1-node cluster. Development instances only support a single 1 node cluster.
4140
* </dl>
4241
*
4342
* When creating an Instance, you must create at least one cluster in it.
@@ -56,7 +55,8 @@
5655
*
5756
* }</pre>
5857
*
59-
* @see <a href="https://cloud.google.com/bigtable/docs/instances-clusters-nodes#instances">For more details</a>
58+
* @see <a href="https://cloud.google.com/bigtable/docs/instances-clusters-nodes#instances">For more
59+
* details</a>
6060
*/
6161
public final class CreateInstanceRequest {
6262
private final com.google.bigtable.admin.v2.CreateInstanceRequest.Builder builder =
@@ -93,11 +93,11 @@ public CreateInstanceRequest setDisplayName(@Nonnull String displayName) {
9393
* <p>Can be either DEVELOPMENT or PRODUCTION. Defaults to PRODUCTION.
9494
* Please see class javadoc for details.
9595
*/
96-
// TODO(igorbernstein2): try to avoid leaking protobuf generated enums
9796
@SuppressWarnings("WeakerAccess")
98-
public CreateInstanceRequest setType(@Nonnull Type type) {
97+
public CreateInstanceRequest setType(@Nonnull Instance.Type type) {
9998
Preconditions.checkNotNull(type);
100-
builder.getInstanceBuilder().setType(type);
99+
Preconditions.checkArgument(type != Instance.Type.UNRECOGNIZED, "Type is unrecognized");
100+
builder.getInstanceBuilder().setType(type.toProto());
101101
return this;
102102
}
103103

@@ -107,7 +107,8 @@ public CreateInstanceRequest setType(@Nonnull Type type) {
107107
* <p>Labels are key-value pairs that you can use to group related instances and store metadata
108108
* about an instance.
109109
*
110-
* @see <a href="https://cloud.google.com/bigtable/docs/creating-managing-labels">For more details</a>
110+
* @see <a href="https://cloud.google.com/bigtable/docs/creating-managing-labels">For more
111+
* details</a>
111112
*/
112113
@SuppressWarnings("WeakerAccess")
113114
public CreateInstanceRequest addLabel(@Nonnull String key, @Nonnull String value) {
@@ -125,12 +126,14 @@ public CreateInstanceRequest addLabel(@Nonnull String key, @Nonnull String value
125126
*
126127
* @param clusterId The name of the cluster.
127128
* @param zone The zone where the cluster will be created.
128-
* @param serveNodes The number of nodes that cluster will contain. DEVELOPMENT instance clusters must have exactly one node.
129-
* @param storageType The type of storage used by this cluster to serve its parent instance's tables.
129+
* @param serveNodes The number of nodes that cluster will contain. DEVELOPMENT instance clusters
130+
* must have exactly one node.
131+
* @param storageType The type of storage used by this cluster to serve its parent instance's
132+
* tables.
130133
*/
131-
// TODO(igorbernstein2): try to avoid leaking protobuf generated enums
132134
@SuppressWarnings("WeakerAccess")
133-
public CreateInstanceRequest addCluster(@Nonnull String clusterId, @Nonnull String zone, int serveNodes, @Nonnull StorageType storageType) {
135+
public CreateInstanceRequest addCluster(@Nonnull String clusterId, @Nonnull String zone,
136+
int serveNodes, @Nonnull StorageType storageType) {
134137
CreateClusterRequest clusterRequest = CreateClusterRequest
135138
.of("ignored-instance-id", clusterId)
136139
.setZone(zone)
@@ -152,7 +155,8 @@ public com.google.bigtable.admin.v2.CreateInstanceRequest toProto(ProjectName pr
152155
.clearClusters();
153156

154157
for (CreateClusterRequest clusterRequest : clusterRequests) {
155-
builder.putClusters(clusterRequest.getClusterId(), clusterRequest.toEmbeddedProto(projectName));
158+
builder
159+
.putClusters(clusterRequest.getClusterId(), clusterRequest.toEmbeddedProto(projectName));
156160
}
157161

158162
return builder.build();

0 commit comments

Comments
 (0)