Skip to content

Commit 997d2a3

Browse files
Bigtable: add CRUD for AppProfiles (#3619)
1 parent e3eedeb commit 997d2a3

File tree

8 files changed

+1331
-35
lines changed

8 files changed

+1331
-35
lines changed

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

+340-29
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.bigtable.admin.v2.models;
17+
18+
import com.google.api.core.InternalApi;
19+
import com.google.bigtable.admin.v2.AppProfile.MultiClusterRoutingUseAny;
20+
import com.google.bigtable.admin.v2.AppProfileName;
21+
import com.google.common.base.Objects;
22+
import com.google.common.base.Preconditions;
23+
import com.google.common.base.Verify;
24+
import com.google.common.base.VerifyException;
25+
import javax.annotation.Nonnull;
26+
27+
/**
28+
* <p>An application profile, or app profile, stores settings that tell your Cloud Bigtable
29+
* instance how to handle incoming requests from an application. When one of your applications
30+
* connects to a Cloud Bigtable instance, it can specify an app profile, and Cloud Bigtable uses
31+
* that app profile for any requests that the application sends over that connection.
32+
*
33+
* <p>App profiles affect how your applications communicate with an instance that uses replication.
34+
* As a result, app profiles are especially useful for instances that have 2 clusters. Even if your
35+
* instance has only 1 cluster, you can use a unique app profile for each application that you run,
36+
* or for different components within a single application.
37+
*
38+
* @see <a href="https://cloud.google.com/bigtable/docs/app-profiles">For more details</a>.
39+
*/
40+
public final class AppProfile {
41+
private final com.google.bigtable.admin.v2.AppProfile proto;
42+
43+
/**
44+
* Wraps a protobuf response.
45+
*
46+
* <p>This method is considered an internal implementation detail and not meant to be used by
47+
* applications.
48+
*/
49+
@InternalApi
50+
public static AppProfile fromProto(@Nonnull com.google.bigtable.admin.v2.AppProfile proto) {
51+
return new AppProfile(proto);
52+
}
53+
54+
private AppProfile(@Nonnull com.google.bigtable.admin.v2.AppProfile proto) {
55+
Preconditions.checkNotNull(proto);
56+
Preconditions
57+
.checkArgument(proto.hasSingleClusterRouting() || proto.hasMultiClusterRoutingUseAny(),
58+
"AppProfile must have a routing policy");
59+
Preconditions.checkArgument(!proto.getName().isEmpty(), "AppProfile must have a name");
60+
this.proto = proto;
61+
}
62+
63+
64+
/** Gets the routing policy for all read/write requests which use this app profile. */
65+
@SuppressWarnings("WeakerAccess")
66+
public RoutingPolicy getPolicy() {
67+
if (proto.hasMultiClusterRoutingUseAny()) {
68+
return MultiClusterRoutingPolicy.of();
69+
} else if (proto.hasSingleClusterRouting()) {
70+
return new SingleClusterRoutingPolicy(proto.getSingleClusterRouting());
71+
} else {
72+
// Should never happen because the constructor verifies that one must exist.
73+
throw new VerifyException();
74+
}
75+
}
76+
77+
/** Gets the id of this AppProfile. */
78+
@SuppressWarnings("WeakerAccess")
79+
public String getId() {
80+
AppProfileName fullName = Verify.verifyNotNull(
81+
AppProfileName.parse(proto.getName()),
82+
"Name can never be null");
83+
84+
//noinspection ConstantConditions
85+
return fullName.getAppProfile();
86+
}
87+
88+
/** Gets the id of the instance that owns this AppProfile. */
89+
@SuppressWarnings("WeakerAccess")
90+
public String getInstanceId() {
91+
AppProfileName fullName = Verify.verifyNotNull(
92+
AppProfileName.parse(proto.getName()),
93+
"Name can never be null");
94+
95+
//noinspection ConstantConditions
96+
return fullName.getInstance();
97+
}
98+
99+
/** Gets long form description of the use case for this AppProfile. */
100+
@SuppressWarnings("WeakerAccess")
101+
public String getDescription() {
102+
return proto.getDescription();
103+
}
104+
105+
106+
/**
107+
* Creates the request protobuf. This method is considered an internal implementation detail and
108+
* not meant to be used by applications.
109+
*/
110+
com.google.bigtable.admin.v2.AppProfile toProto() {
111+
return proto;
112+
}
113+
114+
@Override
115+
public boolean equals(Object o) {
116+
if (this == o) {
117+
return true;
118+
}
119+
if (o == null || getClass() != o.getClass()) {
120+
return false;
121+
}
122+
AppProfile that = (AppProfile) o;
123+
return Objects.equal(proto, that.proto);
124+
}
125+
126+
@Override
127+
public int hashCode() {
128+
return Objects.hashCode(proto);
129+
}
130+
131+
132+
/**
133+
* Represents the routing for read/write requests. Please check the implementations of this
134+
* interface for more details.
135+
*/
136+
@SuppressWarnings("WeakerAccess")
137+
public interface RoutingPolicy {
138+
}
139+
140+
/**
141+
* A {@link RoutingPolicy} that routes all requests to a specific cluster.
142+
*/
143+
@SuppressWarnings("WeakerAccess")
144+
public static class SingleClusterRoutingPolicy implements RoutingPolicy {
145+
private final com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting proto;
146+
147+
/**
148+
* Wraps a protobuf response.
149+
*
150+
* <p>This method is considered an internal implementation detail and not meant to be used by
151+
* applications.
152+
*/
153+
@InternalApi
154+
public static SingleClusterRoutingPolicy fromProto(
155+
com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting proto) {
156+
return new SingleClusterRoutingPolicy(proto);
157+
}
158+
159+
/**
160+
* Builds a new instance of the routing policy that will send all requests to the specified
161+
* cluster.
162+
*
163+
* <p>Please note that atomic row transactions will be disabled.
164+
*/
165+
public static SingleClusterRoutingPolicy of(String clusterId) {
166+
return of(clusterId, false);
167+
}
168+
169+
/**
170+
* Builds a new instance of the routing policy that will send all requests to the specified
171+
* cluster. This variant enables the ability to re-enable single row transactions at the cost of
172+
* consistency.
173+
*
174+
* <p>Please see the <a href="https://cloud.google.com/bigtable/docs/app-profiles#single-row-transactions">online
175+
* documentation</a> for more details.
176+
*/
177+
public static SingleClusterRoutingPolicy of(String clusterId, boolean allowTransactionWrites) {
178+
return fromProto(
179+
com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting.newBuilder()
180+
.setClusterId(clusterId)
181+
.setAllowTransactionalWrites(allowTransactionWrites)
182+
.build()
183+
);
184+
}
185+
186+
private SingleClusterRoutingPolicy(
187+
com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting proto) {
188+
this.proto = proto;
189+
}
190+
191+
/** Gets the target cluster of this policy. */
192+
@SuppressWarnings("WeakerAccess")
193+
public String getClusterId() {
194+
return proto.getClusterId();
195+
}
196+
197+
/** Checks if transactional writes are enabled. */
198+
@SuppressWarnings("WeakerAccess")
199+
public boolean getAllowTransactionalWrites() {
200+
return proto.getAllowTransactionalWrites();
201+
}
202+
203+
/**
204+
* Wraps a protobuf response.
205+
*
206+
* <p>This method is considered an internal implementation detail and not meant to be used by
207+
* applications.
208+
*/
209+
@InternalApi
210+
com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting toProto() {
211+
return proto;
212+
}
213+
214+
@Override
215+
public boolean equals(Object o) {
216+
if (this == o) {
217+
return true;
218+
}
219+
if (o == null || getClass() != o.getClass()) {
220+
return false;
221+
}
222+
SingleClusterRoutingPolicy that = (SingleClusterRoutingPolicy) o;
223+
return Objects.equal(proto, that.proto);
224+
}
225+
226+
@Override
227+
public int hashCode() {
228+
return Objects.hashCode(proto);
229+
}
230+
}
231+
232+
/**
233+
* A {@link RoutingPolicy} that tells Cloud Bigtable that it can route each request to any
234+
* available cluster.
235+
*/
236+
public static class MultiClusterRoutingPolicy implements RoutingPolicy {
237+
private static final MultiClusterRoutingUseAny proto = MultiClusterRoutingUseAny
238+
.getDefaultInstance();
239+
240+
/** Creates a new instance of {@link MultiClusterRoutingPolicy}. */
241+
public static MultiClusterRoutingPolicy of() {
242+
return new MultiClusterRoutingPolicy();
243+
}
244+
245+
private MultiClusterRoutingPolicy() {
246+
}
247+
248+
/**
249+
* Creates the request protobuf. This method is considered an internal implementation detail and
250+
* not meant to be used by applications.
251+
*/
252+
@InternalApi
253+
MultiClusterRoutingUseAny toProto() {
254+
return proto;
255+
}
256+
257+
@Override
258+
public boolean equals(Object o) {
259+
if (this == o) {
260+
return true;
261+
}
262+
if (o == null || getClass() != o.getClass()) {
263+
return false;
264+
}
265+
266+
return true;
267+
}
268+
269+
@Override
270+
public int hashCode() {
271+
return Objects.hashCode(proto);
272+
}
273+
}
274+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.bigtable.admin.v2.models;
17+
18+
import com.google.api.core.InternalApi;
19+
import com.google.bigtable.admin.v2.InstanceName;
20+
import com.google.bigtable.admin.v2.ProjectName;
21+
import com.google.cloud.bigtable.admin.v2.models.AppProfile.MultiClusterRoutingPolicy;
22+
import com.google.cloud.bigtable.admin.v2.models.AppProfile.RoutingPolicy;
23+
import com.google.cloud.bigtable.admin.v2.models.AppProfile.SingleClusterRoutingPolicy;
24+
import com.google.common.base.Preconditions;
25+
import javax.annotation.Nonnull;
26+
27+
/**
28+
* Parameters for creating a new Cloud Bigtable app profile.
29+
*
30+
* <p>An application profile, or app profile, stores settings that tell your Cloud Bigtable
31+
* instance how to handle incoming requests from an application. When one of your applications
32+
* connects to a Cloud Bigtable instance, it can specify an app profile, and Cloud Bigtable uses
33+
* that app profile for any requests that the application sends over that connection.
34+
*
35+
* <p>Sample code:
36+
*
37+
* <pre>{@code
38+
* AppProfile existingAppProfile = ...;
39+
* CreateAppProfileRequest appProfileRequest = CreateAppProfileRequest.of("my-instance", "my-new-app-profile")
40+
* .setRoutingPolicy(SingleClusterRoutingPolicy.of("my-cluster"));
41+
* }</pre>
42+
*
43+
* @see AppProfile for more details
44+
*/
45+
public final class CreateAppProfileRequest {
46+
private final String instanceId;
47+
private final com.google.bigtable.admin.v2.CreateAppProfileRequest.Builder proto;
48+
49+
/** Builds a new request to create a new app profile in the specified instance. */
50+
public static CreateAppProfileRequest of(String instanceId, String appProfileId) {
51+
return new CreateAppProfileRequest(instanceId, appProfileId);
52+
}
53+
54+
private CreateAppProfileRequest(String instanceId, String appProfileId) {
55+
this.instanceId = instanceId;
56+
this.proto = com.google.bigtable.admin.v2.CreateAppProfileRequest.newBuilder();
57+
58+
proto.setAppProfileId(appProfileId);
59+
proto.getAppProfileBuilder().setDescription(appProfileId);
60+
}
61+
62+
/** Configures if safety warnings should be disabled. */
63+
@SuppressWarnings("WeakerAccess")
64+
public CreateAppProfileRequest setIgnoreWarnings(boolean value) {
65+
proto.setIgnoreWarnings(value);
66+
return this;
67+
}
68+
69+
/** Sets the optional long form description of the use case for the AppProfile. */
70+
@SuppressWarnings("WeakerAccess")
71+
public CreateAppProfileRequest setDescription(@Nonnull String description) {
72+
proto.getAppProfileBuilder().setDescription(description);
73+
return this;
74+
}
75+
76+
/** Sets the routing policy for all read/write requests that use this app profile. */
77+
@SuppressWarnings("WeakerAccess")
78+
public CreateAppProfileRequest setRoutingPolicy(RoutingPolicy routingPolicy) {
79+
Preconditions.checkNotNull(routingPolicy);
80+
81+
if (routingPolicy instanceof MultiClusterRoutingPolicy) {
82+
proto.getAppProfileBuilder().setMultiClusterRoutingUseAny(((MultiClusterRoutingPolicy)routingPolicy).toProto());
83+
} else if (routingPolicy instanceof SingleClusterRoutingPolicy) {
84+
proto.getAppProfileBuilder().setSingleClusterRouting(((SingleClusterRoutingPolicy)routingPolicy).toProto());
85+
} else {
86+
throw new IllegalArgumentException("Unknown policy type: " + routingPolicy);
87+
}
88+
89+
return this;
90+
}
91+
92+
/**
93+
* Creates the request protobuf. This method is considered an internal implementation detail and
94+
* not meant to be used by applications.
95+
*/
96+
@InternalApi
97+
public com.google.bigtable.admin.v2.CreateAppProfileRequest toProto(ProjectName projectName) {
98+
InstanceName name = InstanceName.of(projectName.getProject(), instanceId);
99+
100+
return proto.setParent(name.toString()).build();
101+
}
102+
}

0 commit comments

Comments
 (0)