Search in sources :

Example 6 with InstanceConfigId

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

the class UpdateDatabaseWithDefaultLeaderSampleIT method testUpdateDatabaseWithDefaultLeader.

@Test
public void testUpdateDatabaseWithDefaultLeader() throws Exception {
    // Create database
    final String databaseId = idGenerator.generateDatabaseId();
    final Database createdDatabase = databaseAdminClient.createDatabase(multiRegionalInstanceId, databaseId, Collections.emptyList()).get(5, TimeUnit.MINUTES);
    final String defaultLeader = createdDatabase.getDefaultLeader();
    // Finds a possible new leader option
    final InstanceConfigId instanceConfigId = instanceAdminClient.getInstance(multiRegionalInstanceId).getInstanceConfigId();
    final InstanceConfig config = instanceAdminClient.getInstanceConfig(instanceConfigId.getInstanceConfig());
    final String newLeader = config.getLeaderOptions().stream().filter(leader -> !leader.equals(defaultLeader)).findFirst().orElseThrow(() -> new RuntimeException("Expected to find a leader option different than " + defaultLeader));
    // Runs sample
    final String out = SampleRunner.runSample(() -> UpdateDatabaseWithDefaultLeaderSample.updateDatabaseWithDefaultLeader(projectId, multiRegionalInstanceId, databaseId, newLeader));
    assertTrue("Expected that database new leader would had been updated to " + newLeader + "." + " Output received was " + out, out.contains("Updated default leader to " + newLeader));
}
Also used : InstanceConfigId(com.google.cloud.spanner.InstanceConfigId) InstanceConfig(com.google.cloud.spanner.InstanceConfig) Database(com.google.cloud.spanner.Database) Test(org.junit.Test)

Example 7 with InstanceConfigId

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

the class GetDatabaseDdlSampleIT method testGetDatabaseDdl.

@Test
public void testGetDatabaseDdl() 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(() -> GetDatabaseDdlSample.getDatabaseDdl(projectId, multiRegionalInstanceId, databaseId));
    assertTrue("Expected to have retrieved database DDL for " + databaseId + "." + " Output received was " + out, out.contains("Retrieved database DDL for " + databaseId));
    assertTrue("Expected leader to be set to " + defaultLeader + "." + " Output received was " + out, out.contains("default_leader = '" + defaultLeader + "'"));
    assertTrue("Expected table to have been created in " + databaseId + "." + " Output received was " + out, out.contains("CREATE TABLE Singers"));
}
Also used : InstanceConfigId(com.google.cloud.spanner.InstanceConfigId) InstanceConfig(com.google.cloud.spanner.InstanceConfig) Test(org.junit.Test)

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