Skip to content

Commit

Permalink
fix: fix lint errors
Browse files Browse the repository at this point in the history
Change-Id: If6398a89dbe81fcb98df94ead36b0b13f14f5605
  • Loading branch information
ad548 committed Mar 13, 2021
1 parent 7047488 commit ad27676
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 53 deletions.
Expand Up @@ -32,13 +32,9 @@
public class Cluster {

public enum State {
/**
* The state of the cluster could not be determined.
*/
/** 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.
*/
/** 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
Expand All @@ -57,9 +53,7 @@ public enum State {
* performed on the cluster.
*/
DISABLED(com.google.bigtable.admin.v2.Cluster.State.DISABLED),
/**
* The state of the cluster is not known by this client. Please upgrade your client.
*/
/** 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;
Expand Down Expand Up @@ -92,8 +86,7 @@ public com.google.bigtable.admin.v2.Cluster.State toProto() {
}
}

@Nonnull
private final com.google.bigtable.admin.v2.Cluster stateProto;
@Nonnull private final com.google.bigtable.admin.v2.Cluster stateProto;

/**
* Wraps a protobuf response.
Expand All @@ -112,9 +105,7 @@ private Cluster(@Nonnull com.google.bigtable.admin.v2.Cluster proto) {
this.stateProto = proto;
}

/**
* Gets the cluster's id.
*/
/** Gets the cluster's id. */
@SuppressWarnings("WeakerAccess")
public String getId() {
// Constructor ensures that name is not null
Expand All @@ -124,9 +115,7 @@ public String getId() {
return fullName.getCluster();
}

/**
* Gets the instance id.
*/
/** Gets the instance id. */
@SuppressWarnings("WeakerAccess")
public String getInstanceId() {
// Constructor ensures that name is not null
Expand All @@ -136,19 +125,15 @@ public String getInstanceId() {
return fullName.getInstance();
}

/**
* Get the zone where this cluster is located.
*/
/** Get the zone where this cluster is located. */
@SuppressWarnings("WeakerAccess")
public String getZone() {
LocationName location = Verify.verifyNotNull(LocationName.parse(stateProto.getLocation()));
//noinspection ConstantConditions
return location.getLocation();
}

/**
* Gets the current state of the cluster
*/
/** Gets the current state of the cluster */
@SuppressWarnings("WeakerAccess")
public State getState() {
return State.fromProto(stateProto.getState());
Expand Down
Expand Up @@ -25,8 +25,8 @@
/**
* Parameters for creating a new Bigtable cluster.
*
* <p>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
* <p>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.
*
* <p>Each cluster is located in a single zone. An instance's clusters must be in unique zones that
Expand All @@ -46,7 +46,7 @@
* }</pre>
*
* @see <a href="https://cloud.google.com/bigtable/docs/instances-clusters-nodes#clusters">For more
* details</a>
* details</a>
*/
public final class CreateClusterRequest {

Expand Down
Expand Up @@ -157,7 +157,7 @@ public CreateInstanceRequest addCluster(
* @param storageType the type of storage used by this cluster to serve its parent instance's
* tables.
* @param kmsKeyName the full name of the KMS key name to use in the format
* `projects/{key_project_id}/locations/{location}/keyRings/{ring_name}/cryptoKeys/{key_name}`
* `projects/{key_project_id}/locations/{location}/keyRings/{ring_name}/cryptoKeys/{key_name}`
*/
public CreateInstanceRequest addCmekCluster(
@Nonnull String clusterId,
Expand Down
Expand Up @@ -24,7 +24,6 @@
*
* <p>If this resource is protected with customer managed encryption, the in-use Google Cloud Key
* Management Service (KMS) key versions will be specified along with their status.
*
*/
public final class EncryptionInfo {
public enum Type {
Expand Down
Expand Up @@ -63,8 +63,7 @@ public class BigtableCmekIT {
private static final Logger LOGGER = Logger.getLogger(BigtableCmekIT.class.getName());
private static final String TEST_TABLE_ID = "test-table-for-cmek-it";

@ClassRule
public static TestEnvRule testEnvRule = new TestEnvRule();
@ClassRule public static TestEnvRule testEnvRule = new TestEnvRule();

private String instanceId;
private String clusterId;
Expand Down Expand Up @@ -122,12 +121,14 @@ public void instanceAndClusterTest() throws Exception {
final String secondClusterId = instanceId + "-c2";
String zoneId = testEnvRule.env().getPrimaryZone();

// With a KMS_KEY created and specified using `bigtable.kms_key_name` env variable, create a CMEK protected instance
// With a KMS_KEY created and specified using `bigtable.kms_key_name` env variable, create a
// CMEK protected instance
instanceAdmin.createInstance(
CreateInstanceRequest.of(instanceId)
.addCmekCluster(clusterId, zoneId, 1, StorageType.SSD, kmsKeyName));

// Keys are specified per-cluster with each cluster requesting the same key and the cluster's zone must be within the region of the key
// Keys are specified per-cluster with each cluster requesting the same key and the cluster's
// zone must be within the region of the key
Cluster cluster = instanceAdmin.getCluster(instanceId, clusterId);
assertThat(cluster.getKmsKeyName()).isEqualTo(kmsKeyName);

Expand All @@ -144,17 +145,21 @@ public void instanceAndClusterTest() throws Exception {

final String nonPrimaryRegionZoneId = testEnvRule.env().getSecondaryZone();
try {
instanceAdmin.createCluster(CreateClusterRequest.of(instanceId, secondClusterId)
.setZone(nonPrimaryRegionZoneId)
.setServeNodes(1)
.setStorageType(StorageType.SSD)
.setKmsKeyName(kmsKeyName));
instanceAdmin.createCluster(
CreateClusterRequest.of(instanceId, secondClusterId)
.setZone(nonPrimaryRegionZoneId)
.setServeNodes(1)
.setStorageType(StorageType.SSD)
.setKmsKeyName(kmsKeyName));
} catch (com.google.api.gax.rpc.FailedPreconditionException e) {
assertThat(e.getMessage()).contains(
"FAILED_PRECONDITION: Error in field 'cluster' : "
+ "Error in field 'encryption_config.kms_key_name' : CMEK key " + kmsKeyName
+ " cannot be used to protect a cluster in zone " + NameUtil
.formatLocationName(testEnvRule.env().getProjectId(), nonPrimaryRegionZoneId));
assertThat(e.getMessage())
.contains(
"FAILED_PRECONDITION: Error in field 'cluster' : "
+ "Error in field 'encryption_config.kms_key_name' : CMEK key "
+ kmsKeyName
+ " cannot be used to protect a cluster in zone "
+ NameUtil.formatLocationName(
testEnvRule.env().getProjectId(), nonPrimaryRegionZoneId));
}
}

Expand All @@ -181,7 +186,8 @@ public void tableTest() throws Exception {
if (testEnvRule.env().shouldWaitForCmekKeyStatusUpdate()) {
assertThat(encryptionInfo.getStatus().getCode()).isEqualTo(Status.Code.OK);
}
// For up to 5 minutes after a table is newly created, the key version and status fields are not populated.
// For up to 5 minutes after a table is newly created, the key version and status fields are not
// populated.
// Set the `bigtable.wait-for-cmek-key-status` system property to `true` when running the test
// in order to poll until the final state can be asserted.
if (encryptionInfo.getStatus().getCode() == Code.UNKNOWN) {
Expand All @@ -204,7 +210,8 @@ public void backupTest() throws Exception {
tableAdmin.createTable(CreateTableRequest.of(TEST_TABLE_ID).addFamily("cf"));

// Create a backup.
// Backups are pinned to the primary version of their table's CMEK key at the time they are taken
// Backups are pinned to the primary version of their table's CMEK key at the time they are
// taken
tableAdmin.createBackup(
CreateBackupRequest.of(clusterId, backupId)
.setExpireTime(Instant.now().plus(6, ChronoUnit.HOURS))
Expand Down Expand Up @@ -232,8 +239,12 @@ private void waitForCmekStatus(String tableId, String clusterId) throws Interrup
}
} catch (ApiException ex) {
LOGGER.info(
"Wait for " + BACKOFF_DURATION[i] + " seconds for key status for table " + tableId
+ " and cluster " + clusterId);
"Wait for "
+ BACKOFF_DURATION[i]
+ " seconds for key status for table "
+ tableId
+ " and cluster "
+ clusterId);
}
Thread.sleep(BACKOFF_DURATION[i] * 1000);
}
Expand Down
Expand Up @@ -47,6 +47,7 @@ public void testFromProto() {
assertThat(result.getState()).isEqualTo(Cluster.State.READY);
assertThat(result.getServeNodes()).isEqualTo(30);
assertThat(result.getStorageType()).isEqualTo(StorageType.SSD);
assertThat(result.getKmsKeyName()).isEqualTo(null);
}

@Test
Expand Down
Expand Up @@ -20,13 +20,15 @@

import com.google.bigtable.admin.v2.EncryptionInfo.EncryptionType;
import com.google.cloud.bigtable.common.Status;
import com.google.common.base.Objects;
import com.google.rpc.Code;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class EncryptionInfoTest {

@Test
public void testAllTypes() {
for (EncryptionType protoValue :
Expand All @@ -47,16 +49,18 @@ public void testFromProto() {
.setMessage("kms is unavailable")
.build();

EncryptionInfo encryptionInfo =
EncryptionInfo.fromProto(
com.google.bigtable.admin.v2.EncryptionInfo.newBuilder()
.setEncryptionType(EncryptionType.CUSTOMER_MANAGED_ENCRYPTION)
.setKmsKeyVersion("some version")
.setEncryptionStatus(protoStatus)
.build());
com.google.bigtable.admin.v2.EncryptionInfo proto =
com.google.bigtable.admin.v2.EncryptionInfo.newBuilder()
.setEncryptionType(EncryptionType.CUSTOMER_MANAGED_ENCRYPTION)
.setKmsKeyVersion("some version")
.setEncryptionStatus(protoStatus)
.build();
EncryptionInfo encryptionInfo = EncryptionInfo.fromProto(proto);

assertThat(encryptionInfo.getStatus()).isEqualTo(Status.fromProto(protoStatus));
assertThat(encryptionInfo.getType()).isEqualTo(EncryptionInfo.Type.CUSTOMER_MANAGED_ENCRYPTION);
assertThat(encryptionInfo.getKmsKeyVersion()).isEqualTo("some version");
assertThat(encryptionInfo.toString()).isEqualTo(proto.toString());
assertThat(encryptionInfo.hashCode()).isEqualTo(Objects.hashCode(proto));
}
}
Expand Up @@ -25,6 +25,7 @@

@RunWith(JUnit4.class)
public class StatusTest {

@Test
public void testAllCodes() {
for (Code protoValue : com.google.rpc.Code.values()) {
Expand Down Expand Up @@ -62,4 +63,20 @@ public void testFromProto() {
assertThat(model.getCode()).isEqualTo(Status.Code.UNAVAILABLE);
assertThat(model.getMessage()).isEqualTo("some message");
}

@Test
public void testToProto() {
com.google.rpc.Code code = Code.UNAVAILABLE;
com.google.rpc.Status proto =
com.google.rpc.Status.newBuilder()
.setCode(code.getNumber())
.setMessage("some message")
.build();

Status model = Status.fromProto(proto);
assertThat(model.getCode().toProto()).isEqualTo(code);
assertThat(model.toProto()).isEqualTo(proto);

assertThat(model.toString()).isEqualTo(proto.toString());
}
}

0 comments on commit ad27676

Please sign in to comment.