From edc5bbf0d9d4faf48fd9a8d479d5bc5de938c82d Mon Sep 17 00:00:00 2001 From: Arpan Mishra Date: Mon, 6 Feb 2023 13:29:38 +0530 Subject: [PATCH 1/7] fix: prevent illegal negative timeout values into thread sleep() method while retrying exceptions in unit tests. * For details on issue see - https://github.com/googleapis/java-spanner/issues/2206 --- .../com/google/cloud/spanner/it/ITClosedSessionTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITClosedSessionTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITClosedSessionTest.java index aeb0256285b..227611a10de 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITClosedSessionTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITClosedSessionTest.java @@ -251,7 +251,10 @@ public void testTransactionManager() throws InterruptedException { break; } } catch (AbortedException e) { - Thread.sleep(e.getRetryDelayInMillis()); + long retryDelayInMillis = e.getRetryDelayInMillis(); + if(retryDelayInMillis > 0) { + Thread.sleep(retryDelayInMillis); + } txn = manager.resetForRetry(); } } From 4cd497b05eab3e3b6b89b582bfafde80d42c1518 Mon Sep 17 00:00:00 2001 From: Arpan Mishra Date: Wed, 8 Feb 2023 15:27:18 +0530 Subject: [PATCH 2/7] Fixing lint issues. --- .../java/com/google/cloud/spanner/it/ITClosedSessionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITClosedSessionTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITClosedSessionTest.java index 227611a10de..efbffcfa899 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITClosedSessionTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITClosedSessionTest.java @@ -252,7 +252,7 @@ public void testTransactionManager() throws InterruptedException { } } catch (AbortedException e) { long retryDelayInMillis = e.getRetryDelayInMillis(); - if(retryDelayInMillis > 0) { + if (retryDelayInMillis > 0) { Thread.sleep(retryDelayInMillis); } txn = manager.resetForRetry(); From fd4fa001222a562a481a6a17494a4f4327e75c42 Mon Sep 17 00:00:00 2001 From: Arpan Mishra Date: Fri, 23 Feb 2024 20:15:03 +0530 Subject: [PATCH 3/7] chore: remove unused imports. --- .../spanner/admin/generated/CreateBackupWithEncryptionKey.java | 2 -- .../generated/CreateTableWithForeignKeyDeleteCascadeSample.java | 1 - .../spanner/admin/generated/RestoreBackupWithEncryptionKey.java | 1 - .../admin/generated/UpdateDatabaseWithDefaultLeaderSample.java | 1 - 4 files changed, 5 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateBackupWithEncryptionKey.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateBackupWithEncryptionKey.java index 39f070e639e..76276c7e121 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateBackupWithEncryptionKey.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateBackupWithEncryptionKey.java @@ -20,13 +20,11 @@ import com.google.cloud.spanner.SpannerExceptionFactory; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; -import com.google.cloud.spanner.encryption.EncryptionConfigs; import com.google.protobuf.Timestamp; import com.google.spanner.admin.database.v1.Backup; import com.google.spanner.admin.database.v1.BackupName; import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig.EncryptionType; -import com.google.spanner.admin.database.v1.CreateBackupMetadata; import com.google.spanner.admin.database.v1.CreateBackupRequest; import com.google.spanner.admin.database.v1.DatabaseName; import com.google.spanner.admin.database.v1.InstanceName; diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateTableWithForeignKeyDeleteCascadeSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateTableWithForeignKeyDeleteCascadeSample.java index cc46f1e214e..04474f7e3b5 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateTableWithForeignKeyDeleteCascadeSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateTableWithForeignKeyDeleteCascadeSample.java @@ -20,7 +20,6 @@ import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; -import com.google.spanner.admin.database.v1.CreateDatabaseRequest; import com.google.spanner.admin.database.v1.DatabaseName; import java.io.IOException; diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/RestoreBackupWithEncryptionKey.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/RestoreBackupWithEncryptionKey.java index 9c2ef5b3a75..714eb67af21 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/RestoreBackupWithEncryptionKey.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/RestoreBackupWithEncryptionKey.java @@ -24,7 +24,6 @@ import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.spanner.admin.database.v1.BackupName; import com.google.spanner.admin.database.v1.Database; -import com.google.spanner.admin.database.v1.DatabaseName; import com.google.spanner.admin.database.v1.InstanceName; import com.google.spanner.admin.database.v1.RestoreDatabaseEncryptionConfig; import com.google.spanner.admin.database.v1.RestoreDatabaseRequest; diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseWithDefaultLeaderSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseWithDefaultLeaderSample.java index 5306358714e..0e72214b97b 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseWithDefaultLeaderSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseWithDefaultLeaderSample.java @@ -18,7 +18,6 @@ //[START spanner_update_database_with_default_leader] -import com.google.api.gax.longrunning.OperationFuture; import com.google.cloud.spanner.SpannerException; import com.google.cloud.spanner.SpannerExceptionFactory; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; From 65d79431827ddc7f4d487726f0bf967210bb7941 Mon Sep 17 00:00:00 2001 From: Arpan Mishra Date: Fri, 23 Feb 2024 20:26:27 +0530 Subject: [PATCH 4/7] chore: replace with new client creation methods. --- .../generated/AddAndDropDatabaseRole.java | 70 ++++++++------- .../generated/CreateInstanceConfigSample.java | 85 ++++++++++--------- 2 files changed, 84 insertions(+), 71 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java index d88fe72dc8f..f54b558fc2f 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java @@ -18,17 +18,18 @@ // [START spanner_add_and_drop_database_role] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class AddAndDropDatabaseRole { - static void addAndDropDatabaseRole() throws IOException { + static void addAndDropDatabaseRole() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -39,36 +40,41 @@ static void addAndDropDatabaseRole() throws IOException { } static void addAndDropDatabaseRole( - String projectId, String instanceId, String databaseId, String parentRole, String childRole) - throws IOException { - final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - try { - System.out.println("Waiting for role create operation to complete..."); - databaseAdminClient.updateDatabaseDdlAsync( - DatabaseName.of(projectId, instanceId, databaseId), - ImmutableList.of( - String.format("CREATE ROLE %s", parentRole), - String.format("GRANT SELECT ON TABLE Albums TO ROLE %s", parentRole), - String.format("CREATE ROLE %s", childRole), - String.format("GRANT ROLE %s TO ROLE %s", parentRole, childRole))) - .get(5, TimeUnit.MINUTES); - System.out.printf( - "Created roles %s and %s and granted privileges%n", parentRole, childRole); - // Delete role and membership. - System.out.println("Waiting for role revoke & drop operation to complete..."); - databaseAdminClient.updateDatabaseDdlAsync( - DatabaseName.of(projectId, instanceId, databaseId), - ImmutableList.of( - String.format("REVOKE ROLE %s FROM ROLE %s", parentRole, childRole), - String.format("DROP ROLE %s", childRole))).get(5, TimeUnit.MINUTES); - System.out.printf("Revoked privileges and dropped role %s%n", childRole); - } catch (ExecutionException | TimeoutException e) { - System.out.printf( - "Error: AddAndDropDatabaseRole failed with error message %s\n", e.getMessage()); - e.printStackTrace(); - } catch (InterruptedException e) { - System.out.println( - "Error: Waiting for AddAndDropDatabaseRole operation to finish was interrupted"); + String projectId, String instanceId, String databaseId, + String parentRole, String childRole) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService()) { + try(final DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + System.out.println("Waiting for role create operation to complete..."); + databaseAdminClient.updateDatabaseDdlAsync( + DatabaseName.of(projectId, instanceId, databaseId), + ImmutableList.of( + String.format("CREATE ROLE %s", parentRole), + String.format("GRANT SELECT ON TABLE Albums TO ROLE %s", parentRole), + String.format("CREATE ROLE %s", childRole), + String.format("GRANT ROLE %s TO ROLE %s", parentRole, childRole))) + .get(5, TimeUnit.MINUTES); + System.out.printf( + "Created roles %s and %s and granted privileges%n", parentRole, childRole); + // Delete role and membership. + System.out.println("Waiting for role revoke & drop operation to complete..."); + databaseAdminClient.updateDatabaseDdlAsync( + DatabaseName.of(projectId, instanceId, databaseId), + ImmutableList.of( + String.format("REVOKE ROLE %s FROM ROLE %s", parentRole, childRole), + String.format("DROP ROLE %s", childRole))).get(5, TimeUnit.MINUTES); + System.out.printf("Revoked privileges and dropped role %s%n", childRole); + } catch (ExecutionException | TimeoutException e) { + System.out.printf( + "Error: AddAndDropDatabaseRole failed with error message %s\n", e.getMessage()); + e.printStackTrace(); + } catch (InterruptedException e) { + System.out.println( + "Error: Waiting for AddAndDropDatabaseRole operation to finish was interrupted"); + } } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java index 49ce023b24a..196c620ec03 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java @@ -18,13 +18,14 @@ // [START spanner_create_instance_config] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; import com.google.spanner.admin.instance.v1.CreateInstanceConfigRequest; import com.google.spanner.admin.instance.v1.InstanceConfig; import com.google.spanner.admin.instance.v1.InstanceConfigName; import com.google.spanner.admin.instance.v1.ProjectName; import com.google.spanner.admin.instance.v1.ReplicaInfo; -import java.io.IOException; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -34,7 +35,7 @@ class CreateInstanceConfigSample { - static void createInstanceConfig() throws IOException { + static void createInstanceConfig() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String baseInstanceConfigId = "nam11"; @@ -44,43 +45,49 @@ static void createInstanceConfig() throws IOException { } static void createInstanceConfig( - String projectId, String baseInstanceConfigId, String instanceConfigId) throws IOException { - try (InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { - final InstanceConfigName baseInstanceConfigName = InstanceConfigName.of(projectId, - baseInstanceConfigId); - final InstanceConfig baseConfig = - instanceAdminClient.getInstanceConfig(baseInstanceConfigName.toString()); - final InstanceConfigName instanceConfigName = InstanceConfigName.of(projectId, - instanceConfigId); - /** - * The replicas for the custom instance configuration must include all the replicas of the - * base configuration, in addition to at least one from the list of optional replicas of the - * base configuration. - */ - final List replicas = - Stream.concat(baseConfig.getReplicasList().stream(), - baseConfig.getOptionalReplicasList().stream().limit(1)).collect(Collectors.toList()); - final InstanceConfig instanceConfig = - InstanceConfig.newBuilder().setName(instanceConfigName.toString()) - .setBaseConfig(baseInstanceConfigName.toString()) - .setDisplayName("Instance Configuration").addAllReplicas(replicas).build(); - final CreateInstanceConfigRequest createInstanceConfigRequest = - CreateInstanceConfigRequest.newBuilder().setParent(ProjectName.of(projectId).toString()) - .setInstanceConfigId(instanceConfigId).setInstanceConfig(instanceConfig).build(); - try { - System.out.printf("Waiting for create operation for %s to complete...\n", - instanceConfigName); - InstanceConfig instanceConfigResult = - instanceAdminClient.createInstanceConfigAsync( - createInstanceConfigRequest).get(5, TimeUnit.MINUTES); - System.out.printf("Created instance configuration %s\n", instanceConfigResult.getName()); - } catch (ExecutionException | TimeoutException e) { - System.out.printf( - "Error: Creating instance configuration %s failed with error message %s\n", - instanceConfig.getName(), e.getMessage()); - } catch (InterruptedException e) { - System.out.println( - "Error: Waiting for createInstanceConfig operation to finish was interrupted"); + String projectId, String baseInstanceConfigId, String instanceConfigId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService()) { + try (InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { + final InstanceConfigName baseInstanceConfigName = InstanceConfigName.of(projectId, + baseInstanceConfigId); + final InstanceConfig baseConfig = + instanceAdminClient.getInstanceConfig(baseInstanceConfigName.toString()); + final InstanceConfigName instanceConfigName = InstanceConfigName.of(projectId, + instanceConfigId); + /** + * The replicas for the custom instance configuration must include all the replicas of the + * base configuration, in addition to at least one from the list of optional replicas of the + * base configuration. + */ + final List replicas = + Stream.concat(baseConfig.getReplicasList().stream(), + baseConfig.getOptionalReplicasList().stream().limit(1)).collect(Collectors.toList()); + final InstanceConfig instanceConfig = + InstanceConfig.newBuilder().setName(instanceConfigName.toString()) + .setBaseConfig(baseInstanceConfigName.toString()) + .setDisplayName("Instance Configuration").addAllReplicas(replicas).build(); + final CreateInstanceConfigRequest createInstanceConfigRequest = + CreateInstanceConfigRequest.newBuilder().setParent(ProjectName.of(projectId).toString()) + .setInstanceConfigId(instanceConfigId).setInstanceConfig(instanceConfig).build(); + try { + System.out.printf("Waiting for create operation for %s to complete...\n", + instanceConfigName); + InstanceConfig instanceConfigResult = + instanceAdminClient.createInstanceConfigAsync( + createInstanceConfigRequest).get(5, TimeUnit.MINUTES); + System.out.printf("Created instance configuration %s\n", instanceConfigResult.getName()); + } catch (ExecutionException | TimeoutException e) { + System.out.printf( + "Error: Creating instance configuration %s failed with error message %s\n", + instanceConfig.getName(), e.getMessage()); + } catch (InterruptedException e) { + System.out.println( + "Error: Waiting for createInstanceConfig operation to finish was interrupted"); + } } } } From 7743b142751e76119c9c2b3b29e6f0f68a2af172 Mon Sep 17 00:00:00 2001 From: Arpan Mishra Date: Fri, 23 Feb 2024 21:49:13 +0530 Subject: [PATCH 5/7] Update samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Knut Olav Løite --- .../example/spanner/admin/generated/AddAndDropDatabaseRole.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java index f54b558fc2f..5fd78c87aa6 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java @@ -47,7 +47,7 @@ static void addAndDropDatabaseRole( .setProjectId(projectId) .build() .getService()) { - try(final DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + try(DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { System.out.println("Waiting for role create operation to complete..."); databaseAdminClient.updateDatabaseDdlAsync( DatabaseName.of(projectId, instanceId, databaseId), From 48f100ebf6b0a81f20de4ddbbf5257d67e748eb0 Mon Sep 17 00:00:00 2001 From: Arpan Mishra Date: Sat, 24 Feb 2024 13:32:54 +0530 Subject: [PATCH 6/7] chore: address comments. --- .../generated/AddAndDropDatabaseRole.java | 57 +++++++------- .../generated/CreateInstanceConfigSample.java | 75 +++++++++---------- .../admin/generated/SampleTestBaseV2.java | 2 + 3 files changed, 67 insertions(+), 67 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java index 5fd78c87aa6..69a154e8826 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddAndDropDatabaseRole.java @@ -46,35 +46,34 @@ static void addAndDropDatabaseRole( SpannerOptions.newBuilder() .setProjectId(projectId) .build() - .getService()) { - try(DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { - System.out.println("Waiting for role create operation to complete..."); - databaseAdminClient.updateDatabaseDdlAsync( - DatabaseName.of(projectId, instanceId, databaseId), - ImmutableList.of( - String.format("CREATE ROLE %s", parentRole), - String.format("GRANT SELECT ON TABLE Albums TO ROLE %s", parentRole), - String.format("CREATE ROLE %s", childRole), - String.format("GRANT ROLE %s TO ROLE %s", parentRole, childRole))) - .get(5, TimeUnit.MINUTES); - System.out.printf( - "Created roles %s and %s and granted privileges%n", parentRole, childRole); - // Delete role and membership. - System.out.println("Waiting for role revoke & drop operation to complete..."); - databaseAdminClient.updateDatabaseDdlAsync( - DatabaseName.of(projectId, instanceId, databaseId), - ImmutableList.of( - String.format("REVOKE ROLE %s FROM ROLE %s", parentRole, childRole), - String.format("DROP ROLE %s", childRole))).get(5, TimeUnit.MINUTES); - System.out.printf("Revoked privileges and dropped role %s%n", childRole); - } catch (ExecutionException | TimeoutException e) { - System.out.printf( - "Error: AddAndDropDatabaseRole failed with error message %s\n", e.getMessage()); - e.printStackTrace(); - } catch (InterruptedException e) { - System.out.println( - "Error: Waiting for AddAndDropDatabaseRole operation to finish was interrupted"); - } + .getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + System.out.println("Waiting for role create operation to complete..."); + databaseAdminClient.updateDatabaseDdlAsync( + DatabaseName.of(projectId, instanceId, databaseId), + ImmutableList.of( + String.format("CREATE ROLE %s", parentRole), + String.format("GRANT SELECT ON TABLE Albums TO ROLE %s", parentRole), + String.format("CREATE ROLE %s", childRole), + String.format("GRANT ROLE %s TO ROLE %s", parentRole, childRole))) + .get(5, TimeUnit.MINUTES); + System.out.printf( + "Created roles %s and %s and granted privileges%n", parentRole, childRole); + // Delete role and membership. + System.out.println("Waiting for role revoke & drop operation to complete..."); + databaseAdminClient.updateDatabaseDdlAsync( + DatabaseName.of(projectId, instanceId, databaseId), + ImmutableList.of( + String.format("REVOKE ROLE %s FROM ROLE %s", parentRole, childRole), + String.format("DROP ROLE %s", childRole))).get(5, TimeUnit.MINUTES); + System.out.printf("Revoked privileges and dropped role %s%n", childRole); + } catch (ExecutionException | TimeoutException e) { + System.out.printf( + "Error: AddAndDropDatabaseRole failed with error message %s\n", e.getMessage()); + e.printStackTrace(); + } catch (InterruptedException e) { + System.out.println( + "Error: Waiting for AddAndDropDatabaseRole operation to finish was interrupted"); } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java index 196c620ec03..8671c6eda38 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java @@ -50,44 +50,43 @@ static void createInstanceConfig( SpannerOptions.newBuilder() .setProjectId(projectId) .build() - .getService()) { - try (InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { - final InstanceConfigName baseInstanceConfigName = InstanceConfigName.of(projectId, - baseInstanceConfigId); - final InstanceConfig baseConfig = - instanceAdminClient.getInstanceConfig(baseInstanceConfigName.toString()); - final InstanceConfigName instanceConfigName = InstanceConfigName.of(projectId, - instanceConfigId); - /** - * The replicas for the custom instance configuration must include all the replicas of the - * base configuration, in addition to at least one from the list of optional replicas of the - * base configuration. - */ - final List replicas = - Stream.concat(baseConfig.getReplicasList().stream(), - baseConfig.getOptionalReplicasList().stream().limit(1)).collect(Collectors.toList()); - final InstanceConfig instanceConfig = - InstanceConfig.newBuilder().setName(instanceConfigName.toString()) - .setBaseConfig(baseInstanceConfigName.toString()) - .setDisplayName("Instance Configuration").addAllReplicas(replicas).build(); - final CreateInstanceConfigRequest createInstanceConfigRequest = - CreateInstanceConfigRequest.newBuilder().setParent(ProjectName.of(projectId).toString()) - .setInstanceConfigId(instanceConfigId).setInstanceConfig(instanceConfig).build(); - try { - System.out.printf("Waiting for create operation for %s to complete...\n", - instanceConfigName); - InstanceConfig instanceConfigResult = - instanceAdminClient.createInstanceConfigAsync( - createInstanceConfigRequest).get(5, TimeUnit.MINUTES); - System.out.printf("Created instance configuration %s\n", instanceConfigResult.getName()); - } catch (ExecutionException | TimeoutException e) { - System.out.printf( - "Error: Creating instance configuration %s failed with error message %s\n", - instanceConfig.getName(), e.getMessage()); - } catch (InterruptedException e) { - System.out.println( - "Error: Waiting for createInstanceConfig operation to finish was interrupted"); - } + .getService(); + InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { + final InstanceConfigName baseInstanceConfigName = InstanceConfigName.of(projectId, + baseInstanceConfigId); + final InstanceConfig baseConfig = + instanceAdminClient.getInstanceConfig(baseInstanceConfigName.toString()); + final InstanceConfigName instanceConfigName = InstanceConfigName.of(projectId, + instanceConfigId); + /** + * The replicas for the custom instance configuration must include all the replicas of the + * base configuration, in addition to at least one from the list of optional replicas of the + * base configuration. + */ + final List replicas = + Stream.concat(baseConfig.getReplicasList().stream(), + baseConfig.getOptionalReplicasList().stream().limit(1)).collect(Collectors.toList()); + final InstanceConfig instanceConfig = + InstanceConfig.newBuilder().setName(instanceConfigName.toString()) + .setBaseConfig(baseInstanceConfigName.toString()) + .setDisplayName("Instance Configuration").addAllReplicas(replicas).build(); + final CreateInstanceConfigRequest createInstanceConfigRequest = + CreateInstanceConfigRequest.newBuilder().setParent(ProjectName.of(projectId).toString()) + .setInstanceConfigId(instanceConfigId).setInstanceConfig(instanceConfig).build(); + try { + System.out.printf("Waiting for create operation for %s to complete...\n", + instanceConfigName); + InstanceConfig instanceConfigResult = + instanceAdminClient.createInstanceConfigAsync( + createInstanceConfigRequest).get(5, TimeUnit.MINUTES); + System.out.printf("Created instance configuration %s\n", instanceConfigResult.getName()); + } catch (ExecutionException | TimeoutException e) { + System.out.printf( + "Error: Creating instance configuration %s failed with error message %s\n", + instanceConfig.getName(), e.getMessage()); + } catch (InterruptedException e) { + System.out.println( + "Error: Waiting for createInstanceConfig operation to finish was interrupted"); } } } diff --git a/samples/snippets/src/test/java/com/example/spanner/admin/generated/SampleTestBaseV2.java b/samples/snippets/src/test/java/com/example/spanner/admin/generated/SampleTestBaseV2.java index e5ff002110c..32e1c26fcb8 100644 --- a/samples/snippets/src/test/java/com/example/spanner/admin/generated/SampleTestBaseV2.java +++ b/samples/snippets/src/test/java/com/example/spanner/admin/generated/SampleTestBaseV2.java @@ -113,6 +113,7 @@ public static void afterClass() throws InterruptedException { } } for (String backupId : idGenerator.getBackupIds()) { + System.out.println("Trying to delete " + backupId); try { // If the backup is not found, it is ignored (no exception is thrown) databaseAdminClient.deleteBackup( @@ -125,6 +126,7 @@ public static void afterClass() throws InterruptedException { } } for (String configId : idGenerator.getInstanceConfigIds()) { + System.out.println("Trying to delete " + configId); try { // If the config is not found, it is ignored (no exception is thrown) instanceAdminClient.deleteInstanceConfig(getInstanceConfigName(projectId, configId)); From fdd7217c56f445bb99b9f4f5106b7bec27e4d421 Mon Sep 17 00:00:00 2001 From: Arpan Mishra Date: Sat, 24 Feb 2024 15:57:40 +0530 Subject: [PATCH 7/7] chore: replace with new client creation methods. --- .../admin/generated/AddJsonColumnSample.java | 29 +++-- .../admin/generated/AddJsonbColumnSample.java | 31 +++-- .../generated/AddNumericColumnSample.java | 28 +++-- .../admin/generated/AlterSequenceSample.java | 10 +- ...ableWithForeignKeyDeleteCascadeSample.java | 39 ++++--- .../admin/generated/CopyBackupSample.java | 9 +- .../CreateBackupWithEncryptionKey.java | 9 +- .../CreateDatabaseWithEncryptionKey.java | 9 +- ...abaseWithVersionRetentionPeriodSample.java | 13 ++- .../generated/CreateInstanceExample.java | 18 ++- ...eInstanceWithAutoscalingConfigExample.java | 14 ++- ...ateInstanceWithProcessingUnitsExample.java | 15 ++- .../admin/generated/CreateSequenceSample.java | 11 +- ...ableWithForeignKeyDeleteCascadeSample.java | 52 +++++---- .../generated/DeleteInstanceConfigSample.java | 14 ++- ...reignKeyConstraintDeleteCascadeSample.java | 34 +++--- .../admin/generated/DropSequenceSample.java | 13 ++- .../generated/EnableFineGrainedAccess.java | 110 +++++++++--------- .../admin/generated/GetDatabaseDdlSample.java | 24 ++-- .../generated/GetInstanceConfigSample.java | 14 ++- .../admin/generated/ListDatabaseRoles.java | 27 +++-- .../admin/generated/ListDatabasesSample.java | 33 +++--- .../ListInstanceConfigOperationsSample.java | 14 ++- .../generated/ListInstanceConfigsSample.java | 14 ++- .../generated/PgAlterSequenceSample.java | 11 +- .../generated/PgCaseSensitivitySample.java | 13 +-- .../generated/PgCreateSequenceSample.java | 11 +- .../admin/generated/PgDropSequenceSample.java | 10 +- .../generated/PgInterleavedTableSample.java | 14 ++- .../admin/generated/PgSpannerSample.java | 5 +- .../RestoreBackupWithEncryptionKey.java | 12 +- .../admin/generated/SpannerSample.java | 4 +- .../admin/generated/UpdateDatabaseSample.java | 12 +- ...UpdateDatabaseWithDefaultLeaderSample.java | 14 +-- .../generated/UpdateInstanceConfigSample.java | 14 ++- 35 files changed, 399 insertions(+), 305 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddJsonColumnSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddJsonColumnSample.java index 32d1daea2d1..69d2c229e2e 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddJsonColumnSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddJsonColumnSample.java @@ -17,15 +17,17 @@ package com.example.spanner.admin.generated; // [START spanner_add_json_column] + +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.concurrent.ExecutionException; class AddJsonColumnSample { - static void addJsonColumn() throws InterruptedException, ExecutionException, IOException { + static void addJsonColumn() throws InterruptedException, ExecutionException { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -35,15 +37,20 @@ static void addJsonColumn() throws InterruptedException, ExecutionException, IOE } static void addJsonColumn(String projectId, String instanceId, String databaseId) - throws InterruptedException, ExecutionException, IOException { - final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - - // Wait for the operation to finish. - // This will throw an ExecutionException if the operation fails. - databaseAdminClient.updateDatabaseDdlAsync( - DatabaseName.of(projectId, instanceId, databaseId), - ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSON")).get(); - System.out.printf("Successfully added column `VenueDetails`%n"); + throws InterruptedException, ExecutionException { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + // Wait for the operation to finish. + // This will throw an ExecutionException if the operation fails. + databaseAdminClient.updateDatabaseDdlAsync( + DatabaseName.of(projectId, instanceId, databaseId), + ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSON")).get(); + System.out.printf("Successfully added column `VenueDetails`%n"); + } } } // [END spanner_add_json_column] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddJsonbColumnSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddJsonbColumnSample.java index 800c2d3d655..fa5604e65ee 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddJsonbColumnSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddJsonbColumnSample.java @@ -17,15 +17,17 @@ package com.example.spanner.admin.generated; // [START spanner_postgresql_jsonb_add_column] + +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.concurrent.ExecutionException; class AddJsonbColumnSample { - static void addJsonbColumn() throws InterruptedException, ExecutionException, IOException { + static void addJsonbColumn() throws InterruptedException, ExecutionException { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -35,16 +37,21 @@ static void addJsonbColumn() throws InterruptedException, ExecutionException, IO } static void addJsonbColumn(String projectId, String instanceId, String databaseId) - throws InterruptedException, ExecutionException, IOException { - final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - - // JSONB datatype is only supported with PostgreSQL-dialect databases. - // Wait for the operation to finish. - // This will throw an ExecutionException if the operation fails. - databaseAdminClient.updateDatabaseDdlAsync( - DatabaseName.of(projectId, instanceId, databaseId), - ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSONB")).get(); - System.out.printf("Successfully added column `VenueDetails`%n"); + throws InterruptedException, ExecutionException { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + // JSONB datatype is only supported with PostgreSQL-dialect databases. + // Wait for the operation to finish. + // This will throw an ExecutionException if the operation fails. + databaseAdminClient.updateDatabaseDdlAsync( + DatabaseName.of(projectId, instanceId, databaseId), + ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSONB")).get(); + System.out.printf("Successfully added column `VenueDetails`%n"); + } } } // [END spanner_postgresql_jsonb_add_column] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddNumericColumnSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddNumericColumnSample.java index 191e0377b66..09f4e632167 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddNumericColumnSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddNumericColumnSample.java @@ -18,15 +18,16 @@ // [START spanner_add_numeric_column] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.concurrent.ExecutionException; class AddNumericColumnSample { - static void addNumericColumn() throws InterruptedException, ExecutionException, IOException { + static void addNumericColumn() throws InterruptedException, ExecutionException { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -36,15 +37,20 @@ static void addNumericColumn() throws InterruptedException, ExecutionException, } static void addNumericColumn(String projectId, String instanceId, String databaseId) - throws InterruptedException, ExecutionException, IOException { - final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - - // Wait for the operation to finish. - // This will throw an ExecutionException if the operation fails. - databaseAdminClient.updateDatabaseDdlAsync( - DatabaseName.of(projectId, instanceId, databaseId), - ImmutableList.of("ALTER TABLE Venues ADD COLUMN Revenue NUMERIC")).get(); - System.out.printf("Successfully added column `Revenue`%n"); + throws InterruptedException, ExecutionException { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + // Wait for the operation to finish. + // This will throw an ExecutionException if the operation fails. + databaseAdminClient.updateDatabaseDdlAsync( + DatabaseName.of(projectId, instanceId, databaseId), + ImmutableList.of("ALTER TABLE Venues ADD COLUMN Revenue NUMERIC")).get(); + System.out.printf("Successfully added column `Revenue`%n"); + } } } // [END spanner_add_numeric_column] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterSequenceSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterSequenceSample.java index 05ae63a7a53..08b02b4243c 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterSequenceSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterSequenceSample.java @@ -28,7 +28,6 @@ import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -36,7 +35,7 @@ public class AlterSequenceSample { - static void alterSequence() throws IOException { + static void alterSequence() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; @@ -44,11 +43,10 @@ static void alterSequence() throws IOException { alterSequence(projectId, instanceId, databaseId); } - static void alterSequence(String projectId, String instanceId, String databaseId) - throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); + static void alterSequence(String projectId, String instanceId, String databaseId) { try (Spanner spanner = - SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { databaseAdminClient .updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId, databaseId), diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterTableWithForeignKeyDeleteCascadeSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterTableWithForeignKeyDeleteCascadeSample.java index 5784bfab0c7..60f386c75f2 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterTableWithForeignKeyDeleteCascadeSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterTableWithForeignKeyDeleteCascadeSample.java @@ -18,14 +18,15 @@ // [START spanner_alter_table_with_foreign_key_delete_cascade] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; class AlterTableWithForeignKeyDeleteCascadeSample { - static void alterForeignKeyDeleteCascadeConstraint() throws IOException { + static void alterForeignKeyDeleteCascadeConstraint() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -35,22 +36,24 @@ static void alterForeignKeyDeleteCascadeConstraint() throws IOException { } static void alterForeignKeyDeleteCascadeConstraint( - String projectId, String instanceId, String databaseId) throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - - databaseAdminClient.updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId, - databaseId), - ImmutableList.of( - "ALTER TABLE ShoppingCarts\n" - + " ADD CONSTRAINT FKShoppingCartsCustomerName\n" - + " FOREIGN KEY (CustomerName)\n" - + " REFERENCES Customers(CustomerName)\n" - + " ON DELETE CASCADE\n")); - System.out.printf( - String.format( - "Altered ShoppingCarts table with FKShoppingCartsCustomerName\n" - + "foreign key constraint on database %s on instance %s", - databaseId, instanceId)); + String projectId, String instanceId, String databaseId) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + databaseAdminClient.updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId, + databaseId), + ImmutableList.of( + "ALTER TABLE ShoppingCarts\n" + + " ADD CONSTRAINT FKShoppingCartsCustomerName\n" + + " FOREIGN KEY (CustomerName)\n" + + " REFERENCES Customers(CustomerName)\n" + + " ON DELETE CASCADE\n")); + System.out.printf( + String.format( + "Altered ShoppingCarts table with FKShoppingCartsCustomerName\n" + + "foreign key constraint on database %s on instance %s", + databaseId, instanceId)); + } } } // [END spanner_alter_table_with_foreign_key_delete_cascade] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CopyBackupSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CopyBackupSample.java index ebac469ca7a..245abd94299 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CopyBackupSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CopyBackupSample.java @@ -19,13 +19,14 @@ // [START spanner_copy_backup] import com.google.cloud.Timestamp; +import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerException; import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.spanner.admin.database.v1.Backup; import com.google.spanner.admin.database.v1.BackupName; import com.google.spanner.admin.database.v1.InstanceName; -import java.io.IOException; import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneId; @@ -34,14 +35,16 @@ public class CopyBackupSample { - static void copyBackup() throws IOException { + static void copyBackup() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; String sourceBackupId = "my-backup"; String destinationBackupId = "my-destination-backup"; - try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { copyBackup(databaseAdminClient, projectId, instanceId, sourceBackupId, destinationBackupId); } } diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateBackupWithEncryptionKey.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateBackupWithEncryptionKey.java index 76276c7e121..9152b26a0fe 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateBackupWithEncryptionKey.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateBackupWithEncryptionKey.java @@ -18,7 +18,9 @@ // [START spanner_create_backup_with_encryption_key] +import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.protobuf.Timestamp; import com.google.spanner.admin.database.v1.Backup; @@ -28,7 +30,6 @@ import com.google.spanner.admin.database.v1.CreateBackupRequest; import com.google.spanner.admin.database.v1.DatabaseName; import com.google.spanner.admin.database.v1.InstanceName; -import java.io.IOException; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -37,7 +38,7 @@ public class CreateBackupWithEncryptionKey { - static void createBackupWithEncryptionKey() throws IOException { + static void createBackupWithEncryptionKey() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -46,7 +47,9 @@ static void createBackupWithEncryptionKey() throws IOException { String kmsKeyName = "projects/" + projectId + "/locations//keyRings//cryptoKeys/"; - try (DatabaseAdminClient adminClient = DatabaseAdminClient.create()) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient adminClient = spanner.createDatabaseAdminClient()) { createBackupWithEncryptionKey( adminClient, projectId, diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithEncryptionKey.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithEncryptionKey.java index 2e9d9889f4c..fe0265409c4 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithEncryptionKey.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithEncryptionKey.java @@ -18,21 +18,22 @@ // [START spanner_create_database_with_encryption_key] +import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.CreateDatabaseRequest; import com.google.spanner.admin.database.v1.Database; import com.google.spanner.admin.database.v1.EncryptionConfig; import com.google.spanner.admin.database.v1.InstanceName; -import java.io.IOException; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class CreateDatabaseWithEncryptionKey { - static void createDatabaseWithEncryptionKey() throws IOException { + static void createDatabaseWithEncryptionKey() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -40,7 +41,9 @@ static void createDatabaseWithEncryptionKey() throws IOException { String kmsKeyName = "projects/" + projectId + "/locations//keyRings//cryptoKeys/"; - try (DatabaseAdminClient adminClient = DatabaseAdminClient.create()) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient adminClient = spanner.createDatabaseAdminClient()) { createDatabaseWithEncryptionKey( adminClient, projectId, diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithVersionRetentionPeriodSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithVersionRetentionPeriodSample.java index 8d26f1ced56..fbea5417927 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithVersionRetentionPeriodSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithVersionRetentionPeriodSample.java @@ -18,19 +18,20 @@ // [START spanner_create_database_with_version_retention_period] +import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerException; import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.Lists; import com.google.spanner.admin.database.v1.CreateDatabaseRequest; import com.google.spanner.admin.database.v1.Database; import com.google.spanner.admin.database.v1.InstanceName; -import java.io.IOException; import java.util.concurrent.ExecutionException; public class CreateDatabaseWithVersionRetentionPeriodSample { - static void createDatabaseWithVersionRetentionPeriod() throws IOException { + static void createDatabaseWithVersionRetentionPeriod() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -42,10 +43,10 @@ static void createDatabaseWithVersionRetentionPeriod() throws IOException { } static void createDatabaseWithVersionRetentionPeriod(String projectId, - String instanceId, String databaseId, String versionRetentionPeriod) throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - - try { + String instanceId, String databaseId, String versionRetentionPeriod) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { CreateDatabaseRequest request = CreateDatabaseRequest.newBuilder() .setParent(InstanceName.of(projectId, instanceId).toString()) diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceExample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceExample.java index 925b0984992..e53b46ede2d 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceExample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceExample.java @@ -18,26 +18,25 @@ //[START spanner_create_instance] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; import com.google.spanner.admin.instance.v1.CreateInstanceRequest; import com.google.spanner.admin.instance.v1.Instance; import com.google.spanner.admin.instance.v1.InstanceConfigName; import com.google.spanner.admin.instance.v1.ProjectName; -import java.io.IOException; import java.util.concurrent.ExecutionException; class CreateInstanceExample { - static void createInstance() throws IOException { + static void createInstance() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; createInstance(projectId, instanceId); } - static void createInstance(String projectId, String instanceId) throws IOException { - InstanceAdminClient instanceAdminClient = InstanceAdminClient.create(); - + static void createInstance(String projectId, String instanceId) { // Set Instance configuration. int nodeCount = 2; String displayName = "Descriptive name"; @@ -50,7 +49,14 @@ static void createInstance(String projectId, String instanceId) throws IOExcepti .setConfig( InstanceConfigName.of(projectId, "regional-us-central1").toString()) .build(); - try { + + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService(); + InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { + // Wait for the createInstance operation to finish. Instance createdInstance = instanceAdminClient.createInstanceAsync( CreateInstanceRequest.newBuilder() diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigExample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigExample.java index aa785fe57d0..3faaced75b9 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigExample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithAutoscalingConfigExample.java @@ -18,26 +18,32 @@ // [START spanner_create_instance_with_autoscaling_config] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; import com.google.spanner.admin.instance.v1.AutoscalingConfig; import com.google.spanner.admin.instance.v1.CreateInstanceRequest; import com.google.spanner.admin.instance.v1.Instance; import com.google.spanner.admin.instance.v1.InstanceConfigName; import com.google.spanner.admin.instance.v1.ProjectName; -import java.io.IOException; import java.util.concurrent.ExecutionException; class CreateInstanceWithAutoscalingConfigExample { - static void createInstance() throws IOException { + static void createInstance() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; createInstance(projectId, instanceId); } - static void createInstance(String projectId, String instanceId) throws IOException { - try (InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + static void createInstance(String projectId, String instanceId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService(); + InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { // Set Instance configuration. String configId = "regional-us-central1"; String displayName = "Descriptive name"; diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsExample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsExample.java index 9aa0487098b..2f6b6d98874 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsExample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceWithProcessingUnitsExample.java @@ -18,24 +18,31 @@ //[START spanner_create_instance_with_processing_units] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; import com.google.spanner.admin.instance.v1.CreateInstanceRequest; import com.google.spanner.admin.instance.v1.Instance; import com.google.spanner.admin.instance.v1.InstanceConfigName; import com.google.spanner.admin.instance.v1.ProjectName; -import java.io.IOException; class CreateInstanceWithProcessingUnitsExample { - static void createInstance() throws IOException { + static void createInstance() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; createInstance(projectId, instanceId); } - static void createInstance(String projectId, String instanceId) throws IOException { - try (InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + static void createInstance(String projectId, String instanceId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService(); + InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { + // Set Instance configuration. String configId = "regional-us-central1"; // This will create an instance with the processing power of 0.2 nodes. diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateSequenceSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateSequenceSample.java index 6614d988b78..d49eacd92e7 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateSequenceSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateSequenceSample.java @@ -28,7 +28,6 @@ import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -36,7 +35,7 @@ public class CreateSequenceSample { - static void createSequence() throws IOException { + static void createSequence() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; @@ -44,13 +43,11 @@ static void createSequence() throws IOException { createSequence(projectId, instanceId, databaseId); } - static void createSequence(String projectId, String instanceId, String databaseId) - throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); + static void createSequence(String projectId, String instanceId, String databaseId) { try (Spanner spanner = - SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { - + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { databaseAdminClient .updateDatabaseDdlAsync( DatabaseName.of(projectId, instanceId, databaseId), diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateTableWithForeignKeyDeleteCascadeSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateTableWithForeignKeyDeleteCascadeSample.java index 04474f7e3b5..84d09792c1f 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateTableWithForeignKeyDeleteCascadeSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateTableWithForeignKeyDeleteCascadeSample.java @@ -18,14 +18,15 @@ // [START spanner_create_table_with_foreign_key_delete_cascade] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; class CreateTableWithForeignKeyDeleteCascadeSample { - static void createForeignKeyDeleteCascadeConstraint() throws IOException { + static void createForeignKeyDeleteCascadeConstraint() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -35,28 +36,31 @@ static void createForeignKeyDeleteCascadeConstraint() throws IOException { } static void createForeignKeyDeleteCascadeConstraint( - String projectId, String instanceId, String databaseId) throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - databaseAdminClient.updateDatabaseDdlAsync( - DatabaseName.of(projectId, instanceId, databaseId), - ImmutableList.of( - "CREATE TABLE Customers (\n" - + " CustomerId INT64 NOT NULL,\n" - + " CustomerName STRING(62) NOT NULL,\n" - + " ) PRIMARY KEY (CustomerId)", - "CREATE TABLE ShoppingCarts (\n" - + " CartId INT64 NOT NULL,\n" - + " CustomerId INT64 NOT NULL,\n" - + " CustomerName STRING(62) NOT NULL,\n" - + " CONSTRAINT FKShoppingCartsCustomerId FOREIGN KEY (CustomerId)\n" - + " REFERENCES Customers (CustomerId) ON DELETE CASCADE\n" - + " ) PRIMARY KEY (CartId)\n")); - - System.out.printf( - String.format( - "Created Customers and ShoppingCarts table with FKShoppingCartsCustomerId\n" - + "foreign key constraint on database %s on instance %s\n", - databaseId, instanceId)); + String projectId, String instanceId, String databaseId) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + databaseAdminClient.updateDatabaseDdlAsync( + DatabaseName.of(projectId, instanceId, databaseId), + ImmutableList.of( + "CREATE TABLE Customers (\n" + + " CustomerId INT64 NOT NULL,\n" + + " CustomerName STRING(62) NOT NULL,\n" + + " ) PRIMARY KEY (CustomerId)", + "CREATE TABLE ShoppingCarts (\n" + + " CartId INT64 NOT NULL,\n" + + " CustomerId INT64 NOT NULL,\n" + + " CustomerName STRING(62) NOT NULL,\n" + + " CONSTRAINT FKShoppingCartsCustomerId FOREIGN KEY (CustomerId)\n" + + " REFERENCES Customers (CustomerId) ON DELETE CASCADE\n" + + " ) PRIMARY KEY (CartId)\n")); + + System.out.printf( + String.format( + "Created Customers and ShoppingCarts table with FKShoppingCartsCustomerId\n" + + "foreign key constraint on database %s on instance %s\n", + databaseId, instanceId)); + } } } // [END spanner_create_table_with_foreign_key_delete_cascade] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/DeleteInstanceConfigSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/DeleteInstanceConfigSample.java index 1fa9aee586c..479ca6f2ffb 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/DeleteInstanceConfigSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/DeleteInstanceConfigSample.java @@ -18,23 +18,29 @@ // [START spanner_delete_instance_config] +import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerException; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; import com.google.spanner.admin.instance.v1.DeleteInstanceConfigRequest; import com.google.spanner.admin.instance.v1.InstanceConfigName; -import java.io.IOException; class DeleteInstanceConfigSample { - static void deleteInstanceConfig() throws IOException { + static void deleteInstanceConfig() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceConfigId = "custom-user-config"; deleteInstanceConfig(projectId, instanceConfigId); } - static void deleteInstanceConfig(String projectId, String instanceConfigId) throws IOException { - try (final InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + static void deleteInstanceConfig(String projectId, String instanceConfigId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService(); + InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { final InstanceConfigName instanceConfigName = InstanceConfigName.of(projectId, instanceConfigId); final DeleteInstanceConfigRequest request = diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/DropForeignKeyConstraintDeleteCascadeSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/DropForeignKeyConstraintDeleteCascadeSample.java index 7d569acea8d..991216a770f 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/DropForeignKeyConstraintDeleteCascadeSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/DropForeignKeyConstraintDeleteCascadeSample.java @@ -18,14 +18,15 @@ // [START spanner_drop_foreign_key_constraint_delete_cascade] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; class DropForeignKeyConstraintDeleteCascadeSample { - static void deleteForeignKeyDeleteCascadeConstraint() throws IOException { + static void deleteForeignKeyDeleteCascadeConstraint() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -35,19 +36,22 @@ static void deleteForeignKeyDeleteCascadeConstraint() throws IOException { } static void deleteForeignKeyDeleteCascadeConstraint( - String projectId, String instanceId, String databaseId) throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - databaseAdminClient.updateDatabaseDdlAsync( - DatabaseName.of(projectId, instanceId, databaseId), - ImmutableList.of( - "ALTER TABLE ShoppingCarts\n" - + " DROP CONSTRAINT FKShoppingCartsCustomerName\n")); - - System.out.printf( - String.format( - "Altered ShoppingCarts table to drop FKShoppingCartsCustomerName\n" - + "foreign key constraint on database %s on instance %s\n", - databaseId, instanceId)); + String projectId, String instanceId, String databaseId) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + databaseAdminClient.updateDatabaseDdlAsync( + DatabaseName.of(projectId, instanceId, databaseId), + ImmutableList.of( + "ALTER TABLE ShoppingCarts\n" + + " DROP CONSTRAINT FKShoppingCartsCustomerName\n")); + + System.out.printf( + String.format( + "Altered ShoppingCarts table to drop FKShoppingCartsCustomerName\n" + + "foreign key constraint on database %s on instance %s\n", + databaseId, instanceId)); + } } } // [END spanner_drop_foreign_key_constraint_delete_cascade] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/DropSequenceSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/DropSequenceSample.java index f9917677de1..ff3522484fe 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/DropSequenceSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/DropSequenceSample.java @@ -18,18 +18,19 @@ // [START spanner_drop_sequence] +import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class DropSequenceSample { - static void dropSequence() throws IOException { + static void dropSequence() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; @@ -37,10 +38,10 @@ static void dropSequence() throws IOException { dropSequence(projectId, instanceId, databaseId); } - static void dropSequence(String projectId, String instanceId, String databaseId) - throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - try { + static void dropSequence(String projectId, String instanceId, String databaseId) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { databaseAdminClient .updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId, databaseId), ImmutableList.of( diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/EnableFineGrainedAccess.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/EnableFineGrainedAccess.java index 7f66992cad1..6538ba87d8e 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/EnableFineGrainedAccess.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/EnableFineGrainedAccess.java @@ -18,6 +18,8 @@ // [START spanner_enable_fine_grained_access] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.iam.v1.Binding; @@ -27,11 +29,10 @@ import com.google.iam.v1.SetIamPolicyRequest; import com.google.spanner.admin.database.v1.DatabaseName; import com.google.type.Expr; -import java.io.IOException; public class EnableFineGrainedAccess { - static void enableFineGrainedAccess() throws IOException { + static void enableFineGrainedAccess() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -48,59 +49,62 @@ static void enableFineGrainedAccess( String databaseId, String iamMember, String title, - String role) throws IOException { - final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - final GetPolicyOptions options = - GetPolicyOptions.newBuilder().setRequestedPolicyVersion(3).build(); - final GetIamPolicyRequest getRequest = - GetIamPolicyRequest.newBuilder() - .setResource(DatabaseName.of(projectId, instanceId, databaseId).toString()) - .setOptions(options).build(); - final Policy policy = databaseAdminClient.getIamPolicy(getRequest); - int policyVersion = policy.getVersion(); - // The policy in the response from getDatabaseIAMPolicy might use the policy version - // that you specified, or it might use a lower policy version. For example, if you - // specify version 3, but the policy has no conditional role bindings, the response - // uses version 1. Valid values are 0, 1, and 3. - if (policy.getVersion() < 3) { - // conditional role bindings work with policy version 3 - policyVersion = 3; - } + String role) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + final GetPolicyOptions options = + GetPolicyOptions.newBuilder().setRequestedPolicyVersion(3).build(); + final GetIamPolicyRequest getRequest = + GetIamPolicyRequest.newBuilder() + .setResource(DatabaseName.of(projectId, instanceId, databaseId).toString()) + .setOptions(options).build(); + final Policy policy = databaseAdminClient.getIamPolicy(getRequest); + int policyVersion = policy.getVersion(); + // The policy in the response from getDatabaseIAMPolicy might use the policy version + // that you specified, or it might use a lower policy version. For example, if you + // specify version 3, but the policy has no conditional role bindings, the response + // uses version 1. Valid values are 0, 1, and 3. + if (policy.getVersion() < 3) { + // conditional role bindings work with policy version 3 + policyVersion = 3; + } - Binding binding1 = - Binding.newBuilder() - .setRole("roles/spanner.fineGrainedAccessUser") - .addAllMembers(ImmutableList.of(iamMember)) - .build(); + Binding binding1 = + Binding.newBuilder() + .setRole("roles/spanner.fineGrainedAccessUser") + .addAllMembers(ImmutableList.of(iamMember)) + .build(); - Binding binding2 = - Binding.newBuilder() - .setRole("roles/spanner.databaseRoleUser") - .setCondition( - Expr.newBuilder().setDescription(title).setExpression( - String.format("resource.name.endsWith(\"/databaseRoles/%s\")", role) - ).setTitle(title).build()) - .addAllMembers(ImmutableList.of(iamMember)) - .build(); - ImmutableList bindings = - ImmutableList.builder() - .addAll(policy.getBindingsList()) - .add(binding1) - .add(binding2) - .build(); - Policy policyWithConditions = - Policy.newBuilder() - .setVersion(policyVersion) - .setEtag(policy.getEtag()) - .addAllBindings(bindings) - .build(); - final SetIamPolicyRequest setRequest = - SetIamPolicyRequest.newBuilder() - .setResource(DatabaseName.of(projectId, instanceId, databaseId).toString()) - .setPolicy(policyWithConditions).build(); - final Policy response = databaseAdminClient.setIamPolicy(setRequest); - System.out.printf( - "Enabled fine-grained access in IAM with version %d%n", response.getVersion()); + Binding binding2 = + Binding.newBuilder() + .setRole("roles/spanner.databaseRoleUser") + .setCondition( + Expr.newBuilder().setDescription(title).setExpression( + String.format("resource.name.endsWith(\"/databaseRoles/%s\")", role) + ).setTitle(title).build()) + .addAllMembers(ImmutableList.of(iamMember)) + .build(); + ImmutableList bindings = + ImmutableList.builder() + .addAll(policy.getBindingsList()) + .add(binding1) + .add(binding2) + .build(); + Policy policyWithConditions = + Policy.newBuilder() + .setVersion(policyVersion) + .setEtag(policy.getEtag()) + .addAllBindings(bindings) + .build(); + final SetIamPolicyRequest setRequest = + SetIamPolicyRequest.newBuilder() + .setResource(DatabaseName.of(projectId, instanceId, databaseId).toString()) + .setPolicy(policyWithConditions).build(); + final Policy response = databaseAdminClient.setIamPolicy(setRequest); + System.out.printf( + "Enabled fine-grained access in IAM with version %d%n", response.getVersion()); + } } } // [END spanner_enable_fine_grained_access] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/GetDatabaseDdlSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/GetDatabaseDdlSample.java index 250136c1fb6..5e666ee4f1f 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/GetDatabaseDdlSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/GetDatabaseDdlSample.java @@ -18,14 +18,15 @@ //[START spanner_get_database_ddl] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.spanner.admin.database.v1.DatabaseName; import com.google.spanner.admin.database.v1.GetDatabaseDdlResponse; -import java.io.IOException; public class GetDatabaseDdlSample { - static void getDatabaseDdl() throws IOException { + static void getDatabaseDdl() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; @@ -34,15 +35,16 @@ static void getDatabaseDdl() throws IOException { } static void getDatabaseDdl( - String projectId, String instanceId, String databaseId) throws IOException { - - final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - - final GetDatabaseDdlResponse response = - databaseAdminClient.getDatabaseDdl(DatabaseName.of(projectId, instanceId, databaseId)); - System.out.println("Retrieved database DDL for " + databaseId); - for (String ddl : response.getStatementsList()) { - System.out.println(ddl); + String projectId, String instanceId, String databaseId) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + final GetDatabaseDdlResponse response = + databaseAdminClient.getDatabaseDdl(DatabaseName.of(projectId, instanceId, databaseId)); + System.out.println("Retrieved database DDL for " + databaseId); + for (String ddl : response.getStatementsList()) { + System.out.println(ddl); + } } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/GetInstanceConfigSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/GetInstanceConfigSample.java index 5fc1b907479..334741c3d38 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/GetInstanceConfigSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/GetInstanceConfigSample.java @@ -18,22 +18,28 @@ //[START spanner_get_instance_config] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; import com.google.spanner.admin.instance.v1.InstanceConfig; import com.google.spanner.admin.instance.v1.InstanceConfigName; -import java.io.IOException; public class GetInstanceConfigSample { - static void getInstanceConfig() throws IOException { + static void getInstanceConfig() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceConfigId = "nam6"; getInstanceConfig(projectId, instanceConfigId); } - static void getInstanceConfig(String projectId, String instanceConfigId) throws IOException { - try (final InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + static void getInstanceConfig(String projectId, String instanceConfigId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService(); + InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { final InstanceConfigName instanceConfigName = InstanceConfigName.of(projectId, instanceConfigId); diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListDatabaseRoles.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListDatabaseRoles.java index d5d8859673d..7fea5a9c557 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListDatabaseRoles.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListDatabaseRoles.java @@ -18,16 +18,17 @@ // [START spanner_list_database_roles] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListDatabaseRolesPage; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListDatabaseRolesPagedResponse; import com.google.spanner.admin.database.v1.DatabaseName; import com.google.spanner.admin.database.v1.DatabaseRole; -import java.io.IOException; public class ListDatabaseRoles { - static void listDatabaseRoles() throws IOException { + static void listDatabaseRoles() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -35,16 +36,18 @@ static void listDatabaseRoles() throws IOException { listDatabaseRoles(projectId, instanceId, databaseId); } - static void listDatabaseRoles(String projectId, String instanceId, String databaseId) - throws IOException { - final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - DatabaseName databaseName = DatabaseName.of(projectId, instanceId, databaseId); - ListDatabaseRolesPagedResponse response - = databaseAdminClient.listDatabaseRoles(databaseName); - System.out.println("List of Database roles"); - for (ListDatabaseRolesPage page : response.iteratePages()) { - for (DatabaseRole role : page.iterateAll()) { - System.out.printf("Obtained role %s%n", role.getName()); + static void listDatabaseRoles(String projectId, String instanceId, String databaseId) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + DatabaseName databaseName = DatabaseName.of(projectId, instanceId, databaseId); + ListDatabaseRolesPagedResponse response + = databaseAdminClient.listDatabaseRoles(databaseName); + System.out.println("List of Database roles"); + for (ListDatabaseRolesPage page : response.iteratePages()) { + for (DatabaseRole role : page.iterateAll()) { + System.out.printf("Obtained role %s%n", role.getName()); + } } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListDatabasesSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListDatabasesSample.java index 7518064f2cd..703e89b9f12 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListDatabasesSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListDatabasesSample.java @@ -18,35 +18,38 @@ //[START spanner_list_databases] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListDatabasesPage; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListDatabasesPagedResponse; import com.google.spanner.admin.database.v1.Database; import com.google.spanner.admin.database.v1.InstanceName; -import java.io.IOException; public class ListDatabasesSample { - static void listDatabases() throws IOException { + static void listDatabases() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; listDatabases(projectId, instanceId); } - static void listDatabases(String projectId, String instanceId) throws IOException { - final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - - ListDatabasesPagedResponse response = - databaseAdminClient.listDatabases(InstanceName.of(projectId, instanceId)); - - System.out.println("Databases for projects/" + projectId + "/instances/" + instanceId); - - for (ListDatabasesPage page : response.iteratePages()) { - for (Database database : page.iterateAll()) { - final String defaultLeader = database.getDefaultLeader().equals("") - ? "" : "(default leader = " + database.getDefaultLeader() + ")"; - System.out.println("\t" + database.getName() + " " + defaultLeader); + static void listDatabases(String projectId, String instanceId) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + ListDatabasesPagedResponse response = + databaseAdminClient.listDatabases(InstanceName.of(projectId, instanceId)); + + System.out.println("Databases for projects/" + projectId + "/instances/" + instanceId); + + for (ListDatabasesPage page : response.iteratePages()) { + for (Database database : page.iterateAll()) { + final String defaultLeader = database.getDefaultLeader().equals("") + ? "" : "(default leader = " + database.getDefaultLeader() + ")"; + System.out.println("\t" + database.getName() + " " + defaultLeader); + } } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigOperationsSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigOperationsSample.java index 6970c500694..cbfd1908d45 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigOperationsSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigOperationsSample.java @@ -18,24 +18,30 @@ // [START spanner_list_instance_config_operations] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; import com.google.longrunning.Operation; import com.google.protobuf.InvalidProtocolBufferException; import com.google.spanner.admin.instance.v1.CreateInstanceConfigMetadata; import com.google.spanner.admin.instance.v1.ListInstanceConfigOperationsRequest; import com.google.spanner.admin.instance.v1.ProjectName; -import java.io.IOException; public class ListInstanceConfigOperationsSample { - static void listInstanceConfigOperations() throws IOException { + static void listInstanceConfigOperations() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; listInstanceConfigOperations(projectId); } - static void listInstanceConfigOperations(String projectId) throws IOException { - try (final InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + static void listInstanceConfigOperations(String projectId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService(); + InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { final ProjectName projectName = ProjectName.of(projectId); System.out.printf( "Getting list of instance config operations for project %s...\n", diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigsSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigsSample.java index 92a0c7014e9..f2d6f5fba61 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigsSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/ListInstanceConfigsSample.java @@ -18,21 +18,27 @@ //[START spanner_list_instance_configs] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; import com.google.spanner.admin.instance.v1.InstanceConfig; import com.google.spanner.admin.instance.v1.ProjectName; -import java.io.IOException; public class ListInstanceConfigsSample { - static void listInstanceConfigs() throws IOException { + static void listInstanceConfigs() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; listInstanceConfigs(projectId); } - static void listInstanceConfigs(String projectId) throws IOException { - try (final InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + static void listInstanceConfigs(String projectId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService(); + InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { final ProjectName projectName = ProjectName.of(projectId); for (InstanceConfig instanceConfig : instanceAdminClient.listInstanceConfigs(projectName).iterateAll()) { diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgAlterSequenceSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgAlterSequenceSample.java index 39a4cd543a6..1168bdcfadf 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgAlterSequenceSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgAlterSequenceSample.java @@ -28,7 +28,6 @@ import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -36,7 +35,7 @@ public class PgAlterSequenceSample { - static void pgAlterSequence() throws IOException { + static void pgAlterSequence() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; @@ -44,11 +43,11 @@ static void pgAlterSequence() throws IOException { pgAlterSequence(projectId, instanceId, databaseId); } - static void pgAlterSequence(String projectId, String instanceId, String databaseId) - throws IOException { + static void pgAlterSequence(String projectId, String instanceId, String databaseId) { try (Spanner spanner = - SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { - final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { + databaseAdminClient .updateDatabaseDdlAsync( DatabaseName.of(projectId, instanceId, databaseId), diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgCaseSensitivitySample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgCaseSensitivitySample.java index 951b46446f1..8fbaf6a9c01 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgCaseSensitivitySample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgCaseSensitivitySample.java @@ -29,13 +29,12 @@ import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.Lists; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.Collections; import java.util.concurrent.ExecutionException; public class PgCaseSensitivitySample { - static void pgCaseSensitivity() throws IOException { + static void pgCaseSensitivity() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; @@ -43,15 +42,11 @@ static void pgCaseSensitivity() throws IOException { pgCaseSensitivity(projectId, instanceId, databaseId); } - static void pgCaseSensitivity(String projectId, String instanceId, String databaseId) - throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); + static void pgCaseSensitivity(String projectId, String instanceId, String databaseId) { try (Spanner spanner = - SpannerOptions.newBuilder() - .setProjectId(projectId) - .build() - .getService()) { + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { // Spanner PostgreSQL follows the case sensitivity rules of PostgreSQL. This means that: // 1. Identifiers that are not double-quoted are folded to lower case. diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgCreateSequenceSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgCreateSequenceSample.java index 6e7d9c34c57..2148427ccdd 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgCreateSequenceSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgCreateSequenceSample.java @@ -28,7 +28,6 @@ import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.Objects; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -36,7 +35,7 @@ public class PgCreateSequenceSample { - static void pgCreateSequence() throws IOException { + static void pgCreateSequence() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; @@ -44,12 +43,10 @@ static void pgCreateSequence() throws IOException { pgCreateSequence(projectId, instanceId, databaseId); } - static void pgCreateSequence(String projectId, String instanceId, String databaseId) - throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - + static void pgCreateSequence(String projectId, String instanceId, String databaseId) { try (Spanner spanner = - SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { databaseAdminClient .updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId, databaseId).toString(), ImmutableList.of( diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgDropSequenceSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgDropSequenceSample.java index dec927d20ac..de856f51a3d 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgDropSequenceSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgDropSequenceSample.java @@ -24,14 +24,13 @@ import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.ImmutableList; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class PgDropSequenceSample { - static void pgDropSequence() throws IOException { + static void pgDropSequence() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; @@ -39,12 +38,11 @@ static void pgDropSequence() throws IOException { pgDropSequence(projectId, instanceId, databaseId); } - static void pgDropSequence(String projectId, String instanceId, String databaseId) - throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); + static void pgDropSequence(String projectId, String instanceId, String databaseId) { try (Spanner spanner = - SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { databaseAdminClient .updateDatabaseDdlAsync( DatabaseName.of(projectId, instanceId, databaseId), diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgInterleavedTableSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgInterleavedTableSample.java index 14af53dc5a8..754920c8dd3 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgInterleavedTableSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgInterleavedTableSample.java @@ -18,16 +18,17 @@ // [START spanner_postgresql_interleaved_table] +import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.Arrays; import java.util.concurrent.ExecutionException; public class PgInterleavedTableSample { - static void pgInterleavedTable() throws IOException { + static void pgInterleavedTable() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; @@ -35,10 +36,11 @@ static void pgInterleavedTable() throws IOException { pgInterleavedTable(projectId, instanceId, databaseId); } - static void pgInterleavedTable(String projectId, String instanceId, String databaseId) - throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - try { + static void pgInterleavedTable(String projectId, String instanceId, String databaseId) { + + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { // The Spanner PostgreSQL dialect extends the PostgreSQL dialect with certain Spanner // specific features, such as interleaved tables. // See https://cloud.google.com/spanner/docs/postgresql/data-definition-language#create_table diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgSpannerSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgSpannerSample.java index b2e6eafb0f5..c4e319c66c4 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgSpannerSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgSpannerSample.java @@ -26,7 +26,6 @@ import com.google.cloud.spanner.KeyRange; import com.google.cloud.spanner.KeySet; import com.google.cloud.spanner.Mutation; -import com.google.cloud.spanner.Options; import com.google.cloud.spanner.ReadOnlyTransaction; import com.google.cloud.spanner.ResultSet; import com.google.cloud.spanner.Spanner; @@ -1544,7 +1543,7 @@ static void printUsageAndExit() { System.exit(1); } - public static void main(String[] args) throws Exception { + public static void main(String[] args) { if (args.length != 3) { printUsageAndExit(); } @@ -1574,7 +1573,7 @@ public static void main(String[] args) throws Exception { // [START spanner_init_client] DatabaseClient dbClient = spanner.getDatabaseClient(db); - dbAdminClient = DatabaseAdminClient.create(); + dbAdminClient = spanner.createDatabaseAdminClient(); // [END spanner_init_client] // Use client here... diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/RestoreBackupWithEncryptionKey.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/RestoreBackupWithEncryptionKey.java index 714eb67af21..3c129860f01 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/RestoreBackupWithEncryptionKey.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/RestoreBackupWithEncryptionKey.java @@ -20,19 +20,20 @@ import static com.google.spanner.admin.database.v1.RestoreDatabaseEncryptionConfig.EncryptionType.CUSTOMER_MANAGED_ENCRYPTION; +import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.spanner.admin.database.v1.BackupName; import com.google.spanner.admin.database.v1.Database; import com.google.spanner.admin.database.v1.InstanceName; import com.google.spanner.admin.database.v1.RestoreDatabaseEncryptionConfig; import com.google.spanner.admin.database.v1.RestoreDatabaseRequest; -import java.io.IOException; import java.util.concurrent.ExecutionException; public class RestoreBackupWithEncryptionKey { - static void restoreBackupWithEncryptionKey() throws IOException { + static void restoreBackupWithEncryptionKey() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -41,7 +42,9 @@ static void restoreBackupWithEncryptionKey() throws IOException { String kmsKeyName = "projects/" + projectId + "/locations//keyRings//cryptoKeys/"; - try (DatabaseAdminClient adminClient = DatabaseAdminClient.create()) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient adminClient = spanner.createDatabaseAdminClient()) { restoreBackupWithEncryptionKey( adminClient, projectId, @@ -64,7 +67,8 @@ static Void restoreBackupWithEncryptionKey(DatabaseAdminClient adminClient, Database database; try { System.out.println("Waiting for operation to complete..."); - database = adminClient.restoreDatabaseAsync(request).get();; + database = adminClient.restoreDatabaseAsync(request).get(); + ; } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw SpannerExceptionFactory.asSpannerException(e.getCause()); diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/SpannerSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/SpannerSample.java index 6e805dc1322..7cc4af7c74f 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/SpannerSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/SpannerSample.java @@ -2225,7 +2225,7 @@ static void printUsageAndExit() { System.exit(1); } - public static void main(String[] args) throws Exception { + public static void main(String[] args) { if (args.length != 3 && args.length != 4) { printUsageAndExit(); } @@ -2254,7 +2254,7 @@ public static void main(String[] args) throws Exception { // [START init_client] DatabaseClient dbClient = spanner.getDatabaseClient(db); - dbAdminClient = DatabaseAdminClient.create(); + dbAdminClient = spanner.createDatabaseAdminClient(); // Use client here... // [END init_client] diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseSample.java index 958be6e20a4..4e4f76d7002 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseSample.java @@ -19,7 +19,9 @@ // [START spanner_update_database] import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.common.collect.Lists; import com.google.protobuf.FieldMask; @@ -27,14 +29,13 @@ import com.google.spanner.admin.database.v1.DatabaseName; import com.google.spanner.admin.database.v1.UpdateDatabaseMetadata; import com.google.spanner.admin.database.v1.UpdateDatabaseRequest; -import java.io.IOException; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class UpdateDatabaseSample { - static void updateDatabase() throws IOException { + static void updateDatabase() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; @@ -44,9 +45,10 @@ static void updateDatabase() throws IOException { } static void updateDatabase( - String projectId, String instanceId, String databaseId) throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - try { + String projectId, String instanceId, String databaseId) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { final Database database = Database.newBuilder() .setName(DatabaseName.of(projectId, instanceId, databaseId).toString()) diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseWithDefaultLeaderSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseWithDefaultLeaderSample.java index 0e72214b97b..26c252ac1e0 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseWithDefaultLeaderSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseWithDefaultLeaderSample.java @@ -18,17 +18,18 @@ //[START spanner_update_database_with_default_leader] +import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerException; import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.spanner.admin.database.v1.DatabaseName; -import java.io.IOException; import java.util.Collections; import java.util.concurrent.ExecutionException; public class UpdateDatabaseWithDefaultLeaderSample { - static void updateDatabaseWithDefaultLeader() throws IOException { + static void updateDatabaseWithDefaultLeader() { // TODO(developer): Replace these variables before running the sample. final String projectId = "my-project"; final String instanceId = "my-instance"; @@ -38,11 +39,10 @@ static void updateDatabaseWithDefaultLeader() throws IOException { } static void updateDatabaseWithDefaultLeader( - String projectId, String instanceId, String databaseId, String defaultLeader) - throws IOException { - DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create(); - - try { + String projectId, String instanceId, String databaseId, String defaultLeader) { + try (Spanner spanner = + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { databaseAdminClient .updateDatabaseDdlAsync( DatabaseName.of(projectId, instanceId, databaseId), diff --git a/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateInstanceConfigSample.java b/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateInstanceConfigSample.java index 1fb5cd0e6fb..e74d8071c28 100644 --- a/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateInstanceConfigSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateInstanceConfigSample.java @@ -18,28 +18,34 @@ // [START spanner_update_instance_config] +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient; import com.google.common.collect.ImmutableList; import com.google.protobuf.FieldMask; import com.google.spanner.admin.instance.v1.InstanceConfig; import com.google.spanner.admin.instance.v1.InstanceConfigName; import com.google.spanner.admin.instance.v1.UpdateInstanceConfigRequest; -import java.io.IOException; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; class UpdateInstanceConfigSample { - static void updateInstanceConfig() throws IOException { + static void updateInstanceConfig() { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceConfigId = "custom-instance-config"; updateInstanceConfig(projectId, instanceConfigId); } - static void updateInstanceConfig(String projectId, String instanceConfigId) throws IOException { - try (final InstanceAdminClient instanceAdminClient = InstanceAdminClient.create()) { + static void updateInstanceConfig(String projectId, String instanceConfigId) { + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .build() + .getService(); + InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { final InstanceConfigName instanceConfigName = InstanceConfigName.of(projectId, instanceConfigId); final InstanceConfig instanceConfig =