Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

spanner: Add snippets for InstanceAdminClient #3578

Merged
merged 3 commits into from
Aug 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,23 @@
public interface InstanceAdminClient {

/** Gets an instance config. */
/* <!--SNIPPET instance_admin_client_get_instance_config-->
* <pre>{@code
* final String configId = my_config_id;
* InstanceConfig instanceConfig = instanceAdminClient.getInstanceConfig(configId);
* }</pre>
* <!--SNIPPET instance_admin_client_get_instance_config-->
*/
InstanceConfig getInstanceConfig(String configId) throws SpannerException;

/** Lists the supported instance configs for current project. */
/* <!--SNIPPET instance_admin_client_list_configs-->
* <pre>{@code
* List<InstanceConfig> configs =
* Lists.newArrayList(instanceAdminClient.listInstanceConfigs(Options.pageSize(1)).iterateAll());
* }</pre>
* <!--SNIPPET instance_admin_client_list_configs-->
*/
Page<InstanceConfig> listInstanceConfigs(ListOption... options) throws SpannerException;

/**
Expand Down Expand Up @@ -58,11 +72,35 @@ public interface InstanceAdminClient {
* <li> Databases can be created in the instance.
* <li> The instance's allocated resource levels are readable via the
* </ul>
*
* <!--SNIPPET instance_admin_client_create_instance-->
* <pre>{@code
* final String instanceId = my_instance_id;
* final String configId = my_config_id;
* final String clientProject = my_client_project;
*
* Operation<Instance, CreateInstanceMetadata> op =
* instanceAdminClient.createInstance(InstanceInfo
* .newBuilder(InstanceId.of(clientProject, instanceId))
* .setInstanceConfigId(InstanceConfigId.of(clientProject, configId))
* .setDisplayName(instanceId)
* .setNodeCount(1)
* .build());
* op.waitFor();
* }</pre>
* <!--SNIPPET instance_admin_client_create_instance-->
*/
Operation<Instance, CreateInstanceMetadata> createInstance(InstanceInfo instance)
throws SpannerException;

/** Gets an instance. */
/* <!--SNIPPET instance_admin_client_get_instance-->
* <pre>{@code
* final String instanceId = my_instance_id;
* Instance ins = instanceAdminClient.getInstance(instanceId);
* }</pre>
* <!--SNIPPET instance_admin_client_get_instance-->
*/
Instance getInstance(String instanceId) throws SpannerException;

/**
Expand All @@ -75,10 +113,25 @@ Operation<Instance, CreateInstanceMetadata> createInstance(InstanceInfo instance
* <li> display_name
* <li> labels.key where key is the name of a label
* </ul>
*
* <!--SNIPPET instance_admin_client_list_instances-->
* <pre>{@code
* List<Instance> instances =
* Lists.newArrayList(
* instanceAdminClient.listInstances(Options.pageSize(1)).iterateAll());
* }</pre>
* <!--SNIPPET instance_admin_client_list_instances-->
*/
Page<Instance> listInstances(ListOption... options) throws SpannerException;

/** Deletes an instance. */
/* <!--SNIPPET instance_admin_client_delete_instance-->
* <pre>{@code
* final String instanceId = my_instance_id;
* instanceAdminClient.deleteInstance(instanceId);
* }</pre>
* <!--SNIPPET instance_admin_client_delete_instance-->
*/
void deleteInstance(String instanceId) throws SpannerException;

/**
Expand Down Expand Up @@ -112,6 +165,26 @@ Operation<Instance, CreateInstanceMetadata> createInstance(InstanceInfo instance
* <li> All newly-reserved resources are available for serving the instance's tables.
* <li> The instance's new resource levels are readable via the API.
* </ul>
*
* <!--SNIPPET instance_admin_client_update_instance-->
* <pre>{@code
* Instance instance = my_instance;
* final String clientProject = my_client_project;
* final String instanceId = my_instance_id;
*
* final String newDisplayName = my_display_name;
*
* InstanceInfo toUpdate =
* InstanceInfo.newBuilder(InstanceId.of(clientProject, instanceId))
* .setDisplayName(newDisplayName)
* .setNodeCount(instance.getNodeCount() + 1)
* .build();
* // Only update display name
* Operation<Instance, UpdateInstanceMetadata> op =
* instanceAdminClient.updateInstance(toUpdate, InstanceInfo.InstanceField.DISPLAY_NAME);
* op.waitFor().getResult();
* }</pre>
* <!--SNIPPET instance_admin_client_update_instance-->
*/
Operation<Instance, UpdateInstanceMetadata> updateInstance(
InstanceInfo instance, InstanceInfo.InstanceField... fieldsToUpdate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private Subscriber createSubscriberWithCustomCredentials() throws Exception {
Subscriber.newBuilder(subscriptionName, receiver)
.setCredentialsProvider(credentialsProvider)
.build();
// [START pubsub_subscriber_custom_credentials]
// [END pubsub_subscriber_custom_credentials]
return subscriber;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* 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
*
* http://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.
*/

/*
* EDITING INSTRUCTIONS
* This file is referenced in spanner/InstanceAdminClient's javadoc. Any change
* to this file should be reflected in spanner/InstanceAdminClient's javadoc.
*/

package com.google.cloud.examples.spanner.snippets;

import com.google.cloud.spanner.Instance;
import com.google.cloud.spanner.InstanceAdminClient;
import com.google.cloud.spanner.InstanceConfig;
import com.google.cloud.spanner.InstanceConfigId;
import com.google.cloud.spanner.InstanceId;
import com.google.cloud.spanner.InstanceInfo;
import com.google.cloud.spanner.Operation;
import com.google.cloud.spanner.Options;
import com.google.common.collect.Lists;
import com.google.spanner.admin.instance.v1.CreateInstanceMetadata;
import com.google.spanner.admin.instance.v1.UpdateInstanceMetadata;
import java.util.List;

/**
* This class contains snippets for {@link InstanceAdminClient} interface.
*/
public class InstanceAdminClientSnippets {

private final InstanceAdminClient instanceAdminClient;

public InstanceAdminClientSnippets(InstanceAdminClient instanceAdminClient) {
this.instanceAdminClient = instanceAdminClient;
}

/**
* Example to get instance config.
*/
public InstanceConfig getInstanceConfig(final String my_config_id) {
// [START instance_admin_client_get_instance_config]
final String configId = my_config_id;
InstanceConfig instanceConfig = instanceAdminClient.getInstanceConfig(configId);
// [END instance_admin_client_get_instance_config]
return instanceConfig;
}

/**
* Example to list instance configs.
*/
public List<InstanceConfig> listInstanceConfigs() {
// [START instance_admin_client_list_configs]
List<InstanceConfig> configs =
Lists.newArrayList(instanceAdminClient.listInstanceConfigs(Options.pageSize(1)).iterateAll());
// [END instance_admin_client_list_configs]
return configs;
}

/**
* Example to create an instance.
*/
public void createInstance(
final String my_instance_id,
final String my_config_id,
final String my_client_project) {
// [START instance_admin_client_create_instance]
final String instanceId = my_instance_id;
final String configId = my_config_id;
final String clientProject = my_client_project;

Operation<Instance, CreateInstanceMetadata> op =
instanceAdminClient.createInstance(InstanceInfo
.newBuilder(InstanceId.of(clientProject, instanceId))
.setInstanceConfigId(InstanceConfigId.of(clientProject, configId))
.setDisplayName(instanceId)
.setNodeCount(1)
.build());
op.waitFor();
// [END instance_admin_client_create_instance]
}

/**
* Example to get an instance.
*/
public Instance getInstance(final String my_instance_id) {
// [START instance_admin_client_get_instance]
final String instanceId = my_instance_id;
Instance ins = instanceAdminClient.getInstance(instanceId);
// [END instance_admin_client_get_instance]
return ins;
}

/**
* Example to list instances.
*/
public List<Instance> listInstances() {
// [START instance_admin_client_list_instances]
List<Instance> instances =
Lists.newArrayList(
instanceAdminClient.listInstances(Options.pageSize(1)).iterateAll());
// [END instance_admin_client_list_instances]
return instances;
}

/**
* Example to delete an instance.
*/
public void deleteInstance(final String my_instance_id) {
// [START instance_admin_client_delete_instance]
final String instanceId = my_instance_id;
instanceAdminClient.deleteInstance(instanceId);
// [END instance_admin_client_delete_instance]
}

/**
* Example to update an instance.
*/
public void updateInstance(Instance my_instance,
final String my_client_project,
final String my_instance_id,
final String my_display_name) {
// [START instance_admin_client_update_instance]
Instance instance = my_instance;
final String clientProject = my_client_project;
final String instanceId = my_instance_id;

final String newDisplayName = my_display_name;

InstanceInfo toUpdate =
InstanceInfo.newBuilder(InstanceId.of(clientProject, instanceId))
.setDisplayName(newDisplayName)
.setNodeCount(instance.getNodeCount() + 1)
.build();
// Only update display name
Operation<Instance, UpdateInstanceMetadata> op =
instanceAdminClient.updateInstance(toUpdate, InstanceInfo.InstanceField.DISPLAY_NAME);
op.waitFor().getResult();
// [END instance_admin_client_update_instance]
}
}