Search in sources :

Example 1 with InstanceConfigId

use of com.google.cloud.spanner.InstanceConfigId in project java-spanner by googleapis.

the class CreateDatabaseWithDefaultLeaderSampleIT method testCreateDatabaseWithDefaultLeader.

@Test
public void testCreateDatabaseWithDefaultLeader() throws Exception {
    final String databaseId = idGenerator.generateDatabaseId();
    // Finds possible default leader
    final InstanceConfigId instanceConfigId = instanceAdminClient.getInstance(multiRegionalInstanceId).getInstanceConfigId();
    final InstanceConfig config = instanceAdminClient.getInstanceConfig(instanceConfigId.getInstanceConfig());
    assertTrue("Expected instance config " + instanceConfigId + " to have at least one leader option", config.getLeaderOptions().size() > 0);
    final String defaultLeader = config.getLeaderOptions().get(0);
    // Runs sample
    final String out = SampleRunner.runSample(() -> CreateDatabaseWithDefaultLeaderSample.createDatabaseWithDefaultLeader(projectId, multiRegionalInstanceId, databaseId, defaultLeader));
    assertTrue("Expected created database to have default leader " + defaultLeader + "." + " Output received was " + out, out.contains("Default leader: " + defaultLeader));
}
Also used : InstanceConfigId(com.google.cloud.spanner.InstanceConfigId) InstanceConfig(com.google.cloud.spanner.InstanceConfig) Test(org.junit.Test)

Example 2 with InstanceConfigId

use of com.google.cloud.spanner.InstanceConfigId in project java-spanner by googleapis.

the class QueryInformationSchemaDatabaseOptionsSampleIT method testQueryInformationSchemaDatabaseOptions.

@Test
public void testQueryInformationSchemaDatabaseOptions() throws Exception {
    // Finds a possible new leader option
    final InstanceConfigId instanceConfigId = instanceAdminClient.getInstance(multiRegionalInstanceId).getInstanceConfigId();
    final InstanceConfig config = instanceAdminClient.getInstanceConfig(instanceConfigId.getInstanceConfig());
    assertTrue("Expected instance config " + instanceConfigId + " to have at least one leader option", config.getLeaderOptions().size() > 0);
    final String defaultLeader = config.getLeaderOptions().get(0);
    // Creates database
    final String databaseId = idGenerator.generateDatabaseId();
    databaseAdminClient.createDatabase(multiRegionalInstanceId, databaseId, Arrays.asList("CREATE TABLE Singers (Id INT64 NOT NULL) PRIMARY KEY (Id)", "ALTER DATABASE `" + databaseId + "` SET OPTIONS ( default_leader = '" + defaultLeader + "')")).get(5, TimeUnit.MINUTES);
    // Runs sample
    final String out = SampleRunner.runSample(() -> QueryInformationSchemaDatabaseOptionsSample.queryInformationSchemaDatabaseOptions(projectId, multiRegionalInstanceId, databaseId));
    assertTrue("Expected to have retrieved default_leader for " + databaseId + " as " + defaultLeader + "." + " Output received was " + out, out.contains("The default_leader for projects/" + projectId + "/instances/" + multiRegionalInstanceId + "/databases/" + databaseId + " is " + defaultLeader));
}
Also used : InstanceConfigId(com.google.cloud.spanner.InstanceConfigId) InstanceConfig(com.google.cloud.spanner.InstanceConfig) Test(org.junit.Test)

Example 3 with InstanceConfigId

use of com.google.cloud.spanner.InstanceConfigId 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 4 with InstanceConfigId

use of com.google.cloud.spanner.InstanceConfigId 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 5 with InstanceConfigId

use of com.google.cloud.spanner.InstanceConfigId in project testcontainers-java by testcontainers.

the class SpannerEmulatorContainerTest method createInstance.

// }
// createInstance {
private InstanceId createInstance(Spanner spanner) throws InterruptedException, ExecutionException {
    InstanceConfigId instanceConfig = InstanceConfigId.of(PROJECT_NAME, "emulator-config");
    InstanceId instanceId = InstanceId.of(PROJECT_NAME, INSTANCE_NAME);
    InstanceAdminClient insAdminClient = spanner.getInstanceAdminClient();
    Instance instance = insAdminClient.createInstance(InstanceInfo.newBuilder(instanceId).setNodeCount(1).setDisplayName("Test instance").setInstanceConfigId(instanceConfig).build()).get();
    return instanceId;
}
Also used : InstanceConfigId(com.google.cloud.spanner.InstanceConfigId) InstanceId(com.google.cloud.spanner.InstanceId) Instance(com.google.cloud.spanner.Instance) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient)

Aggregations

InstanceConfigId (com.google.cloud.spanner.InstanceConfigId)7 InstanceConfig (com.google.cloud.spanner.InstanceConfig)6 Test (org.junit.Test)4 Instance (com.google.cloud.spanner.Instance)3 InstanceInfo (com.google.cloud.spanner.InstanceInfo)2 CreateInstanceMetadata (com.google.spanner.admin.instance.v1.CreateInstanceMetadata)2 Database (com.google.cloud.spanner.Database)1 InstanceAdminClient (com.google.cloud.spanner.InstanceAdminClient)1 InstanceId (com.google.cloud.spanner.InstanceId)1 SpannerException (com.google.cloud.spanner.SpannerException)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 ExpectedException (org.junit.rules.ExpectedException)1