Skip to content

Commit 78356e8

Browse files
chore: Changed samples in Bigtable to new format for the 'list samples' - issue GoogleCloudPlatform#1389 (GoogleCloudPlatform#1449)
1 parent 11ee0b2 commit 78356e8

File tree

5 files changed

+98
-100
lines changed

5 files changed

+98
-100
lines changed

bigtable/src/list_column_families.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,35 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependencies using Composer
26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) != 4) {
29-
return printf("Usage: php %s PROJECT_ID INSTANCE_ID TABLE_ID" . PHP_EOL, __FILE__);
30-
}
31-
list($_, $projectId, $instanceId, $tableId) = $argv;
32-
3325
// [START bigtable_list_column_families]
34-
3526
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
3627

37-
/** Uncomment and populate these variables in your code */
38-
// $projectId = 'The Google project ID';
39-
// $instanceId = 'The Bigtable instance ID';
40-
// $tableId = 'The Bigtable table ID';
41-
42-
$tableAdminClient = new BigtableTableAdminClient();
43-
44-
$tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);
45-
46-
47-
$table = $tableAdminClient->getTable($tableName);
48-
$columnFamilies = $table->getColumnFamilies()->getIterator();
49-
50-
foreach ($columnFamilies as $k => $columnFamily) {
51-
printf('Column Family: %s' . PHP_EOL, $k);
52-
print('GC Rule:' . PHP_EOL);
53-
printf('%s' . PHP_EOL, $columnFamily->serializeToJsonString());
28+
/**
29+
* List column families of a table
30+
* @param string $projectId The Google Cloud project ID
31+
* @param string $instanceId The ID of the Bigtable instance
32+
* @param string $tableId The ID of the table for which the families need to be displayed
33+
*/
34+
function list_column_families(
35+
string $projectId,
36+
string $instanceId,
37+
string $tableId
38+
): void {
39+
$tableAdminClient = new BigtableTableAdminClient();
40+
41+
$tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);
42+
43+
$table = $tableAdminClient->getTable($tableName);
44+
$columnFamilies = $table->getColumnFamilies()->getIterator();
45+
46+
foreach ($columnFamilies as $k => $columnFamily) {
47+
printf('Column Family: %s' . PHP_EOL, $k);
48+
print('GC Rule:' . PHP_EOL);
49+
printf('%s' . PHP_EOL, $columnFamily->serializeToJsonString());
50+
}
5451
}
5552
// [END bigtable_list_column_families]
53+
54+
// The following 2 lines are only needed to run the samples
55+
require_once __DIR__ . '/../../testing/sample_helpers.php';
56+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/list_instance.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,30 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependencies using Composer
26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) != 3) {
29-
return printf("Usage: php %s PROJECT_ID INSTANCE_ID" . PHP_EOL, __FILE__);
30-
}
31-
list($_, $projectId, $instanceId) = $argv;
32-
3325
// [START bigtable_list_instances]
34-
3526
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
3627

37-
/** Uncomment and populate these variables in your code */
38-
// $projectId = 'The Google project ID';
39-
// $instanceId = 'The Bigtable instance ID';
40-
41-
$instanceAdminClient = new BigtableInstanceAdminClient();
28+
/**
29+
* List Bigtable instances in a project
30+
* @param string $projectId The Google Cloud project ID
31+
*/
32+
function list_instance(string $projectId): void
33+
{
34+
$instanceAdminClient = new BigtableInstanceAdminClient();
4235

43-
$projectName = $instanceAdminClient->projectName($projectId);
36+
$projectName = $instanceAdminClient->projectName($projectId);
4437

45-
printf("Listing Instances:" . PHP_EOL);
38+
printf("Listing Instances:" . PHP_EOL);
4639

47-
$getInstances = $instanceAdminClient->listInstances($projectName)->getInstances();
48-
$instances = $getInstances->getIterator();
40+
$getInstances = $instanceAdminClient->listInstances($projectName)->getInstances();
41+
$instances = $getInstances->getIterator();
4942

50-
foreach ($instances as $instance) {
51-
print($instance->getDisplayName() . PHP_EOL);
43+
foreach ($instances as $instance) {
44+
print($instance->getDisplayName() . PHP_EOL);
45+
}
5246
}
53-
5447
// [END bigtable_list_instances]
48+
49+
// The following 2 lines are only needed to run the samples
50+
require_once __DIR__ . '/../../testing/sample_helpers.php';
51+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/list_instance_clusters.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,33 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependencies using Composer
26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) != 3) {
29-
return printf("Usage: php %s PROJECT_ID INSTANCE_ID" . PHP_EOL, __FILE__);
30-
}
31-
list($_, $projectId, $instanceId) = $argv;
32-
3325
// [START bigtable_get_clusters]
34-
3526
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
3627

37-
/** Uncomment and populate these variables in your code */
38-
// $projectId = 'The Google project ID';
39-
// $instanceId = 'The Bigtable instance ID';
40-
41-
$instanceAdminClient = new BigtableInstanceAdminClient();
42-
43-
$projectName = $instanceAdminClient->projectName($projectId);
44-
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
45-
46-
47-
printf("Listing Clusters:" . PHP_EOL);
48-
$getClusters = $instanceAdminClient->listClusters($instanceName)->getClusters();
49-
$clusters = $getClusters->getIterator();
50-
51-
foreach ($clusters as $cluster) {
52-
print($cluster->getName() . PHP_EOL);
28+
/**
29+
* List clusters of an instance
30+
* @param string $projectId The Google Cloud project ID
31+
* @param string $instanceId The ID of the Bigtable instance
32+
*/
33+
function list_instance_clusters(
34+
string $projectId,
35+
string $instanceId
36+
): void {
37+
$instanceAdminClient = new BigtableInstanceAdminClient();
38+
39+
$projectName = $instanceAdminClient->projectName($projectId);
40+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
41+
42+
printf("Listing Clusters:" . PHP_EOL);
43+
$getClusters = $instanceAdminClient->listClusters($instanceName)->getClusters();
44+
$clusters = $getClusters->getIterator();
45+
46+
foreach ($clusters as $cluster) {
47+
print($cluster->getName() . PHP_EOL);
48+
}
5349
}
5450
// [END bigtable_get_clusters]
51+
52+
// The following 2 lines are only needed to run the samples
53+
require_once __DIR__ . '/../../testing/sample_helpers.php';
54+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/list_tables.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,36 @@
2222
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2323
*/
2424

25-
// Include Google Cloud dependencies using Composer
26-
require_once __DIR__ . '/../vendor/autoload.php';
27-
28-
if (count($argv) != 3) {
29-
return printf("Usage: php %s PROJECT_ID INSTANCE_ID" . PHP_EOL, __FILE__);
30-
}
31-
list($_, $projectId, $instanceId) = $argv;
32-
3325
// [START bigtable_list_tables]
34-
3526
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
3627
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
3728

38-
/** Uncomment and populate these variables in your code */
39-
// $projectId = 'The Google project ID';
40-
// $instanceId = 'The Bigtable instance ID';
41-
42-
$instanceAdminClient = new BigtableInstanceAdminClient();
43-
$tableAdminClient = new BigtableTableAdminClient();
44-
45-
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
46-
47-
printf("Listing Tables:" . PHP_EOL);
48-
$tables = $tableAdminClient->listTables($instanceName)->iterateAllElements();
49-
if (empty($tables)) {
50-
print('No table exists.' . PHP_EOL);
51-
return;
52-
}
53-
foreach ($tables as $table) {
54-
print($table->getName() . PHP_EOL);
29+
/**
30+
* List tables in an instance
31+
* @param string $projectId The Google Cloud project ID
32+
* @param string $instanceId The ID of the Bigtable instance
33+
*/
34+
function list_tables(
35+
string $projectId,
36+
string $instanceId
37+
): void {
38+
$instanceAdminClient = new BigtableInstanceAdminClient();
39+
$tableAdminClient = new BigtableTableAdminClient();
40+
41+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
42+
43+
printf("Listing Tables:" . PHP_EOL);
44+
$tables = $tableAdminClient->listTables($instanceName)->iterateAllElements();
45+
if (empty($tables)) {
46+
print('No table exists.' . PHP_EOL);
47+
return;
48+
}
49+
foreach ($tables as $table) {
50+
print($table->getName() . PHP_EOL);
51+
}
5552
}
5653
// [END bigtable_list_tables]
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/test/bigtableTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ public function testCreateDevInstance()
109109
public function testListInstances()
110110
{
111111
$content = self::runFileSnippet('list_instance', [
112-
self::$projectId,
113-
self::$instanceId
112+
self::$projectId
114113
]);
115114

116115
$array = explode(PHP_EOL, $content);

0 commit comments

Comments
 (0)