Skip to content

Commit 5d08768

Browse files
samples: Bigtable add missing samples 1439 (GoogleCloudPlatform#1453)
* Added samples for get_cluste, get_instance, update_cluster, update_instance, get_iam_policy, set_iam_policy, test_iam_permissions * Modified list_instance response and test as the update_instance now changes the displayName * Fixed lint issues * Added namespaces in samples, and renamed runSnippet -> runFunctionSnippet for these samples * Bigtable: Addressed PR comments Changed double quotes to dingle quotes Removed redundant else blocks Changed echo statements to printf * Bigtable: Added Tests for get_iam_policy.php and set_iam_policy.php * Bigtable: Added the use of a dynamic service account for testing get_iam_policy and set_iam_policy snippets Co-authored-by: Brent Shaffer
1 parent 7dfed39 commit 5d08768

11 files changed

+689
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ credentials.*
88
**/vendor/
99
**/build/
1010
.php_cs.cache
11+
.php-cs-fixer.cache
1112
.vscode/
1213
.kokoro/secrets.sh
1314
.phpunit.result.cache

bigtable/src/get_cluster.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
3+
namespace Google\Cloud\Samples\Bigtable;
4+
5+
/**
6+
* Copyright 2019 Google LLC.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* For instructions on how to run the full sample:
23+
*
24+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
25+
*/
26+
27+
// [START bigtable_get_cluster]
28+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
29+
use Google\Cloud\Bigtable\Admin\V2\StorageType;
30+
use Google\Cloud\Bigtable\Admin\V2\Instance\State;
31+
use Google\ApiCore\ApiException;
32+
33+
/**
34+
* Get a Bigtable cluster
35+
* @param string $projectId The Google Cloud project ID
36+
* @param string $instanceId The ID of the Bigtable instance
37+
* @param string $clusterId The ID of the cluster to fetch
38+
*/
39+
function get_cluster(
40+
string $projectId,
41+
string $instanceId,
42+
string $clusterId
43+
): void {
44+
$instanceAdminClient = new BigtableInstanceAdminClient();
45+
46+
printf('Fetching the Cluster %s' . PHP_EOL, $clusterId);
47+
try {
48+
$clusterName = $instanceAdminClient->clusterName($projectId, $instanceId, $clusterId);
49+
$cluster = $instanceAdminClient->getCluster($clusterName);
50+
} catch (ApiException $e) {
51+
if ($e->getStatus() === 'NOT_FOUND') {
52+
printf('Cluster %s does not exists.' . PHP_EOL, $clusterId);
53+
return;
54+
}
55+
throw $e;
56+
}
57+
58+
printf('Printing Details:' . PHP_EOL);
59+
60+
// Fetch some commonly used metadata
61+
printf('Name: ' . $cluster->getName() . PHP_EOL);
62+
printf('Location: ' . $cluster->getLocation() . PHP_EOL);
63+
printf('State: ' . State::name($cluster->getState()) . PHP_EOL);
64+
printf('Default Storage Type: ' . StorageType::name($cluster->getDefaultStorageType()) . PHP_EOL);
65+
printf('Nodes: ' . $cluster->getServeNodes() . PHP_EOL);
66+
printf('Encryption Config: ' . ($cluster->hasEncryptionConfig() ? $cluster->getEncryptionConfig()->getKmsKeyName() : "N/A") . PHP_EOL);
67+
}
68+
// [END bigtable_get_cluster]
69+
70+
// The following 2 lines are only needed to run the samples
71+
require_once __DIR__ . '/../../testing/sample_helpers.php';
72+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/get_iam_policy.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
3+
namespace Google\Cloud\Samples\Bigtable;
4+
5+
/**
6+
* Copyright 2019 Google LLC.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* For instructions on how to run the full sample:
23+
*
24+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
25+
*/
26+
27+
// [START bigtable_get_iam_policy]
28+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
29+
use Google\ApiCore\ApiException;
30+
31+
/**
32+
* Get the IAM policy for a Bigtable instance
33+
* @param string $projectId The Google Cloud project ID
34+
* @param string $instanceId The ID of the Bigtable instance
35+
*/
36+
function get_iam_policy(
37+
string $projectId,
38+
string $instanceId
39+
): void {
40+
$instanceAdminClient = new BigtableInstanceAdminClient();
41+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
42+
43+
try {
44+
// we could instantiate the BigtableTableAdminClient and pass the tableName to get the IAM policy for the table resource as well.
45+
$iamPolicy = $instanceAdminClient->getIamPolicy($instanceName);
46+
47+
printf($iamPolicy->getVersion() . PHP_EOL);
48+
49+
foreach ($iamPolicy->getBindings() as $binding) {
50+
foreach ($binding->getmembers() as $member) {
51+
printf('%s:%s' . PHP_EOL, $binding->getRole(), $member);
52+
}
53+
}
54+
} catch (ApiException $e) {
55+
if ($e->getStatus() === 'NOT_FOUND') {
56+
printf('Instance %s does not exist.' . PHP_EOL, $instanceId);
57+
return;
58+
}
59+
throw $e;
60+
}
61+
}
62+
// [END bigtable_get_iam_policy]
63+
64+
// The following 2 lines are only needed to run the samples
65+
require_once __DIR__ . '/../../testing/sample_helpers.php';
66+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/get_instance.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
3+
namespace Google\Cloud\Samples\Bigtable;
4+
5+
/**
6+
* Copyright 2019 Google LLC.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* For instructions on how to run the full sample:
23+
*
24+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
25+
*/
26+
27+
// [START bigtable_get_instance]
28+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
29+
use Google\Cloud\Bigtable\Admin\V2\Instance\Type;
30+
use Google\Cloud\Bigtable\Admin\V2\Instance\State;
31+
use Google\ApiCore\ApiException;
32+
33+
/**
34+
* Get a Bigtable instance
35+
* @param string $projectId The Google Cloud project ID
36+
* @param string $instanceId The ID of the Bigtable instance
37+
*/
38+
function get_instance(
39+
string $projectId,
40+
string $instanceId
41+
): void {
42+
$instanceAdminClient = new BigtableInstanceAdminClient();
43+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
44+
45+
printf('Fetching the Instance %s' . PHP_EOL, $instanceId);
46+
try {
47+
$instance = $instanceAdminClient->getInstance($instanceName);
48+
} catch (ApiException $e) {
49+
if ($e->getStatus() === 'NOT_FOUND') {
50+
printf('Instance %s does not exists.' . PHP_EOL, $instanceId);
51+
return;
52+
}
53+
throw $e;
54+
}
55+
56+
printf('Printing Details:' . PHP_EOL);
57+
58+
// Fetch some commonly used metadata
59+
printf('Name: ' . $instance->getName() . PHP_EOL);
60+
printf('Display Name: ' . $instance->getDisplayName() . PHP_EOL);
61+
printf('State: ' . State::name($instance->getState()) . PHP_EOL);
62+
printf('Type: ' . Type::name($instance->getType()) . PHP_EOL);
63+
printf('Labels: ' . PHP_EOL);
64+
65+
$labels = $instance->getLabels();
66+
67+
// Labels are an object of the MapField class which implement the IteratorAggregate, Countable
68+
// and ArrayAccess interfaces so you can do the following:
69+
printf("\tNum of Labels: " . $labels->count() . PHP_EOL);
70+
printf("\tLabel with a key(dev-label): " . ($labels->offsetExists('dev-label') ? $labels['dev-label'] : 'N/A') . PHP_EOL);
71+
72+
// we can even loop over all the labels
73+
foreach ($labels as $key => $val) {
74+
printf("\t$key: $val" . PHP_EOL);
75+
}
76+
}
77+
// [END bigtable_get_instance]
78+
79+
// The following 2 lines are only needed to run the samples
80+
require_once __DIR__ . '/../../testing/sample_helpers.php';
81+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/list_instance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function list_instance(string $projectId): void
4141
$instances = $getInstances->getIterator();
4242

4343
foreach ($instances as $instance) {
44-
print($instance->getDisplayName() . PHP_EOL);
44+
print($instance->getName() . PHP_EOL);
4545
}
4646
}
4747
// [END bigtable_list_instances]

bigtable/src/set_iam_policy.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
3+
namespace Google\Cloud\Samples\Bigtable;
4+
5+
/**
6+
* Copyright 2019 Google LLC.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* For instructions on how to run the full sample:
23+
*
24+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
25+
*/
26+
27+
// [START bigtable_set_iam_policy]
28+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
29+
use Google\ApiCore\ApiException;
30+
use Google\Cloud\Iam\V1\Binding;
31+
use Google\Cloud\Iam\V1\Policy;
32+
33+
/**
34+
* Set the IAM policy for a Bigtable instance
35+
* @param string $projectId The Google Cloud project ID
36+
* @param string $instanceId The ID of the Bigtable instance
37+
* @param string $email The email of the member to be assigned the role(Format: 'user:EMAIL_ID')
38+
* @param string $role The role to be assigned. For a list of roles check out https://cloud.google.com/bigtable/docs/access-control
39+
*/
40+
function set_iam_policy(
41+
string $projectId,
42+
string $instanceId,
43+
string $email,
44+
string $role = 'roles/bigtable.user'
45+
): void {
46+
$instanceAdminClient = new BigtableInstanceAdminClient();
47+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
48+
49+
try {
50+
$policy = new Policy([
51+
'bindings'=>[
52+
new Binding([
53+
'role'=>$role,
54+
'members'=>[$email]
55+
])
56+
]
57+
]);
58+
59+
$iamPolicy = $instanceAdminClient->setIamPolicy($instanceName, $policy);
60+
61+
foreach ($iamPolicy->getBindings() as $binding) {
62+
foreach ($binding->getmembers() as $member) {
63+
printf('%s:%s' . PHP_EOL, $binding->getRole(), $member);
64+
}
65+
}
66+
} catch (ApiException $e) {
67+
if ($e->getStatus() === 'NOT_FOUND') {
68+
printf('Instance %s does not exist.' . PHP_EOL, $instanceId);
69+
return;
70+
}
71+
throw $e;
72+
}
73+
}
74+
// [END bigtable_set_iam_policy]
75+
76+
// The following 2 lines are only needed to run the samples
77+
require_once __DIR__ . '/../../testing/sample_helpers.php';
78+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/test_iam_permissions.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
3+
namespace Google\Cloud\Samples\Bigtable;
4+
5+
/**
6+
* Copyright 2019 Google LLC.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* For instructions on how to run the full sample:
23+
*
24+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
25+
*/
26+
27+
// [START bigtable_test_iam_permissions]
28+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
29+
30+
/**
31+
* Test IAM permissions for the current caller
32+
* @param string $projectId The Google Cloud project ID
33+
* @param string $instanceId The ID of the Bigtable instance
34+
*/
35+
function test_iam_permissions(
36+
string $projectId,
37+
string $instanceId
38+
): void {
39+
$instanceAdminClient = new BigtableInstanceAdminClient();
40+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
41+
42+
// The set of permissions to check for the `resource`. Permissions with
43+
// wildcards (such as '*' or 'bigtable.*') are not allowed. For more
44+
// information see
45+
// [IAM Overview](https://cloud.google.com/iam/docs/overview#permissions)
46+
$permissions = ['bigtable.clusters.create', 'bigtable.tables.create', 'bigtable.tables.list'];
47+
48+
$response = $instanceAdminClient->testIamPermissions($instanceName, $permissions);
49+
50+
// This array will contain the permissions that are passed for the current caller
51+
foreach ($response->getPermissions() as $permission) {
52+
printf($permission . PHP_EOL);
53+
}
54+
}
55+
// [END bigtable_test_iam_permissions]
56+
57+
// The following 2 lines are only needed to run the samples
58+
require_once __DIR__ . '/../../testing/sample_helpers.php';
59+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)