Search in sources :

Example 6 with InstanceConfig

use of com.google.spanner.admin.instance.v1.InstanceConfig in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerClient method initializeInstance.

private void initializeInstance(InstanceId instanceId) {
    InstanceConfig instanceConfig = Iterators.get(spClient.getInstanceAdminClient().listInstanceConfigs().iterateAll().iterator(), // us-east1
    32, null);
    checkState(instanceConfig != null, "No instance configs found");
    InstanceConfigId configId = instanceConfig.getId();
    logger.log(Level.INFO, "Creating instance using config {0}", configId);
    InstanceInfo instance = InstanceInfo.newBuilder(instanceId).setNodeCount(1).setDisplayName("Test instance").setInstanceConfigId(configId).build();
    OperationFuture<Instance, CreateInstanceMetadata> op = spClient.getInstanceAdminClient().createInstance(instance);
    Instance createdInstance;
    try {
        createdInstance = op.get();
    } catch (Exception e) {
        boolean cancelled = false;
        try {
            // Try to cancel the createInstance operation.
            spClient.getInstanceAdminClient().cancelOperation(op.getName());
            com.google.longrunning.Operation createOperation = spClient.getInstanceAdminClient().getOperation(op.getName());
            cancelled = createOperation.hasError() && createOperation.getError().getCode() == Status.CANCELLED.getCode().value();
            if (cancelled) {
                logger.info("Cancelled the createInstance operation because the operation failed");
            } else {
                logger.info("Tried to cancel the createInstance operation because the operation failed, but the" + " operation could not be cancelled. Current status: " + createOperation.getError().getCode());
            }
        } catch (Throwable t) {
            logger.log(Level.WARNING, "Failed to cancel the createInstance operation", t);
        }
        if (!cancelled) {
            try {
                spClient.getInstanceAdminClient().deleteInstance(instanceId.getInstance());
                logger.info("Deleted the test instance because the createInstance operation failed and" + " cancelling the operation did not succeed");
            } catch (Throwable t) {
                logger.log(Level.WARNING, "Failed to delete the test instance", t);
            }
        }
        throw SpannerExceptionFactory.newSpannerException(e);
    }
    logger.log(Level.INFO, "Created test instance: {0}", createdInstance.getId());
}
Also used : InstanceConfigId(com.google.cloud.spanner.InstanceConfigId) InstanceConfig(com.google.cloud.spanner.InstanceConfig) Instance(com.google.cloud.spanner.Instance) CreateInstanceMetadata(com.google.spanner.admin.instance.v1.CreateInstanceMetadata) InstanceInfo(com.google.cloud.spanner.InstanceInfo) SpannerException(com.google.cloud.spanner.SpannerException)

Example 7 with InstanceConfig

use of com.google.spanner.admin.instance.v1.InstanceConfig in project grpc-gcp-java by GoogleCloudPlatform.

the class SpannerIntegrationTest method initializeInstance.

private static void initializeInstance(InstanceAdminClient instanceAdminClient, InstanceId instanceId) {
    InstanceConfig instanceConfig = Iterators.get(instanceAdminClient.listInstanceConfigs().iterateAll().iterator(), 0, null);
    checkState(instanceConfig != null, "No instance configs found");
    InstanceConfigId configId = instanceConfig.getId();
    InstanceInfo instance = InstanceInfo.newBuilder(instanceId).setNodeCount(1).setDisplayName("grpc-gcp test instance").setInstanceConfigId(configId).build();
    OperationFuture<Instance, CreateInstanceMetadata> op = instanceAdminClient.createInstance(instance);
    try {
        op.get();
    } catch (Exception e) {
        throw SpannerExceptionFactory.newSpannerException(e);
    }
}
Also used : InstanceConfigId(com.google.cloud.spanner.InstanceConfigId) InstanceConfig(com.google.cloud.spanner.InstanceConfig) Instance(com.google.cloud.spanner.Instance) CreateInstanceMetadata(com.google.spanner.admin.instance.v1.CreateInstanceMetadata) InstanceInfo(com.google.cloud.spanner.InstanceInfo) TimeoutException(java.util.concurrent.TimeoutException) ExpectedException(org.junit.rules.ExpectedException) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with InstanceConfig

use of com.google.spanner.admin.instance.v1.InstanceConfig in project google-cloud-java by GoogleCloudPlatform.

the class GrpcSpannerRpc method listInstanceConfigs.

@Override
public Paginated<InstanceConfig> listInstanceConfigs(int pageSize, @Nullable String pageToken) throws SpannerException {
    ListInstanceConfigsRequest.Builder request = ListInstanceConfigsRequest.newBuilder().setParent(projectName()).setPageSize(0);
    if (pageToken != null) {
        request.setPageToken(pageToken);
    }
    ListInstanceConfigsResponse response = get(doUnaryCall(InstanceAdminGrpc.METHOD_LIST_INSTANCE_CONFIGS, request.build(), projectName(), null));
    return new Paginated<>(response.getInstanceConfigsList(), response.getNextPageToken());
}
Also used : ListInstanceConfigsRequest(com.google.spanner.admin.instance.v1.ListInstanceConfigsRequest) ListInstanceConfigsResponse(com.google.spanner.admin.instance.v1.ListInstanceConfigsResponse)

Example 9 with InstanceConfig

use of com.google.spanner.admin.instance.v1.InstanceConfig in project spanner-jdbc by olavloite.

the class AbstractSpecificIntegrationTest method createInstance.

private static void createInstance() {
    InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
    InstanceConfig config = instanceAdminClient.getInstanceConfig("regional-europe-west1");
    Instance instance = instanceAdminClient.newInstanceBuilder(InstanceId.of(projectId, instanceId)).setDisplayName("Test Instance").setInstanceConfigId(config.getId()).setNodeCount(1).build();
    Operation<Instance, CreateInstanceMetadata> createInstance = instanceAdminClient.createInstance(instance);
    createInstance = createInstance.waitFor();
}
Also used : InstanceConfig(com.google.cloud.spanner.InstanceConfig) Instance(com.google.cloud.spanner.Instance) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) CreateInstanceMetadata(com.google.spanner.admin.instance.v1.CreateInstanceMetadata)

Example 10 with InstanceConfig

use of com.google.spanner.admin.instance.v1.InstanceConfig in project spanner-jdbc by olavloite.

the class CloudSpannerIT method createInstance.

private void createInstance() {
    InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
    InstanceConfig config = instanceAdminClient.getInstanceConfig("regional-europe-west1");
    Instance instance = instanceAdminClient.newInstanceBuilder(InstanceId.of(getProject(), instanceId)).setDisplayName("Test Instance").setInstanceConfigId(config.getId()).setNodeCount(1).build();
    Operation<Instance, CreateInstanceMetadata> createInstance = instanceAdminClient.createInstance(instance);
    createInstance = createInstance.waitFor();
}
Also used : InstanceConfig(com.google.cloud.spanner.InstanceConfig) Instance(com.google.cloud.spanner.Instance) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) CreateInstanceMetadata(com.google.spanner.admin.instance.v1.CreateInstanceMetadata)

Aggregations

AbstractMessage (com.google.protobuf.AbstractMessage)6 InstanceConfig (com.google.spanner.admin.instance.v1.InstanceConfig)6 Test (org.junit.Test)6 ListInstanceConfigsRequest (com.google.spanner.admin.instance.v1.ListInstanceConfigsRequest)5 ListInstanceConfigsResponse (com.google.spanner.admin.instance.v1.ListInstanceConfigsResponse)5 Instance (com.google.cloud.spanner.Instance)4 InstanceConfig (com.google.cloud.spanner.InstanceConfig)4 CreateInstanceMetadata (com.google.spanner.admin.instance.v1.CreateInstanceMetadata)4 GetInstanceConfigRequest (com.google.spanner.admin.instance.v1.GetInstanceConfigRequest)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3 ArrayList (java.util.ArrayList)3 ExecutionException (java.util.concurrent.ExecutionException)3 ExpectedException (org.junit.rules.ExpectedException)3 GrpcCallContext (com.google.api.gax.grpc.GrpcCallContext)2 InstanceAdminClient (com.google.cloud.spanner.InstanceAdminClient)2 InstanceConfigId (com.google.cloud.spanner.InstanceConfigId)2 InstanceInfo (com.google.cloud.spanner.InstanceInfo)2 ListInstanceConfigsPagedResponse (com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient.ListInstanceConfigsPagedResponse)2 ByteString (com.google.protobuf.ByteString)2 InstanceConfigName (com.google.spanner.admin.instance.v1.InstanceConfigName)2