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));
}
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"));
}
Aggregations