1717package com .example .spanner ;
1818
1919// [START spanner_add_and_drop_database_role]
20- import com .google .api .gax .longrunning .OperationFuture ;
21- import com .google .cloud .spanner .DatabaseAdminClient ;
20+
2221import com .google .cloud .spanner .Spanner ;
2322import com .google .cloud .spanner .SpannerOptions ;
23+ import com .google .cloud .spanner .admin .database .v1 .DatabaseAdminClient ;
2424import com .google .common .collect .ImmutableList ;
25- import com .google .spanner .admin .database .v1 .UpdateDatabaseDdlMetadata ;
25+ import com .google .spanner .admin .database .v1 .DatabaseName ;
2626import java .util .concurrent .ExecutionException ;
2727import java .util .concurrent .TimeUnit ;
2828import java .util .concurrent .TimeoutException ;
@@ -34,54 +34,46 @@ static void addAndDropDatabaseRole() {
3434 String projectId = "my-project" ;
3535 String instanceId = "my-instance" ;
3636 String databaseId = "my-database" ;
37- String parentRole = "my-new-parent-role " ;
38- String childRole = "my-new-child-role " ;
37+ String parentRole = "parent_role " ;
38+ String childRole = "child_role " ;
3939 addAndDropDatabaseRole (projectId , instanceId , databaseId , parentRole , childRole );
4040 }
4141
4242 static void addAndDropDatabaseRole (
43- String projectId , String instanceId , String databaseId , String parentRole , String childRole ) {
43+ String projectId , String instanceId , String databaseId ,
44+ String parentRole , String childRole ) {
4445 try (Spanner spanner =
45- SpannerOptions .newBuilder ()
46- .setProjectId (projectId )
47- .build ()
48- .getService ()) {
49- final DatabaseAdminClient adminClient = spanner .getDatabaseAdminClient ();
50- OperationFuture <Void , UpdateDatabaseDdlMetadata > operation =
51- adminClient .updateDatabaseDdl (
52- instanceId ,
53- databaseId ,
46+ SpannerOptions .newBuilder ()
47+ .setProjectId (projectId )
48+ .build ()
49+ .getService ();
50+ DatabaseAdminClient databaseAdminClient = spanner .createDatabaseAdminClient ()) {
51+ System .out .println ("Waiting for role create operation to complete..." );
52+ databaseAdminClient .updateDatabaseDdlAsync (
53+ DatabaseName .of (projectId , instanceId , databaseId ),
5454 ImmutableList .of (
55- "CREATE ROLE " + parentRole ,
56- "GRANT SELECT ON TABLE Albums TO ROLE " + parentRole ,
57- "CREATE ROLE " + childRole ,
58- "GRANT ROLE " + parentRole + " TO ROLE " + childRole ),
59- null );
60- try {
61- System .out .println ("Waiting for role create operation to complete..." );
62- operation .get (5 , TimeUnit .MINUTES );
63- System .out .printf (
64- "Created roles %s and %s and granted privileges%n" , parentRole , childRole );
65- // Delete role and membership.
66- operation =
67- adminClient .updateDatabaseDdl (
68- instanceId ,
69- databaseId ,
70- ImmutableList .of (
71- "REVOKE ROLE " + parentRole + " FROM ROLE " + childRole ,
72- "DROP ROLE " + childRole ),
73- null );
74- System .out .println ("Waiting for role revoke & drop operation to complete..." );
75- operation .get (5 , TimeUnit .MINUTES );
76- System .out .printf ("Revoked privileges and dropped role %s%n" , childRole );
77- } catch (ExecutionException | TimeoutException e ) {
78- System .out .printf (
79- "Error: AddAndDropDatabaseRole failed with error message %s\n " , e .getMessage ());
80- e .printStackTrace ();
81- } catch (InterruptedException e ) {
82- System .out .println (
83- "Error: Waiting for AddAndDropDatabaseRole operation to finish was interrupted" );
84- }
55+ String .format ("CREATE ROLE %s" , parentRole ),
56+ String .format ("GRANT SELECT ON TABLE Albums TO ROLE %s" , parentRole ),
57+ String .format ("CREATE ROLE %s" , childRole ),
58+ String .format ("GRANT ROLE %s TO ROLE %s" , parentRole , childRole )))
59+ .get (5 , TimeUnit .MINUTES );
60+ System .out .printf (
61+ "Created roles %s and %s and granted privileges%n" , parentRole , childRole );
62+ // Delete role and membership.
63+ System .out .println ("Waiting for role revoke & drop operation to complete..." );
64+ databaseAdminClient .updateDatabaseDdlAsync (
65+ DatabaseName .of (projectId , instanceId , databaseId ),
66+ ImmutableList .of (
67+ String .format ("REVOKE ROLE %s FROM ROLE %s" , parentRole , childRole ),
68+ String .format ("DROP ROLE %s" , childRole ))).get (5 , TimeUnit .MINUTES );
69+ System .out .printf ("Revoked privileges and dropped role %s%n" , childRole );
70+ } catch (ExecutionException | TimeoutException e ) {
71+ System .out .printf (
72+ "Error: AddAndDropDatabaseRole failed with error message %s\n " , e .getMessage ());
73+ e .printStackTrace ();
74+ } catch (InterruptedException e ) {
75+ System .out .println (
76+ "Error: Waiting for AddAndDropDatabaseRole operation to finish was interrupted" );
8577 }
8678 }
8779}
0 commit comments