Skip to content

Commit 75cc785

Browse files
chore(bigtable): new sample format for write and delete samples (GoogleCloudPlatform#1450)
1 parent 0340e40 commit 75cc785

25 files changed

+412
-372
lines changed

bigtable/src/create_cluster.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*

bigtable/src/create_dev_instance.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*

bigtable/src/create_family_gc_intersection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*

bigtable/src/create_family_gc_max_age.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*

bigtable/src/create_family_gc_max_versions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*

bigtable/src/create_family_gc_nested.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*

bigtable/src/create_family_gc_union.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*

bigtable/src/create_production_instance.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*

bigtable/src/create_table.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*

bigtable/src/delete_cluster.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*
@@ -22,39 +24,38 @@
2224
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2325
*/
2426

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 CLUSTER_ID" . PHP_EOL, __FILE__);
30-
}
31-
list($_, $projectId, $instanceId, $clusterId) = $argv;
32-
3327
// [START bigtable_delete_cluster]
34-
3528
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
3629
use Google\ApiCore\ApiException;
3730

38-
/** Uncomment and populate these variables in your code */
39-
// $projectId = 'The Google project ID';
40-
// $instanceId = 'The Bigtable instance ID';
41-
// $clusterId = 'The Bigtable cluster ID';
42-
43-
44-
$instanceAdminClient = new BigtableInstanceAdminClient();
45-
46-
$clusterName = $instanceAdminClient->clusterName($projectId, $instanceId, $clusterId);
47-
48-
49-
printf("Deleting Cluster" . PHP_EOL);
50-
try {
51-
$instanceAdminClient->deleteCluster($clusterName);
52-
printf("Cluster %s deleted." . PHP_EOL, $clusterId);
53-
} catch (ApiException $e) {
54-
if ($e->getStatus() === 'NOT_FOUND') {
55-
printf("Cluster %s does not exist." . PHP_EOL, $clusterId);
56-
} else {
57-
throw $e;
31+
/**
32+
* Delete a cluster
33+
* @param string $projectId The Google Cloud project ID
34+
* @param string $instanceId The ID of the Bigtable instance
35+
* @param string $clusterId The ID of the cluster to be deleted
36+
*/
37+
function delete_cluster(
38+
string $projectId,
39+
string $instanceId,
40+
string $clusterId
41+
): void {
42+
$instanceAdminClient = new BigtableInstanceAdminClient();
43+
$clusterName = $instanceAdminClient->clusterName($projectId, $instanceId, $clusterId);
44+
45+
printf("Deleting Cluster" . PHP_EOL);
46+
try {
47+
$instanceAdminClient->deleteCluster($clusterName);
48+
printf("Cluster %s deleted." . PHP_EOL, $clusterId);
49+
} catch (ApiException $e) {
50+
if ($e->getStatus() === 'NOT_FOUND') {
51+
printf("Cluster %s does not exist." . PHP_EOL, $clusterId);
52+
} else {
53+
throw $e;
54+
}
5855
}
5956
}
6057
// [END bigtable_delete_cluster]
58+
59+
// The following 2 lines are only needed to run the samples
60+
require_once __DIR__ . '/../../testing/sample_helpers.php';
61+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/delete_family.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*
@@ -22,36 +24,36 @@
2224
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2325
*/
2426

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-
$familyId = isset($argv[4]) ? $argv[4] : 'cf2';
33-
3427
// [START bigtable_delete_family]
35-
3628
use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;
3729
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
3830

39-
/** Uncomment and populate these variables in your code */
40-
// $projectId = 'The Google project ID';
41-
// $instanceId = 'The Bigtable instance ID';
42-
// $tableId = 'The Bigtable table ID';
43-
// $locationId = 'The Bigtable region ID';
44-
45-
$tableAdminClient = new BigtableTableAdminClient();
46-
47-
$tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);
48-
49-
50-
print('Delete a column family cf2...' . PHP_EOL);
51-
// Delete a column family
52-
$columnModification = new Modification();
53-
$columnModification->setId($familyId);
54-
$columnModification->setDrop(true);
55-
$tableAdminClient->modifyColumnFamilies($tableName, [$columnModification]);
56-
print('Column family cf2 deleted successfully.' . PHP_EOL);
31+
/**
32+
* Delete a column family in a table
33+
* @param string $projectId The Google Cloud project ID
34+
* @param string $instanceId The ID of the Bigtable instance
35+
* @param string $tableId The ID of the table where the column family needs to be deleted
36+
* @param string $familyId The ID of the column family to be deleted
37+
*/
38+
function delete_family(
39+
string $projectId,
40+
string $instanceId,
41+
string $tableId,
42+
string $familyId = 'cf2'
43+
): void {
44+
$tableAdminClient = new BigtableTableAdminClient();
45+
$tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);
46+
47+
print("Delete a column family $familyId..." . PHP_EOL);
48+
// Delete a column family
49+
$columnModification = new Modification();
50+
$columnModification->setId($familyId);
51+
$columnModification->setDrop(true);
52+
$tableAdminClient->modifyColumnFamilies($tableName, [$columnModification]);
53+
print("Column family $familyId deleted successfully." . PHP_EOL);
54+
}
5755
// [END bigtable_delete_family]
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);

bigtable/src/delete_instance.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*
@@ -22,37 +24,36 @@
2224
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2325
*/
2426

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-
27+
// [START bigtable_delete_instance]
3328
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
3429
use Google\ApiCore\ApiException;
3530

36-
/** Uncomment and populate these variables in your code */
37-
// $projectId = 'The Google project ID';
38-
// $instanceId = 'The Bigtable instance ID';
39-
40-
41-
$instanceAdminClient = new BigtableInstanceAdminClient();
42-
43-
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
44-
45-
46-
// [START bigtable_delete_instance]
47-
printf("Deleting Instance" . PHP_EOL);
48-
try {
49-
$instanceAdminClient->deleteInstance($instanceName);
50-
printf("Deleted Instance: %s." . PHP_EOL, $instanceId);
51-
} catch (ApiException $e) {
52-
if ($e->getStatus() === 'NOT_FOUND') {
53-
printf("Instance %s does not exists." . PHP_EOL, $instanceId);
54-
} else {
55-
throw $e;
31+
/**
32+
* Delete a bigtable instance
33+
* @param string $projectId The Google Cloud project ID
34+
* @param string $instanceId The ID of the Bigtable instance to be deleted
35+
*/
36+
function delete_instance(
37+
string $projectId,
38+
string $instanceId
39+
): void {
40+
$instanceAdminClient = new BigtableInstanceAdminClient();
41+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
42+
43+
printf("Deleting Instance" . PHP_EOL);
44+
try {
45+
$instanceAdminClient->deleteInstance($instanceName);
46+
printf("Deleted Instance: %s." . PHP_EOL, $instanceId);
47+
} catch (ApiException $e) {
48+
if ($e->getStatus() === 'NOT_FOUND') {
49+
printf("Instance %s does not exists." . PHP_EOL, $instanceId);
50+
} else {
51+
throw $e;
52+
}
5653
}
5754
}
5855
// [END bigtable_delete_instance]
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);

bigtable/src/delete_table.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
22

3+
namespace Google\Cloud\Samples\Bigtable;
4+
35
/**
46
* Copyright 2019 Google LLC.
57
*
@@ -22,39 +24,39 @@
2224
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
2325
*/
2426

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-
3327
// [START bigtable_delete_table]
34-
3528
use Google\Cloud\Bigtable\Admin\V2\BigtableTableAdminClient;
3629
use Google\ApiCore\ApiException;
3730

38-
/** Uncomment and populate these variables in your code */
39-
// $projectId = 'The Google project ID';
40-
// $instanceId = 'The Bigtable instance ID';
41-
// $tableId = 'The Bigtable table ID';
42-
43-
$tableAdminClient = new BigtableTableAdminClient();
44-
45-
$tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);
46-
47-
// Delete the entire table
48-
49-
try {
50-
printf('Attempting to delete table %s.' . PHP_EOL, $tableId);
51-
$tableAdminClient->deleteTable($tableName);
52-
printf('Deleted %s table.' . PHP_EOL, $tableId);
53-
} catch (ApiException $e) {
54-
if ($e->getStatus() === 'NOT_FOUND') {
55-
printf('Table %s does not exists' . PHP_EOL, $tableId);
56-
} else {
57-
throw $e;
31+
/**
32+
* Delete a table
33+
* @param string $projectId The Google Cloud project ID
34+
* @param string $instanceId The ID of the Bigtable instance
35+
* @param string $tableId The ID of the table to be deleted
36+
*/
37+
function delete_table(
38+
string $projectId,
39+
string $instanceId,
40+
string $tableId
41+
): void {
42+
$tableAdminClient = new BigtableTableAdminClient();
43+
$tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);
44+
45+
// Delete the entire table
46+
try {
47+
printf('Attempting to delete table %s.' . PHP_EOL, $tableId);
48+
$tableAdminClient->deleteTable($tableName);
49+
printf('Deleted %s table.' . PHP_EOL, $tableId);
50+
} catch (ApiException $e) {
51+
if ($e->getStatus() === 'NOT_FOUND') {
52+
printf('Table %s does not exists' . PHP_EOL, $tableId);
53+
} else {
54+
throw $e;
55+
}
5856
}
5957
}
6058
// [END bigtable_delete_table]
59+
60+
// The following 2 lines are only needed to run the samples
61+
require_once __DIR__ . '/../../testing/sample_helpers.php';
62+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)