use Google\Cloud\Bigtable\Admin\V2\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\AppProfile;
use Google\Cloud\Bigtable\Admin\V2\AppProfile\SingleClusterRouting;
use Google\ApiCore\ApiException;
/**
* Create an App Profile
*
* @param string $projectId The Google Cloud project ID
* @param string $instanceId The ID of the Bigtable instance
* @param string $clusterId The ID of the cluster where the new App Profile will route it's requests(in case of single cluster routing)
* @param string $appProfileId The ID of the App Profile to create
*/
function create_app_profile(
string $projectId,
string $instanceId,
string $clusterId,
string $appProfileId
): void {
$instanceAdminClient = new BigtableInstanceAdminClient();
$instanceName = $instanceAdminClient->instanceName($projectId, $instanceId);
$appProfile = new AppProfile([
'name' => $appProfileId,
'description' => 'Description for this newly created AppProfile'
]);
// create a new routing policy
// allow_transactional_writes refers to Single-Row-Transactions(https://cloud.google.com/bigtable/docs/app-profiles#single-row-transactions)
$routingPolicy = new SingleClusterRouting([
'cluster_id' => $clusterId,
'allow_transactional_writes' => false
]);
// set the newly created routing policy to our app profile
$appProfile->setSingleClusterRouting($routingPolicy);
// we could also create a multi cluster routing policy like so:
// $routingPolicy = new \Google\Cloud\Bigtable\Admin\V2\AppProfile\MultiClusterRoutingUseAny();
// $appProfile->setMultiClusterRoutingUseAny($routingPolicy);
printf('Creating a new AppProfile %s' . PHP_EOL, $appProfileId);
try {
$newAppProfile = $instanceAdminClient->createAppProfile($instanceName, $appProfileId, $appProfile);
} catch (ApiException $e) {
if ($e->getStatus() === 'ALREADY_EXISTS') {
printf('AppProfile %s already exists.', $appProfileId);
return;
}
throw $e;
}
printf('AppProfile created: %s', $newAppProfile->getName());
}