|
| 1 | +/* |
| 2 | + * Copyright 2022 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.spanner; |
| 18 | + |
| 19 | +// [START spanner_add_and_drop_database_role] |
| 20 | +import com.google.api.gax.longrunning.OperationFuture; |
| 21 | +import com.google.cloud.spanner.DatabaseAdminClient; |
| 22 | +import com.google.cloud.spanner.Spanner; |
| 23 | +import com.google.cloud.spanner.SpannerOptions; |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata; |
| 26 | +import java.util.concurrent.ExecutionException; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import java.util.concurrent.TimeoutException; |
| 29 | + |
| 30 | +public class AddAndDropDatabaseRole { |
| 31 | + |
| 32 | + static void addAndDropDatabaseRole() { |
| 33 | + // TODO(developer): Replace these variables before running the sample. |
| 34 | + String projectId = "my-project"; |
| 35 | + String instanceId = "my-instance"; |
| 36 | + String databaseId = "my-database"; |
| 37 | + String parentRole = "my-new-parent-role"; |
| 38 | + String childRole = "my-new-child-role"; |
| 39 | + addAndDropDatabaseRole(projectId, instanceId, databaseId, parentRole, childRole); |
| 40 | + } |
| 41 | + |
| 42 | + static void addAndDropDatabaseRole( |
| 43 | + String projectId, String instanceId, String databaseId, String parentRole, String childRole) { |
| 44 | + 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, |
| 54 | + 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 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +// [END spanner_add_and_drop_database_role] |
0 commit comments