Update an AppProfile

Update an AppProfile.

Code sample

C++

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

namespace cbt = ::google::cloud::bigtable;
using ::google::cloud::future;
using ::google::cloud::StatusOr;
[](cbt::InstanceAdmin instance_admin, std::string const& instance_id,
   std::string const& profile_id, std::string const& description) {
  future<StatusOr<google::bigtable::admin::v2::AppProfile>> profile_future =
      instance_admin.UpdateAppProfile(
          instance_id, profile_id,
          cbt::AppProfileUpdateConfig().set_description(description));
  auto profile = profile_future.get();
  if (!profile) throw std::runtime_error(profile.status().message());
  std::cout << "Updated AppProfile: " << profile->DebugString() << "\n";
}
namespace cbt = ::google::cloud::bigtable;
using ::google::cloud::future;
using ::google::cloud::StatusOr;
[](cbt::InstanceAdmin instance_admin, std::string const& instance_id,
   std::string const& profile_id) {
  future<StatusOr<google::bigtable::admin::v2::AppProfile>> profile_future =
      instance_admin.UpdateAppProfile(instance_id, profile_id,
                                      cbt::AppProfileUpdateConfig()
                                          .set_multi_cluster_use_any()
                                          .set_ignore_warnings(true));
  auto profile = profile_future.get();
  if (!profile) throw std::runtime_error(profile.status().message());
  std::cout << "Updated AppProfile: " << profile->DebugString() << "\n";
}

PHP

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

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;
use Google\Protobuf\FieldMask;

/**
 * Update 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 new 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 update
 */
function update_app_profile(
    string $projectId,
    string $instanceId,
    string $clusterId,
    string $appProfileId
): void {
    $instanceAdminClient = new BigtableInstanceAdminClient();
    $appProfileName = $instanceAdminClient->appProfileName($projectId, $instanceId, $appProfileId);

    $appProfile = new AppProfile([
        'name' => $appProfileName,
        'description' => 'The updated description',
    ]);

    // 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' => true
    ]);

    // set the newly created routing policy to our app profile
    $appProfile->setSingleClusterRouting($routingPolicy);

    // or we could also create a multi cluster routing policy like so:
    // $routingPolicy = new \Google\Cloud\Bigtable\Admin\V2\AppProfile\MultiClusterRoutingUseAny();
    // $appProfile->setMultiClusterRoutingUseAny($routingPolicy);

    // returns a string identifier depending on SingleClusterRouting or MultiClusterRoutingUseAny
    $routingPolicyStr = $appProfile->getRoutingPolicy();

    $updateMask = new FieldMask([
        'paths' => ['description', $routingPolicyStr]
    ]);

    printf('Updating the AppProfile %s' . PHP_EOL, $appProfileId);

    try {
        // Bigtable warns you while updating the routing policy, or when toggling the allow_transactional_writes
        // to force it to update, we set ignoreWarnings to true.
        // If you just want to update something simple like description, you can remove it.
        $operationResponse = $instanceAdminClient->updateAppProfile($appProfile, $updateMask, ['ignoreWarnings' => true]);

        $operationResponse->pollUntilComplete();
        if ($operationResponse->operationSucceeded()) {
            $updatedAppProfile = $operationResponse->getResult();
            printf('App profile updated: %s' . PHP_EOL, $updatedAppProfile->getName());
        // doSomethingWith($updatedAppProfile)
        } else {
            $error = $operationResponse->getError();
            // handleError($error)
        }
    } catch (ApiException $e) {
        if ($e->getStatus() === 'NOT_FOUND') {
            printf('App Profile %s does not exist.' . PHP_EOL, $appProfileId);
            return;
        }
        throw $e;
    }
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.