diff --git a/.kokoro/continuous/bigtableadmin-it.cfg b/.kokoro/continuous/bigtableadmin-it.cfg index 697ae187e3c4..5625893dd94b 100644 --- a/.kokoro/continuous/bigtableadmin-it.cfg +++ b/.kokoro/continuous/bigtableadmin-it.cfg @@ -8,7 +8,7 @@ env_vars: { env_vars: { key: "INTEGRATION_TEST_ARGS" - value: "google-cloud-clients/google-cloud-bigtable-admin -Dbigtable.instance=projects/gcloud-devel/instances/google-cloud-bigtable" + value: "google-cloud-clients/google-cloud-bigtable-admin -Dbigtable.project=gcloud-devel -Dbigtable.instance=google-cloud-bigtable" } env_vars: { diff --git a/.kokoro/nightly/bigtableadmin-it.cfg b/.kokoro/nightly/bigtableadmin-it.cfg index 697ae187e3c4..5625893dd94b 100644 --- a/.kokoro/nightly/bigtableadmin-it.cfg +++ b/.kokoro/nightly/bigtableadmin-it.cfg @@ -8,7 +8,7 @@ env_vars: { env_vars: { key: "INTEGRATION_TEST_ARGS" - value: "google-cloud-clients/google-cloud-bigtable-admin -Dbigtable.instance=projects/gcloud-devel/instances/google-cloud-bigtable" + value: "google-cloud-clients/google-cloud-bigtable-admin -Dbigtable.project=gcloud-devel -Dbigtable.instance=google-cloud-bigtable" } env_vars: { diff --git a/.kokoro/presubmit/bigtableadmin-it.cfg b/.kokoro/presubmit/bigtableadmin-it.cfg index 697ae187e3c4..5625893dd94b 100644 --- a/.kokoro/presubmit/bigtableadmin-it.cfg +++ b/.kokoro/presubmit/bigtableadmin-it.cfg @@ -8,7 +8,7 @@ env_vars: { env_vars: { key: "INTEGRATION_TEST_ARGS" - value: "google-cloud-clients/google-cloud-bigtable-admin -Dbigtable.instance=projects/gcloud-devel/instances/google-cloud-bigtable" + value: "google-cloud-clients/google-cloud-bigtable-admin -Dbigtable.project=gcloud-devel -Dbigtable.instance=google-cloud-bigtable" } env_vars: { diff --git a/TESTING.md b/TESTING.md index a3375d13e45c..1b37a4b0d4eb 100644 --- a/TESTING.md +++ b/TESTING.md @@ -79,7 +79,8 @@ To run the tests: created earlier. Example: ```shell mvn verify -am -pl google-cloud-bigtable-admin \ - -Dbigtable.instance=projects/my-project/instances/my-instance + -Dbigtable.project=my-project + -Dbigtable.instance=my-instance ``` ### Testing code that uses Compute diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminClient.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminClient.java index ccec11154fdc..7c673bae7b1d 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminClient.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminClient.java @@ -22,18 +22,14 @@ 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; import com.google.bigtable.admin.v2.DeleteAppProfileRequest; import com.google.bigtable.admin.v2.GetAppProfileRequest; -import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ListAppProfilesRequest; -import com.google.bigtable.admin.v2.LocationName; -import com.google.bigtable.admin.v2.ProjectName; import com.google.cloud.Policy; import com.google.cloud.Policy.DefaultMarshaller; import com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPagedResponse; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.cloud.bigtable.admin.v2.models.AppProfile; import com.google.cloud.bigtable.admin.v2.models.Cluster; import com.google.cloud.bigtable.admin.v2.models.CreateAppProfileRequest; @@ -57,7 +53,6 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; -import java.util.Objects; import javax.annotation.Nonnull; /** @@ -67,7 +62,7 @@ *
See the individual methods for example code. * *
{@code - * try(BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create(ProjectName.of("my-project"))) { + * try(BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create("my-project")) { * CreateInstanceRequest request = CreateInstanceRequest.of("my-instance") * .addCluster("my-cluster", "us-east1-c", 3, StorageType.SSD); * @@ -85,7 +80,7 @@ * *{@code * BigtableInstanceAdminSettings settings = BigtableInstanceAdminSettings.newBuilder() - * .setProjectName(ProjectName.of("my-project")) + * .setProjectId("my-project") * .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) * .build(); * @@ -96,7 +91,7 @@ * *{@code * BigtableInstanceAdminSettings settings = BigtableInstanceAdminSettings.newBuilder() - * .setProjectName(ProjectName.of("my-project")) + * .setProjectId("my-project") * .setEndpoint(myEndpoint) * .build(); * @@ -104,40 +99,73 @@ * }*/ public final class BigtableInstanceAdminClient implements AutoCloseable { - private final ProjectName projectName; + + private final String projectId; private final BigtableInstanceAdminStub stub; - /** Constructs an instance of BigtableInstanceAdminClient with the given ProjectName. */ - public static BigtableInstanceAdminClient create(@Nonnull ProjectName projectName) - throws IOException { - return create(BigtableInstanceAdminSettings.newBuilder().setProjectName(projectName).build()); + /** Constructs an instance of BigtableInstanceAdminClient with the given project id. */ + public static BigtableInstanceAdminClient create(@Nonnull String projectId) throws IOException { + return create(BigtableInstanceAdminSettings.newBuilder().setProjectId(projectId).build()); + } + + /** + * Constructs an instance of BigtableInstanceAdminClient with the given project id. + * + * @deprecated Please use {@link #create(String)}. + */ + @Deprecated + public static BigtableInstanceAdminClient create( + @Nonnull com.google.bigtable.admin.v2.ProjectName projectName) throws IOException { + return create(projectName.getProject()); } /** Constructs an instance of BigtableInstanceAdminClient with the given settings. */ public static BigtableInstanceAdminClient create(@Nonnull BigtableInstanceAdminSettings settings) throws IOException { - return create(settings.getProjectName(), settings.getStubSettings().createStub()); + return create(settings.getProjectId(), settings.getStubSettings().createStub()); + } + + /** Constructs an instance of BigtableInstanceAdminClient with the given project id and stub. */ + public static BigtableInstanceAdminClient create( + @Nonnull String projectId, @Nonnull BigtableInstanceAdminStub stub) { + return new BigtableInstanceAdminClient(projectId, stub); } - /** Constructs an instance of BigtableInstanceAdminClient with the given ProjectName and stub. */ + /** + * Constructs an instance of BigtableInstanceAdminClient with the given ProjectName and stub. + * + * @deprecated Please use {@link #create(String, BigtableInstanceAdminStub)}. + */ + @Deprecated public static BigtableInstanceAdminClient create( - @Nonnull ProjectName projectName, @Nonnull BigtableInstanceAdminStub stub) { - return new BigtableInstanceAdminClient(projectName, stub); + @Nonnull com.google.bigtable.admin.v2.ProjectName projectName, + @Nonnull BigtableInstanceAdminStub stub) { + return create(projectName.getProject(), stub); } private BigtableInstanceAdminClient( - @Nonnull ProjectName projectName, @Nonnull BigtableInstanceAdminStub stub) { - this.projectName = projectName; + @Nonnull String projectId, @Nonnull BigtableInstanceAdminStub stub) { + this.projectId = projectId; this.stub = stub; } - /** Gets the ProjectName this client is associated with. */ + /** Gets the project id this client is associated with. */ + public String getProjectId() { + return projectId; + } + + /** + * Gets the ProjectName this client is associated with. + * + * @deprecated Please use {@link #getProjectId()}. + */ + @Deprecated @SuppressWarnings("WeakerAccess") - public ProjectName getProjectName() { - return projectName; + public com.google.bigtable.admin.v2.ProjectName getProjectName() { + return com.google.bigtable.admin.v2.ProjectName.of(projectId); } - /** Closes the client and frees all resources associated with it (like thread pools) */ + /** Closes the client and frees all resources associated with it (like thread pools). */ @Override public void close() { stub.close(); @@ -181,7 +209,7 @@ public Instance createInstance(CreateInstanceRequest request) { @SuppressWarnings("WeakerAccess") public ApiFuturecreateInstanceAsync(CreateInstanceRequest request) { return ApiFutures.transform( - stub.createInstanceOperationCallable().futureCall(request.toProto(projectName)), + stub.createInstanceOperationCallable().futureCall(request.toProto(projectId)), new ApiFunction () { @Override public Instance apply(com.google.bigtable.admin.v2.Instance proto) { @@ -229,7 +257,7 @@ public Instance updateInstance(UpdateInstanceRequest request) { @SuppressWarnings("WeakerAccess") public ApiFuture updateInstanceAsync(UpdateInstanceRequest request) { return ApiFutures.transform( - stub.partialUpdateInstanceOperationCallable().futureCall(request.toProto(projectName)), + stub.partialUpdateInstanceOperationCallable().futureCall(request.toProto(projectId)), new ApiFunction () { @Override public Instance apply(com.google.bigtable.admin.v2.Instance proto) { @@ -265,12 +293,10 @@ public Instance getInstance(String id) { */ @SuppressWarnings("WeakerAccess") public ApiFuture getInstanceAsync(String instanceId) { - InstanceName name = InstanceName.of(projectName.getProject(), instanceId); + String name = NameUtil.formatInstanceName(projectId, instanceId); com.google.bigtable.admin.v2.GetInstanceRequest request = - com.google.bigtable.admin.v2.GetInstanceRequest.newBuilder() - .setName(name.toString()) - .build(); + com.google.bigtable.admin.v2.GetInstanceRequest.newBuilder().setName(name).build(); return ApiFutures.transform( stub.getInstanceCallable().futureCall(request), @@ -337,7 +363,7 @@ public List listInstances() { public ApiFuture > listInstancesAsync() { com.google.bigtable.admin.v2.ListInstancesRequest request = com.google.bigtable.admin.v2.ListInstancesRequest.newBuilder() - .setParent(projectName.toString()) + .setParent(NameUtil.formatProjectName(projectId)) .build(); ApiFuture
responseFuture = @@ -362,8 +388,7 @@ public List apply(com.google.bigtable.admin.v2.ListInstancesResponse p ImmutableList.Builder failedZones = ImmutableList.builder(); for (String locationStr : proto.getFailedLocationsList()) { - LocationName fullLocation = Objects.requireNonNull(LocationName.parse(locationStr)); - failedZones.add(fullLocation.getLocation()); + failedZones.add(NameUtil.extractZoneIdFromLocationName(locationStr)); } if (!failedZones.build().isEmpty()) { @@ -402,11 +427,11 @@ public void deleteInstance(String instanceId) { */ @SuppressWarnings("WeakerAccess") public ApiFuture deleteInstanceAsync(String instanceId) { - InstanceName instanceName = InstanceName.of(projectName.getProject(), instanceId); + String instanceName = NameUtil.formatInstanceName(projectId, instanceId); com.google.bigtable.admin.v2.DeleteInstanceRequest request = com.google.bigtable.admin.v2.DeleteInstanceRequest.newBuilder() - .setName(instanceName.toString()) + .setName(instanceName) .build(); return ApiFutures.transform( @@ -526,7 +551,7 @@ public Cluster createCluster(CreateClusterRequest request) { @SuppressWarnings("WeakerAccess") public ApiFuture createClusterAsync(CreateClusterRequest request) { return ApiFutures.transform( - stub.createClusterOperationCallable().futureCall(request.toProto(projectName)), + stub.createClusterOperationCallable().futureCall(request.toProto(projectId)), new ApiFunction () { @Override public Cluster apply(com.google.bigtable.admin.v2.Cluster proto) { @@ -562,12 +587,10 @@ public Cluster getCluster(String instanceId, String clusterId) { */ @SuppressWarnings("WeakerAccess") public ApiFuture getClusterAsync(String instanceId, String clusterId) { - ClusterName name = ClusterName.of(projectName.getProject(), instanceId, clusterId); + String name = NameUtil.formatClusterName(projectId, instanceId, clusterId); com.google.bigtable.admin.v2.GetClusterRequest request = - com.google.bigtable.admin.v2.GetClusterRequest.newBuilder() - .setName(name.toString()) - .build(); + com.google.bigtable.admin.v2.GetClusterRequest.newBuilder().setName(name).build(); return ApiFutures.transform( stub.getClusterCallable().futureCall(request), @@ -632,11 +655,9 @@ public List listClusters(String instanceId) { */ @SuppressWarnings("WeakerAccess") public ApiFuture > listClustersAsync(String instanceId) { - InstanceName name = InstanceName.of(projectName.getProject(), instanceId); + String name = NameUtil.formatInstanceName(projectId, instanceId); com.google.bigtable.admin.v2.ListClustersRequest request = - com.google.bigtable.admin.v2.ListClustersRequest.newBuilder() - .setParent(name.toString()) - .build(); + com.google.bigtable.admin.v2.ListClustersRequest.newBuilder().setParent(name).build(); return ApiFutures.transform( stub.listClustersCallable().futureCall(request), @@ -657,8 +678,7 @@ public List
apply(com.google.bigtable.admin.v2.ListClustersResponse pro ImmutableList.Builder failedZones = ImmutableList.builder(); for (String locationStr : proto.getFailedLocationsList()) { - LocationName fullLocation = Objects.requireNonNull(LocationName.parse(locationStr)); - failedZones.add(fullLocation.getLocation()); + failedZones.add(NameUtil.extractZoneIdFromLocationName(locationStr)); } if (!failedZones.build().isEmpty()) { @@ -700,11 +720,11 @@ public Cluster resizeCluster(String instanceId, String clusterId, int numServeNo public ApiFuture resizeClusterAsync( String instanceId, String clusterId, int numServeNodes) { - ClusterName name = ClusterName.of(projectName.getProject(), instanceId, clusterId); + String name = NameUtil.formatClusterName(projectId, instanceId, clusterId); com.google.bigtable.admin.v2.Cluster request = com.google.bigtable.admin.v2.Cluster.newBuilder() - .setName(name.toString()) + .setName(name) .setServeNodes(numServeNodes) .build(); @@ -748,7 +768,7 @@ public void deleteCluster(String instanceId, String clusterId) { */ @SuppressWarnings("WeakerAccess") public ApiFuture deleteClusterAsync(String instanceId, String clusterId) { - ClusterName name = ClusterName.of(projectName.getProject(), instanceId, clusterId); + String name = NameUtil.formatClusterName(projectId, instanceId, clusterId); com.google.bigtable.admin.v2.DeleteClusterRequest request = com.google.bigtable.admin.v2.DeleteClusterRequest.newBuilder() @@ -804,7 +824,7 @@ public AppProfile createAppProfile(CreateAppProfileRequest request) { @SuppressWarnings("WeakerAccess") public ApiFuture createAppProfileAsync(CreateAppProfileRequest request) { return ApiFutures.transform( - stub.createAppProfileCallable().futureCall(request.toProto(projectName)), + stub.createAppProfileCallable().futureCall(request.toProto(projectId)), new ApiFunction () { @Override public AppProfile apply(com.google.bigtable.admin.v2.AppProfile proto) { @@ -845,7 +865,7 @@ public AppProfile getAppProfile(String instanceId, String appProfileId) { */ @SuppressWarnings("WeakerAccess") public ApiFuture getAppProfileAsync(String instanceId, String appProfileId) { - AppProfileName name = AppProfileName.of(projectName.getProject(), instanceId, appProfileId); + String name = NameUtil.formatAppProfileName(projectId, instanceId, appProfileId); GetAppProfileRequest request = GetAppProfileRequest.newBuilder().setName(name.toString()).build(); @@ -892,10 +912,10 @@ public List listAppProfiles(String instanceId) { */ @SuppressWarnings("WeakerAccess") public ApiFuture > listAppProfilesAsync(String instanceId) { - InstanceName instanceName = InstanceName.of(projectName.getProject(), instanceId); + String instanceName = NameUtil.formatInstanceName(projectId, instanceId); ListAppProfilesRequest request = - ListAppProfilesRequest.newBuilder().setParent(instanceName.toString()).build(); + ListAppProfilesRequest.newBuilder().setParent(instanceName).build(); // TODO(igorbernstein2): try to upstream pagination spooling or figure out a way to expose the // paginated responses while maintaining the wrapper facade. @@ -1005,7 +1025,7 @@ public AppProfile updateAppProfile(UpdateAppProfileRequest request) { @SuppressWarnings("WeakerAccess") public ApiFuture
updateAppProfileAsync(UpdateAppProfileRequest request) { return ApiFutures.transform( - stub.updateAppProfileOperationCallable().futureCall(request.toProto(projectName)), + stub.updateAppProfileOperationCallable().futureCall(request.toProto(projectId)), new ApiFunction () { @Override public AppProfile apply(com.google.bigtable.admin.v2.AppProfile proto) { @@ -1042,9 +1062,8 @@ public void deleteAppProfile(String instanceId, String appProfileId) { */ @SuppressWarnings("WeakerAccess") public ApiFuture deleteAppProfileAsync(String instanceId, String appProfileId) { - AppProfileName name = AppProfileName.of(projectName.getProject(), instanceId, appProfileId); - DeleteAppProfileRequest request = - DeleteAppProfileRequest.newBuilder().setName(name.toString()).build(); + String name = NameUtil.formatAppProfileName(projectId, instanceId, appProfileId); + DeleteAppProfileRequest request = DeleteAppProfileRequest.newBuilder().setName(name).build(); return ApiFutures.transform( stub.deleteAppProfileCallable().futureCall(request), @@ -1107,10 +1126,9 @@ public Policy getIamPolicy(String instanceId) { */ @SuppressWarnings("WeakerAccess") public ApiFuture getIamPolicyAsync(String instanceId) { - InstanceName name = InstanceName.of(projectName.getProject(), instanceId); + String name = NameUtil.formatInstanceName(projectId, instanceId); - GetIamPolicyRequest request = - GetIamPolicyRequest.newBuilder().setResource(name.toString()).build(); + GetIamPolicyRequest request = GetIamPolicyRequest.newBuilder().setResource(name).build(); final IamPolicyMarshaller marshaller = new IamPolicyMarshaller(); @@ -1180,12 +1198,12 @@ public Policy setIamPolicy(String instanceId, Policy policy) { */ @SuppressWarnings("WeakerAccess") public ApiFuture setIamPolicyAsync(String instanceId, Policy policy) { - InstanceName name = InstanceName.of(projectName.getProject(), instanceId); + String name = NameUtil.formatInstanceName(projectId, instanceId); final IamPolicyMarshaller marshaller = new IamPolicyMarshaller(); SetIamPolicyRequest request = SetIamPolicyRequest.newBuilder() - .setResource(name.toString()) + .setResource(name) .setPolicy(marshaller.toPb(policy)) .build(); @@ -1218,9 +1236,13 @@ public Policy apply(com.google.iam.v1.Policy proto) { * @see Cloud Bigtable * permissions */ - @SuppressWarnings("WeakerAccess") + @SuppressWarnings({"WeakerAccess", "deprecation"}) public List testIamPermission(String instanceId, String... permissions) { - return testIamPermission(InstanceName.of(projectName.getProject(), instanceId), permissions); + // TODO(igorbernstein2): Stop using typesafe names + com.google.bigtable.admin.v2.InstanceName instanceName = + com.google.bigtable.admin.v2.InstanceName.of(projectId, instanceId); + + return testIamPermission(instanceName, permissions); } /** @@ -1250,10 +1272,11 @@ public List testIamPermission(String instanceId, String... permissions) * @see Cloud Bigtable * permissions */ - @SuppressWarnings("WeakerAccess") + @SuppressWarnings({"WeakerAccess", "deprecation"}) public ApiFuture > testIamPermissionAsync(String instanceId, String... permissions) { + // TODO(igorbernstein2): Stop using typesafe names return testIamPermissionAsync( - InstanceName.of(projectName.getProject(), instanceId), permissions); + com.google.bigtable.admin.v2.InstanceName.of(projectId, instanceId), permissions); } /** @@ -1275,7 +1298,9 @@ public ApiFuture
> testIamPermissionAsync(String instanceId, String. * * @see Cloud Bigtable * permissions + * @deprecated Please use {@link #testIamPermission(String, String...)}. */ + @Deprecated @SuppressWarnings("WeakerAccess") public List
testIamPermission(ResourceName resourceName, String... permissions) { return ApiExceptions.callAndTranslateApiException( @@ -1310,6 +1335,7 @@ public List testIamPermission(ResourceName resourceName, String... permi * * @see Cloud Bigtable * permissions + * @deprecated Please use {@link #testIamPermissionAsync(String, String...)} */ @SuppressWarnings("WeakerAccess") public ApiFuture > testIamPermissionAsync( diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminSettings.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminSettings.java index 77cbe102f1ab..e92ff7333289 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminSettings.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminSettings.java @@ -26,14 +26,13 @@ /** * Settings class to configure an instance of {@link BigtableInstanceAdminClient}. * - *
It must be configured with a {@link ProjectName} and can be used to change default RPC - * settings. + *
It must be configured with a project id and can be used to change default RPC settings. * *
Example usage: * *
{@code * BigtableInstanceAdminSettings.Builder settingsBuilder = BigtableInstanceAdminSettings.newBuilder() - * .setProjectName(ProjectName.of("my-project")); + * .setProjectId("my-project"); * * settingsBuilder.stubSettings().createInstanceSettings() * .setRetrySettings( @@ -45,21 +44,32 @@ * }*/ public final class BigtableInstanceAdminSettings { - private final ProjectName projectName; + private final String projectId; private final BigtableInstanceAdminStubSettings stubSettings; private BigtableInstanceAdminSettings(Builder builder) throws IOException { - Preconditions.checkNotNull(builder.projectName, "ProjectName must be set"); + Preconditions.checkNotNull(builder.projectId, "Project ud must be set"); Verify.verifyNotNull(builder.stubSettings, "stubSettings should never be null"); - this.projectName = builder.projectName; + this.projectId = builder.projectId; this.stubSettings = builder.stubSettings.build(); } - /** Gets the anme of the project whose instances the client will manager. */ + /** Gets the id of the project whose instances the client will manage. */ @Nonnull - public ProjectName getProjectName() { - return projectName; + public String getProjectId() { + return projectId; + } + + /** + * Gets the name of the project whose instances the client will manager. + * + * @deprecated Please use {@link #getProjectId()}. + */ + @Deprecated + @Nonnull + public com.google.bigtable.admin.v2.ProjectName getProjectName() { + return ProjectName.of(projectId); } /** Gets the underlying RPC settings. */ @@ -80,7 +90,7 @@ public static Builder newBuilder() { /** Builder for BigtableInstanceAdminSettings. */ public static final class Builder { - @Nullable private ProjectName projectName; + @Nullable private String projectId; private final BigtableInstanceAdminStubSettings.Builder stubSettings; private Builder() { @@ -88,21 +98,45 @@ private Builder() { } private Builder(BigtableInstanceAdminSettings settings) { - this.projectName = settings.projectName; + this.projectId = settings.projectId; this.stubSettings = settings.stubSettings.toBuilder(); } - /** Sets the name of instance whose tables the client will manage. */ - public Builder setProjectName(@Nonnull ProjectName projectName) { - Preconditions.checkNotNull(projectName); - this.projectName = projectName; + /** Sets the id of the project whose instances the client will manage. */ + public Builder setProjectId(@Nonnull String projectId) { + Preconditions.checkNotNull(projectId); + this.projectId = projectId; return this; } - /** Gets the name of the project whose instances the client will manage. */ + /** Gets the id of the project whose instances the client will manage. */ + @Nullable + public String getProjectId() { + return projectId; + } + + /** + * Sets the name of instance whose tables the client will manage. + * + * @deprecated Please use {@link #setProjectId(String)}. + */ + @Deprecated + public Builder setProjectName(@Nonnull com.google.bigtable.admin.v2.ProjectName projectName) { + return setProjectId(projectName.getProject()); + } + + /** + * Gets the name of the project whose instances the client will manage. + * + * @deprecated Please use {@link #getProjectId()}. + */ + @Deprecated @Nullable public ProjectName getProjectName() { - return projectName; + if (projectId != null) { + return ProjectName.of(projectId); + } + return null; } /** diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java index f2b4ddec753b..c4d3b88314f9 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java @@ -24,11 +24,10 @@ import com.google.bigtable.admin.v2.DeleteTableRequest; import com.google.bigtable.admin.v2.DropRowRangeRequest; import com.google.bigtable.admin.v2.GetTableRequest; -import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ListTablesRequest; -import com.google.bigtable.admin.v2.TableName; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest; import com.google.cloud.bigtable.admin.v2.models.Table; @@ -52,7 +51,7 @@ *Sample code to get started: * *
{@code - * try(BigtableTableAdminClient client = BigtableTableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) { + * try(BigtableTableAdminClient client = BigtableTableAdminClient.create("[PROJECT]", "[INSTANCE]")) { * CreateTable request = * CreateTableRequest.of("my-table") * .addFamily("cf1") @@ -73,7 +72,8 @@ * *{@code * BigtableTableAdminSettings tableAdminSettings = BigtableTableAdminSettings.newBuilder() - * .setInstanceName(InstanceName.of("[PROJECT]", "[INSTANCE]")) + * .setProjectId("[PROJECT]") + * .setInstanceId("[INSTANCE]") * .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) * .build(); * @@ -85,20 +85,38 @@ * *{@code * BigtableTableAdminSettings tableAdminSettings = BigtableTableAdminSettings.newBuilder() - * .setInstanceName(InstanceName.of("[PROJECT]", "[INSTANCE]")) + * .setProjectId("[PROJECT]") + * .setInstanceId("[INSTANCE]") * .setEndpoint(myEndpoint).build(); * * BigtableTableAdminClient client = BigtableTableAdminClient.create(tableAdminSettings); * }*/ public final class BigtableTableAdminClient implements AutoCloseable { + private final EnhancedBigtableTableAdminStub stub; - private final InstanceName instanceName; + private final String projectId; + private final String instanceId; - /** Constructs an instance of BigtableTableAdminClient with the given instanceName. */ - public static BigtableTableAdminClient create(@Nonnull InstanceName instanceName) - throws IOException { - return create(BigtableTableAdminSettings.newBuilder().setInstanceName(instanceName).build()); + /** Constructs an instance of BigtableTableAdminClient with the given project and instance ids. */ + public static BigtableTableAdminClient create( + @Nonnull String projectId, @Nonnull String instanceId) throws IOException { + return create( + BigtableTableAdminSettings.newBuilder() + .setProjectId(projectId) + .setInstanceId(instanceId) + .build()); + } + + /** + * Constructs an instance of BigtableTableAdminClient with the given instanceName. + * + * @deprecated Please {@link #create(String, String)}. + */ + @Deprecated + public static BigtableTableAdminClient create( + @Nonnull com.google.bigtable.admin.v2.InstanceName instanceName) throws IOException { + return create(instanceName.getProject(), instanceName.getInstance()); } /** Constructs an instance of BigtableTableAdminClient with the given settings. */ @@ -106,26 +124,47 @@ public static BigtableTableAdminClient create(@Nonnull BigtableTableAdminSetting throws IOException { EnhancedBigtableTableAdminStub stub = EnhancedBigtableTableAdminStub.createEnhanced(settings.getStubSettings()); - return create(settings.getInstanceName(), stub); + return create(settings.getProjectId(), settings.getInstanceId(), stub); } /** Constructs an instance of BigtableTableAdminClient with the given instanceName and stub. */ public static BigtableTableAdminClient create( - @Nonnull InstanceName instanceName, @Nonnull EnhancedBigtableTableAdminStub stub) { - return new BigtableTableAdminClient(instanceName, stub); + @Nonnull String projectId, + @Nonnull String instanceId, + @Nonnull EnhancedBigtableTableAdminStub stub) { + return new BigtableTableAdminClient(projectId, instanceId, stub); } private BigtableTableAdminClient( - @Nonnull InstanceName instanceName, @Nonnull EnhancedBigtableTableAdminStub stub) { - Preconditions.checkNotNull(instanceName); + @Nonnull String projectId, + @Nonnull String instanceId, + @Nonnull EnhancedBigtableTableAdminStub stub) { + Preconditions.checkNotNull(projectId); + Preconditions.checkNotNull(instanceId); Preconditions.checkNotNull(stub); - this.instanceName = instanceName; + this.projectId = projectId; + this.instanceId = instanceId; this.stub = stub; } - /** Gets the instanceName this client is associated with. */ - public InstanceName getInstanceName() { - return instanceName; + /** Gets the project id of the instance whose tables this client manages. */ + public String getProjectId() { + return projectId; + } + + /** Gets the id of the instance whose tables this client manages. */ + public String getInstanceId() { + return instanceId; + } + + /** + * Gets the instanceName this client is associated with. + * + * @deprecated Please use {@link #getProjectId()} and {@link #getInstanceId()}. + */ + @Deprecated + public com.google.bigtable.admin.v2.InstanceName getInstanceName() { + return com.google.bigtable.admin.v2.InstanceName.of(projectId, instanceId); } @Override @@ -183,7 +222,7 @@ public Table createTable(CreateTableRequest request) { @SuppressWarnings("WeakerAccess") public ApiFuturecreateTableAsync(CreateTableRequest request) { return transformToTableResponse( - this.stub.createTableCallable().futureCall(request.toProto(instanceName))); + this.stub.createTableCallable().futureCall(request.toProto(projectId, instanceId))); } /** @@ -275,7 +314,9 @@ public Table modifyFamilies(ModifyColumnFamiliesRequest request) { @SuppressWarnings("WeakerAccess") public ApiFuture
modifyFamiliesAsync(ModifyColumnFamiliesRequest request) { return transformToTableResponse( - this.stub.modifyColumnFamiliesCallable().futureCall(request.toProto(instanceName))); + this.stub + .modifyColumnFamiliesCallable() + .futureCall(request.toProto(projectId, instanceId))); } /** @@ -500,7 +541,9 @@ public List
listTables() { @SuppressWarnings("WeakerAccess") public ApiFuture > listTablesAsync() { ListTablesRequest request = - ListTablesRequest.newBuilder().setParent(instanceName.toString()).build(); + ListTablesRequest.newBuilder() + .setParent(NameUtil.formatInstanceName(projectId, instanceId)) + .build(); // TODO(igorbernstein2): try to upstream pagination spooling or figure out a way to expose the // paginated responses while maintaining the wrapper facade. @@ -550,7 +593,7 @@ public ApiFuture
> apply( public List
apply(List protos) { List results = Lists.newArrayListWithCapacity(protos.size()); for (com.google.bigtable.admin.v2.Table proto : protos) { - results.add(TableName.parse(proto.getName()).getTable()); + results.add(NameUtil.extractTableIdFromTableName(proto.getName())); } return results; } @@ -718,8 +761,10 @@ public ApiFuture dropAllRowsAsync(String tableId) { */ @SuppressWarnings("WeakerAccess") public void awaitReplication(String tableId) { - TableName tableName = - TableName.of(instanceName.getProject(), instanceName.getInstance(), tableId); + // TODO(igorbernstein2): remove usage of typesafe names + com.google.bigtable.admin.v2.TableName tableName = + com.google.bigtable.admin.v2.TableName.of(projectId, instanceId, tableId); + ApiExceptions.callAndTranslateApiException( stub.awaitReplicationCallable().futureCall(tableName)); } @@ -752,8 +797,9 @@ public void awaitReplication(String tableId) { */ @SuppressWarnings("WeakerAccess") public ApiFuture awaitReplicationAsync(final String tableId) { - TableName tableName = - TableName.of(instanceName.getProject(), instanceName.getInstance(), tableId); + // TODO(igorbernstein2): remove usage of trypesafe names + com.google.bigtable.admin.v2.TableName tableName = + com.google.bigtable.admin.v2.TableName.of(projectId, instanceId, tableId); return stub.awaitReplicationCallable().futureCall(tableName); } @@ -762,7 +808,7 @@ public ApiFuture awaitReplicationAsync(final String tableId) { * projects/{project}/instances/{instance}/tables/{tableId} */ private String getTableName(String tableId) { - return TableName.of(instanceName.getProject(), instanceName.getInstance(), tableId).toString(); + return NameUtil.formatTableName(projectId, instanceId, tableId); } // TODO(igorbernstein): rename methods to make clear that they deal with futures. diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminSettings.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminSettings.java index bc17c1df2e1e..f613bf3f7369 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminSettings.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminSettings.java @@ -15,7 +15,6 @@ */ package com.google.cloud.bigtable.admin.v2; -import com.google.bigtable.admin.v2.InstanceName; import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStubSettings; import com.google.common.base.Preconditions; import com.google.common.base.Verify; @@ -26,14 +25,14 @@ /** * Settings class to configure an instance of {@link BigtableTableAdminClient}. * - * It must be configured with an {@link InstanceName} and can be used to change default RPC - * settings. + *
It must be configured with a project id and instance id. * *
Example usage: * *
{@code * BigtableTableAdminSettings.Builder tableAdminSettingsBuilder = BigtableTableAdminSettings.newBuilder() - * .setInstanceName(InstanceName.of("my-project", "my-instance"); + * .setProjectId("my-project") + * .setInstanceId("my-instance"); * * tableAdminSettingsBuilder.stubSettings().createTableSettings() * .setRetrySettings( @@ -45,20 +44,36 @@ * }*/ public final class BigtableTableAdminSettings { - private final InstanceName instanceName; + private final String projectId; + private final String instanceId; private final BigtableTableAdminStubSettings stubSettings; private BigtableTableAdminSettings(Builder builder) throws IOException { - this.instanceName = - Preconditions.checkNotNull(builder.instanceName, "InstanceName must be set"); + this.projectId = Preconditions.checkNotNull(builder.projectId, "Project id must be set"); + this.instanceId = Preconditions.checkNotNull(builder.instanceId, "Instance id must be set"); this.stubSettings = Verify.verifyNotNull(builder.stubSettings, "stubSettings should never be null").build(); } - /** Gets the name of instance whose tables the client will manage. */ + /** Gets the project id of instance whose tables the client will manage. */ + public String getProjectId() { + return projectId; + } + + /** Gets the instance id whose tables the client will manage. */ + public String getInstanceId() { + return instanceId; + } + + /** + * Gets the name of instance whose tables the client will manage. + * + * @deprecated Please use {@link #getProjectId()} and {@link #getInstanceId()}. + */ + @Deprecated @Nonnull - public InstanceName getInstanceName() { - return instanceName; + public com.google.bigtable.admin.v2.InstanceName getInstanceName() { + return com.google.bigtable.admin.v2.InstanceName.of(projectId, instanceId); } /** Gets the underlying RPC settings. */ @@ -78,7 +93,8 @@ public static Builder newBuilder() { /** Builder for BigtableTableAdminSettings. */ public static final class Builder { - @Nullable private InstanceName instanceName; + @Nullable private String projectId; + @Nullable private String instanceId; private final BigtableTableAdminStubSettings.Builder stubSettings; private Builder() { @@ -86,21 +102,63 @@ private Builder() { } private Builder(BigtableTableAdminSettings settings) { - this.instanceName = settings.instanceName; + this.projectId = settings.projectId; + this.instanceId = settings.instanceId; this.stubSettings = settings.stubSettings.toBuilder(); } - /** Sets the name of instance whose tables the client will manage. */ - public Builder setInstanceName(@Nonnull InstanceName instanceName) { + /** Sets the project id of the instance whose tables the client will manage. */ + public Builder setProjectId(@Nullable String projectId) { + Preconditions.checkNotNull(projectId); + this.projectId = projectId; + return this; + } + + /** Gets the project id of the instance whose tables the client will manage. */ + @Nullable + public String getProjectId() { + return projectId; + } + + /** Sets the instance id of the instance whose tables the client will manage. */ + public Builder setInstanceId(@Nullable String instanceId) { + Preconditions.checkNotNull(instanceId); + this.instanceId = instanceId; + return this; + } + + /** Gets the instance id of the instance whose tables the client will manage. */ + @Nullable + public String getInstanceId() { + return instanceId; + } + + /** + * Sets the name of instance whose tables the client will manage. + * + * @deprecated Please use {@link #setProjectId(String)} and {@link #setInstanceId(String)}. + */ + @Deprecated + public Builder setInstanceName( + @Nonnull com.google.bigtable.admin.v2.InstanceName instanceName) { Preconditions.checkNotNull(instanceName); - this.instanceName = instanceName; + this.projectId = instanceName.getProject(); + this.instanceId = instanceName.getInstance(); return this; } - /** Gets the name of instance whose tables the client will manage. */ + /** + * Gets the name of instance whose tables the client will manage. + * + * @deprecated Please use {@link #getProjectId()} and {@link #getInstanceId()}. + */ + @Deprecated @Nullable - public InstanceName getInstanceName() { - return instanceName; + public com.google.bigtable.admin.v2.InstanceName getInstanceName() { + if (projectId != null && instanceId != null) { + return com.google.bigtable.admin.v2.InstanceName.of(projectId, instanceId); + } + return null; } /** diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/internal/NameUtil.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/internal/NameUtil.java new file mode 100644 index 000000000000..54fc1f88fa8a --- /dev/null +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/internal/NameUtil.java @@ -0,0 +1,75 @@ +/* + * Copyright 2019 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.internal; + +import com.google.api.core.InternalApi; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Internal helper to compose full resource names. + * + *This class is considered an internal implementation detail and not meant to be used by + * applications. + */ +@InternalApi +public class NameUtil { + private static final Pattern TABLE_PATTERN = + Pattern.compile("projects/([^/]+)/instances/([^/]+)/tables/([^/]+)"); + private static final Pattern LOCATION_PATTERN = + Pattern.compile("projects/([^/]+)/locations/([^/]+)"); + + public static String formatProjectName(String projectId) { + return "projects/" + projectId; + } + + public static String formatInstanceName(String projectId, String instanceId) { + return formatProjectName(projectId) + "/instances/" + instanceId; + } + + public static String formatTableName(String projectId, String instanceId, String tableId) { + return formatInstanceName(projectId, instanceId) + "/tables/" + tableId; + } + + public static String formatLocationName(String projectId, String zone) { + return formatProjectName(projectId) + "/locations/" + zone; + } + + public static String extractTableIdFromTableName(String fullTableName) { + Matcher matcher = TABLE_PATTERN.matcher(fullTableName); + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid table name: " + fullTableName); + } + return matcher.group(3); + } + + public static String extractZoneIdFromLocationName(String fullLocationName) { + Matcher matcher = LOCATION_PATTERN.matcher(fullLocationName); + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid location name: " + fullLocationName); + } + return matcher.group(2); + } + + public static String formatClusterName(String projectId, String instanceId, String clusterId) { + return formatInstanceName(projectId, instanceId) + "/clusters/" + clusterId; + } + + public static String formatAppProfileName( + String projectId, String instanceId, String appProfileId) { + return formatInstanceName(projectId, instanceId) + "/appProfiles/" + appProfileId; + } +} diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateAppProfileRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateAppProfileRequest.java index 2fbdb373bf04..35c41208bebb 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateAppProfileRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateAppProfileRequest.java @@ -16,8 +16,7 @@ package com.google.cloud.bigtable.admin.v2.models; import com.google.api.core.InternalApi; -import com.google.bigtable.admin.v2.InstanceName; -import com.google.bigtable.admin.v2.ProjectName; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.cloud.bigtable.admin.v2.models.AppProfile.MultiClusterRoutingPolicy; import com.google.cloud.bigtable.admin.v2.models.AppProfile.RoutingPolicy; import com.google.cloud.bigtable.admin.v2.models.AppProfile.SingleClusterRoutingPolicy; @@ -98,9 +97,9 @@ public CreateAppProfileRequest setRoutingPolicy(RoutingPolicy routingPolicy) { * not meant to be used by applications. */ @InternalApi - public com.google.bigtable.admin.v2.CreateAppProfileRequest toProto(ProjectName projectName) { - InstanceName name = InstanceName.of(projectName.getProject(), instanceId); + public com.google.bigtable.admin.v2.CreateAppProfileRequest toProto(String projectId) { + String name = NameUtil.formatInstanceName(projectId, instanceId); - return proto.setParent(name.toString()).build(); + return proto.setParent(name).build(); } } 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 3acd52c43737..4711097d5321 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 @@ -18,9 +18,7 @@ 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.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.common.base.Preconditions; import javax.annotation.Nonnull; @@ -111,11 +109,9 @@ public CreateClusterRequest setStorageType(@Nonnull StorageType storageType) { * not meant to be used by applications. */ @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()); + public com.google.bigtable.admin.v2.CreateClusterRequest toProto(String projectId) { + proto.setParent(NameUtil.formatInstanceName(projectId, instanceId)); + proto.getClusterBuilder().setLocation(NameUtil.formatLocationName(projectId, zone)); return proto.build(); } @@ -140,10 +136,8 @@ String getClusterId() { * applications. */ @InternalApi - com.google.bigtable.admin.v2.Cluster toEmbeddedProto(ProjectName projectName) { - proto - .getClusterBuilder() - .setLocation(LocationName.of(projectName.getProject(), zone).toString()); + com.google.bigtable.admin.v2.Cluster toEmbeddedProto(String projectId) { + proto.getClusterBuilder().setLocation(NameUtil.formatLocationName(projectId, zone)); 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 dd711ffa6d3c..af8b91c8bd28 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 @@ -17,7 +17,7 @@ import com.google.api.core.InternalApi; import com.google.bigtable.admin.v2.Instance.Type; -import com.google.bigtable.admin.v2.ProjectName; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import java.util.List; @@ -151,12 +151,11 @@ public CreateInstanceRequest addCluster( * not meant to be used by applications. */ @InternalApi - public com.google.bigtable.admin.v2.CreateInstanceRequest toProto(ProjectName projectName) { - builder.setParent(projectName.toString()).clearClusters(); + public com.google.bigtable.admin.v2.CreateInstanceRequest toProto(String projectId) { + builder.setParent(NameUtil.formatProjectName(projectId)).clearClusters(); for (CreateClusterRequest clusterRequest : clusterRequests) { - builder.putClusters( - clusterRequest.getClusterId(), clusterRequest.toEmbeddedProto(projectName)); + builder.putClusters(clusterRequest.getClusterId(), clusterRequest.toEmbeddedProto(projectId)); } return builder.build(); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java index a5ee8364d3d4..fbee5785db8c 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/CreateTableRequest.java @@ -17,12 +17,13 @@ import com.google.api.core.InternalApi; import com.google.bigtable.admin.v2.ColumnFamily; -import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.Table; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.protobuf.ByteString; +import javax.annotation.Nonnull; /** * Fluent wrapper for {@link com.google.bigtable.admin.v2.CreateTableRequest} @@ -110,10 +111,13 @@ public int hashCode() { } @InternalApi - public com.google.bigtable.admin.v2.CreateTableRequest toProto(InstanceName instanceName) { - Preconditions.checkNotNull(instanceName); + public com.google.bigtable.admin.v2.CreateTableRequest toProto( + @Nonnull String projectId, @Nonnull String instanceId) { + Preconditions.checkNotNull(projectId); + Preconditions.checkNotNull(instanceId); + return createTableRequest - .setParent(instanceName.toString()) + .setParent(NameUtil.formatInstanceName(projectId, instanceId)) .setTable(tableRequest.build()) .build(); } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ModifyColumnFamiliesRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ModifyColumnFamiliesRequest.java index fd699d41792a..35f02671e590 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ModifyColumnFamiliesRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ModifyColumnFamiliesRequest.java @@ -16,11 +16,11 @@ package com.google.cloud.bigtable.admin.v2.models; import com.google.api.core.InternalApi; -import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; -import com.google.bigtable.admin.v2.TableName; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.cloud.bigtable.admin.v2.models.GCRules.GCRule; import com.google.common.base.Preconditions; +import javax.annotation.Nonnull; /** * Fluent wrapper for {@link com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest} @@ -106,10 +106,11 @@ public ModifyColumnFamiliesRequest dropFamily(String familyId) { @InternalApi public com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest toProto( - InstanceName instanceName) { - Preconditions.checkNotNull(instanceName); - String tableName = - TableName.of(instanceName.getProject(), instanceName.getInstance(), tableId).toString(); + @Nonnull String projectId, @Nonnull String instanceId) { + Preconditions.checkNotNull(projectId, "Project id can't be null"); + Preconditions.checkNotNull(instanceId, "Instance id can't be null"); + + String tableName = NameUtil.formatTableName(projectId, instanceId, tableId); return modFamilyRequest.setName(tableName).build(); } } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateAppProfileRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateAppProfileRequest.java index c6f56d35b0cb..1697b47f761a 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateAppProfileRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateAppProfileRequest.java @@ -16,8 +16,7 @@ package com.google.cloud.bigtable.admin.v2.models; import com.google.api.core.InternalApi; -import com.google.bigtable.admin.v2.AppProfileName; -import com.google.bigtable.admin.v2.ProjectName; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.cloud.bigtable.admin.v2.models.AppProfile.MultiClusterRoutingPolicy; import com.google.cloud.bigtable.admin.v2.models.AppProfile.RoutingPolicy; import com.google.cloud.bigtable.admin.v2.models.AppProfile.SingleClusterRoutingPolicy; @@ -133,10 +132,10 @@ private void updateFieldMask(int fieldNumber) { * not meant to be used by applications. */ @InternalApi - public com.google.bigtable.admin.v2.UpdateAppProfileRequest toProto(ProjectName projectName) { - AppProfileName name = AppProfileName.of(projectName.getProject(), instanceId, appProfileId); + public com.google.bigtable.admin.v2.UpdateAppProfileRequest toProto(String projectId) { + String name = NameUtil.formatAppProfileName(projectId, instanceId, appProfileId); - proto.getAppProfileBuilder().setName(name.toString()); + proto.getAppProfileBuilder().setName(name); return proto.build(); } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateInstanceRequest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateInstanceRequest.java index bbb93703806b..ac64fa1f910c 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateInstanceRequest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/UpdateInstanceRequest.java @@ -18,9 +18,8 @@ import com.google.api.core.InternalApi; import com.google.bigtable.admin.v2.Instance; import com.google.bigtable.admin.v2.Instance.Type; -import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.PartialUpdateInstanceRequest; -import com.google.bigtable.admin.v2.ProjectName; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.common.base.Preconditions; import com.google.protobuf.FieldMask; import com.google.protobuf.util.FieldMaskUtil; @@ -97,14 +96,13 @@ private void updateFieldMask(int fieldNumber) { * not meant to be used by applications. */ @InternalApi - public PartialUpdateInstanceRequest toProto(ProjectName projectName) { + public PartialUpdateInstanceRequest toProto(String projectId) { // Empty field mask implies full resource replacement, which would clear all fields in an empty // update request. Preconditions.checkState( !builder.getUpdateMask().getPathsList().isEmpty(), "Update request is empty"); - InstanceName instanceName = InstanceName.of(projectName.getProject(), instanceId); - builder.getInstanceBuilder().setName(instanceName.toString()); + builder.getInstanceBuilder().setName(NameUtil.formatInstanceName(projectId, instanceId)); return builder.build(); } 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 41846e6cf665..91437fc965ee 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 @@ -27,13 +27,8 @@ 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; import com.google.bigtable.admin.v2.UpdateClusterMetadata; import com.google.bigtable.admin.v2.UpdateInstanceMetadata; import com.google.cloud.Identity; @@ -41,6 +36,7 @@ import com.google.cloud.Role; import com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPagedResponse; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.cloud.bigtable.admin.v2.models.AppProfile; import com.google.cloud.bigtable.admin.v2.models.AppProfile.MultiClusterRoutingPolicy; import com.google.cloud.bigtable.admin.v2.models.Cluster; @@ -76,13 +72,17 @@ @RunWith(MockitoJUnitRunner.class) public class BigtableInstanceAdminClientTest { - private static final ProjectName PROJECT_NAME = ProjectName.of("my-project"); - private static final InstanceName INSTANCE_NAME = - InstanceName.of(PROJECT_NAME.getProject(), "my-instance"); - private static final ClusterName CLUSTER_NAME = - ClusterName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "my-cluster"); - private static final AppProfileName APP_PROFILE_NAME = - AppProfileName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "my-cluster"); + private static final String PROJECT_ID = "my-project"; + private static final String INSTANCE_ID = "my-instance"; + private static final String CLUSTER_ID = "my-cluster"; + private static final String APP_PROFILE_ID = "my-app-profile"; + + private static final String PROJECT_NAME = NameUtil.formatProjectName(PROJECT_ID); + private static final String INSTANCE_NAME = NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID); + private static final String CLUSTER_NAME = + NameUtil.formatClusterName(PROJECT_ID, INSTANCE_ID, CLUSTER_ID); + private static final String APP_PROFILE_NAME = + NameUtil.formatAppProfileName(PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID); private BigtableInstanceAdminClient adminClient; @@ -189,7 +189,7 @@ public class BigtableInstanceAdminClientTest { @Before public void setUp() { - adminClient = BigtableInstanceAdminClient.create(PROJECT_NAME, mockStub); + adminClient = BigtableInstanceAdminClient.create(PROJECT_ID, mockStub); Mockito.when(mockStub.createInstanceOperationCallable()).thenReturn(mockCreateInstanceCallable); Mockito.when(mockStub.partialUpdateInstanceOperationCallable()) @@ -218,7 +218,7 @@ public void setUp() { @Test public void testProjectName() { - assertThat(adminClient.getProjectName()).isEqualTo(PROJECT_NAME); + assertThat(adminClient.getProjectId()).isEqualTo(PROJECT_ID); } @Test @@ -232,12 +232,12 @@ public void testCreateInstance() { // Setup com.google.bigtable.admin.v2.CreateInstanceRequest expectedRequest = com.google.bigtable.admin.v2.CreateInstanceRequest.newBuilder() - .setParent(PROJECT_NAME.toString()) - .setInstanceId(INSTANCE_NAME.getInstance()) + .setParent(PROJECT_NAME) + .setInstanceId(INSTANCE_ID) .setInstance( com.google.bigtable.admin.v2.Instance.newBuilder() .setType(com.google.bigtable.admin.v2.Instance.Type.DEVELOPMENT) - .setDisplayName(INSTANCE_NAME.getInstance())) + .setDisplayName(INSTANCE_ID)) .putClusters( "cluster1", com.google.bigtable.admin.v2.Cluster.newBuilder() @@ -248,16 +248,14 @@ public void testCreateInstance() { .build(); com.google.bigtable.admin.v2.Instance expectedResponse = - com.google.bigtable.admin.v2.Instance.newBuilder() - .setName(INSTANCE_NAME.toString()) - .build(); + com.google.bigtable.admin.v2.Instance.newBuilder().setName(INSTANCE_NAME).build(); mockOperationResult(mockCreateInstanceCallable, expectedRequest, expectedResponse); // Execute Instance actualResult = adminClient.createInstance( - CreateInstanceRequest.of(INSTANCE_NAME.getInstance()) + CreateInstanceRequest.of(INSTANCE_ID) .setType(Instance.Type.DEVELOPMENT) .addCluster("cluster1", "us-east1-c", 1, StorageType.SSD)); @@ -273,22 +271,19 @@ public void testUpdateInstance() { .setUpdateMask(FieldMask.newBuilder().addPaths("display_name")) .setInstance( com.google.bigtable.admin.v2.Instance.newBuilder() - .setName(INSTANCE_NAME.toString()) + .setName(INSTANCE_NAME) .setDisplayName("new display name")) .build(); com.google.bigtable.admin.v2.Instance expectedResponse = - com.google.bigtable.admin.v2.Instance.newBuilder() - .setName(INSTANCE_NAME.toString()) - .build(); + com.google.bigtable.admin.v2.Instance.newBuilder().setName(INSTANCE_NAME).build(); mockOperationResult(mockUpdateInstanceCallable, expectedRequest, expectedResponse); // Execute Instance actualResult = adminClient.updateInstance( - UpdateInstanceRequest.of(INSTANCE_NAME.getInstance()) - .setDisplayName("new display name")); + UpdateInstanceRequest.of(INSTANCE_ID).setDisplayName("new display name")); // Verify assertThat(actualResult).isEqualTo(Instance.fromProto(expectedResponse)); @@ -298,20 +293,16 @@ public void testUpdateInstance() { public void testGetInstance() { // Setup com.google.bigtable.admin.v2.GetInstanceRequest expectedRequest = - com.google.bigtable.admin.v2.GetInstanceRequest.newBuilder() - .setName(INSTANCE_NAME.toString()) - .build(); + com.google.bigtable.admin.v2.GetInstanceRequest.newBuilder().setName(INSTANCE_NAME).build(); com.google.bigtable.admin.v2.Instance expectedResponse = - com.google.bigtable.admin.v2.Instance.newBuilder() - .setName(INSTANCE_NAME.toString()) - .build(); + com.google.bigtable.admin.v2.Instance.newBuilder().setName(INSTANCE_NAME).build(); Mockito.when(mockGetInstanceCallable.futureCall(expectedRequest)) .thenReturn(ApiFutures.immediateFuture(expectedResponse)); // Execute - Instance actualResult = adminClient.getInstance(INSTANCE_NAME.getInstance()); + Instance actualResult = adminClient.getInstance(INSTANCE_ID); // Verify assertThat(actualResult).isEqualTo(Instance.fromProto(expectedResponse)); @@ -322,7 +313,7 @@ public void testListInstances() { // Setup com.google.bigtable.admin.v2.ListInstancesRequest expectedRequest = com.google.bigtable.admin.v2.ListInstancesRequest.newBuilder() - .setParent(PROJECT_NAME.toString()) + .setParent(PROJECT_NAME) .build(); com.google.bigtable.admin.v2.ListInstancesResponse expectedResponse = @@ -355,7 +346,7 @@ public void testListInstancesFailedZone() { // Setup com.google.bigtable.admin.v2.ListInstancesRequest expectedRequest = com.google.bigtable.admin.v2.ListInstancesRequest.newBuilder() - .setParent(PROJECT_NAME.toString()) + .setParent(PROJECT_NAME) .build(); com.google.bigtable.admin.v2.ListInstancesResponse expectedResponse = @@ -364,7 +355,7 @@ public void testListInstancesFailedZone() { com.google.bigtable.admin.v2.Instance.newBuilder() .setName(INSTANCE_NAME + "1") .build()) - .addFailedLocations(LocationName.of(PROJECT_NAME.getProject(), "us-east1-d").toString()) + .addFailedLocations(NameUtil.formatLocationName(PROJECT_ID, "us-east1-d")) .build(); Mockito.when(mockListInstancesCallable.futureCall(expectedRequest)) @@ -394,7 +385,7 @@ public void testDeleteInstance() { // Setup com.google.bigtable.admin.v2.DeleteInstanceRequest expectedRequest = com.google.bigtable.admin.v2.DeleteInstanceRequest.newBuilder() - .setName(INSTANCE_NAME.toString()) + .setName(INSTANCE_NAME) .build(); final AtomicBoolean wasCalled = new AtomicBoolean(false); @@ -410,7 +401,7 @@ public ApiFuture
answer(InvocationOnMock invocationOnMock) { }); // Execute - adminClient.deleteInstance(INSTANCE_NAME.getInstance()); + adminClient.deleteInstance(INSTANCE_ID); // Verify assertThat(wasCalled.get()).isTrue(); @@ -421,8 +412,8 @@ public void testCreateCluster() { // Setup com.google.bigtable.admin.v2.CreateClusterRequest expectedRequest = com.google.bigtable.admin.v2.CreateClusterRequest.newBuilder() - .setParent(INSTANCE_NAME.toString()) - .setClusterId(CLUSTER_NAME.getCluster()) + .setParent(INSTANCE_NAME) + .setClusterId(CLUSTER_ID) .setCluster( com.google.bigtable.admin.v2.Cluster.newBuilder() .setLocation("projects/my-project/locations/us-east1-c") @@ -430,13 +421,13 @@ public void testCreateCluster() { .setDefaultStorageType(com.google.bigtable.admin.v2.StorageType.SSD)) .build(); com.google.bigtable.admin.v2.Cluster expectedResponse = - com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME.toString()).build(); + com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME).build(); mockOperationResult(mockCreateClusterCallable, expectedRequest, expectedResponse); // Execute Cluster actualResult = adminClient.createCluster( - CreateClusterRequest.of(CLUSTER_NAME.getInstance(), CLUSTER_NAME.getCluster()) + CreateClusterRequest.of(INSTANCE_ID, CLUSTER_ID) .setZone("us-east1-c") .setServeNodes(3) .setStorageType(StorageType.SSD)); @@ -448,19 +439,16 @@ public void testCreateCluster() { public void testGetCluster() { // Setup com.google.bigtable.admin.v2.GetClusterRequest expectedRequest = - com.google.bigtable.admin.v2.GetClusterRequest.newBuilder() - .setName(CLUSTER_NAME.toString()) - .build(); + com.google.bigtable.admin.v2.GetClusterRequest.newBuilder().setName(CLUSTER_NAME).build(); com.google.bigtable.admin.v2.Cluster expectedResponse = - com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME.toString()).build(); + com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME).build(); Mockito.when(mockGetClusterCallable.futureCall(expectedRequest)) .thenReturn(ApiFutures.immediateFuture(expectedResponse)); // Execute - Cluster actualResult = - adminClient.getCluster(CLUSTER_NAME.getInstance(), CLUSTER_NAME.getCluster()); + Cluster actualResult = adminClient.getCluster(INSTANCE_ID, CLUSTER_ID); // Verify assertThat(actualResult).isEqualTo(Cluster.fromProto(expectedResponse)); @@ -471,24 +459,22 @@ public void testListClusters() { // Setup com.google.bigtable.admin.v2.ListClustersRequest expectedRequest = com.google.bigtable.admin.v2.ListClustersRequest.newBuilder() - .setParent(INSTANCE_NAME.toString()) + .setParent(INSTANCE_NAME) .build(); com.google.bigtable.admin.v2.ListClustersResponse expectedResponse = com.google.bigtable.admin.v2.ListClustersResponse.newBuilder() .addClusters( - com.google.bigtable.admin.v2.Cluster.newBuilder() - .setName(CLUSTER_NAME.toString() + "1")) + com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME + "1")) .addClusters( - com.google.bigtable.admin.v2.Cluster.newBuilder() - .setName(CLUSTER_NAME.toString() + "2")) + com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME + "2")) .build(); Mockito.when(mockListClustersCallable.futureCall(expectedRequest)) .thenReturn(ApiFutures.immediateFuture(expectedResponse)); // Execute - List actualResult = adminClient.listClusters(INSTANCE_NAME.getInstance()); + List actualResult = adminClient.listClusters(INSTANCE_ID); // Verify assertThat(actualResult) @@ -502,14 +488,13 @@ public void testListClustersFailedZone() { // Setup com.google.bigtable.admin.v2.ListClustersRequest expectedRequest = com.google.bigtable.admin.v2.ListClustersRequest.newBuilder() - .setParent(INSTANCE_NAME.toString()) + .setParent(INSTANCE_NAME) .build(); com.google.bigtable.admin.v2.ListClustersResponse expectedResponse = com.google.bigtable.admin.v2.ListClustersResponse.newBuilder() - .addClusters( - com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME.toString())) - .addFailedLocations(LocationName.of(PROJECT_NAME.getProject(), "us-east1-c").toString()) + .addClusters(com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME)) + .addFailedLocations(NameUtil.formatLocationName(PROJECT_ID, "us-east1-c")) .build(); Mockito.when(mockListClustersCallable.futureCall(expectedRequest)) @@ -519,7 +504,7 @@ public void testListClustersFailedZone() { Exception actualError = null; try { - adminClient.listClusters(INSTANCE_NAME.getInstance()); + adminClient.listClusters(INSTANCE_ID); } catch (Exception e) { actualError = e; } @@ -538,22 +523,21 @@ public void testResizeCluster() { // Setup com.google.bigtable.admin.v2.Cluster expectedRequest = com.google.bigtable.admin.v2.Cluster.newBuilder() - .setName(CLUSTER_NAME.toString()) + .setName(CLUSTER_NAME) .setServeNodes(30) .build(); com.google.bigtable.admin.v2.Cluster expectedResponse = com.google.bigtable.admin.v2.Cluster.newBuilder() - .setName(CLUSTER_NAME.toString()) - .setLocation(LocationName.of(PROJECT_NAME.getProject(), "us-east1-c").toString()) + .setName(CLUSTER_NAME) + .setLocation(NameUtil.formatLocationName(PROJECT_ID, "us-east1-c")) .setServeNodes(30) .build(); mockOperationResult(mockUpdateClusterCallable, expectedRequest, expectedResponse); // Execute - Cluster actualResult = - adminClient.resizeCluster(CLUSTER_NAME.getInstance(), CLUSTER_NAME.getCluster(), 30); + Cluster actualResult = adminClient.resizeCluster(INSTANCE_ID, CLUSTER_ID, 30); // Verify assertThat(actualResult).isEqualTo(Cluster.fromProto(expectedResponse)); @@ -564,7 +548,7 @@ public void testDeleteCluster() { // Setup com.google.bigtable.admin.v2.DeleteClusterRequest expectedRequest = com.google.bigtable.admin.v2.DeleteClusterRequest.newBuilder() - .setName(CLUSTER_NAME.toString()) + .setName(CLUSTER_NAME) .build(); final AtomicBoolean wasCalled = new AtomicBoolean(false); @@ -580,7 +564,7 @@ public ApiFuture answer(InvocationOnMock invocationOnMock) { }); // Execute - adminClient.deleteCluster(CLUSTER_NAME.getInstance(), CLUSTER_NAME.getCluster()); + adminClient.deleteCluster(INSTANCE_ID, CLUSTER_ID); // Verify assertThat(wasCalled.get()).isTrue(); @@ -591,8 +575,8 @@ public void testCreateAppProfile() { // Setup com.google.bigtable.admin.v2.CreateAppProfileRequest expectedRequest = com.google.bigtable.admin.v2.CreateAppProfileRequest.newBuilder() - .setParent(INSTANCE_NAME.toString()) - .setAppProfileId(APP_PROFILE_NAME.getAppProfile()) + .setParent(NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID)) + .setAppProfileId(APP_PROFILE_ID) .setAppProfile( com.google.bigtable.admin.v2.AppProfile.newBuilder() .setDescription("my description") @@ -603,7 +587,7 @@ public void testCreateAppProfile() { com.google.bigtable.admin.v2.AppProfile expectedResponse = com.google.bigtable.admin.v2.AppProfile.newBuilder() - .setName(APP_PROFILE_NAME.toString()) + .setName(APP_PROFILE_NAME) .setDescription("my description") .setMultiClusterRoutingUseAny( com.google.bigtable.admin.v2.AppProfile.MultiClusterRoutingUseAny @@ -616,8 +600,7 @@ public void testCreateAppProfile() { // Execute AppProfile actualResult = adminClient.createAppProfile( - CreateAppProfileRequest.of( - APP_PROFILE_NAME.getInstance(), APP_PROFILE_NAME.getAppProfile()) + CreateAppProfileRequest.of(INSTANCE_ID, APP_PROFILE_ID) .setDescription("my description") .setRoutingPolicy(MultiClusterRoutingPolicy.of())); @@ -630,12 +613,12 @@ public void testGetAppProfile() { // Setup com.google.bigtable.admin.v2.GetAppProfileRequest expectedRequest = com.google.bigtable.admin.v2.GetAppProfileRequest.newBuilder() - .setName(APP_PROFILE_NAME.toString()) + .setName(APP_PROFILE_NAME) .build(); com.google.bigtable.admin.v2.AppProfile expectedResponse = com.google.bigtable.admin.v2.AppProfile.newBuilder() - .setName(APP_PROFILE_NAME.toString()) + .setName(APP_PROFILE_NAME) .setDescription("my description") .setMultiClusterRoutingUseAny( com.google.bigtable.admin.v2.AppProfile.MultiClusterRoutingUseAny @@ -646,8 +629,7 @@ public void testGetAppProfile() { .thenReturn(ApiFutures.immediateFuture(expectedResponse)); // Execute - AppProfile actualResult = - adminClient.getAppProfile(APP_PROFILE_NAME.getInstance(), APP_PROFILE_NAME.getAppProfile()); + AppProfile actualResult = adminClient.getAppProfile(INSTANCE_ID, APP_PROFILE_ID); // Verify assertThat(actualResult).isEqualTo(AppProfile.fromProto(expectedResponse)); @@ -658,7 +640,7 @@ public void testListAppProfiles() { // Setup com.google.bigtable.admin.v2.ListAppProfilesRequest expectedRequest = com.google.bigtable.admin.v2.ListAppProfilesRequest.newBuilder() - .setParent(INSTANCE_NAME.toString()) + .setParent(NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID)) .build(); // 3 AppProfiles spread across 2 pages @@ -666,7 +648,7 @@ public void testListAppProfiles() { for (int i = 0; i < 3; i++) { expectedProtos.add( com.google.bigtable.admin.v2.AppProfile.newBuilder() - .setName(APP_PROFILE_NAME.toString() + i) + .setName(APP_PROFILE_NAME + i) .setDescription("profile" + i) .setMultiClusterRoutingUseAny( com.google.bigtable.admin.v2.AppProfile.MultiClusterRoutingUseAny @@ -694,7 +676,7 @@ public void testListAppProfiles() { .thenReturn(ApiFutures.immediateFuture(response0)); // Execute - List actualResults = adminClient.listAppProfiles(INSTANCE_NAME.getInstance()); + List actualResults = adminClient.listAppProfiles(INSTANCE_ID); // Verify List expectedResults = Lists.newArrayList(); @@ -712,14 +694,14 @@ public void testUpdateAppProfile() { com.google.bigtable.admin.v2.UpdateAppProfileRequest.newBuilder() .setAppProfile( com.google.bigtable.admin.v2.AppProfile.newBuilder() - .setName(APP_PROFILE_NAME.toString()) + .setName(APP_PROFILE_NAME) .setDescription("updated description")) .setUpdateMask(FieldMask.newBuilder().addPaths("description")) .build(); com.google.bigtable.admin.v2.AppProfile expectedResponse = com.google.bigtable.admin.v2.AppProfile.newBuilder() - .setName(APP_PROFILE_NAME.toString()) + .setName(APP_PROFILE_NAME) .setDescription("updated description") .setMultiClusterRoutingUseAny( com.google.bigtable.admin.v2.AppProfile.MultiClusterRoutingUseAny @@ -731,8 +713,7 @@ public void testUpdateAppProfile() { // Execute AppProfile actualResult = adminClient.updateAppProfile( - UpdateAppProfileRequest.of( - APP_PROFILE_NAME.getInstance(), APP_PROFILE_NAME.getAppProfile()) + UpdateAppProfileRequest.of(INSTANCE_ID, APP_PROFILE_ID) .setDescription("updated description")); // Verify @@ -744,7 +725,7 @@ public void testDeleteAppProfile() { // Setup com.google.bigtable.admin.v2.DeleteAppProfileRequest expectedRequest = com.google.bigtable.admin.v2.DeleteAppProfileRequest.newBuilder() - .setName(APP_PROFILE_NAME.toString()) + .setName(APP_PROFILE_NAME) .build(); final AtomicBoolean wasCalled = new AtomicBoolean(false); @@ -760,7 +741,7 @@ public ApiFuture answer(InvocationOnMock invocationOnMock) { }); // Execute - adminClient.deleteAppProfile(APP_PROFILE_NAME.getInstance(), APP_PROFILE_NAME.getAppProfile()); + adminClient.deleteAppProfile(INSTANCE_ID, APP_PROFILE_ID); // Verify assertThat(wasCalled.get()).isTrue(); @@ -785,7 +766,7 @@ public void testGetIamPolicy() { // Setup com.google.iam.v1.GetIamPolicyRequest expectedRequest = com.google.iam.v1.GetIamPolicyRequest.newBuilder() - .setResource(INSTANCE_NAME.toString()) + .setResource(NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID)) .build(); com.google.iam.v1.Policy expectedResponse = @@ -801,7 +782,7 @@ public void testGetIamPolicy() { .thenReturn(ApiFutures.immediateFuture(expectedResponse)); // Execute - Policy actualResult = adminClient.getIamPolicy(INSTANCE_NAME.getInstance()); + Policy actualResult = adminClient.getIamPolicy(INSTANCE_ID); // Verify assertThat(actualResult) @@ -817,7 +798,7 @@ public void testSetIamPolicy() { // Setup com.google.iam.v1.SetIamPolicyRequest expectedRequest = com.google.iam.v1.SetIamPolicyRequest.newBuilder() - .setResource(INSTANCE_NAME.toString()) + .setResource(NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID)) .setPolicy( com.google.iam.v1.Policy.newBuilder() .addBindings( @@ -841,7 +822,7 @@ public void testSetIamPolicy() { // Execute Policy actualResult = adminClient.setIamPolicy( - INSTANCE_NAME.getInstance(), + INSTANCE_ID, Policy.newBuilder() .addIdentity(Role.of("bigtable.user"), Identity.user("someone@example.com")) .build()); @@ -860,7 +841,7 @@ public void testTestIamPermissions() { // Setup com.google.iam.v1.TestIamPermissionsRequest expectedRequest = com.google.iam.v1.TestIamPermissionsRequest.newBuilder() - .setResource(INSTANCE_NAME.toString()) + .setResource(NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID)) .addPermissions("bigtable.tables.readRows") .build(); @@ -874,7 +855,7 @@ public void testTestIamPermissions() { // Execute List actualResult = - adminClient.testIamPermission(INSTANCE_NAME.getInstance(), "bigtable.tables.readRows"); + adminClient.testIamPermission(INSTANCE_ID, "bigtable.tables.readRows"); // Verify assertThat(actualResult).containsExactly("bigtable.tables.readRows"); @@ -885,14 +866,14 @@ public void testExistsTrue() { // Setup com.google.bigtable.admin.v2.Instance expectedResponse = com.google.bigtable.admin.v2.Instance.newBuilder() - .setName(INSTANCE_NAME.toString()) + .setName(NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID)) .build(); Mockito.when(mockGetInstanceCallable.futureCall(Matchers.any(GetInstanceRequest.class))) .thenReturn(ApiFutures.immediateFuture(expectedResponse)); // Execute - boolean found = adminClient.exists(INSTANCE_NAME.getInstance()); + boolean found = adminClient.exists(INSTANCE_ID); // Verify assertThat(found).isTrue(); @@ -909,7 +890,7 @@ public void testExistsFalse() { ApiFutures. immediateFailedFuture(exception)); // Execute - boolean found = adminClient.exists(INSTANCE_NAME.getInstance()); + boolean found = adminClient.exists(INSTANCE_ID); // Verify assertThat(found).isFalse(); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminSettingsTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminSettingsTest.java index 278a939f897a..1567b4323a85 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminSettingsTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminSettingsTest.java @@ -18,7 +18,6 @@ import static com.google.common.truth.Truth.assertThat; import com.google.api.gax.rpc.StatusCode.Code; -import com.google.bigtable.admin.v2.ProjectName; import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings.Builder; import java.io.IOException; import org.junit.Test; @@ -26,12 +25,12 @@ public class BigtableInstanceAdminSettingsTest { @Test public void testProjectName() throws Exception { - ProjectName projectName = ProjectName.of("my-project"); - Builder builder = BigtableInstanceAdminSettings.newBuilder().setProjectName(projectName); + String projectId = "my-project"; + Builder builder = BigtableInstanceAdminSettings.newBuilder().setProjectId(projectId); - assertThat(builder.getProjectName()).isEqualTo(projectName); - assertThat(builder.build().getProjectName()).isEqualTo(projectName); - assertThat(builder.build().toBuilder().getProjectName()).isEqualTo(projectName); + assertThat(builder.getProjectId()).isEqualTo(projectId); + assertThat(builder.build().getProjectId()).isEqualTo(projectId); + assertThat(builder.build().toBuilder().getProjectId()).isEqualTo(projectId); } @Test @@ -39,7 +38,7 @@ public void testMissingProjectName() { Exception actualException = null; Builder settingsBuilder = BigtableInstanceAdminSettings.newBuilder(); - assertThat(settingsBuilder.getProjectName()).isNull(); + assertThat(settingsBuilder.getProjectId()).isNull(); try { settingsBuilder.build(); @@ -52,10 +51,10 @@ public void testMissingProjectName() { @Test public void testStubSettings() throws IOException { - ProjectName projectName = ProjectName.of("my-project"); + String projectId = "my-project"; BigtableInstanceAdminSettings.Builder builder = - BigtableInstanceAdminSettings.newBuilder().setProjectName(projectName); + BigtableInstanceAdminSettings.newBuilder().setProjectId(projectId); builder.stubSettings().createInstanceSettings().setRetryableCodes(Code.INVALID_ARGUMENT); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java index 9a590a22a225..99f86dca33dc 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClientTest.java @@ -27,13 +27,13 @@ import com.google.bigtable.admin.v2.DropRowRangeRequest; import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.GetTableRequest; -import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.ListTablesRequest; import com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification; import com.google.bigtable.admin.v2.Table.View; import com.google.bigtable.admin.v2.TableName; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage; import com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest; import com.google.cloud.bigtable.admin.v2.models.Table; @@ -57,9 +57,14 @@ @RunWith(MockitoJUnitRunner.class) public class BigtableTableAdminClientTest { - private static final InstanceName INSTANCE_NAME = InstanceName.of("my-project", "my-instance"); - private static final TableName TABLE_NAME = - TableName.of(INSTANCE_NAME.getProject(), INSTANCE_NAME.getInstance(), "my-table"); + private static final String PROJECT_ID = "my-project"; + private static final String INSTANCE_ID = "my-instance"; + private static final String TABLE_ID = "my-table"; + + private static final String PROJECT_NAME = NameUtil.formatProjectName(PROJECT_ID); + private static final String INSTANCE_NAME = NameUtil.formatInstanceName(PROJECT_ID, INSTANCE_ID); + private static final String TABLE_NAME = + NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID); private BigtableTableAdminClient adminClient; @Mock private EnhancedBigtableTableAdminStub mockStub; @@ -86,7 +91,7 @@ public class BigtableTableAdminClientTest { @Before public void setUp() { - adminClient = BigtableTableAdminClient.create(INSTANCE_NAME, mockStub); + adminClient = BigtableTableAdminClient.create(PROJECT_ID, INSTANCE_ID, mockStub); Mockito.when(mockStub.createTableCallable()).thenReturn(mockCreateTableCallable); Mockito.when(mockStub.modifyColumnFamiliesCallable()).thenReturn(mockModifyTableCallable); @@ -108,8 +113,8 @@ public void testCreateTable() { // Setup com.google.bigtable.admin.v2.CreateTableRequest expectedRequest = com.google.bigtable.admin.v2.CreateTableRequest.newBuilder() - .setParent(INSTANCE_NAME.toString()) - .setTableId(TABLE_NAME.getTable()) + .setParent(INSTANCE_NAME) + .setTableId(TABLE_ID) .setTable(com.google.bigtable.admin.v2.Table.getDefaultInstance()) .build(); @@ -120,7 +125,7 @@ public void testCreateTable() { .thenReturn(ApiFutures.immediateFuture(expectedResponse)); // Execute - Table result = adminClient.createTable(CreateTableRequest.of(TABLE_NAME.getTable())); + Table result = adminClient.createTable(CreateTableRequest.of(TABLE_ID)); // Verify assertThat(result).isEqualTo(Table.fromProto(expectedResponse)); @@ -131,7 +136,7 @@ public void testModifyFamilies() { // Setup com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest expectedRequest = com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.newBuilder() - .setName(TABLE_NAME.toString()) + .setName(TABLE_NAME) .addModifications( Modification.newBuilder() .setId("cf") @@ -140,7 +145,7 @@ public void testModifyFamilies() { com.google.bigtable.admin.v2.Table fakeResponse = com.google.bigtable.admin.v2.Table.newBuilder() - .setName(TABLE_NAME.toString()) + .setName(TABLE_NAME) .putColumnFamilies( "cf", ColumnFamily.newBuilder().setGcRule(GcRule.getDefaultInstance()).build()) .build(); @@ -150,8 +155,7 @@ public void testModifyFamilies() { // Execute Table actualResult = - adminClient.modifyFamilies( - ModifyColumnFamiliesRequest.of(TABLE_NAME.getTable()).addFamily("cf")); + adminClient.modifyFamilies(ModifyColumnFamiliesRequest.of(TABLE_ID).addFamily("cf")); // Verify assertThat(actualResult).isEqualTo(Table.fromProto(fakeResponse)); @@ -176,7 +180,7 @@ public ApiFuture answer(InvocationOnMock invocationOnMock) { }); // Execute - adminClient.deleteTable(TABLE_NAME.getTable()); + adminClient.deleteTable(TABLE_ID); // Verify assertThat(wasCalled.get()).isTrue(); @@ -198,7 +202,7 @@ public void testGetTable() { .thenReturn(ApiFutures.immediateFuture(expectedResponse)); // Execute - Table actualResult = adminClient.getTable(TABLE_NAME.getTable()); + Table actualResult = adminClient.getTable(TABLE_ID); // Verify assertThat(actualResult).isEqualTo(Table.fromProto(expectedResponse)); @@ -209,16 +213,14 @@ public void testListTables() { // Setup com.google.bigtable.admin.v2.ListTablesRequest expectedRequest = com.google.bigtable.admin.v2.ListTablesRequest.newBuilder() - .setParent(INSTANCE_NAME.toString()) + .setParent(INSTANCE_NAME) .build(); // 3 Tables spread across 2 pages List expectedProtos = Lists.newArrayList(); for (int i = 0; i < 3; i++) { expectedProtos.add( - com.google.bigtable.admin.v2.Table.newBuilder() - .setName(TABLE_NAME.toString() + i) - .build()); + com.google.bigtable.admin.v2.Table.newBuilder().setName(TABLE_NAME + i).build()); } // 2 on the first page ListTablesPage page0 = Mockito.mock(ListTablesPage.class); @@ -257,7 +259,7 @@ public void testDropRowRange() { // Setup DropRowRangeRequest expectedRequest = DropRowRangeRequest.newBuilder() - .setName(TABLE_NAME.toString()) + .setName(TABLE_NAME) .setRowKeyPrefix(ByteString.copyFromUtf8("rowKeyPrefix")) .build(); @@ -276,7 +278,7 @@ public ApiFuture answer(InvocationOnMock invocationOnMock) { }); // Execute - adminClient.dropRowRange(TABLE_NAME.getTable(), "rowKeyPrefix"); + adminClient.dropRowRange(TABLE_ID, "rowKeyPrefix"); // Verify assertThat(wasCalled.get()).isTrue(); @@ -286,7 +288,7 @@ public ApiFuture answer(InvocationOnMock invocationOnMock) { public void testAwaitReplication() { // Setup @SuppressWarnings("UnnecessaryLocalVariable") - TableName expectedRequest = TABLE_NAME; + TableName expectedRequest = TableName.parse(TABLE_NAME); final AtomicBoolean wasCalled = new AtomicBoolean(false); @@ -294,14 +296,14 @@ public void testAwaitReplication() { .thenAnswer( new Answer >() { @Override - public ApiFuture answer(InvocationOnMock invocationOnMock) throws Throwable { + public ApiFuture answer(InvocationOnMock invocationOnMock) { wasCalled.set(true); return ApiFutures.immediateFuture(null); } }); // Execute - adminClient.awaitReplication(TABLE_NAME.getTable()); + adminClient.awaitReplication(TABLE_ID); // Verify assertThat(wasCalled.get()).isTrue(); @@ -317,7 +319,7 @@ public void testExistsTrue() { .thenReturn(ApiFutures.immediateFuture(expectedResponse)); // Execute - boolean found = adminClient.exists(TABLE_NAME.getTable()); + boolean found = adminClient.exists(TABLE_ID); // Verify assertThat(found).isTrue(); @@ -334,7 +336,7 @@ public void testExistsFalse() { ApiFutures. immediateFailedFuture(exception)); // Execute - boolean found = adminClient.exists(TABLE_NAME.getTable()); + boolean found = adminClient.exists(TABLE_ID); // Verify assertThat(found).isFalse(); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminSettingsTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminSettingsTest.java index 611c3fc8c0e2..116504c8cd6a 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminSettingsTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminSettingsTest.java @@ -18,7 +18,6 @@ import static com.google.common.truth.Truth.assertThat; import com.google.api.gax.rpc.StatusCode.Code; -import com.google.bigtable.admin.v2.InstanceName; import java.io.IOException; import org.junit.Test; @@ -26,14 +25,17 @@ public class BigtableTableAdminSettingsTest { @Test public void testInstanceName() throws IOException { - InstanceName instanceName = InstanceName.of("my-project", "my-instance"); - BigtableTableAdminSettings.Builder builder = - BigtableTableAdminSettings.newBuilder().setInstanceName(instanceName); + BigtableTableAdminSettings.newBuilder() + .setProjectId("my-project") + .setInstanceId("my-instance"); - assertThat(builder.getInstanceName()).isEqualTo(instanceName); - assertThat(builder.build().getInstanceName()).isEqualTo(instanceName); - assertThat(builder.build().toBuilder().getInstanceName()).isEqualTo(instanceName); + assertThat(builder.getProjectId()).isEqualTo("my-project"); + assertThat(builder.getInstanceId()).isEqualTo("my-instance"); + assertThat(builder.build().getProjectId()).isEqualTo("my-project"); + assertThat(builder.build().getInstanceId()).isEqualTo("my-instance"); + assertThat(builder.build().toBuilder().getProjectId()).isEqualTo("my-project"); + assertThat(builder.build().toBuilder().getInstanceId()).isEqualTo("my-instance"); } @Test @@ -51,10 +53,10 @@ public void testMissingInstanceName() { @Test public void testStubSettings() throws IOException { - InstanceName instanceName = InstanceName.of("my-project", "my-instance"); - BigtableTableAdminSettings.Builder builder = - BigtableTableAdminSettings.newBuilder().setInstanceName(instanceName); + BigtableTableAdminSettings.newBuilder() + .setProjectId("my-project") + .setInstanceId("my-instance"); builder.stubSettings().createTableSettings().setRetryableCodes(Code.INVALID_ARGUMENT); diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableTableAdminClientIT.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableTableAdminClientIT.java index 25ad2d28bc20..7788d534a9e2 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableTableAdminClientIT.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/it/BigtableTableAdminClientIT.java @@ -21,7 +21,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import com.google.bigtable.admin.v2.InstanceName; import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; import com.google.cloud.bigtable.admin.v2.models.ColumnFamily; import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; @@ -31,12 +30,15 @@ import com.google.cloud.bigtable.admin.v2.models.GCRules.VersionRule; import com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest; import com.google.cloud.bigtable.admin.v2.models.Table; +import com.google.common.base.Joiner; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.protobuf.ByteString; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.logging.Logger; import org.junit.AfterClass; import org.junit.AssumptionViolatedException; import org.junit.Before; @@ -45,22 +47,35 @@ import org.threeten.bp.Duration; public class BigtableTableAdminClientIT { + private static final Logger LOGGER = Logger.getLogger(BigtableTableAdminClientIT.class.getName()); + + private static final String PROJECT_PROPERTY_NAME = "bigtable.project"; private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; + private static List missingProperties; private static BigtableTableAdminClient tableAdmin; private static String prefix; @BeforeClass public static void createClient() throws IOException { - String targetInstance = System.getProperty(INSTANCE_PROPERTY_NAME); + missingProperties = Lists.newArrayList(); + + String targetProject = System.getProperty(PROJECT_PROPERTY_NAME); + if (targetProject == null) { + missingProperties.add(PROJECT_PROPERTY_NAME); + } + String targetInstance = System.getProperty(INSTANCE_PROPERTY_NAME); if (targetInstance == null) { - tableAdmin = null; + missingProperties.add(INSTANCE_PROPERTY_NAME); + } + + if (!missingProperties.isEmpty()) { + LOGGER.warning("Missing properties: " + Joiner.on(",").join(missingProperties)); return; } - InstanceName instanceName = InstanceName.parse(targetInstance); - tableAdmin = BigtableTableAdminClient.create(instanceName); + tableAdmin = BigtableTableAdminClient.create(targetProject, targetInstance); // Setup a prefix to avoid collisions between concurrent test runs prefix = String.format("020%d", System.currentTimeMillis()); @@ -86,7 +101,7 @@ public static void closeClient() { public void setup() { if (tableAdmin == null) { throw new AssumptionViolatedException( - INSTANCE_PROPERTY_NAME + " property is not set, skipping integration tests."); + "Required properties are not set, skipping integration tests."); } } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateAppProfileRequestTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateAppProfileRequestTest.java index 44b89ec2388e..9b7141d3ab2e 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateAppProfileRequestTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/CreateAppProfileRequestTest.java @@ -20,7 +20,6 @@ import com.google.bigtable.admin.v2.AppProfile.MultiClusterRoutingUseAny; import com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting; import com.google.bigtable.admin.v2.InstanceName; -import com.google.bigtable.admin.v2.ProjectName; import com.google.cloud.bigtable.admin.v2.models.AppProfile.MultiClusterRoutingPolicy; import com.google.cloud.bigtable.admin.v2.models.AppProfile.SingleClusterRoutingPolicy; import org.junit.Test; @@ -37,7 +36,7 @@ public void testToProto() { .setRoutingPolicy(SingleClusterRoutingPolicy.of("my-cluster", true)) .setIgnoreWarnings(true); - assertThat(wrapper.toProto(ProjectName.of("my-project"))) + assertThat(wrapper.toProto("my-project")) .isEqualTo( com.google.bigtable.admin.v2.CreateAppProfileRequest.newBuilder() .setParent(InstanceName.of("my-project", "my-instance").toString()) @@ -59,11 +58,7 @@ public void testMultiClusterRouting() { CreateAppProfileRequest.of("my-instance", "my-profile") .setRoutingPolicy(MultiClusterRoutingPolicy.of()); - assertThat( - wrapper - .toProto(ProjectName.of("my-project")) - .getAppProfile() - .getMultiClusterRoutingUseAny()) + assertThat(wrapper.toProto("my-project").getAppProfile().getMultiClusterRoutingUseAny()) .isEqualTo(MultiClusterRoutingUseAny.getDefaultInstance()); } @@ -73,7 +68,7 @@ public void testDefaultDescription() { CreateAppProfileRequest.of("my-instance", "my-profile") .setRoutingPolicy(MultiClusterRoutingPolicy.of()); - assertThat(wrapper.toProto(ProjectName.of("my-project")).getAppProfile().getDescription()) + assertThat(wrapper.toProto("my-project").getAppProfile().getDescription()) .isEqualTo("my-profile"); } } 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 53ce781b55f0..3b3e6340cf90 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,7 +17,7 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.bigtable.admin.v2.ProjectName; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -31,12 +31,11 @@ public void testProductionSingle() { .setType(Instance.Type.PRODUCTION) .addCluster("cluster1", "us-east1-c", 3, StorageType.SSD); - com.google.bigtable.admin.v2.CreateInstanceRequest actual = - input.toProto(ProjectName.of("my-project")); + com.google.bigtable.admin.v2.CreateInstanceRequest actual = input.toProto("my-project"); com.google.bigtable.admin.v2.CreateInstanceRequest expected = com.google.bigtable.admin.v2.CreateInstanceRequest.newBuilder() - .setParent(ProjectName.of("my-project").toString()) + .setParent(NameUtil.formatProjectName("my-project")) .setInstanceId("my-instance") .setInstance( com.google.bigtable.admin.v2.Instance.newBuilder() @@ -62,12 +61,11 @@ public void testProductionReplication() { .addCluster("cluster1", "us-east1-c", 3, StorageType.SSD) .addCluster("cluster2", "us-east1-a", 3, StorageType.SSD); - com.google.bigtable.admin.v2.CreateInstanceRequest actual = - input.toProto(ProjectName.of("my-project")); + com.google.bigtable.admin.v2.CreateInstanceRequest actual = input.toProto("my-project"); com.google.bigtable.admin.v2.CreateInstanceRequest expected = com.google.bigtable.admin.v2.CreateInstanceRequest.newBuilder() - .setParent(ProjectName.of("my-project").toString()) + .setParent(NameUtil.formatProjectName("my-project")) .setInstanceId("my-instance") .setInstance( com.google.bigtable.admin.v2.Instance.newBuilder() @@ -99,12 +97,11 @@ public void testDevelopment() { .setType(Instance.Type.DEVELOPMENT) .addCluster("cluster1", "us-east1-c", 1, StorageType.SSD); - com.google.bigtable.admin.v2.CreateInstanceRequest actual = - input.toProto(ProjectName.of("my-project")); + com.google.bigtable.admin.v2.CreateInstanceRequest actual = input.toProto("my-project"); com.google.bigtable.admin.v2.CreateInstanceRequest expected = com.google.bigtable.admin.v2.CreateInstanceRequest.newBuilder() - .setParent(ProjectName.of("my-project").toString()) + .setParent(NameUtil.formatProjectName("my-project")) .setInstanceId("my-instance") .setInstance( com.google.bigtable.admin.v2.Instance.newBuilder() @@ -132,12 +129,11 @@ public void testOptionalFields() { .setType(Instance.Type.DEVELOPMENT) .addCluster("cluster1", "us-east1-c", 1, StorageType.SSD); - com.google.bigtable.admin.v2.CreateInstanceRequest actual = - input.toProto(ProjectName.of("my-project")); + com.google.bigtable.admin.v2.CreateInstanceRequest actual = input.toProto("my-project"); com.google.bigtable.admin.v2.CreateInstanceRequest expected = com.google.bigtable.admin.v2.CreateInstanceRequest.newBuilder() - .setParent(ProjectName.of("my-project").toString()) + .setParent(NameUtil.formatProjectName("my-project")) .setInstanceId("my-instance") .setInstance( com.google.bigtable.admin.v2.Instance.newBuilder() diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java index da10ce27f747..21366854cde4 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/TableAdminRequestsTest.java @@ -21,7 +21,7 @@ import com.google.bigtable.admin.v2.GcRule; import com.google.bigtable.admin.v2.InstanceName; import com.google.bigtable.admin.v2.Table; -import com.google.bigtable.admin.v2.TableName; +import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.protobuf.ByteString; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,7 +29,8 @@ @RunWith(JUnit4.class) public class TableAdminRequestsTest { - private final InstanceName instanceName = InstanceName.of("project", "instance"); + private final String PROJECT_ID = "project"; + private final String INSTANCE_ID = "instance"; @Test public void createTable() { @@ -38,7 +39,7 @@ public void createTable() { .addFamily("cf1") .addFamily("cf2", GCRules.GCRULES.maxVersions(1)) .addSplit(ByteString.copyFromUtf8("c")) - .toProto(instanceName); + .toProto(PROJECT_ID, INSTANCE_ID); com.google.bigtable.admin.v2.CreateTableRequest expected = com.google.bigtable.admin.v2.CreateTableRequest.newBuilder() @@ -64,9 +65,10 @@ public void createTableRequiredTableId() { CreateTableRequest.of(null); } + @SuppressWarnings("ConstantConditions") @Test(expected = NullPointerException.class) public void createTableRequiredParent() { - CreateTableRequest.of("tableId").toProto(null); + CreateTableRequest.of("tableId").toProto(null, null); } @Test @@ -78,13 +80,11 @@ public void modifyFamilies() { .addFamily("cf3") .updateFamily("cf1", GCRules.GCRULES.maxVersions(5)) .dropFamily("cf3") - .toProto(instanceName); + .toProto(PROJECT_ID, INSTANCE_ID); com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest expected = com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.newBuilder() - .setName( - TableName.of(instanceName.getProject(), instanceName.getInstance(), "tableId") - .toString()) + .setName(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, "tableId")) .addModifications( com.google.bigtable.admin.v2.ModifyColumnFamiliesRequest.Modification.newBuilder() .setId("cf1") @@ -119,11 +119,11 @@ public void modifyFamilies() { @Test(expected = NullPointerException.class) public void modifyFamiliesRequiredTableId() { - ModifyColumnFamiliesRequest.of(null).toProto(instanceName); + ModifyColumnFamiliesRequest.of(null).toProto(PROJECT_ID, INSTANCE_ID); } @Test(expected = NullPointerException.class) public void modifyFamiliesRequiredParent() { - ModifyColumnFamiliesRequest.of("tableId").toProto(null); + ModifyColumnFamiliesRequest.of("tableId").toProto(null, null); } } diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateAppProfileRequestTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateAppProfileRequestTest.java index dfa04d5ad9f8..ae3119b81a44 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateAppProfileRequestTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateAppProfileRequestTest.java @@ -19,7 +19,6 @@ import com.google.bigtable.admin.v2.AppProfile.MultiClusterRoutingUseAny; import com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting; -import com.google.bigtable.admin.v2.ProjectName; import com.google.cloud.bigtable.admin.v2.models.AppProfile.SingleClusterRoutingPolicy; import com.google.protobuf.FieldMask; import org.junit.Test; @@ -36,7 +35,7 @@ public void testToProto() { .setRoutingPolicy(SingleClusterRoutingPolicy.of("my-cluster", true)) .setIgnoreWarnings(true); - assertThat(wrapper.toProto(ProjectName.of("my-project"))) + assertThat(wrapper.toProto("my-project")) .isEqualTo( com.google.bigtable.admin.v2.UpdateAppProfileRequest.newBuilder() .setAppProfile( @@ -70,7 +69,7 @@ public void testUpdateExisting() { UpdateAppProfileRequest updateWrapper = UpdateAppProfileRequest.of(existingWrapper).setDescription("new description"); - assertThat(updateWrapper.toProto(ProjectName.of("my-project"))) + assertThat(updateWrapper.toProto("my-project")) .isEqualTo( com.google.bigtable.admin.v2.UpdateAppProfileRequest.newBuilder() .setAppProfile(existingProto.toBuilder().setDescription("new description")) diff --git a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateInstanceRequestTest.java b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateInstanceRequestTest.java index 05f382681956..b5561769b604 100644 --- a/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateInstanceRequestTest.java +++ b/google-cloud-clients/google-cloud-bigtable-admin/src/test/java/com/google/cloud/bigtable/admin/v2/models/UpdateInstanceRequestTest.java @@ -20,7 +20,6 @@ import com.google.bigtable.admin.v2.Instance; import com.google.bigtable.admin.v2.Instance.Type; import com.google.bigtable.admin.v2.PartialUpdateInstanceRequest; -import com.google.bigtable.admin.v2.ProjectName; import com.google.common.collect.ImmutableMap; import com.google.protobuf.FieldMask; import org.junit.Test; @@ -36,7 +35,7 @@ public void testEmpty() { Exception actualError = null; try { - input.toProto(ProjectName.of("my-project")); + input.toProto("my-project"); } catch (Exception e) { actualError = e; } @@ -49,7 +48,7 @@ public void testDisplayName() { UpdateInstanceRequest input = UpdateInstanceRequest.of("my-instance").setDisplayName("my display name"); - PartialUpdateInstanceRequest actual = input.toProto(ProjectName.of("my-project")); + PartialUpdateInstanceRequest actual = input.toProto("my-project"); PartialUpdateInstanceRequest expected = PartialUpdateInstanceRequest.newBuilder() @@ -72,7 +71,7 @@ public void testLabels() { "label1", "value1", "label2", "value2")); - PartialUpdateInstanceRequest actual = input.toProto(ProjectName.of("my-project")); + PartialUpdateInstanceRequest actual = input.toProto("my-project"); PartialUpdateInstanceRequest expected = PartialUpdateInstanceRequest.newBuilder() @@ -91,7 +90,7 @@ public void testLabels() { public void testType() { UpdateInstanceRequest input = UpdateInstanceRequest.of("my-instance").setProductionType(); - PartialUpdateInstanceRequest actual = input.toProto(ProjectName.of("my-project")); + PartialUpdateInstanceRequest actual = input.toProto("my-project"); PartialUpdateInstanceRequest expected = PartialUpdateInstanceRequest.newBuilder()