-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bigtable: start working on BigtableInstanceAdmin #3564
Merged
igorbernstein2
merged 4 commits into
googleapis:master
from
igorbernstein2:instance-admin
Aug 17, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
...e-admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2018 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; | ||
|
||
import com.google.bigtable.admin.v2.ProjectName; | ||
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStub; | ||
import java.io.IOException; | ||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* Client for creating, configuring and delete Cloud Bigtable instances (including AppProfiles and | ||
* Clusters). | ||
* | ||
* <p>See the individual methods for example code. | ||
* | ||
* <pre>{@code | ||
* try(BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create(ProjectName.of("my-project"))) { | ||
* CreateInstanceRequest request = CreateInstanceRequest.of("my-instance") | ||
* .addFamily("cf1") | ||
* .addFamily("cf2", GCRULES.maxVersions(10)) | ||
* .addSplit(ByteString.copyFromUtf8("b")) | ||
* .addSplit(ByteString.copyFromUtf8("q")); | ||
* | ||
* client.createInstance(request); | ||
* } | ||
* }</pre> | ||
* | ||
* <p>Note: close() needs to be called on the client object to clean up resources such as threads. | ||
* In the example above, try-with-resources is used, which automatically calls close(). | ||
* | ||
* <p>This class can be customized by passing in a custom instance of BigtableInstanceAdminSettings | ||
* to create(). For example: | ||
* | ||
* <p>To customize credentials: | ||
* | ||
* <pre>{@code | ||
* BigtableInstanceAdminSettings settings = BigtableInstanceAdminSettings.newBuilder() | ||
* .setProjectName(ProjectName.of("[PROJECT]")) | ||
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) | ||
* .build(); | ||
* | ||
* BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create(settings); | ||
* }</pre> | ||
* | ||
* To customize the endpoint: | ||
* | ||
* <pre>{@code | ||
* BigtableInstanceAdminSettings settings = BigtableInstanceAdminSettings.newBuilder() | ||
* .setProjectName(ProjectName.of("[PROJECT]")) | ||
* .setEndpoint(myEndpoint) | ||
* .build(); | ||
* | ||
* BigtableInstanceAdminClient client = BigtableInstanceAdminClient.create(settings); | ||
* }</pre> | ||
*/ | ||
public final class BigtableInstanceAdminClient implements AutoCloseable { | ||
private final ProjectName projectName; | ||
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 settings. */ | ||
public static BigtableInstanceAdminClient create(@Nonnull BigtableInstanceAdminSettings settings) | ||
throws IOException { | ||
return create(settings.getProjectName(), settings.getStubSettings().createStub()); | ||
} | ||
|
||
/** Constructs an instance of BigtableInstanceAdminClient with the given Projectname and stub. */ | ||
public static BigtableInstanceAdminClient create(@Nonnull ProjectName projectName, | ||
@Nonnull BigtableInstanceAdminStub stub) { | ||
return new BigtableInstanceAdminClient(projectName, stub); | ||
} | ||
|
||
|
||
private BigtableInstanceAdminClient( | ||
@Nonnull ProjectName projectName, @Nonnull BigtableInstanceAdminStub stub) { | ||
this.projectName = projectName; | ||
this.stub = stub; | ||
} | ||
|
||
/** Gets the ProjectName this client is associated with. */ | ||
public ProjectName getProjectName() { | ||
return projectName; | ||
} | ||
|
||
/** Closes the client and frees all resources associated with it (like thread pools) */ | ||
@Override | ||
public void close() { | ||
stub.close(); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
...admin/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2018 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; | ||
|
||
import com.google.bigtable.admin.v2.ProjectName; | ||
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStubSettings; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.base.Verify; | ||
import java.io.IOException; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Settings class to configure an instance of {@link BigtableInstanceAdminClient}. | ||
* | ||
* <p>It must be configured with a {@link ProjectName} and can be used to change default RPC settings. | ||
* | ||
* <p>Example usage: | ||
* | ||
* <pre>{@code | ||
* BigtableInstanceAdminSettings.Builder settingsBuilder = BigtableInstanceAdminSettings.newBuilder() | ||
* .setProjectName(ProjectName.of("my-project")); | ||
* | ||
* settingsBuilder.stubSettings().createInstanceSettings() | ||
* .setRetrySettings( | ||
* RetrySettings.newBuilder() | ||
* .setTotalTimeout(Duration.ofMinutes(15)) | ||
* .build()); | ||
* | ||
* BigtableInstanceAdminSettings settings = settingsBuilder.build(); | ||
* }</pre> | ||
*/ | ||
public final class BigtableInstanceAdminSettings { | ||
private final ProjectName projectName; | ||
private final BigtableInstanceAdminStubSettings stubSettings; | ||
|
||
private BigtableInstanceAdminSettings(Builder builder) throws IOException { | ||
Preconditions.checkNotNull(builder.projectName, "ProjectName must be set"); | ||
Verify.verifyNotNull(builder.stubSettings, "stubSettings should never be null"); | ||
|
||
this.projectName = builder.projectName; | ||
this.stubSettings = builder.stubSettings.build(); | ||
} | ||
|
||
/** Gets the anme of the project whose instances the client will manager. */ | ||
@Nonnull | ||
public ProjectName getProjectName() { | ||
return projectName; | ||
} | ||
|
||
/** Gets the underlying RPC settings. */ | ||
@Nonnull | ||
public BigtableInstanceAdminStubSettings getStubSettings() { | ||
return stubSettings; | ||
} | ||
|
||
/** Returns a builder containing all the values of this settings class. */ | ||
public Builder toBuilder() { | ||
return new Builder(this); | ||
} | ||
|
||
/** Returns a new builder for this class. */ | ||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
|
||
/** Builder for BigtableInstanceAdminSettings. */ | ||
public static final class Builder { | ||
@Nullable | ||
private ProjectName projectName; | ||
private final BigtableInstanceAdminStubSettings.Builder stubSettings; | ||
|
||
private Builder() { | ||
stubSettings = BigtableInstanceAdminStubSettings.newBuilder(); | ||
} | ||
|
||
private Builder(BigtableInstanceAdminSettings settings) { | ||
this.projectName = settings.projectName; | ||
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; | ||
return this; | ||
} | ||
|
||
/** Gets the name of the project whose instances the client will manage. */ | ||
@Nullable | ||
public ProjectName getProjectName() { | ||
return projectName; | ||
} | ||
|
||
/** | ||
* Returns the builder for the settings used for all RPCs. | ||
* | ||
* <p>This is meant for advanced usage. The default RPC settings are set to their recommended | ||
* values. | ||
*/ | ||
public BigtableInstanceAdminStubSettings.Builder stubSettings() { | ||
return stubSettings; | ||
} | ||
|
||
/** Builds an instance of the settings. */ | ||
public BigtableInstanceAdminSettings build() throws IOException { | ||
return new BigtableInstanceAdminSettings(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...min/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2018 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; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.bigtable.admin.v2.ProjectName; | ||
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStub; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class BigtableInstanceAdminClientTest { | ||
private BigtableInstanceAdminClient adminClient; | ||
@Mock | ||
private BigtableInstanceAdminStub mockStub; | ||
|
||
@Before | ||
public void setUp() { | ||
adminClient = BigtableInstanceAdminClient | ||
.create(ProjectName.of("[PROJECT]"), mockStub); | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
} | ||
|
||
@Test | ||
public void testProjectName() { | ||
assertThat(adminClient.getProjectName()).isEqualTo(ProjectName.of("[PROJECT]")); | ||
} | ||
|
||
@Test | ||
public void testClose() { | ||
adminClient.close(); | ||
Mockito.verify(mockStub).close(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...n/src/test/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminSettingsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2018 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; | ||
|
||
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; | ||
|
||
public class BigtableInstanceAdminSettingsTest { | ||
@Test | ||
public void testProjectName() throws Exception { | ||
ProjectName projectName = ProjectName.of("my-project"); | ||
Builder builder = BigtableInstanceAdminSettings.newBuilder() | ||
.setProjectName(projectName); | ||
|
||
assertThat(builder.getProjectName()).isEqualTo(projectName); | ||
assertThat(builder.build().getProjectName()).isEqualTo(projectName); | ||
assertThat(builder.build().toBuilder().getProjectName()).isEqualTo(projectName); | ||
} | ||
|
||
@Test | ||
public void testMissingProjectName() { | ||
Exception actualException = null; | ||
|
||
Builder settingsBuilder = BigtableInstanceAdminSettings.newBuilder(); | ||
assertThat(settingsBuilder.getProjectName()).isNull(); | ||
|
||
try { | ||
settingsBuilder.build(); | ||
} catch (Exception e) { | ||
actualException = e; | ||
} | ||
|
||
assertThat(actualException).isInstanceOf(NullPointerException.class); | ||
} | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
@Test | ||
public void testStubSettings() throws IOException { | ||
ProjectName projectName = ProjectName.of("my-project"); | ||
|
||
BigtableInstanceAdminSettings.Builder builder = BigtableInstanceAdminSettings.newBuilder() | ||
.setProjectName(projectName); | ||
|
||
builder.stubSettings().createInstanceSettings() | ||
.setRetryableCodes(Code.INVALID_ARGUMENT); | ||
|
||
assertThat(builder.build().getStubSettings().createInstanceSettings().getRetryableCodes()) | ||
.containsExactly(Code.INVALID_ARGUMENT); | ||
|
||
assertThat(builder.build().toBuilder().build().getStubSettings().createInstanceSettings() | ||
.getRetryableCodes()) | ||
.containsExactly(Code.INVALID_ARGUMENT); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.