23
23
import com .google .cloud .spanner .admin .database .v1 .DatabaseAdminClient ;
24
24
import com .google .common .collect .ImmutableList ;
25
25
import com .google .spanner .admin .database .v1 .DatabaseName ;
26
+ import java .util .ArrayList ;
27
+ import java .util .List ;
26
28
import java .util .concurrent .ExecutionException ;
27
29
import java .util .concurrent .TimeUnit ;
28
30
import java .util .concurrent .TimeoutException ;
@@ -36,26 +38,28 @@ static void addAndDropDatabaseRole() {
36
38
String databaseId = "my-database" ;
37
39
String parentRole = "parent_role" ;
38
40
String childRole = "child_role" ;
39
- addAndDropDatabaseRole (projectId , instanceId , databaseId , parentRole , childRole );
41
+ addAndDropDatabaseRole (projectId , instanceId , databaseId , parentRole , childRole , "Albums" );
40
42
}
41
43
42
44
static void addAndDropDatabaseRole (
43
45
String projectId , String instanceId , String databaseId ,
44
- String parentRole , String childRole ) {
46
+ String parentRole , String childRole , String ... tables ) {
45
47
try (Spanner spanner =
46
48
SpannerOptions .newBuilder ()
47
49
.setProjectId (projectId )
48
50
.build ()
49
51
.getService ();
50
52
DatabaseAdminClient databaseAdminClient = spanner .createDatabaseAdminClient ()) {
51
53
System .out .println ("Waiting for role create operation to complete..." );
54
+ List <String > roleStatements = new ArrayList <>(ImmutableList .of (
55
+ String .format ("CREATE ROLE %s" , parentRole ),
56
+ String .format ("CREATE ROLE %s" , childRole ),
57
+ String .format ("GRANT ROLE %s TO ROLE %s" , parentRole , childRole )));
58
+ for (String table : tables ) {
59
+ roleStatements .add (String .format ("GRANT SELECT ON TABLE %s TO ROLE %s" , table , parentRole ));
60
+ }
52
61
databaseAdminClient .updateDatabaseDdlAsync (
53
- DatabaseName .of (projectId , instanceId , databaseId ),
54
- ImmutableList .of (
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 )))
62
+ DatabaseName .of (projectId , instanceId , databaseId ), roleStatements )
59
63
.get (5 , TimeUnit .MINUTES );
60
64
System .out .printf (
61
65
"Created roles %s and %s and granted privileges%n" , parentRole , childRole );
0 commit comments