|
22 | 22 | * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/bigtable/README.md
|
23 | 23 | */
|
24 | 24 |
|
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 |
| - |
34 | 25 | // [START bigtable_create_cluster]
|
35 |
| - |
36 | 26 | use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
|
37 | 27 | use Google\Cloud\Bigtable\Admin\V2\Cluster;
|
38 | 28 | use Google\Cloud\Bigtable\Admin\V2\StorageType;
|
39 | 29 | use Google\ApiCore\ApiException;
|
40 | 30 |
|
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(); |
49 | 45 |
|
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); |
52 | 48 |
|
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 | + } |
62 | 59 | }
|
63 |
| -} |
64 |
| -printf("Listing Clusters:" . PHP_EOL); |
| 60 | + printf("Listing Clusters:" . PHP_EOL); |
65 | 61 |
|
66 |
| -$storage_type = StorageType::SSD; |
67 |
| -$serve_nodes = 3; |
| 62 | + $storage_type = StorageType::SSD; |
| 63 | + $serve_nodes = 3; |
68 | 64 |
|
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 | + } |
74 | 70 |
|
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); |
90 | 86 |
|
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 | + } |
95 | 95 | } else {
|
96 |
| - $error = $operationResponse->getError(); |
97 |
| - printf("Cluster not created: %s", $error); |
| 96 | + throw $e; |
98 | 97 | }
|
99 |
| - } else { |
100 |
| - throw $e; |
101 | 98 | }
|
102 | 99 | }
|
103 | 100 | // [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); |
0 commit comments