Skip to content

Commit 0340e40

Browse files
chore(bigtable): update 'create' samples to use the new format (GoogleCloudPlatform#1448)
1 parent d64a387 commit 0340e40

9 files changed

+421
-415
lines changed

bigtable/src/create_cluster.php

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,82 +22,83 @@
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 || count($argv) > 5) {
29-
return printf("Usage: php %s PROJECT_ID INSTANCE_ID CLUSTER_ID [LOCATION_ID]" . PHP_EOL, __FILE__);
30-
}
31-
list($_, $projectId, $instanceId, $clusterId) = $argv;
32-
$locationId = isset($argv[4]) ? $argv[4] : 'us-east1-b';
33-
3425
// [START bigtable_create_cluster]
35-
3626
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
3727
use Google\Cloud\Bigtable\Admin\V2\Cluster;
3828
use Google\Cloud\Bigtable\Admin\V2\StorageType;
3929
use Google\ApiCore\ApiException;
4030

41-
/** Uncomment and populate these variables in your code */
42-
// $projectId = 'The Google project ID';
43-
// $instanceId = 'The Bigtable instance ID';
44-
// $clusterId = 'The Bigtable cluster ID';
45-
// $locationId = 'The Bigtable region ID';
46-
47-
48-
$instanceAdminClient = new BigtableInstanceAdminClient();
31+
/**
32+
* Create a cluster in an existing Bigtable instance
33+
* @param string $projectId The Google Cloud project ID
34+
* @param string $instanceId The ID of the parent Bigtable instance
35+
* @param string $clusterId The ID of the cluster to be generated
36+
* @param string $locationId The Bigtable region ID where you want your cluster to reside
37+
*/
38+
function create_cluster(
39+
string $projectId,
40+
string $instanceId,
41+
string $clusterId,
42+
string $locationId = 'us-east1-b'
43+
): void {
44+
$instanceAdminClient = new BigtableInstanceAdminClient();
4945

50-
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
51-
$clusterName = $instanceAdminClient->clusterName($projectId, $instanceId, $clusterId);
46+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
47+
$clusterName = $instanceAdminClient->clusterName($projectId, $instanceId, $clusterId);
5248

53-
printf("Adding Cluster to Instance %s" . PHP_EOL, $instanceId);
54-
try {
55-
$instanceAdminClient->getInstance($instanceName);
56-
} catch (ApiException $e) {
57-
if ($e->getStatus() === 'NOT_FOUND') {
58-
printf("Instance %s does not exists." . PHP_EOL, $instanceId);
59-
return;
60-
} else {
61-
throw $e;
49+
printf("Adding Cluster to Instance %s" . PHP_EOL, $instanceId);
50+
try {
51+
$instanceAdminClient->getInstance($instanceName);
52+
} catch (ApiException $e) {
53+
if ($e->getStatus() === 'NOT_FOUND') {
54+
printf("Instance %s does not exists." . PHP_EOL, $instanceId);
55+
return;
56+
} else {
57+
throw $e;
58+
}
6259
}
63-
}
64-
printf("Listing Clusters:" . PHP_EOL);
60+
printf("Listing Clusters:" . PHP_EOL);
6561

66-
$storage_type = StorageType::SSD;
67-
$serve_nodes = 3;
62+
$storage_type = StorageType::SSD;
63+
$serve_nodes = 3;
6864

69-
$clustersBefore = $instanceAdminClient->listClusters($instanceName)->getClusters();
70-
$clusters = $clustersBefore->getIterator();
71-
foreach ($clusters as $cluster) {
72-
print($cluster->getName() . PHP_EOL);
73-
}
65+
$clustersBefore = $instanceAdminClient->listClusters($instanceName)->getClusters();
66+
$clusters = $clustersBefore->getIterator();
67+
foreach ($clusters as $cluster) {
68+
print($cluster->getName() . PHP_EOL);
69+
}
7470

75-
$cluster = new Cluster();
76-
$cluster->setServeNodes($serve_nodes);
77-
$cluster->setDefaultStorageType($storage_type);
78-
$cluster->setLocation(
79-
$instanceAdminClient->locationName(
80-
$projectId,
81-
$locationId
82-
)
83-
);
84-
try {
85-
$instanceAdminClient->getCluster($clusterName);
86-
printf("Cluster %s already exists, aborting...", $clusterId);
87-
} catch (ApiException $e) {
88-
if ($e->getStatus() === 'NOT_FOUND') {
89-
$operationResponse = $instanceAdminClient->createCluster($instanceName, $clusterId, $cluster);
71+
$cluster = new Cluster();
72+
$cluster->setServeNodes($serve_nodes);
73+
$cluster->setDefaultStorageType($storage_type);
74+
$cluster->setLocation(
75+
$instanceAdminClient->locationName(
76+
$projectId,
77+
$locationId
78+
)
79+
);
80+
try {
81+
$instanceAdminClient->getCluster($clusterName);
82+
printf("Cluster %s already exists, aborting...", $clusterId);
83+
} catch (ApiException $e) {
84+
if ($e->getStatus() === 'NOT_FOUND') {
85+
$operationResponse = $instanceAdminClient->createCluster($instanceName, $clusterId, $cluster);
9086

91-
$operationResponse->pollUntilComplete();
92-
if ($operationResponse->operationSucceeded()) {
93-
$result = $operationResponse->getResult();
94-
printf("Cluster created: %s", $clusterId);
87+
$operationResponse->pollUntilComplete();
88+
if ($operationResponse->operationSucceeded()) {
89+
$result = $operationResponse->getResult();
90+
printf("Cluster created: %s", $clusterId);
91+
} else {
92+
$error = $operationResponse->getError();
93+
printf("Cluster not created: %s", $error);
94+
}
9595
} else {
96-
$error = $operationResponse->getError();
97-
printf("Cluster not created: %s", $error);
96+
throw $e;
9897
}
99-
} else {
100-
throw $e;
10198
}
10299
}
103100
// [END bigtable_create_cluster]
101+
102+
// The following 2 lines are only needed to run the samples
103+
require_once __DIR__ . '/../../testing/sample_helpers.php';
104+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/create_dev_instance.php

Lines changed: 68 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,84 +22,83 @@
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 || count($argv) > 5) {
29-
return printf("Usage: php %s PROJECT_ID INSTANCE_ID CLUSTER_ID [LOCATION_ID]" . PHP_EOL, __FILE__);
30-
}
31-
list($_, $projectId, $instanceId, $clusterId) = $argv;
32-
$locationId = isset($argv[4]) ? $argv[4] : 'us-east1-b';
33-
3425
// [START bigtable_create_dev_instance]
35-
3626
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
3727
use Google\Cloud\Bigtable\Admin\V2\Instance;
3828
use Google\Cloud\Bigtable\Admin\V2\Cluster;
3929
use Google\Cloud\Bigtable\Admin\V2\StorageType;
4030
use Google\Cloud\Bigtable\Admin\V2\Instance\Type as InstanceType;
4131
use Google\ApiCore\ApiException;
4232

43-
/** Uncomment and populate these variables in your code */
44-
// $projectId = 'The Google project ID';
45-
// $instanceId = 'The Bigtable instance ID';
46-
// $clusterId = 'The Bigtable cluster ID';
47-
// $locationId = 'The Bigtable region ID';
48-
49-
50-
$instanceAdminClient = new BigtableInstanceAdminClient();
51-
52-
$projectName = $instanceAdminClient->projectName($projectId);
53-
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
54-
55-
56-
printf("Creating a DEVELOPMENT Instance" . PHP_EOL);
57-
// Set options to create an Instance
58-
59-
$storageType = StorageType::HDD;
60-
$development = InstanceType::DEVELOPMENT;
61-
$labels = ['dev-label' => 'dev-label'];
62-
63-
64-
# Create instance with given options
65-
$instance = new Instance();
66-
$instance->setDisplayName($instanceId);
67-
$instance->setLabels($labels);
68-
$instance->setType($development);
69-
70-
// Create cluster with given options
71-
$cluster = new Cluster();
72-
$cluster->setDefaultStorageType($storageType);
73-
$cluster->setLocation(
74-
$instanceAdminClient->locationName(
75-
$projectId,
76-
$locationId
77-
)
78-
);
79-
$clusters = [
80-
$clusterId => $cluster
81-
];
82-
// Create development instance with given options
83-
try {
84-
$instanceAdminClient->getInstance($instanceName);
85-
printf("Instance %s already exists." . PHP_EOL, $instanceId);
86-
} catch (ApiException $e) {
87-
if ($e->getStatus() === 'NOT_FOUND') {
88-
printf("Creating a development Instance: %s" . PHP_EOL, $instanceId);
89-
$operationResponse = $instanceAdminClient->createInstance(
90-
$projectName,
91-
$instanceId,
92-
$instance,
93-
$clusters
94-
);
95-
$operationResponse->pollUntilComplete();
96-
if (!$operationResponse->operationSucceeded()) {
97-
print('Error: ' . $operationResponse->getError()->getMessage());
33+
/**
34+
* Create a development Bigtable instance
35+
* @param string $projectId The Google Cloud project ID
36+
* @param string $instanceId The ID of the Bigtable instance to be generated
37+
* @param string $clusterId The ID of the cluster to be generated
38+
* @param string $locationId The Bigtable region ID where you want your instance to reside
39+
*/
40+
function create_dev_instance(
41+
string $projectId,
42+
string $instanceId,
43+
string $clusterId,
44+
string $locationId = 'us-east1-b'
45+
): void {
46+
$instanceAdminClient = new BigtableInstanceAdminClient();
47+
48+
$projectName = $instanceAdminClient->projectName($projectId);
49+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
50+
51+
printf("Creating a DEVELOPMENT Instance" . PHP_EOL);
52+
// Set options to create an Instance
53+
54+
$storageType = StorageType::HDD;
55+
$development = InstanceType::DEVELOPMENT;
56+
$labels = ['dev-label' => 'dev-label'];
57+
58+
# Create instance with given options
59+
$instance = new Instance();
60+
$instance->setDisplayName($instanceId);
61+
$instance->setLabels($labels);
62+
$instance->setType($development);
63+
64+
// Create cluster with given options
65+
$cluster = new Cluster();
66+
$cluster->setDefaultStorageType($storageType);
67+
$cluster->setLocation(
68+
$instanceAdminClient->locationName(
69+
$projectId,
70+
$locationId
71+
)
72+
);
73+
$clusters = [
74+
$clusterId => $cluster
75+
];
76+
// Create development instance with given options
77+
try {
78+
$instanceAdminClient->getInstance($instanceName);
79+
printf("Instance %s already exists." . PHP_EOL, $instanceId);
80+
} catch (ApiException $e) {
81+
if ($e->getStatus() === 'NOT_FOUND') {
82+
printf("Creating a development Instance: %s" . PHP_EOL, $instanceId);
83+
$operationResponse = $instanceAdminClient->createInstance(
84+
$projectName,
85+
$instanceId,
86+
$instance,
87+
$clusters
88+
);
89+
$operationResponse->pollUntilComplete();
90+
if (!$operationResponse->operationSucceeded()) {
91+
print('Error: ' . $operationResponse->getError()->getMessage());
92+
} else {
93+
printf("Instance %s created." . PHP_EOL, $instanceId);
94+
}
9895
} else {
99-
printf("Instance %s created.", $instanceId);
96+
throw $e;
10097
}
101-
} else {
102-
throw $e;
10398
}
99+
// [END bigtable_create_dev_instance]
104100
}
105-
// [END bigtable_create_dev_instance]
101+
102+
// The following 2 lines are only needed to run the samples
103+
require_once __DIR__ . '/../../testing/sample_helpers.php';
104+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/create_family_gc_intersection.php

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@
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_create_family_gc_intersection]
3426
use Google\Cloud\Bigtable\Admin\V2\GcRule\Intersection as GcRuleIntersection;
3527
use Google\Cloud\Bigtable\Admin\V2\ModifyColumnFamiliesRequest\Modification;
@@ -38,35 +30,46 @@
3830
use Google\Cloud\Bigtable\Admin\V2\GcRule;
3931
use Google\Protobuf\Duration;
4032

41-
/** Uncomment and populate these variables in your code */
42-
// $projectId = 'The Google project ID';
43-
// $instanceId = 'The Bigtable instance ID';
44-
// $tableId = 'The Bigtable table ID';
45-
46-
$tableAdminClient = new BigtableTableAdminClient();
33+
/**
34+
* Create a new column family with an intersection GC rule
35+
* @param string $projectId The Google Cloud project ID
36+
* @param string $instanceId The ID of the Bigtable instance where the table resides
37+
* @param string $tableId The ID of the table in which the rule needs to be created
38+
*/
39+
function create_family_gc_intersection(
40+
string $projectId,
41+
string $instanceId,
42+
string $tableId
43+
): void {
44+
$tableAdminClient = new BigtableTableAdminClient();
4745

48-
$tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);
46+
$tableName = $tableAdminClient->tableName($projectId, $instanceId, $tableId);
4947

50-
print('Creating column family cf4 with Intersection GC rule...' . PHP_EOL);
51-
$columnFamily4 = new ColumnFamily();
48+
print('Creating column family cf4 with Intersection GC rule...' . PHP_EOL);
49+
$columnFamily4 = new ColumnFamily();
5250

53-
$intersectionRule = new GcRuleIntersection();
54-
$intersectionArray = [
55-
(new GcRule)->setMaxAge((new Duration())->setSeconds(3600 * 24 * 5)),
56-
(new GcRule)->setMaxNumVersions(2)
57-
];
58-
$intersectionRule->setRules($intersectionArray);
51+
$intersectionRule = new GcRuleIntersection();
52+
$intersectionArray = [
53+
(new GcRule())->setMaxAge((new Duration())->setSeconds(3600 * 24 * 5)),
54+
(new GcRule())->setMaxNumVersions(2)
55+
];
56+
$intersectionRule->setRules($intersectionArray);
5957

60-
$intersection = new GcRule();
61-
$intersection->setIntersection($intersectionRule);
58+
$intersection = new GcRule();
59+
$intersection->setIntersection($intersectionRule);
6260

63-
$columnFamily4->setGCRule($intersection);
61+
$columnFamily4->setGCRule($intersection);
6462

65-
$columnModification = new Modification();
66-
$columnModification->setId('cf4');
67-
$columnModification->setCreate($columnFamily4);
68-
$tableAdminClient->modifyColumnFamilies($tableName, [$columnModification]);
63+
$columnModification = new Modification();
64+
$columnModification->setId('cf4');
65+
$columnModification->setCreate($columnFamily4);
66+
$tableAdminClient->modifyColumnFamilies($tableName, [$columnModification]);
6967

70-
print('Created column family cf4 with Union GC rule' . PHP_EOL);
68+
print('Created column family cf4 with Union GC rule' . PHP_EOL);
69+
}
7170

7271
// [END bigtable_create_family_gc_intersection]
72+
73+
// The following 2 lines are only needed to run the samples
74+
require_once __DIR__ . '/../../testing/sample_helpers.php';
75+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)