Skip to content

Commit 332aac5

Browse files
samples: Bigtable: Added App Profile samples and tests (GoogleCloudPlatform#1456)
* Bigtable: Added App Profile samples and tests * Bigtable: Addressed PR comments for App Profile samples Changed double quotes to single quotes Removed redundant else blocks Converted echo statements to printf * Changed quotes for bigtableTest * Fixed lint errors Co-authored-by: Brent Shaffer
1 parent cc72614 commit 332aac5

File tree

6 files changed

+522
-1
lines changed

6 files changed

+522
-1
lines changed

bigtable/src/create_app_profile.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
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_create_app_profile]
28+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
29+
use Google\Cloud\Bigtable\Admin\V2\AppProfile;
30+
use Google\Cloud\Bigtable\Admin\V2\AppProfile\SingleClusterRouting;
31+
use Google\ApiCore\ApiException;
32+
33+
/**
34+
* Create an App Profile
35+
* @param string $projectId The Google Cloud project ID
36+
* @param string $instanceId The ID of the Bigtable instance
37+
* @param string $clusterId The ID of the cluster where the new App Profile will route it's requests(in case of single cluster routing)
38+
* @param string $appProfileId The ID of the App Profile to create
39+
*/
40+
function create_app_profile(
41+
string $projectId,
42+
string $instanceId,
43+
string $clusterId,
44+
string $appProfileId
45+
): void {
46+
$instanceAdminClient = new BigtableInstanceAdminClient();
47+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
48+
49+
$appProfile = new AppProfile([
50+
'name' => $appProfileId,
51+
'description' => 'Description for this newly created AppProfile'
52+
]);
53+
54+
// create a new routing policy
55+
// allow_transactional_writes refers to Single-Row-Transactions(https://cloud.google.com/bigtable/docs/app-profiles#single-row-transactions)
56+
$routingPolicy = new SingleClusterRouting([
57+
'cluster_id' => $clusterId,
58+
'allow_transactional_writes' => false
59+
]);
60+
61+
// set the newly created routing policy to our app profile
62+
$appProfile->setSingleClusterRouting($routingPolicy);
63+
64+
// we could also create a multi cluster routing policy like so:
65+
// $routingPolicy = new \Google\Cloud\Bigtable\Admin\V2\AppProfile\MultiClusterRoutingUseAny();
66+
// $appProfile->setMultiClusterRoutingUseAny($routingPolicy);
67+
68+
printf('Creating a new AppProfile %s' . PHP_EOL, $appProfileId);
69+
70+
try {
71+
$newAppProfile = $instanceAdminClient->createAppProfile($instanceName, $appProfileId, $appProfile);
72+
} catch (ApiException $e) {
73+
if ($e->getStatus() === 'ALREADY_EXISTS') {
74+
printf('AppProfile %s already exists.', $appProfileId);
75+
return;
76+
}
77+
throw $e;
78+
}
79+
80+
printf('AppProfile created: %s', $newAppProfile->getName());
81+
}
82+
// [END bigtable_create_app_profile]
83+
84+
// The following 2 lines are only needed to run the samples
85+
require_once __DIR__ . '/../../testing/sample_helpers.php';
86+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/delete_app_profile.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
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_delete_app_profile]
28+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
29+
use Google\ApiCore\ApiException;
30+
31+
/**
32+
* Delete an App Profile from a Bigtable instance.
33+
* @param string $projectId The Google Cloud project ID
34+
* @param string $instanceId The ID of the Bigtable instance
35+
* @param string $appProfileId The ID of the App Profile to be deleted
36+
*/
37+
function delete_app_profile(
38+
string $projectId,
39+
string $instanceId,
40+
string $appProfileId
41+
): void {
42+
$instanceAdminClient = new BigtableInstanceAdminClient();
43+
$appProfileName = $instanceAdminClient->appProfileName($projectId, $instanceId, $appProfileId);
44+
$ignoreWarnings = true;
45+
46+
printf('Deleting the App Profile: %s' . PHP_EOL, $appProfileId);
47+
48+
try {
49+
// If $ignoreWarnings is set to false, Bigtable will warn you that all future requests using the AppProfile will fail
50+
$instanceAdminClient->deleteAppProfile($appProfileName, $ignoreWarnings);
51+
printf('App Profile %s deleted.' . PHP_EOL, $appProfileId);
52+
} catch (ApiException $e) {
53+
if ($e->getStatus() === 'NOT_FOUND') {
54+
printf('App Profile %s does not exist.' . PHP_EOL, $appProfileId);
55+
return;
56+
}
57+
throw $e;
58+
}
59+
}
60+
// [END bigtable_delete_app_profile]
61+
62+
// The following 2 lines are only needed to run the samples
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/get_app_profile.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
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_app_profile]
28+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
29+
use Google\ApiCore\ApiException;
30+
31+
/**
32+
* Get the App Profile
33+
* @param string $projectId The Google Cloud project ID
34+
* @param string $instanceId The ID of the Bigtable instance
35+
* @param string $appProfileId The ID of the App Profile to fetch
36+
*/
37+
function get_app_profile(
38+
string $projectId,
39+
string $instanceId,
40+
string $appProfileId
41+
): void {
42+
$instanceAdminClient = new BigtableInstanceAdminClient();
43+
$appProfileName = $instanceAdminClient->appProfileName($projectId, $instanceId, $appProfileId);
44+
45+
printf('Fetching the App Profile %s' . PHP_EOL, $appProfileId);
46+
try {
47+
$appProfile = $instanceAdminClient->getAppProfile($appProfileName);
48+
} catch (ApiException $e) {
49+
if ($e->getStatus() === 'NOT_FOUND') {
50+
printf('App profile %s does not exist.' . PHP_EOL, $appProfileId);
51+
return;
52+
}
53+
throw $e;
54+
}
55+
56+
printf('Printing Details:' . PHP_EOL);
57+
58+
// Fetch some commonly used metadata
59+
printf('Name: %s' . PHP_EOL, $appProfile->getName());
60+
printf('Etag: %s' . PHP_EOL, $appProfile->getEtag());
61+
printf('Description: %s' . PHP_EOL, $appProfile->getDescription());
62+
printf('Routing Policy: %s' . PHP_EOL, $appProfile->getRoutingPolicy());
63+
64+
if ($appProfile->hasSingleClusterRouting()) {
65+
$clusterId = $appProfile->getSingleClusterRouting()->getClusterId();
66+
$singleRowTransactions = $appProfile->getSingleClusterRouting()->getAllowTransactionalWrites() ? 'Yes' : 'No';
67+
printf('Cluster: %s' . PHP_EOL, $clusterId);
68+
printf('Single-Row Transactions: %s' . PHP_EOL, $singleRowTransactions);
69+
}
70+
}
71+
// [END bigtable_get_app_profile]
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);

bigtable/src/list_app_profiles.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
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_list_app_profiles]
28+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
29+
use Google\ApiCore\ApiException;
30+
31+
/**
32+
* List the App profiles for an instance
33+
* @param string $projectId The Google Cloud project ID
34+
* @param string $instanceId The ID of the Bigtable instance
35+
*/
36+
function list_app_profiles(
37+
string $projectId,
38+
string $instanceId
39+
): void {
40+
$instanceAdminClient = new BigtableInstanceAdminClient();
41+
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
42+
43+
printf('Fetching App Profiles' . PHP_EOL);
44+
45+
try {
46+
$appProfiles = $instanceAdminClient->listAppProfiles($instanceName);
47+
48+
foreach ($appProfiles->iterateAllElements() as $profile) {
49+
// You can fetch any AppProfile metadata from the $profile object(see get_app_profile.php)
50+
printf('Name: %s' . PHP_EOL, $profile->getName());
51+
}
52+
} catch (ApiException $e) {
53+
if ($e->getStatus() === 'NOT_FOUND') {
54+
printf('Instance %s does not exist.' . PHP_EOL, $instanceId);
55+
return;
56+
}
57+
throw $e;
58+
}
59+
}
60+
// [END bigtable_list_app_profiles]
61+
62+
// The following 2 lines are only needed to run the samples
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigtable/src/update_app_profile.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
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_update_app_profile]
28+
use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
29+
use Google\Cloud\Bigtable\Admin\V2\AppProfile;
30+
use Google\Cloud\Bigtable\Admin\V2\AppProfile\SingleClusterRouting;
31+
use Google\ApiCore\ApiException;
32+
use Google\Protobuf\FieldMask;
33+
34+
/**
35+
* Update an App Profile
36+
* @param string $projectId The Google Cloud project ID
37+
* @param string $instanceId The ID of the Bigtable instance
38+
* @param string $clusterId The ID of the new cluster where the new App Profile will route it's requests(in case of single cluster routing)
39+
* @param string $appProfileId The ID of the App Profile to update
40+
*/
41+
function update_app_profile(
42+
string $projectId,
43+
string $instanceId,
44+
string $clusterId,
45+
string $appProfileId
46+
): void {
47+
$instanceAdminClient = new BigtableInstanceAdminClient();
48+
$appProfileName = $instanceAdminClient->appProfileName($projectId, $instanceId, $appProfileId);
49+
50+
$appProfile = new AppProfile([
51+
'name' => $appProfileName,
52+
'description' => 'The updated description',
53+
]);
54+
55+
// create a new routing policy
56+
// allow_transactional_writes refers to Single-Row-Transactions(https://cloud.google.com/bigtable/docs/app-profiles#single-row-transactions)
57+
$routingPolicy = new SingleClusterRouting([
58+
'cluster_id' => $clusterId,
59+
'allow_transactional_writes' => true
60+
]);
61+
62+
// set the newly created routing policy to our app profile
63+
$appProfile->setSingleClusterRouting($routingPolicy);
64+
65+
// or we could also create a multi cluster routing policy like so:
66+
// $routingPolicy = new \Google\Cloud\Bigtable\Admin\V2\AppProfile\MultiClusterRoutingUseAny();
67+
// $appProfile->setMultiClusterRoutingUseAny($routingPolicy);
68+
69+
// returns a string identifier depending on SingleClusterRouting or MultiClusterRoutingUseAny
70+
$routingPolicyStr = $appProfile->getRoutingPolicy();
71+
72+
$updateMask = new FieldMask([
73+
'paths' => ['description', $routingPolicyStr]
74+
]);
75+
76+
printf('Updating the AppProfile %s' . PHP_EOL, $appProfileId);
77+
78+
try {
79+
// Bigtable warns you while updating the routing policy, or when toggling the allow_transactional_writes
80+
// to force it to update, we set ignoreWarnings to true.
81+
// If you just want to update something simple like description, you can remove it.
82+
$operationResponse = $instanceAdminClient->updateAppProfile($appProfile, $updateMask, ['ignoreWarnings' => true]);
83+
84+
$operationResponse->pollUntilComplete();
85+
if ($operationResponse->operationSucceeded()) {
86+
$updatedAppProfile = $operationResponse->getResult();
87+
printf('App profile updated: %s' . PHP_EOL, $updatedAppProfile->getName());
88+
// doSomethingWith($updatedAppProfile)
89+
} else {
90+
$error = $operationResponse->getError();
91+
// handleError($error)
92+
}
93+
} catch (ApiException $e) {
94+
if ($e->getStatus() === 'NOT_FOUND') {
95+
printf('App Profile %s does not exist.' . PHP_EOL, $appProfileId);
96+
return;
97+
}
98+
throw $e;
99+
}
100+
}
101+
// [END bigtable_update_app_profile]
102+
103+
// The following 2 lines are only needed to run the samples
104+
require_once __DIR__ . '/../../testing/sample_helpers.php';
105+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)