Skip to content

Commit 9cbc0ef

Browse files
Bigtable: start working BigtableInstanceAdmin
This is the beginning of importing work done by spollapally in: https://github.com/spollapally/google-cloud-java/tree/instance-admin-client/google-cloud-clients/google-cloud-bigtable I will be importing & finishing it piecemeal. This first PR establishes the scaffolding for the client & settings
1 parent d55a0f4 commit 9cbc0ef

File tree

5 files changed

+352
-1
lines changed

5 files changed

+352
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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;
17+
18+
import com.google.bigtable.admin.v2.ProjectName;
19+
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStub;
20+
import java.io.IOException;
21+
import javax.annotation.Nonnull;
22+
23+
/**
24+
* Client for creating, configuring and delete Cloud Bigtable instances (including AppProfiles and
25+
* Clusters).
26+
*
27+
* <p>See the individual methods for example code.
28+
*
29+
* <pre>{@code
30+
* try(BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create(ProjectName.of("[PROJECT]"))) {
31+
* CreateInstanceRequest request = CreateInstanceRequest.of(ProjectName)
32+
* .addFamily("cf1")
33+
* .addFamily("cf2", GCRULES.maxVersions(10))
34+
* .addSplit(ByteString.copyFromUtf8("b"))
35+
* .addSplit(ByteString.copyFromUtf8("q"));
36+
*
37+
* client.createInstance(request);
38+
* }
39+
* }</pre>
40+
*
41+
* <p>Note: close() needs to be called on the client object to clean up resources such as threads.
42+
* In the example above, try-with-resources is used, which automatically calls close().
43+
*
44+
* <p>This class can be customized by passing in a custom instance of BigtableInstanceAdminSettings
45+
* to create(). For example:
46+
*
47+
* <p>To customize credentials:
48+
*
49+
* <pre>{@code
50+
* BigtableInstanceAdminSettings settings = BigtableInstanceAdminSettings.newBuilder()
51+
* .setProjectName(ProjectName.of("[PROJECT]"))
52+
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
53+
* .build();
54+
*
55+
* BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create(settings);
56+
* }</pre>
57+
*
58+
* To customize the endpoint:
59+
*
60+
* <pre>{@code
61+
* BigtableInstanceAdminSettings settings = BigtableInstanceAdminSettings.newBuilder()
62+
* .setProjectName(ProjectName.of("[PROJECT]"))
63+
* .setEndpoint(myEndpoint)
64+
* .build();
65+
*
66+
* BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create(settings);
67+
* }</pre>
68+
*/
69+
public class BigtableInstanceAdminClient implements AutoCloseable {
70+
private final ProjectName projectName;
71+
private final BigtableInstanceAdminStub stub;
72+
73+
/** Constructs an instance of BigtableInstanceAdminClient with the given ProjectName. */
74+
public static BigtableInstanceAdminClient create(@Nonnull ProjectName projectName)
75+
throws IOException {
76+
return create(BigtableInstanceAdminSettings.newBuilder().setProjectName(projectName).build());
77+
}
78+
79+
/** Constructs an instance of BigtableInstanceAdminClient with the given settings. */
80+
public static BigtableInstanceAdminClient create(@Nonnull BigtableInstanceAdminSettings settings)
81+
throws IOException {
82+
return create(settings.getProjectName(), settings.getStubSettings().createStub());
83+
}
84+
85+
/** Constructs an instance of BigtableInstanceAdminClient with the given Projectname and stub. */
86+
public static BigtableInstanceAdminClient create(@Nonnull ProjectName projectName,
87+
@Nonnull BigtableInstanceAdminStub stub) {
88+
return new BigtableInstanceAdminClient(projectName, stub);
89+
}
90+
91+
92+
private BigtableInstanceAdminClient(
93+
@Nonnull ProjectName projectName, @Nonnull BigtableInstanceAdminStub stub) {
94+
this.projectName = projectName;
95+
this.stub = stub;
96+
}
97+
98+
/** Gets the ProjectName this client is associated with. */
99+
public ProjectName getProjectName() {
100+
return projectName;
101+
}
102+
103+
/** Closes the client and frees all resources associated with it (like thread pools) */
104+
@Override
105+
public void close() {
106+
stub.close();
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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;
17+
18+
import com.google.bigtable.admin.v2.ProjectName;
19+
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStubSettings;
20+
import com.google.common.base.Preconditions;
21+
import com.google.common.base.Verify;
22+
import java.io.IOException;
23+
import javax.annotation.Nonnull;
24+
import javax.annotation.Nullable;
25+
26+
/**
27+
* Settings class to configure an instance of {@link BigtableInstanceAdminClient}.
28+
*
29+
* <p>It must be configured with a {@link ProjectName} and can be used to change default RPC settings.
30+
*
31+
* <p>Example usage:
32+
*
33+
* <pre>{@code
34+
* BigtableInstanceAdminSettings.Builder settingsBuilder = BigtableInstanceAdminSettings.newBuilder()
35+
* .setProjectName(ProjectName.of("my-project"));
36+
*
37+
* settingsBuilder.stubSettings().createInstanceSettings()
38+
* .setRetrySettings(
39+
* RetrySettings.newBuilder()
40+
* .setTotalTimeout(Duration.ofMinutes(15))
41+
* .build());
42+
*
43+
* BigtableInstanceAdminSettings settings = settingsBuilder.build();
44+
* }</pre>
45+
*/
46+
public class BigtableInstanceAdminSettings {
47+
private final ProjectName projectName;
48+
private final BigtableInstanceAdminStubSettings stubSettings;
49+
50+
private BigtableInstanceAdminSettings(Builder builder) throws IOException {
51+
Preconditions.checkNotNull(builder.projectName, "ProjectName must be set");
52+
Verify.verifyNotNull(builder.stubSettings, "stubSettings should never be null");
53+
54+
this.projectName = builder.projectName;
55+
this.stubSettings = builder.stubSettings.build();
56+
}
57+
58+
/** Gets the anme of the project whose instances the client will manager. */
59+
@Nonnull
60+
public ProjectName getProjectName() {
61+
return projectName;
62+
}
63+
64+
/** Gets the underlying RPC settings. */
65+
@Nonnull
66+
public BigtableInstanceAdminStubSettings getStubSettings() {
67+
return stubSettings;
68+
}
69+
70+
/** Returns a builder containing all the values of this settings class. */
71+
public Builder toBuilder() {
72+
return new Builder(this);
73+
}
74+
75+
/** Returns a new builder for this class. */
76+
public static Builder newBuilder() {
77+
return new Builder();
78+
}
79+
80+
81+
/** Builder for BigtableInstanceAdminSettings. */
82+
public static final class Builder {
83+
@Nullable
84+
private ProjectName projectName;
85+
private final BigtableInstanceAdminStubSettings.Builder stubSettings;
86+
87+
private Builder() {
88+
stubSettings = BigtableInstanceAdminStubSettings.newBuilder();
89+
}
90+
91+
private Builder(BigtableInstanceAdminSettings settings) {
92+
this.projectName = settings.projectName;
93+
this.stubSettings = settings.stubSettings.toBuilder();
94+
}
95+
96+
/** Sets the name of instance whose tables the client will manage. */
97+
public Builder setProjectName(@Nonnull ProjectName projectName) {
98+
Preconditions.checkNotNull(projectName);
99+
this.projectName = projectName;
100+
return this;
101+
}
102+
103+
/** Gets the name of the project whose instances the client will manage. */
104+
@Nullable
105+
public ProjectName getProjectName() {
106+
return projectName;
107+
}
108+
109+
/**
110+
* Returns the builder for the settings used for all RPCs.
111+
*
112+
* <p>This is meant for advanced usage. The default RPC settings are set to their recommended
113+
* values.
114+
*/
115+
public BigtableInstanceAdminStubSettings.Builder stubSettings() {
116+
return stubSettings;
117+
}
118+
119+
/** Builds an instance of the settings. */
120+
public BigtableInstanceAdminSettings build() throws IOException {
121+
return new BigtableInstanceAdminSettings(this);
122+
}
123+
}
124+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* Settings class to configure an instance of {@link BigtableTableAdminClient}.
2828
*
29-
* <p>It must be configured with an {@link InstanceName} and be used to change default RPC settings.
29+
* <p>It must be configured with an {@link InstanceName} and can be used to change default RPC settings.
3030
*
3131
* <p>Example usage:
3232
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
20+
import com.google.bigtable.admin.v2.ProjectName;
21+
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStub;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.Mock;
26+
import org.mockito.Mockito;
27+
import org.mockito.runners.MockitoJUnitRunner;
28+
29+
@RunWith(MockitoJUnitRunner.class)
30+
public class BigtableInstanceAdminClientTest {
31+
private BigtableInstanceAdminClient adminClient;
32+
@Mock
33+
private BigtableInstanceAdminStub mockStub;
34+
35+
@Before
36+
public void setUp() {
37+
adminClient = BigtableInstanceAdminClient
38+
.create(ProjectName.of("[PROJECT]"), mockStub);
39+
}
40+
41+
@Test
42+
public void testProjectName() {
43+
assertThat(adminClient.getProjectName()).isEqualTo("[PROJECT]");
44+
}
45+
46+
@Test
47+
public void testClose() {
48+
adminClient.close();
49+
Mockito.verify(mockStub).close();
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
20+
import com.google.api.gax.rpc.StatusCode.Code;
21+
import com.google.bigtable.admin.v2.ProjectName;
22+
import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings.Builder;
23+
import java.io.IOException;
24+
import org.junit.Test;
25+
26+
public class BigtableInstanceAdminSettingsTest {
27+
@Test
28+
public void testProjectName() throws Exception {
29+
ProjectName projectName = ProjectName.of("my-project");
30+
Builder builder = BigtableInstanceAdminSettings.newBuilder()
31+
.setProjectName(projectName);
32+
33+
assertThat(builder.getProjectName()).isEqualTo(projectName);
34+
assertThat(builder.build().getProjectName()).isEqualTo(projectName);
35+
assertThat(builder.build().toBuilder().getProjectName()).isEqualTo(projectName);
36+
}
37+
38+
@Test
39+
public void testMissingProjectName() {
40+
Exception actualException = null;
41+
42+
try {
43+
BigtableInstanceAdminSettings.newBuilder().build();
44+
} catch (Exception e) {
45+
actualException = e;
46+
}
47+
48+
assertThat(actualException).isInstanceOf(NullPointerException.class);
49+
}
50+
51+
@Test
52+
public void testStubSettings() throws IOException {
53+
ProjectName projectName = ProjectName.of("my-project");
54+
55+
BigtableInstanceAdminSettings.Builder builder = BigtableInstanceAdminSettings.newBuilder()
56+
.setProjectName(projectName);
57+
58+
builder.stubSettings().createInstanceSettings()
59+
.setRetryableCodes(Code.INVALID_ARGUMENT);
60+
61+
assertThat(builder.build().getStubSettings().createInstanceSettings().getRetryableCodes())
62+
.containsExactly(Code.INVALID_ARGUMENT);
63+
64+
assertThat(builder.build().toBuilder().build().getStubSettings().createInstanceSettings()
65+
.getRetryableCodes())
66+
.containsExactly(Code.INVALID_ARGUMENT);
67+
}
68+
}

0 commit comments

Comments
 (0)