|
| 1 | +<?php |
| 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); |
0 commit comments