Skip to content

Commit

Permalink
chore: upgrade spanner samples to new client surface (#1952)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Jan 5, 2024
1 parent 1ec5c14 commit 6d1cbe1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion spanner/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"google/cloud-spanner": "^1.62.1"
"google/cloud-spanner": "^1.68"
}
}
13 changes: 10 additions & 3 deletions spanner/src/enable_fine_grained_access.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
namespace Google\Cloud\Samples\Spanner;

// [START spanner_enable_fine_grained_access]
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseAdminClient;
use \Google\Cloud\Iam\V1\Binding;
use \Google\Type\Expr;
use Google\Cloud\Iam\V1\GetIamPolicyRequest;
use Google\Cloud\Iam\V1\SetIamPolicyRequest;
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;

/**
* Enable Fine Grained Access.
Expand Down Expand Up @@ -54,7 +56,9 @@ function enable_fine_grained_access(
): void {
$adminClient = new DatabaseAdminClient();
$resource = sprintf('projects/%s/instances/%s/databases/%s', $projectId, $instanceId, $databaseId);
$policy = $adminClient->getIamPolicy($resource);
$getIamPolicyRequest = (new GetIamPolicyRequest())
->setResource($resource);
$policy = $adminClient->getIamPolicy($getIamPolicyRequest);

// IAM conditions need at least version 3
if ($policy->getVersion() != 3) {
Expand All @@ -70,7 +74,10 @@ function enable_fine_grained_access(
])
]);
$policy->setBindings([$binding]);
$adminClient->setIamPolicy($resource, $policy);
$setIamPolicyRequest = (new SetIamPolicyRequest())
->setResource($resource)
->setPolicy($policy);
$adminClient->setIamPolicy($setIamPolicyRequest);

printf('Enabled fine-grained access in IAM' . PHP_EOL);
}
Expand Down
7 changes: 5 additions & 2 deletions spanner/src/list_database_roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
namespace Google\Cloud\Samples\Spanner;

// [START spanner_list_database_roles]
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseAdminClient;
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
use Google\Cloud\Spanner\Admin\Database\V1\ListDatabaseRolesRequest;

/**
* List Database roles in the given database.
Expand All @@ -44,8 +45,10 @@ function list_database_roles(
): void {
$adminClient = new DatabaseAdminClient();
$resource = sprintf('projects/%s/instances/%s/databases/%s', $projectId, $instanceId, $databaseId);
$listDatabaseRolesRequest = (new ListDatabaseRolesRequest())
->setParent($resource);

$roles = $adminClient->listDatabaseRoles($resource);
$roles = $adminClient->listDatabaseRoles($listDatabaseRolesRequest);
printf('List of Database roles:' . PHP_EOL);
foreach ($roles as $role) {
printf($role->getName() . PHP_EOL);
Expand Down

0 comments on commit 6d1cbe1

Please sign in to comment.