Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exists method added in BigtableInstanceAdminClient #3891

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.gax.rpc.ApiExceptions;
import com.google.api.gax.rpc.NotFoundException;
import com.google.api.resourcenames.ResourceName;
import com.google.bigtable.admin.v2.AppProfileName;
import com.google.bigtable.admin.v2.ClusterName;
Expand Down Expand Up @@ -417,6 +418,68 @@ public Void apply(Empty input) {
);
}

/**
* Checks if the instance specified by the instanceId exists
*
* <p>Sample code:
*
* <pre>{@code
* if(client.exists("my-instance")) {
* System.out.println("Instance exists");
* }
* }</pre>
*/
public boolean exists(String instanceId) {
return ApiExceptions.callAndTranslateApiException(existsAsync(instanceId));
}

/**
* Asynchronously checks if the instance specified by the instanceId exists
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Boolean> found = client.existsAsync("my-instance");
*
* ApiFutures.addCallback(
* found,
* new ApiFutureCallback<Boolean>() {
* public void onSuccess(Boolean found) {
* if (found) {
* System.out.println("Instance exists");
* } else {
* System.out.println("Instance not found");
* }
* }
*
* public void onFailure(Throwable t) {
* t.printStackTrace();
* }
* },
* MoreExecutors.directExecutor()
* );
* }</pre>
*/
public ApiFuture<Boolean> existsAsync(String instanceId) {
ApiFuture<Instance> protoFuture = getInstanceAsync(instanceId);

ApiFuture<Boolean> existsFuture = ApiFutures
.transform(protoFuture, new ApiFunction<Instance, Boolean>() {
chingor13 marked this conversation as resolved.
Show resolved Hide resolved
@Override
public Boolean apply(Instance ignored) {
return true;
}
}, MoreExecutors.directExecutor());

return ApiFutures.catching(existsFuture, NotFoundException.class,
new ApiFunction<NotFoundException, Boolean>() {
chingor13 marked this conversation as resolved.
Show resolved Hide resolved
@Override
public Boolean apply(NotFoundException ignored) {
return false;
}
}, MoreExecutors.directExecutor());
}

/**
* Creates a new cluster in the specified instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.longrunning.OperationFutures;
import com.google.api.gax.longrunning.OperationSnapshot;
import com.google.api.gax.rpc.NotFoundException;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.gax.rpc.testing.FakeOperationSnapshot;
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.GetInstanceRequest;
import com.google.bigtable.admin.v2.InstanceName;
import com.google.bigtable.admin.v2.LocationName;
import com.google.bigtable.admin.v2.ProjectName;
Expand Down Expand Up @@ -57,12 +59,14 @@
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;
import com.google.protobuf.FieldMask;
import io.grpc.Status;
import io.grpc.Status.Code;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
Expand Down Expand Up @@ -842,4 +846,36 @@ public void testTestIamPermissions() {
// Verify
assertThat(actualResult).containsExactly("bigtable.tables.readRows");
}

@Test
public void testExistsTrue() {
// Setup
com.google.bigtable.admin.v2.Instance expectedResponse = com.google.bigtable.admin.v2.Instance
.newBuilder().setName(INSTANCE_NAME.toString()).build();

Mockito.when(mockGetInstanceCallable.futureCall(Matchers.any(GetInstanceRequest.class)))
.thenReturn(ApiFutures.immediateFuture(expectedResponse));

// Execute
boolean found = adminClient.exists(INSTANCE_NAME.getInstance());

// Verify
assertThat(found).isTrue();
}

@Test
public void testExistsFalse() {
// Setup
NotFoundException exception =
new NotFoundException("fake-error", null, GrpcStatusCode.of(Status.Code.NOT_FOUND), false);

Mockito.when(mockGetInstanceCallable.futureCall(Matchers.any(GetInstanceRequest.class)))
.thenReturn(ApiFutures.<com.google.bigtable.admin.v2.Instance>immediateFailedFuture(exception));

// Execute
boolean found = adminClient.exists(INSTANCE_NAME.getInstance());

// Verify
assertThat(found).isFalse();
}
}