From eec2ccb34f2fe18d19b88b8aa60bd2ee55440c92 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Fri, 7 Sep 2018 17:00:18 -0400 Subject: [PATCH 1/9] Bigtable: wrap proto enums --- .../bigtable/admin/v2/models/Cluster.java | 62 +++++++++++-- .../admin/v2/models/CreateClusterRequest.java | 44 ++++++---- .../v2/models/CreateInstanceRequest.java | 38 ++++---- .../bigtable/admin/v2/models/Instance.java | 86 +++++++++++++++++-- .../bigtable/admin/v2/models/StorageType.java | 57 ++++++++++++ 5 files changed, 243 insertions(+), 44 deletions(-) create mode 100644 google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java index 19ef7a026b69..50bf66725d90 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java @@ -16,10 +16,8 @@ package com.google.cloud.bigtable.admin.v2.models; import com.google.api.core.InternalApi; -import com.google.bigtable.admin.v2.Cluster.State; import com.google.bigtable.admin.v2.ClusterName; import com.google.bigtable.admin.v2.LocationName; -import com.google.bigtable.admin.v2.StorageType; import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.common.base.Verify; @@ -32,6 +30,60 @@ * in the instance. */ public class Cluster { + public enum State { + /** The state of the cluster could not be determined. */ + NOT_KNOWN(com.google.bigtable.admin.v2.Cluster.State.STATE_NOT_KNOWN), + /** The cluster has been successfully created and is ready to serve requests. */ + READY(com.google.bigtable.admin.v2.Cluster.State.READY), + /** + * The cluster is currently being created, and may be destroyed if the creation process + * encounters an error. A cluster may not be able to serve requests while being created. + */ + CREATING(com.google.bigtable.admin.v2.Cluster.State.CREATING), + /** + * The cluster is currently being resized, and may revert to its previous node count if the + * process encounters an error. A cluster is still capable of serving requests while being + * resized, but may exhibit performance as if its number of allocated nodes is between the + * starting and requested states. + */ + RESIZING(com.google.bigtable.admin.v2.Cluster.State.RESIZING), + /** + * The cluster has no backing nodes. The data (tables) still exist, but no operations can be + * performed on the cluster. + */ + DISABLED(com.google.bigtable.admin.v2.Cluster.State.DISABLED); + + + private final com.google.bigtable.admin.v2.Cluster.State proto; + + /** + * Wraps the protobuf. This method is considered an internal implementation detail and not meant + * to be used by applications. + */ + @InternalApi + public static State fromProto(com.google.bigtable.admin.v2.Cluster.State proto) { + for (State state : values()) { + if (state.proto.equals(proto)) { + return state; + } + } + return NOT_KNOWN; + } + + State(com.google.bigtable.admin.v2.Cluster.State proto) { + this.proto = proto; + } + + /** + * Creates the request protobuf. This method is considered an internal implementation detail and + * not meant to be used by applications. + */ + @InternalApi + public com.google.bigtable.admin.v2.Cluster.State toProto() { + return proto; + } + } + @Nonnull private final com.google.bigtable.admin.v2.Cluster proto; @@ -86,10 +138,9 @@ public String getZone() { } /** Gets the current state of the cluster */ - // TODO(igorbernstein2): try to avoid exposing proto enums @SuppressWarnings("WeakerAccess") public State getState() { - return proto.getState(); + return State.fromProto(proto.getState()); } /** @@ -105,10 +156,9 @@ public int getServeNodes() { * The type of storage used by this cluster to serve its parent instance's tables, unless * explicitly overridden. */ - // TODO(igorbernstein2): try to avoid exposing proto enums @SuppressWarnings("WeakerAccess") public StorageType getStorageType() { - return proto.getDefaultStorageType(); + return StorageType.fromProto(proto.getDefaultStorageType()); } @Override diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java index aefccd3f692b..0ba776668fa9 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java @@ -15,23 +15,27 @@ */ package com.google.cloud.bigtable.admin.v2.models; +import static com.google.cloud.bigtable.admin.v2.models.StorageType.SSD; + import com.google.api.core.InternalApi; import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.LocationName; import com.google.bigtable.admin.v2.ProjectName; -import com.google.bigtable.admin.v2.StorageType; +import com.google.common.base.Preconditions; +import javax.annotation.Nonnull; /** * Parameters for creating a new Bigtable cluster. * - *

A cluster represents the actual Cloud Bigtable service. Each cluster belongs to a single Cloud - * Bigtable instance. When your application sends requests to a Cloud Bigtable instance, those + *

A cluster represents the actual Cloud Bigtable service. Each cluster belongs to a single + * Cloud Bigtable instance. When your application sends requests to a Cloud Bigtable instance, those * requests are actually handled by one of the clusters in the instance. * *

Each cluster is located in a single zone. An instance's clusters must be in unique zones that * are within the same region. For example, if the first cluster is in us-east1-b, then us-east1-c * is a valid zone for the second cluster. For a list of zones and regions where Cloud Bigtable is - * available, see Cloud Bigtable Locations. + * available, see Cloud Bigtable + * Locations. * * * Examples: @@ -44,7 +48,8 @@ * .setStorageType(StorageType.SSD); * } * - * @see For more details + * @see For more + * details */ public final class CreateClusterRequest { private final com.google.bigtable.admin.v2.CreateClusterRequest.Builder proto = com.google.bigtable.admin.v2.CreateClusterRequest @@ -57,7 +62,8 @@ public final class CreateClusterRequest { /** * Builds a new request to create a new cluster to the specified instance with the specified - * cluster id. */ + * cluster id. + */ public static CreateClusterRequest of(String instanceId, String clusterId) { return new CreateClusterRequest(instanceId, clusterId); } @@ -65,7 +71,7 @@ public static CreateClusterRequest of(String instanceId, String clusterId) { private CreateClusterRequest(String instanceId, String clusterId) { this.instanceId = instanceId; proto.setClusterId(clusterId); - proto.getClusterBuilder().setDefaultStorageType(StorageType.SSD); + proto.getClusterBuilder().setDefaultStorageType(SSD.toProto()); } /** @@ -78,8 +84,10 @@ public CreateClusterRequest setZone(String zone) { return this; } - /** Sets the number of nodes allocated to this cluster. More nodes enable higher throughput and - * more consistent performance. */ + /** + * Sets the number of nodes allocated to this cluster. More nodes enable higher throughput and + * more consistent performance. + */ @SuppressWarnings("WeakerAccess") public CreateClusterRequest setServeNodes(int numNodes) { proto.getClusterBuilder().setServeNodes(numNodes); @@ -87,13 +95,15 @@ public CreateClusterRequest setServeNodes(int numNodes) { } /** - * Sets the type of storage used by this cluster to serve its parent instance's tables. - * Defaults to {@code SSD}. + * Sets the type of storage used by this cluster to serve its parent instance's tables. Defaults + * to {@code SSD}. */ - // TODO(igorbernstein2): try to avoid leaking protobuf generated enums @SuppressWarnings("WeakerAccess") - public CreateClusterRequest setStorageType(StorageType storageType) { - proto.getClusterBuilder().setDefaultStorageType(storageType); + public CreateClusterRequest setStorageType(@Nonnull StorageType storageType) { + Preconditions.checkNotNull(storageType); + Preconditions.checkArgument(storageType != StorageType.NOT_KNOWN); + + proto.getClusterBuilder().setDefaultStorageType(storageType.toProto()); return this; } @@ -104,7 +114,8 @@ public CreateClusterRequest setStorageType(StorageType storageType) { @InternalApi public com.google.bigtable.admin.v2.CreateClusterRequest toProto(ProjectName projectName) { proto.setParent(InstanceName.of(projectName.getProject(), instanceId).toString()); - proto.getClusterBuilder().setLocation(LocationName.of(projectName.getProject(), zone).toString()); + proto.getClusterBuilder() + .setLocation(LocationName.of(projectName.getProject(), zone).toString()); return proto.build(); } @@ -130,7 +141,8 @@ String getClusterId() { */ @InternalApi com.google.bigtable.admin.v2.Cluster toEmbeddedProto(ProjectName projectName) { - proto.getClusterBuilder().setLocation(LocationName.of(projectName.getProject(), zone).toString()); + proto.getClusterBuilder() + .setLocation(LocationName.of(projectName.getProject(), zone).toString()); return proto.getClusterBuilder().build(); } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java index 3def1a20ca8e..5e6999aa28b3 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java @@ -18,7 +18,6 @@ import com.google.api.core.InternalApi; import com.google.bigtable.admin.v2.Instance.Type; import com.google.bigtable.admin.v2.ProjectName; -import com.google.bigtable.admin.v2.StorageType; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import java.util.List; @@ -31,13 +30,13 @@ * which do all of the real work. Instances come in 2 flavors: * *

- *
Production - *
A standard instance with either 1 or 2 clusters, as well as 3 or more nodes in each cluster. - * You cannot downgrade a production instance to a development instance. + *
Production + *
A standard instance with either 1 or 2 clusters, as well as 3 or more nodes in each cluster. + * You cannot downgrade a production instance to a development instance. * - *
Development - *
A low-cost instance for development and testing, with performance limited to the equivalent - * of a 1-node cluster. Development instances only support a single 1 node cluster. + *
Development + *
A low-cost instance for development and testing, with performance limited to the equivalent + * of a 1-node cluster. Development instances only support a single 1 node cluster. *
* * When creating an Instance, you must create at least one cluster in it. @@ -56,7 +55,8 @@ * * } * - * @see For more details + * @see For more + * details */ public final class CreateInstanceRequest { private final com.google.bigtable.admin.v2.CreateInstanceRequest.Builder builder = @@ -93,11 +93,11 @@ public CreateInstanceRequest setDisplayName(@Nonnull String displayName) { *

Can be either DEVELOPMENT or PRODUCTION. Defaults to PRODUCTION. * Please see class javadoc for details. */ - // TODO(igorbernstein2): try to avoid leaking protobuf generated enums @SuppressWarnings("WeakerAccess") - public CreateInstanceRequest setType(@Nonnull Type type) { + public CreateInstanceRequest setType(@Nonnull Instance.Type type) { Preconditions.checkNotNull(type); - builder.getInstanceBuilder().setType(type); + Preconditions.checkArgument(type != Instance.Type.NOT_KNOWN, "Type must be specified"); + builder.getInstanceBuilder().setType(type.toProto()); return this; } @@ -107,7 +107,8 @@ public CreateInstanceRequest setType(@Nonnull Type type) { *

Labels are key-value pairs that you can use to group related instances and store metadata * about an instance. * - * @see For more details + * @see For more + * details */ @SuppressWarnings("WeakerAccess") public CreateInstanceRequest addLabel(@Nonnull String key, @Nonnull String value) { @@ -125,12 +126,14 @@ public CreateInstanceRequest addLabel(@Nonnull String key, @Nonnull String value * * @param clusterId The name of the cluster. * @param zone The zone where the cluster will be created. - * @param serveNodes The number of nodes that cluster will contain. DEVELOPMENT instance clusters must have exactly one node. - * @param storageType The type of storage used by this cluster to serve its parent instance's tables. + * @param serveNodes The number of nodes that cluster will contain. DEVELOPMENT instance clusters + * must have exactly one node. + * @param storageType The type of storage used by this cluster to serve its parent instance's + * tables. */ - // TODO(igorbernstein2): try to avoid leaking protobuf generated enums @SuppressWarnings("WeakerAccess") - public CreateInstanceRequest addCluster(@Nonnull String clusterId, @Nonnull String zone, int serveNodes, @Nonnull StorageType storageType) { + public CreateInstanceRequest addCluster(@Nonnull String clusterId, @Nonnull String zone, + int serveNodes, @Nonnull StorageType storageType) { CreateClusterRequest clusterRequest = CreateClusterRequest .of("ignored-instance-id", clusterId) .setZone(zone) @@ -152,7 +155,8 @@ public com.google.bigtable.admin.v2.CreateInstanceRequest toProto(ProjectName pr .clearClusters(); for (CreateClusterRequest clusterRequest : clusterRequests) { - builder.putClusters(clusterRequest.getClusterId(), clusterRequest.toEmbeddedProto(projectName)); + builder + .putClusters(clusterRequest.getClusterId(), clusterRequest.toEmbeddedProto(projectName)); } return builder.build(); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Instance.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Instance.java index 5d067aaaa24e..a7490bb8b7e6 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Instance.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Instance.java @@ -16,8 +16,6 @@ package com.google.cloud.bigtable.admin.v2.models; import com.google.api.core.InternalApi; -import com.google.bigtable.admin.v2.Instance.State; -import com.google.bigtable.admin.v2.Instance.Type; import com.google.bigtable.admin.v2.InstanceName; import com.google.common.base.Objects; import com.google.common.base.Preconditions; @@ -32,6 +30,85 @@ * all of the real work. */ public final class Instance { + public enum Type { + /** The type of the instance is unknown */ + NOT_KNOWN(com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED), + /** An instance meant for production use. `serve_nodes` must be set on the cluster. */ + PRODUCTION(com.google.bigtable.admin.v2.Instance.Type.PRODUCTION), + /** The instance is meant for development and testing purposes only. */ + DEVELOPMENT(com.google.bigtable.admin.v2.Instance.Type.DEVELOPMENT); + + private final com.google.bigtable.admin.v2.Instance.Type proto; + + /** + * Wraps the protobuf. This method is considered an internal implementation detail and not meant + * to be used by applications. + */ + @InternalApi + public static Type fromProto(com.google.bigtable.admin.v2.Instance.Type proto) { + for (Type type : values()) { + if (type.proto.equals(proto)) { + return type; + } + } + return NOT_KNOWN; + } + + Type(com.google.bigtable.admin.v2.Instance.Type proto) { + this.proto = proto; + } + + /** + * Creates the request protobuf. This method is considered an internal implementation detail and + * not meant to be used by applications. + */ + @InternalApi + public com.google.bigtable.admin.v2.Instance.Type toProto() { + return proto; + } + } + + public enum State { + /** The state of the instance could not be determined. */ + NOT_KNOWN(com.google.bigtable.admin.v2.Instance.State.STATE_NOT_KNOWN), + /** The instance has been successfully created and can serve requests to its tables. */ + READY(com.google.bigtable.admin.v2.Instance.State.READY), + /** + * The instance is currently being created, and may be destroyed if the creation process + * encounters an error. + */ + CREATING(com.google.bigtable.admin.v2.Instance.State.CREATING); + + private final com.google.bigtable.admin.v2.Instance.State proto; + + /** + * Wraps the protobuf. This method is considered an internal implementation detail and not meant + * to be used by applications. + */ + @InternalApi + public static State fromProto(com.google.bigtable.admin.v2.Instance.State proto) { + for (State state : values()) { + if (state.proto.equals(proto)) { + return state; + } + } + return NOT_KNOWN; + } + + State(com.google.bigtable.admin.v2.Instance.State proto) { + this.proto = proto; + } + + /** + * Creates the request protobuf. This method is considered an internal implementation detail and + * not meant to be used by applications. + */ + @InternalApi + public com.google.bigtable.admin.v2.Instance.State toProto() { + return proto; + } + } + @Nonnull private final com.google.bigtable.admin.v2.Instance proto; @@ -71,7 +148,7 @@ public String getDisplayName() { /** Gets the instance's current type. Can be DEVELOPMENT or PRODUCTION. */ @SuppressWarnings("WeakerAccess") public Type getType() { - return proto.getType(); + return Type.fromProto(proto.getType()); } /** @@ -87,10 +164,9 @@ public Map getLabels() { /** The current state of the instance. */ - // TODO(igorbernstein2): Try to avoid leaking protobuf enums @SuppressWarnings("WeakerAccess") public State getState() { - return proto.getState(); + return State.fromProto(proto.getState()); } @Override diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java new file mode 100644 index 000000000000..6ca3cccea7d4 --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java @@ -0,0 +1,57 @@ +/* + * Copyright 2018 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 + * + * https://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. + */ +package com.google.cloud.bigtable.admin.v2.models; + +import com.google.api.core.InternalApi; + +/** Storage media types for persisting Bigtable data. */ +public enum StorageType { + /** Storage type could not be determined. */ + NOT_KNOWN(com.google.bigtable.admin.v2.StorageType.STORAGE_TYPE_UNSPECIFIED), + /** Flash (SSD) storage should be used. */ + HDD(com.google.bigtable.admin.v2.StorageType.HDD), + /** Magnetic drive (HDD) storage should be used. */ + SSD(com.google.bigtable.admin.v2.StorageType.SSD); + + private final com.google.bigtable.admin.v2.StorageType proto; + + /** + * Wraps the protobuf. This method is considered an internal implementation detail and not meant + * to be used by applications. + */ + @InternalApi + public static StorageType fromProto(com.google.bigtable.admin.v2.StorageType proto) { + for (StorageType type : values()) { + if (type.proto.equals(proto)) { + return type; + } + } + return NOT_KNOWN; + } + + /** + * Creates the request protobuf. This method is considered an internal implementation detail and + * not meant to be used by applications. + */ + @InternalApi + StorageType(com.google.bigtable.admin.v2.StorageType proto) { + this.proto = proto; + } + + public com.google.bigtable.admin.v2.StorageType toProto() { + return proto; + } +} From 58a950ad10feea65edb8f6a49e288bdee1989d5c Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Fri, 7 Sep 2018 17:56:46 -0400 Subject: [PATCH 2/9] fix tests --- .../v2/BigtableInstanceAdminClientTest.java | 11 ++--- .../bigtable/admin/v2/models/ClusterTest.java | 12 ++--- .../v2/models/CreateClusterRequestTest.java | 48 +++++++++---------- .../admin/v2/models/InstanceTest.java | 14 +++--- 4 files changed, 38 insertions(+), 47 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminClientTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminClientTest.java index afee494a1449..dadaa71d355c 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminClientTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminClientTest.java @@ -29,11 +29,9 @@ import com.google.bigtable.admin.v2.AppProfileName; import com.google.bigtable.admin.v2.ClusterName; import com.google.bigtable.admin.v2.CreateInstanceMetadata; -import com.google.bigtable.admin.v2.Instance.Type; import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.LocationName; import com.google.bigtable.admin.v2.ProjectName; -import com.google.bigtable.admin.v2.StorageType; import com.google.bigtable.admin.v2.UpdateClusterMetadata; import com.google.bigtable.admin.v2.UpdateInstanceMetadata; import com.google.cloud.Identity; @@ -50,6 +48,7 @@ import com.google.cloud.bigtable.admin.v2.models.Instance; import com.google.cloud.bigtable.admin.v2.models.PartialListClustersException; import com.google.cloud.bigtable.admin.v2.models.PartialListInstancesException; +import com.google.cloud.bigtable.admin.v2.models.StorageType; import com.google.cloud.bigtable.admin.v2.models.UpdateAppProfileRequest; import com.google.cloud.bigtable.admin.v2.models.UpdateInstanceRequest; import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStub; @@ -176,13 +175,13 @@ public void testCreateInstance() { .setInstanceId(INSTANCE_NAME.getInstance()) .setInstance( com.google.bigtable.admin.v2.Instance.newBuilder() - .setType(Type.DEVELOPMENT) + .setType(com.google.bigtable.admin.v2.Instance.Type.DEVELOPMENT) .setDisplayName(INSTANCE_NAME.getInstance()) ) .putClusters("cluster1", com.google.bigtable.admin.v2.Cluster.newBuilder() .setLocation("projects/my-project/locations/us-east1-c") .setServeNodes(1) - .setDefaultStorageType(StorageType.SSD) + .setDefaultStorageType(com.google.bigtable.admin.v2.StorageType.SSD) .build() ) .build(); @@ -197,7 +196,7 @@ public void testCreateInstance() { // Execute Instance actualResult = adminClient.createInstance( CreateInstanceRequest.of(INSTANCE_NAME.getInstance()) - .setType(Type.DEVELOPMENT) + .setType(Instance.Type.DEVELOPMENT) .addCluster("cluster1", "us-east1-c", 1, StorageType.SSD) ); @@ -374,7 +373,7 @@ public void testCreateCluster() { com.google.bigtable.admin.v2.Cluster.newBuilder() .setLocation("projects/my-project/locations/us-east1-c") .setServeNodes(3) - .setDefaultStorageType(StorageType.SSD) + .setDefaultStorageType(com.google.bigtable.admin.v2.StorageType.SSD) ) .build(); com.google.bigtable.admin.v2.Cluster expectedResponse = com.google.bigtable.admin.v2.Cluster diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java index 76e158c7603d..7dba1e89c3d0 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java @@ -17,8 +17,6 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.bigtable.admin.v2.Cluster.State; -import com.google.bigtable.admin.v2.StorageType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -30,9 +28,9 @@ public void testFromProto() { com.google.bigtable.admin.v2.Cluster proto = com.google.bigtable.admin.v2.Cluster.newBuilder() .setName("projects/my-project/instances/my-instance/clusters/my-cluster") .setLocation("projects/my-project/locations/us-east1-c") - .setState(State.READY) + .setState(com.google.bigtable.admin.v2.Cluster.State.READY) .setServeNodes(30) - .setDefaultStorageType(StorageType.SSD) + .setDefaultStorageType(com.google.bigtable.admin.v2.StorageType.SSD) .build(); Cluster result = Cluster.fromProto(proto); @@ -40,7 +38,7 @@ public void testFromProto() { assertThat(result.getId()).isEqualTo("my-cluster"); assertThat(result.getInstanceId()).isEqualTo("my-instance"); assertThat(result.getZone()).isEqualTo("us-east1-c"); - assertThat(result.getState()).isEqualTo(State.READY); + assertThat(result.getState()).isEqualTo(Cluster.State.READY); assertThat(result.getServeNodes()).isEqualTo(30); assertThat(result.getStorageType()).isEqualTo(StorageType.SSD); } @@ -49,9 +47,9 @@ public void testFromProto() { public void testRequiresName() { com.google.bigtable.admin.v2.Cluster proto = com.google.bigtable.admin.v2.Cluster.newBuilder() .setLocation("projects/my-project/locations/us-east1-c") - .setState(State.READY) + .setState(com.google.bigtable.admin.v2.Cluster.State.READY) .setServeNodes(30) - .setDefaultStorageType(StorageType.SSD) + .setDefaultStorageType(com.google.bigtable.admin.v2.StorageType.SSD) .build(); Exception actualException = null; diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequestTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequestTest.java index dd0c318bd559..bbf8249b1224 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequestTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequestTest.java @@ -17,11 +17,7 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.bigtable.admin.v2.Cluster; -import com.google.bigtable.admin.v2.Instance; -import com.google.bigtable.admin.v2.Instance.Type; import com.google.bigtable.admin.v2.ProjectName; -import com.google.bigtable.admin.v2.StorageType; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -31,7 +27,7 @@ public class CreateClusterRequestTest { @Test public void testProductionSingle() { CreateInstanceRequest input = CreateInstanceRequest.of("my-instance") - .setType(Type.PRODUCTION) + .setType(Instance.Type.PRODUCTION) .addCluster("cluster1", "us-east1-c", 3, StorageType.SSD); com.google.bigtable.admin.v2.CreateInstanceRequest actual = input @@ -42,15 +38,15 @@ public void testProductionSingle() { .setParent(ProjectName.of("my-project").toString()) .setInstanceId("my-instance") .setInstance( - Instance.newBuilder() + com.google.bigtable.admin.v2.Instance.newBuilder() .setDisplayName("my-instance") - .setType(Type.PRODUCTION) + .setType(com.google.bigtable.admin.v2.Instance.Type.PRODUCTION) ) .putClusters("cluster1", - Cluster.newBuilder() + com.google.bigtable.admin.v2.Cluster.newBuilder() .setLocation("projects/my-project/locations/us-east1-c") .setServeNodes(3) - .setDefaultStorageType(StorageType.SSD) + .setDefaultStorageType(com.google.bigtable.admin.v2.StorageType.SSD) .build() ) .build(); @@ -61,7 +57,7 @@ public void testProductionSingle() { @Test public void testProductionReplication() { CreateInstanceRequest input = CreateInstanceRequest.of("my-instance") - .setType(Type.PRODUCTION) + .setType(Instance.Type.PRODUCTION) .addCluster("cluster1", "us-east1-c", 3, StorageType.SSD) .addCluster("cluster2", "us-east1-a", 3, StorageType.SSD); @@ -73,22 +69,22 @@ public void testProductionReplication() { .setParent(ProjectName.of("my-project").toString()) .setInstanceId("my-instance") .setInstance( - Instance.newBuilder() + com.google.bigtable.admin.v2.Instance.newBuilder() .setDisplayName("my-instance") - .setType(Type.PRODUCTION) + .setType(com.google.bigtable.admin.v2.Instance.Type.PRODUCTION) ) .putClusters("cluster1", - Cluster.newBuilder() + com.google.bigtable.admin.v2.Cluster.newBuilder() .setLocation("projects/my-project/locations/us-east1-c") .setServeNodes(3) - .setDefaultStorageType(StorageType.SSD) + .setDefaultStorageType(com.google.bigtable.admin.v2.StorageType.SSD) .build() ) .putClusters("cluster2", - Cluster.newBuilder() + com.google.bigtable.admin.v2.Cluster.newBuilder() .setLocation("projects/my-project/locations/us-east1-a") .setServeNodes(3) - .setDefaultStorageType(StorageType.SSD) + .setDefaultStorageType(com.google.bigtable.admin.v2.StorageType.SSD) .build() ) .build(); @@ -99,7 +95,7 @@ public void testProductionReplication() { @Test public void testDevelopment() { CreateInstanceRequest input = CreateInstanceRequest.of("my-instance") - .setType(Type.DEVELOPMENT) + .setType(Instance.Type.DEVELOPMENT) .addCluster("cluster1", "us-east1-c", 1, StorageType.SSD); com.google.bigtable.admin.v2.CreateInstanceRequest actual = input @@ -110,15 +106,15 @@ public void testDevelopment() { .setParent(ProjectName.of("my-project").toString()) .setInstanceId("my-instance") .setInstance( - Instance.newBuilder() + com.google.bigtable.admin.v2.Instance.newBuilder() .setDisplayName("my-instance") - .setType(Type.DEVELOPMENT) + .setType(com.google.bigtable.admin.v2.Instance.Type.DEVELOPMENT) ) .putClusters("cluster1", - Cluster.newBuilder() + com.google.bigtable.admin.v2.Cluster.newBuilder() .setLocation("projects/my-project/locations/us-east1-c") .setServeNodes(1) - .setDefaultStorageType(StorageType.SSD) + .setDefaultStorageType(com.google.bigtable.admin.v2.StorageType.SSD) .build() ) .build(); @@ -132,7 +128,7 @@ public void testOptionalFields() { .setDisplayName("custom display name") .addLabel("my label", "with some value") .addLabel("my other label", "with some value") - .setType(Type.DEVELOPMENT) + .setType(Instance.Type.DEVELOPMENT) .addCluster("cluster1", "us-east1-c", 1, StorageType.SSD); com.google.bigtable.admin.v2.CreateInstanceRequest actual = input @@ -143,17 +139,17 @@ public void testOptionalFields() { .setParent(ProjectName.of("my-project").toString()) .setInstanceId("my-instance") .setInstance( - Instance.newBuilder() + com.google.bigtable.admin.v2.Instance.newBuilder() .setDisplayName("custom display name") .putLabels("my label", "with some value") .putLabels("my other label", "with some value") - .setType(Type.DEVELOPMENT) + .setType(com.google.bigtable.admin.v2.Instance.Type.DEVELOPMENT) ) .putClusters("cluster1", - Cluster.newBuilder() + com.google.bigtable.admin.v2.Cluster.newBuilder() .setLocation("projects/my-project/locations/us-east1-c") .setServeNodes(1) - .setDefaultStorageType(StorageType.SSD) + .setDefaultStorageType(com.google.bigtable.admin.v2.StorageType.SSD) .build() ) .build(); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java index 3d63bab16d0c..7108b6af5b9d 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java @@ -17,8 +17,6 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.bigtable.admin.v2.Instance.State; -import com.google.bigtable.admin.v2.Instance.Type; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -30,8 +28,8 @@ public void testFromProto() { com.google.bigtable.admin.v2.Instance proto = com.google.bigtable.admin.v2.Instance.newBuilder() .setName("projects/my-project/instances/my-instance") .setDisplayName("my display name") - .setType(Type.PRODUCTION) - .setState(State.READY) + .setType(com.google.bigtable.admin.v2.Instance.Type.PRODUCTION) + .setState(com.google.bigtable.admin.v2.Instance.State.READY) .putLabels("label1", "value1") .putLabels("label2", "value2") .build(); @@ -40,8 +38,8 @@ public void testFromProto() { assertThat(result.getId()).isEqualTo("my-instance"); assertThat(result.getDisplayName()).isEqualTo("my display name"); - assertThat(result.getType()).isEqualTo(Type.PRODUCTION); - assertThat(result.getState()).isEqualTo(State.READY); + assertThat(result.getType()).isEqualTo(Instance.Type.PRODUCTION); + assertThat(result.getState()).isEqualTo(Instance.State.READY); assertThat(result.getLabels()).containsExactly( "label1", "value1", "label2", "value2" @@ -52,8 +50,8 @@ public void testFromProto() { public void testRequiresName() { com.google.bigtable.admin.v2.Instance proto = com.google.bigtable.admin.v2.Instance.newBuilder() .setDisplayName("my display name") - .setType(Type.PRODUCTION) - .setState(State.READY) + .setType(com.google.bigtable.admin.v2.Instance.Type.PRODUCTION) + .setState(com.google.bigtable.admin.v2.Instance.State.READY) .putLabels("label1", "value1") .putLabels("label2", "value2") .build(); From d9e4ae91a86bbe1f99192e5a8a26dee1afb8191d Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Fri, 7 Sep 2018 18:16:20 -0400 Subject: [PATCH 3/9] wrap replication state as well --- .../cloud/bigtable/admin/v2/models/Table.java | 59 ++++++++++++++++++- .../bigtable/admin/v2/models/TableTest.java | 20 ++++--- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java index 0a9733c32c51..d56e065a460c 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java @@ -16,7 +16,7 @@ package com.google.cloud.bigtable.admin.v2.models; import com.google.api.core.InternalApi; -import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState; +import com.google.bigtable.admin.v2.Table.ClusterState; import com.google.bigtable.admin.v2.TableName; import com.google.common.base.Objects; import com.google.common.collect.ImmutableList; @@ -28,6 +28,61 @@ /** Wrapper for {@link Table} protocol buffer object */ public final class Table { + public enum ReplicationState { + /** The replication state of the table is unknown in this cluster. */ + NOT_KNOWN(ClusterState.ReplicationState.STATE_NOT_KNOWN), + /** + * The cluster was recently created, and the table must finish copying over pre-existing data + * from other clusters before it can begin receiving live replication updates and serving Data + * API requests. + */ + INITIALIZING(ClusterState.ReplicationState.INITIALIZING), + /** + * The table is temporarily unable to serve Data API requests from this cluster due to planned + * internal maintenance. + */ + PLANNED_MAINTENANCE(ClusterState.ReplicationState.PLANNED_MAINTENANCE), + /** + * The table is temporarily unable to serve Data API requests from this cluster due to unplanned + * or emergency maintenance. + */ + UNPLANNED_MAINTENANCE(ClusterState.ReplicationState.UNPLANNED_MAINTENANCE), + /** + * The table can serve Data API requests from this cluster. Depending on replication delay, + * reads may not immediately reflect the state of the table in other clusters. + */ + READY(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.READY); + + private final com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState proto; + + /** + * Wraps the protobuf. This method is considered an internal implementation detail and not meant + * to be used by applications. + */ + @InternalApi + public static ReplicationState fromProto(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState proto) { + for (ReplicationState state : values()) { + if (state.proto.equals(proto)) { + return state; + } + } + return NOT_KNOWN; + } + + ReplicationState(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState proto) { + this.proto = proto; + } + + /** + * Creates the request protobuf. This method is considered an internal implementation detail and + * not meant to be used by applications. + */ + @InternalApi + public com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState toProto() { + return proto; + } + } + private final String id; private final String instanceId; private final Map replicationStatesByClusterId; @@ -38,7 +93,7 @@ public static Table fromProto(@Nonnull com.google.bigtable.admin.v2.Table proto) ImmutableMap.Builder replicationStates = ImmutableMap.builder(); for (Entry entry : proto.getClusterStatesMap().entrySet()) { - replicationStates.put(entry.getKey(), entry.getValue().getReplicationState()); + replicationStates.put(entry.getKey(), ReplicationState.fromProto(entry.getValue().getReplicationState())); } ImmutableList.Builder columnFamilies = ImmutableList.builder(); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java index 3ebe004f51d4..007d5d416b29 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java @@ -17,8 +17,6 @@ import com.google.bigtable.admin.v2.ColumnFamily; import com.google.bigtable.admin.v2.GcRule; -import com.google.bigtable.admin.v2.Table.ClusterState; -import com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState; import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.bigtable.admin.v2.TableName; import com.google.common.truth.Truth; @@ -39,12 +37,18 @@ public void testFromProto() { .setGranularity(TimestampGranularity.MILLIS) .putClusterStates( "cluster1", - ClusterState.newBuilder().setReplicationState(ReplicationState.READY).build()) + com.google.bigtable.admin.v2.Table.ClusterState.newBuilder() + .setReplicationState( + com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.READY + ).build() + ) .putClusterStates( "cluster2", - ClusterState.newBuilder() - .setReplicationState(ReplicationState.INITIALIZING) - .build()) + com.google.bigtable.admin.v2.Table.ClusterState.newBuilder() + .setReplicationState( + com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.INITIALIZING + ).build() + ) .putColumnFamilies("cf1", ColumnFamily.newBuilder().build()) .putColumnFamilies( "cf2", @@ -68,8 +72,8 @@ public void testFromProto() { Truth.assertThat(result.getInstanceId()).isEqualTo("my-instance"); Truth.assertThat(result.getId()).isEqualTo("my-table"); Truth.assertThat(result.getReplicationStatesByClusterId()).containsExactly( - "cluster1", ReplicationState.READY, - "cluster2", ReplicationState.INITIALIZING + "cluster1", Table.ReplicationState.READY, + "cluster2", Table.ReplicationState.INITIALIZING ); Truth.assertThat(result.getColumnFamilies()).hasSize(3); From 360c7fc81caa12e9460a8440499ff599907677ca Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Fri, 7 Sep 2018 18:17:35 -0400 Subject: [PATCH 4/9] tweaks --- .../com/google/cloud/bigtable/admin/v2/models/Table.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java index d56e065a460c..168950af33b8 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java @@ -16,7 +16,6 @@ package com.google.cloud.bigtable.admin.v2.models; import com.google.api.core.InternalApi; -import com.google.bigtable.admin.v2.Table.ClusterState; import com.google.bigtable.admin.v2.TableName; import com.google.common.base.Objects; import com.google.common.collect.ImmutableList; @@ -30,23 +29,23 @@ public final class Table { public enum ReplicationState { /** The replication state of the table is unknown in this cluster. */ - NOT_KNOWN(ClusterState.ReplicationState.STATE_NOT_KNOWN), + NOT_KNOWN(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.STATE_NOT_KNOWN), /** * The cluster was recently created, and the table must finish copying over pre-existing data * from other clusters before it can begin receiving live replication updates and serving Data * API requests. */ - INITIALIZING(ClusterState.ReplicationState.INITIALIZING), + INITIALIZING(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.INITIALIZING), /** * The table is temporarily unable to serve Data API requests from this cluster due to planned * internal maintenance. */ - PLANNED_MAINTENANCE(ClusterState.ReplicationState.PLANNED_MAINTENANCE), + PLANNED_MAINTENANCE(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.PLANNED_MAINTENANCE), /** * The table is temporarily unable to serve Data API requests from this cluster due to unplanned * or emergency maintenance. */ - UNPLANNED_MAINTENANCE(ClusterState.ReplicationState.UNPLANNED_MAINTENANCE), + UNPLANNED_MAINTENANCE(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.UNPLANNED_MAINTENANCE), /** * The table can serve Data API requests from this cluster. Depending on replication delay, * reads may not immediately reflect the state of the table in other clusters. From 2dbc7c716f37d9905e77c3cdbd7e1c7ab2639662 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Mon, 17 Sep 2018 14:10:20 -0400 Subject: [PATCH 5/9] address feedback --- .../bigtable/admin/v2/models/Cluster.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java index 50bf66725d90..7d2bd1e3195e 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java @@ -85,7 +85,7 @@ public com.google.bigtable.admin.v2.Cluster.State toProto() { } @Nonnull - private final com.google.bigtable.admin.v2.Cluster proto; + private final com.google.bigtable.admin.v2.Cluster stateProto; /** * Wraps a protobuf response. @@ -101,7 +101,7 @@ public static Cluster fromProto(com.google.bigtable.admin.v2.Cluster proto) { private Cluster(@Nonnull com.google.bigtable.admin.v2.Cluster proto) { Preconditions.checkNotNull(proto); Preconditions.checkArgument(!proto.getName().isEmpty(), "Name must be set"); - this.proto = proto; + this.stateProto = proto; } @@ -110,7 +110,7 @@ private Cluster(@Nonnull com.google.bigtable.admin.v2.Cluster proto) { public String getId() { // Constructor ensures that name is not null ClusterName fullName = Verify.verifyNotNull( - ClusterName.parse(proto.getName()), + ClusterName.parse(stateProto.getName()), "Name can never be null"); //noinspection ConstantConditions return fullName.getCluster(); @@ -121,7 +121,7 @@ public String getId() { public String getInstanceId() { // Constructor ensures that name is not null ClusterName fullName = Verify.verifyNotNull( - ClusterName.parse(proto.getName()), + ClusterName.parse(stateProto.getName()), "Name can never be null"); //noinspection ConstantConditions return fullName.getInstance(); @@ -132,7 +132,7 @@ public String getInstanceId() { /** Get the zone where this cluster is located. */ @SuppressWarnings("WeakerAccess") public String getZone() { - LocationName location = Verify.verifyNotNull(LocationName.parse(proto.getLocation())); + LocationName location = Verify.verifyNotNull(LocationName.parse(stateProto.getLocation())); //noinspection ConstantConditions return location.getLocation(); } @@ -140,7 +140,7 @@ public String getZone() { /** Gets the current state of the cluster */ @SuppressWarnings("WeakerAccess") public State getState() { - return State.fromProto(proto.getState()); + return State.fromProto(stateProto.getState()); } /** @@ -149,7 +149,7 @@ public State getState() { */ @SuppressWarnings("WeakerAccess") public int getServeNodes() { - return proto.getServeNodes(); + return stateProto.getServeNodes(); } /** @@ -158,7 +158,7 @@ public int getServeNodes() { */ @SuppressWarnings("WeakerAccess") public StorageType getStorageType() { - return StorageType.fromProto(proto.getDefaultStorageType()); + return StorageType.fromProto(stateProto.getDefaultStorageType()); } @Override @@ -170,11 +170,11 @@ public boolean equals(Object o) { return false; } Cluster cluster = (Cluster) o; - return Objects.equal(proto, cluster.proto); + return Objects.equal(stateProto, cluster.stateProto); } @Override public int hashCode() { - return Objects.hashCode(proto); + return Objects.hashCode(stateProto); } } From 5e806be1608b77382fdd325d8d5454e1aa566241 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Mon, 17 Sep 2018 14:10:37 -0400 Subject: [PATCH 6/9] add tests to make sure enum wrappers stay in sync --- .../bigtable/admin/v2/models/ClusterTest.java | 36 ++++++++++ .../admin/v2/models/InstanceTest.java | 67 +++++++++++++++++++ .../admin/v2/models/StorageTypeTest.java | 61 +++++++++++++++++ .../bigtable/admin/v2/models/TableTest.java | 52 ++++++++++++-- 4 files changed, 209 insertions(+), 7 deletions(-) create mode 100644 google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java index 7dba1e89c3d0..01f7e50c1110 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java @@ -17,12 +17,17 @@ import static com.google.common.truth.Truth.assertThat; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import java.util.Arrays; +import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class ClusterTest { + @Test public void testFromProto() { com.google.bigtable.admin.v2.Cluster proto = com.google.bigtable.admin.v2.Cluster.newBuilder() @@ -62,4 +67,35 @@ public void testRequiresName() { assertThat(actualException).isInstanceOf(IllegalArgumentException.class); } + + @Test + public void testStateEnumUpToDate() { + Multimap modelToProtoMap = + ArrayListMultimap.create(); + + for (com.google.bigtable.admin.v2.Cluster.State protoValue : com.google.bigtable.admin.v2.Cluster.State + .values()) { + Cluster.State modelValue = Cluster.State.fromProto(protoValue); + modelToProtoMap.put(modelValue, protoValue); + } + + // Make sure all model values are used + assertThat(modelToProtoMap.keys()).containsAllIn(Arrays.asList(Cluster.State.values())); + + // Make sure unknown is handled properly (it has multiple mappings) + assertThat(modelToProtoMap).valuesForKey(Cluster.State.NOT_KNOWN).containsExactly( + com.google.bigtable.admin.v2.Cluster.State.STATE_NOT_KNOWN, + com.google.bigtable.admin.v2.Cluster.State.UNRECOGNIZED + ); + + // Make sure everything else has exactly 1 mapping + modelToProtoMap.removeAll(Cluster.State.NOT_KNOWN); + + for (Cluster.State modelState : modelToProtoMap.keySet()) { + Collection protoStates = modelToProtoMap + .get(modelState); + + assertThat(protoStates).hasSize(1); + } + } } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java index 7108b6af5b9d..aa587044477a 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java @@ -17,12 +17,17 @@ import static com.google.common.truth.Truth.assertThat; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import java.util.Arrays; +import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class InstanceTest { + @Test public void testFromProto() { com.google.bigtable.admin.v2.Instance proto = com.google.bigtable.admin.v2.Instance.newBuilder() @@ -66,4 +71,66 @@ public void testRequiresName() { assertThat(actualException).isInstanceOf(IllegalArgumentException.class); } + + @Test + public void testTypeEnumUpToDate() { + Multimap modelToProtoMap = + ArrayListMultimap.create(); + + for (com.google.bigtable.admin.v2.Instance.Type protoValue : com.google.bigtable.admin.v2.Instance.Type + .values()) { + Instance.Type modelValue = Instance.Type.fromProto(protoValue); + modelToProtoMap.put(modelValue, protoValue); + } + + // Make sure all model values are used + assertThat(modelToProtoMap.keys()).containsAllIn(Arrays.asList(Instance.Type.values())); + + // Make sure unknown is handled properly (it has multiple mappings) + assertThat(modelToProtoMap).valuesForKey(Instance.Type.NOT_KNOWN).containsExactly( + com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED, + com.google.bigtable.admin.v2.Instance.Type.UNRECOGNIZED + ); + + // Make sure everything else has exactly 1 mapping + modelToProtoMap.removeAll(Instance.Type.NOT_KNOWN); + + for (Instance.Type modelState : modelToProtoMap.keySet()) { + Collection protoStates = modelToProtoMap + .get(modelState); + + assertThat(protoStates).hasSize(1); + } + } + + @Test + public void testStateEnumUpToDate() { + Multimap modelToProtoMap = + ArrayListMultimap.create(); + + for (com.google.bigtable.admin.v2.Instance.State protoValue : com.google.bigtable.admin.v2.Instance.State + .values()) { + Instance.State modelValue = Instance.State.fromProto(protoValue); + modelToProtoMap.put(modelValue, protoValue); + } + + // Make sure all model values are used + assertThat(modelToProtoMap.keys()).containsAllIn(Arrays.asList(Instance.State.values())); + + // Make sure unknown is handled properly (it has multiple mappings) + assertThat(modelToProtoMap).valuesForKey(Instance.State.NOT_KNOWN).containsExactly( + com.google.bigtable.admin.v2.Instance.State.STATE_NOT_KNOWN, + com.google.bigtable.admin.v2.Instance.State.UNRECOGNIZED + ); + + // Make sure everything else has exactly 1 mapping + modelToProtoMap.removeAll(Instance.State.NOT_KNOWN); + + for (Instance.State modelState : modelToProtoMap.keySet()) { + Collection protoStates = modelToProtoMap + .get(modelState); + + assertThat(protoStates).hasSize(1); + } + } } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java new file mode 100644 index 000000000000..9f7fa2d5d06d --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2018 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 + * + * https://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. + */ +package com.google.cloud.bigtable.admin.v2.models; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import java.util.Arrays; +import java.util.Collection; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class StorageTypeTest { + + @Test + public void testUpToDate() { + Multimap modelToProtoMap = + ArrayListMultimap.create(); + + for (com.google.bigtable.admin.v2.StorageType protoValue : com.google.bigtable.admin.v2.StorageType + .values()) { + StorageType modelValue = StorageType.fromProto(protoValue); + modelToProtoMap.put(modelValue, protoValue); + } + + // Make sure all model values are used + assertThat(modelToProtoMap.keys()).containsAllIn(Arrays.asList(StorageType.values())); + + // Make sure unknown is handled properly (it has multiple mappings) + assertThat(modelToProtoMap).valuesForKey(StorageType.NOT_KNOWN).containsExactly( + com.google.bigtable.admin.v2.StorageType.UNRECOGNIZED, + com.google.bigtable.admin.v2.StorageType.STORAGE_TYPE_UNSPECIFIED + ); + + // Make sure everything else has exactly 1 mapping + modelToProtoMap.removeAll(StorageType.NOT_KNOWN); + + for (StorageType modelState : modelToProtoMap.keySet()) { + Collection protoStates = modelToProtoMap + .get(modelState); + + assertThat(protoStates).hasSize(1); + } + } +} \ No newline at end of file diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java index 007d5d416b29..f2bf65178f04 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java @@ -15,11 +15,16 @@ */ package com.google.cloud.bigtable.admin.v2.models; +import static com.google.common.truth.Truth.assertThat; + import com.google.bigtable.admin.v2.ColumnFamily; import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.bigtable.admin.v2.TableName; -import com.google.common.truth.Truth; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import java.util.Arrays; +import java.util.Collection; import java.util.Map.Entry; import org.junit.Test; import org.junit.runner.RunWith; @@ -69,18 +74,51 @@ public void testFromProto() { Table result = Table.fromProto(proto); - Truth.assertThat(result.getInstanceId()).isEqualTo("my-instance"); - Truth.assertThat(result.getId()).isEqualTo("my-table"); - Truth.assertThat(result.getReplicationStatesByClusterId()).containsExactly( + assertThat(result.getInstanceId()).isEqualTo("my-instance"); + assertThat(result.getId()).isEqualTo("my-table"); + assertThat(result.getReplicationStatesByClusterId()).containsExactly( "cluster1", Table.ReplicationState.READY, "cluster2", Table.ReplicationState.INITIALIZING ); - Truth.assertThat(result.getColumnFamilies()).hasSize(3); + assertThat(result.getColumnFamilies()).hasSize(3); for (Entry entry : proto.getColumnFamiliesMap().entrySet()) { - Truth.assertThat(result.getColumnFamilies()).contains( - com.google.cloud.bigtable.admin.v2.models.ColumnFamily.fromProto(entry.getKey(), entry.getValue()) + assertThat(result.getColumnFamilies()).contains( + com.google.cloud.bigtable.admin.v2.models.ColumnFamily + .fromProto(entry.getKey(), entry.getValue()) ); } } + + @Test + public void testReplicationStateEnumUpToDate() { + Multimap modelToProtoMap = + ArrayListMultimap.create(); + + for (com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState protoValue : com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState + .values()) { + Table.ReplicationState modelValue = Table.ReplicationState.fromProto(protoValue); + modelToProtoMap.put(modelValue, protoValue); + } + + // Make sure all model values are used + assertThat(modelToProtoMap.keys()) + .containsAllIn(Arrays.asList(Table.ReplicationState.values())); + + // Make sure unknown is handled properly (it has multiple mappings) + assertThat(modelToProtoMap).valuesForKey(Table.ReplicationState.NOT_KNOWN).containsExactly( + com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.STATE_NOT_KNOWN, + com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.UNRECOGNIZED + ); + + // Make sure everything else has exactly 1 mapping + modelToProtoMap.removeAll(Table.ReplicationState.NOT_KNOWN); + + for (Table.ReplicationState modelState : modelToProtoMap.keySet()) { + Collection protoStates = modelToProtoMap + .get(modelState); + + assertThat(protoStates).hasSize(1); + } + } } From e84edecbd6be960d5e931b6f754a5f42c362d2d5 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Tue, 18 Sep 2018 12:55:18 -0400 Subject: [PATCH 7/9] fix up handling of unrecognized enum values --- .../v2/models/CreateInstanceRequest.java | 1 - .../bigtable/admin/v2/models/Instance.java | 20 +++-- .../bigtable/admin/v2/models/StorageType.java | 9 ++- .../cloud/bigtable/admin/v2/models/Table.java | 11 ++- .../admin/v2/models/InstanceTest.java | 77 +++++++------------ .../admin/v2/models/StorageTypeTest.java | 38 +++------ .../bigtable/admin/v2/models/TableTest.java | 41 ++++------ 7 files changed, 81 insertions(+), 116 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java index 5e6999aa28b3..08bf49764c4b 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java @@ -96,7 +96,6 @@ public CreateInstanceRequest setDisplayName(@Nonnull String displayName) { @SuppressWarnings("WeakerAccess") public CreateInstanceRequest setType(@Nonnull Instance.Type type) { Preconditions.checkNotNull(type); - Preconditions.checkArgument(type != Instance.Type.NOT_KNOWN, "Type must be specified"); builder.getInstanceBuilder().setType(type.toProto()); return this; } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Instance.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Instance.java index a7490bb8b7e6..f928afcaa964 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Instance.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Instance.java @@ -31,12 +31,12 @@ */ public final class Instance { public enum Type { - /** The type of the instance is unknown */ - NOT_KNOWN(com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED), /** An instance meant for production use. `serve_nodes` must be set on the cluster. */ PRODUCTION(com.google.bigtable.admin.v2.Instance.Type.PRODUCTION), /** The instance is meant for development and testing purposes only. */ - DEVELOPMENT(com.google.bigtable.admin.v2.Instance.Type.DEVELOPMENT); + DEVELOPMENT(com.google.bigtable.admin.v2.Instance.Type.DEVELOPMENT), + /** The type of instance is not known by this client. Please upgrade your client. */ + UNRECOGNIZED(com.google.bigtable.admin.v2.Instance.Type.UNRECOGNIZED); private final com.google.bigtable.admin.v2.Instance.Type proto; @@ -46,12 +46,15 @@ public enum Type { */ @InternalApi public static Type fromProto(com.google.bigtable.admin.v2.Instance.Type proto) { + Preconditions.checkNotNull(proto); + Preconditions.checkArgument(proto != com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED, + "Server instance type must always be specified"); for (Type type : values()) { if (type.proto.equals(proto)) { return type; } } - return NOT_KNOWN; + return UNRECOGNIZED; } Type(com.google.bigtable.admin.v2.Instance.Type proto) { @@ -69,7 +72,7 @@ public com.google.bigtable.admin.v2.Instance.Type toProto() { } public enum State { - /** The state of the instance could not be determined. */ + /** The state of the instance could not be determined by the server. */ NOT_KNOWN(com.google.bigtable.admin.v2.Instance.State.STATE_NOT_KNOWN), /** The instance has been successfully created and can serve requests to its tables. */ READY(com.google.bigtable.admin.v2.Instance.State.READY), @@ -77,7 +80,9 @@ public enum State { * The instance is currently being created, and may be destroyed if the creation process * encounters an error. */ - CREATING(com.google.bigtable.admin.v2.Instance.State.CREATING); + CREATING(com.google.bigtable.admin.v2.Instance.State.CREATING), + /** The state of instance is not known by this client. Please upgrade your client. */ + UNRECOGNIZED(com.google.bigtable.admin.v2.Instance.State.UNRECOGNIZED); private final com.google.bigtable.admin.v2.Instance.State proto; @@ -87,12 +92,13 @@ public enum State { */ @InternalApi public static State fromProto(com.google.bigtable.admin.v2.Instance.State proto) { + Preconditions.checkNotNull(proto); for (State state : values()) { if (state.proto.equals(proto)) { return state; } } - return NOT_KNOWN; + return UNRECOGNIZED; } State(com.google.bigtable.admin.v2.Instance.State proto) { diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java index 6ca3cccea7d4..49f03a59338c 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java @@ -16,6 +16,7 @@ package com.google.cloud.bigtable.admin.v2.models; import com.google.api.core.InternalApi; +import com.google.common.base.Preconditions; /** Storage media types for persisting Bigtable data. */ public enum StorageType { @@ -24,7 +25,9 @@ public enum StorageType { /** Flash (SSD) storage should be used. */ HDD(com.google.bigtable.admin.v2.StorageType.HDD), /** Magnetic drive (HDD) storage should be used. */ - SSD(com.google.bigtable.admin.v2.StorageType.SSD); + SSD(com.google.bigtable.admin.v2.StorageType.SSD), + /** The storage type is not known by this client. Please upgrade your client. */ + UNRECOGNIZED(com.google.bigtable.admin.v2.StorageType.UNRECOGNIZED); private final com.google.bigtable.admin.v2.StorageType proto; @@ -34,12 +37,14 @@ public enum StorageType { */ @InternalApi public static StorageType fromProto(com.google.bigtable.admin.v2.StorageType proto) { + Preconditions.checkNotNull(proto); + for (StorageType type : values()) { if (type.proto.equals(proto)) { return type; } } - return NOT_KNOWN; + return UNRECOGNIZED; } /** diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java index 168950af33b8..215e62090f78 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Table.java @@ -18,6 +18,7 @@ import com.google.api.core.InternalApi; import com.google.bigtable.admin.v2.TableName; import com.google.common.base.Objects; +import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.util.List; @@ -50,7 +51,11 @@ public enum ReplicationState { * The table can serve Data API requests from this cluster. Depending on replication delay, * reads may not immediately reflect the state of the table in other clusters. */ - READY(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.READY); + READY(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.READY), + + /** The replication state of table is not known by this client. Please upgrade your client. */ + UNRECOGNIZED(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.UNRECOGNIZED); + private final com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState proto; @@ -60,12 +65,14 @@ public enum ReplicationState { */ @InternalApi public static ReplicationState fromProto(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState proto) { + Preconditions.checkNotNull(proto); + for (ReplicationState state : values()) { if (state.proto.equals(proto)) { return state; } } - return NOT_KNOWN; + return UNRECOGNIZED; } ReplicationState(com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState proto) { diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java index aa587044477a..4687bbae8767 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/InstanceTest.java @@ -17,10 +17,8 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Multimap; -import java.util.Arrays; -import java.util.Collection; +import com.google.common.collect.Lists; +import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -74,63 +72,44 @@ public void testRequiresName() { @Test public void testTypeEnumUpToDate() { - Multimap modelToProtoMap = - ArrayListMultimap.create(); + List validProtoValues = + Lists.newArrayList(com.google.bigtable.admin.v2.Instance.Type.values()); - for (com.google.bigtable.admin.v2.Instance.Type protoValue : com.google.bigtable.admin.v2.Instance.Type - .values()) { - Instance.Type modelValue = Instance.Type.fromProto(protoValue); - modelToProtoMap.put(modelValue, protoValue); - } - - // Make sure all model values are used - assertThat(modelToProtoMap.keys()).containsAllIn(Arrays.asList(Instance.Type.values())); - - // Make sure unknown is handled properly (it has multiple mappings) - assertThat(modelToProtoMap).valuesForKey(Instance.Type.NOT_KNOWN).containsExactly( - com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED, - com.google.bigtable.admin.v2.Instance.Type.UNRECOGNIZED - ); + // TYPE_UNSPECIFIED is not surfaced + validProtoValues.remove(com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED); - // Make sure everything else has exactly 1 mapping - modelToProtoMap.removeAll(Instance.Type.NOT_KNOWN); + Exception actualError = null; + try { + Instance.Type.fromProto(com.google.bigtable.admin.v2.Instance.Type.TYPE_UNSPECIFIED); + } catch (Exception e) { + actualError = e; + } + assertThat(actualError).isInstanceOf(IllegalArgumentException.class); - for (Instance.Type modelState : modelToProtoMap.keySet()) { - Collection protoStates = modelToProtoMap - .get(modelState); + List validModelValues = Lists.newArrayList(Instance.Type.values()); - assertThat(protoStates).hasSize(1); + List actualModelValues = Lists.newArrayList(); + for (com.google.bigtable.admin.v2.Instance.Type protoValue : validProtoValues) { + actualModelValues.add(Instance.Type.fromProto(protoValue)); } + + assertThat(actualModelValues).containsExactlyElementsIn(validModelValues); } @Test public void testStateEnumUpToDate() { - Multimap modelToProtoMap = - ArrayListMultimap.create(); + List validProtoValues = + Lists.newArrayList(com.google.bigtable.admin.v2.Instance.State.values()); - for (com.google.bigtable.admin.v2.Instance.State protoValue : com.google.bigtable.admin.v2.Instance.State - .values()) { - Instance.State modelValue = Instance.State.fromProto(protoValue); - modelToProtoMap.put(modelValue, protoValue); - } - - // Make sure all model values are used - assertThat(modelToProtoMap.keys()).containsAllIn(Arrays.asList(Instance.State.values())); - - // Make sure unknown is handled properly (it has multiple mappings) - assertThat(modelToProtoMap).valuesForKey(Instance.State.NOT_KNOWN).containsExactly( - com.google.bigtable.admin.v2.Instance.State.STATE_NOT_KNOWN, - com.google.bigtable.admin.v2.Instance.State.UNRECOGNIZED - ); - - // Make sure everything else has exactly 1 mapping - modelToProtoMap.removeAll(Instance.State.NOT_KNOWN); + List validModelValues = Lists.newArrayList(Instance.State.values()); - for (Instance.State modelState : modelToProtoMap.keySet()) { - Collection protoStates = modelToProtoMap - .get(modelState); + List actualModelValues = Lists.newArrayList(); - assertThat(protoStates).hasSize(1); + for (com.google.bigtable.admin.v2.Instance.State protoValue : validProtoValues) { + Instance.State modelValue = Instance.State.fromProto(protoValue); + actualModelValues.add(modelValue); } + + assertThat(actualModelValues).containsExactlyElementsIn(validModelValues); } } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java index 9f7fa2d5d06d..c5067712c2dc 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java @@ -17,10 +17,8 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Multimap; -import java.util.Arrays; -import java.util.Collection; +import com.google.common.collect.Lists; +import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -30,32 +28,18 @@ public class StorageTypeTest { @Test public void testUpToDate() { - Multimap modelToProtoMap = - ArrayListMultimap.create(); + List validProtoValues = + Lists.newArrayList(com.google.bigtable.admin.v2.StorageType.values()); - for (com.google.bigtable.admin.v2.StorageType protoValue : com.google.bigtable.admin.v2.StorageType - .values()) { - StorageType modelValue = StorageType.fromProto(protoValue); - modelToProtoMap.put(modelValue, protoValue); - } - - // Make sure all model values are used - assertThat(modelToProtoMap.keys()).containsAllIn(Arrays.asList(StorageType.values())); - - // Make sure unknown is handled properly (it has multiple mappings) - assertThat(modelToProtoMap).valuesForKey(StorageType.NOT_KNOWN).containsExactly( - com.google.bigtable.admin.v2.StorageType.UNRECOGNIZED, - com.google.bigtable.admin.v2.StorageType.STORAGE_TYPE_UNSPECIFIED - ); + List validModelValues = Lists.newArrayList(StorageType.values()); - // Make sure everything else has exactly 1 mapping - modelToProtoMap.removeAll(StorageType.NOT_KNOWN); + List actualModelValues = Lists.newArrayList(); - for (StorageType modelState : modelToProtoMap.keySet()) { - Collection protoStates = modelToProtoMap - .get(modelState); - - assertThat(protoStates).hasSize(1); + for (com.google.bigtable.admin.v2.StorageType protoValue : validProtoValues) { + StorageType modelValue = StorageType.fromProto(protoValue); + actualModelValues.add(modelValue); } + + assertThat(actualModelValues).containsExactlyElementsIn(validModelValues); } } \ No newline at end of file diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java index f2bf65178f04..b5140a31eda7 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableTest.java @@ -21,10 +21,8 @@ import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.Table.TimestampGranularity; import com.google.bigtable.admin.v2.TableName; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Multimap; -import java.util.Arrays; -import java.util.Collection; +import com.google.common.collect.Lists; +import java.util.List; import java.util.Map.Entry; import org.junit.Test; import org.junit.runner.RunWith; @@ -92,33 +90,20 @@ public void testFromProto() { @Test public void testReplicationStateEnumUpToDate() { - Multimap modelToProtoMap = - ArrayListMultimap.create(); + List validProtoValues = + Lists.newArrayList( + com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.values()); - for (com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState protoValue : com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState - .values()) { - Table.ReplicationState modelValue = Table.ReplicationState.fromProto(protoValue); - modelToProtoMap.put(modelValue, protoValue); - } - - // Make sure all model values are used - assertThat(modelToProtoMap.keys()) - .containsAllIn(Arrays.asList(Table.ReplicationState.values())); - - // Make sure unknown is handled properly (it has multiple mappings) - assertThat(modelToProtoMap).valuesForKey(Table.ReplicationState.NOT_KNOWN).containsExactly( - com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.STATE_NOT_KNOWN, - com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState.UNRECOGNIZED - ); - - // Make sure everything else has exactly 1 mapping - modelToProtoMap.removeAll(Table.ReplicationState.NOT_KNOWN); + List validModelValues = Lists + .newArrayList(Table.ReplicationState.values()); - for (Table.ReplicationState modelState : modelToProtoMap.keySet()) { - Collection protoStates = modelToProtoMap - .get(modelState); + List actualModelValues = Lists.newArrayList(); - assertThat(protoStates).hasSize(1); + for (com.google.bigtable.admin.v2.Table.ClusterState.ReplicationState protoValue : validProtoValues) { + Table.ReplicationState modelValue = Table.ReplicationState.fromProto(protoValue); + actualModelValues.add(modelValue); } + + assertThat(actualModelValues).containsExactlyElementsIn(validModelValues); } } From fe8a7821819a3fdbe8987e9813f1c0c04d68e6f9 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Tue, 18 Sep 2018 13:30:29 -0400 Subject: [PATCH 8/9] fix cluster.state & ensure that UNRECOGNIZED can't be set in requests --- .../bigtable/admin/v2/models/Cluster.java | 7 ++-- .../admin/v2/models/CreateClusterRequest.java | 5 ++- .../v2/models/CreateInstanceRequest.java | 1 + .../bigtable/admin/v2/models/ClusterTest.java | 38 ++++++------------- 4 files changed, 20 insertions(+), 31 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java index 7d2bd1e3195e..a3812e91f0eb 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/Cluster.java @@ -51,8 +51,9 @@ public enum State { * The cluster has no backing nodes. The data (tables) still exist, but no operations can be * performed on the cluster. */ - DISABLED(com.google.bigtable.admin.v2.Cluster.State.DISABLED); - + DISABLED(com.google.bigtable.admin.v2.Cluster.State.DISABLED), + /** The state of the cluster is not known by this client. Please upgrade your client. */ + UNRECOGNIZED(com.google.bigtable.admin.v2.Cluster.State.UNRECOGNIZED); private final com.google.bigtable.admin.v2.Cluster.State proto; @@ -67,7 +68,7 @@ public static State fromProto(com.google.bigtable.admin.v2.Cluster.State proto) return state; } } - return NOT_KNOWN; + return UNRECOGNIZED; } State(com.google.bigtable.admin.v2.Cluster.State proto) { diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java index 0ba776668fa9..6d9310f5e5ec 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java @@ -101,7 +101,10 @@ public CreateClusterRequest setServeNodes(int numNodes) { @SuppressWarnings("WeakerAccess") public CreateClusterRequest setStorageType(@Nonnull StorageType storageType) { Preconditions.checkNotNull(storageType); - Preconditions.checkArgument(storageType != StorageType.NOT_KNOWN); + Preconditions.checkArgument(storageType != StorageType.NOT_KNOWN, + "StorageType can't be NOT_KNOWN"); + Preconditions.checkArgument(storageType != StorageType.UNRECOGNIZED, + "StorageType can't be UNRECOGNIZED"); proto.getClusterBuilder().setDefaultStorageType(storageType.toProto()); return this; diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java index 08bf49764c4b..0fab41754555 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateInstanceRequest.java @@ -96,6 +96,7 @@ public CreateInstanceRequest setDisplayName(@Nonnull String displayName) { @SuppressWarnings("WeakerAccess") public CreateInstanceRequest setType(@Nonnull Instance.Type type) { Preconditions.checkNotNull(type); + Preconditions.checkArgument(type != Instance.Type.UNRECOGNIZED, "Type is unrecognized"); builder.getInstanceBuilder().setType(type.toProto()); return this; } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java index 01f7e50c1110..a517739c3814 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/ClusterTest.java @@ -17,10 +17,8 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Multimap; -import java.util.Arrays; -import java.util.Collection; +import com.google.common.collect.Lists; +import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -70,32 +68,18 @@ public void testRequiresName() { @Test public void testStateEnumUpToDate() { - Multimap modelToProtoMap = - ArrayListMultimap.create(); + List validProtoValues = + Lists.newArrayList(com.google.bigtable.admin.v2.Cluster.State.values()); - for (com.google.bigtable.admin.v2.Cluster.State protoValue : com.google.bigtable.admin.v2.Cluster.State - .values()) { - Cluster.State modelValue = Cluster.State.fromProto(protoValue); - modelToProtoMap.put(modelValue, protoValue); - } - - // Make sure all model values are used - assertThat(modelToProtoMap.keys()).containsAllIn(Arrays.asList(Cluster.State.values())); - - // Make sure unknown is handled properly (it has multiple mappings) - assertThat(modelToProtoMap).valuesForKey(Cluster.State.NOT_KNOWN).containsExactly( - com.google.bigtable.admin.v2.Cluster.State.STATE_NOT_KNOWN, - com.google.bigtable.admin.v2.Cluster.State.UNRECOGNIZED - ); + List validModelValues = Lists.newArrayList(Cluster.State.values()); - // Make sure everything else has exactly 1 mapping - modelToProtoMap.removeAll(Cluster.State.NOT_KNOWN); + List actualModelValues = Lists.newArrayList(); - for (Cluster.State modelState : modelToProtoMap.keySet()) { - Collection protoStates = modelToProtoMap - .get(modelState); - - assertThat(protoStates).hasSize(1); + for (com.google.bigtable.admin.v2.Cluster.State protoValue : validProtoValues) { + Cluster.State modelValue = Cluster.State.fromProto(protoValue); + actualModelValues.add(modelValue); } + + assertThat(actualModelValues).containsExactlyElementsIn(validModelValues); } } From b73ec7b0ee65bdbf2d4c8fddc79bf44cc5ec1670 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Tue, 18 Sep 2018 14:08:13 -0400 Subject: [PATCH 9/9] remove unspecified storage type --- .../admin/v2/models/CreateClusterRequest.java | 2 -- .../cloud/bigtable/admin/v2/models/StorageType.java | 4 ++-- .../bigtable/admin/v2/models/StorageTypeTest.java | 13 ++++++++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java index 6d9310f5e5ec..477aaf83d790 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateClusterRequest.java @@ -101,8 +101,6 @@ public CreateClusterRequest setServeNodes(int numNodes) { @SuppressWarnings("WeakerAccess") public CreateClusterRequest setStorageType(@Nonnull StorageType storageType) { Preconditions.checkNotNull(storageType); - Preconditions.checkArgument(storageType != StorageType.NOT_KNOWN, - "StorageType can't be NOT_KNOWN"); Preconditions.checkArgument(storageType != StorageType.UNRECOGNIZED, "StorageType can't be UNRECOGNIZED"); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java index 49f03a59338c..b5dbd34c4ebe 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/StorageType.java @@ -20,8 +20,6 @@ /** Storage media types for persisting Bigtable data. */ public enum StorageType { - /** Storage type could not be determined. */ - NOT_KNOWN(com.google.bigtable.admin.v2.StorageType.STORAGE_TYPE_UNSPECIFIED), /** Flash (SSD) storage should be used. */ HDD(com.google.bigtable.admin.v2.StorageType.HDD), /** Magnetic drive (HDD) storage should be used. */ @@ -38,6 +36,8 @@ public enum StorageType { @InternalApi public static StorageType fromProto(com.google.bigtable.admin.v2.StorageType proto) { Preconditions.checkNotNull(proto); + Preconditions.checkArgument(proto != com.google.bigtable.admin.v2.StorageType.STORAGE_TYPE_UNSPECIFIED, + "Storage type must be specified"); for (StorageType type : values()) { if (type.proto.equals(proto)) { diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java index c5067712c2dc..fd704a7ee486 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/StorageTypeTest.java @@ -25,12 +25,23 @@ @RunWith(JUnit4.class) public class StorageTypeTest { - @Test public void testUpToDate() { List validProtoValues = Lists.newArrayList(com.google.bigtable.admin.v2.StorageType.values()); + // TYPE_UNSPECIFIED is not surfaced + validProtoValues.remove(com.google.bigtable.admin.v2.StorageType.STORAGE_TYPE_UNSPECIFIED); + + Exception actualError = null; + try { + StorageType.fromProto(com.google.bigtable.admin.v2.StorageType.STORAGE_TYPE_UNSPECIFIED); + } catch (Exception e) { + actualError = e; + } + assertThat(actualError).isInstanceOf(IllegalArgumentException.class); + + List validModelValues = Lists.newArrayList(StorageType.values()); List actualModelValues = Lists.newArrayList();