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.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

namespace cbt = ::google::cloud::bigtable;
namespace cbta = ::google::cloud::bigtable_admin;
using ::google::cloud::future;
using ::google::cloud::StatusOr;
[](cbta::BigtableInstanceAdminClient instance_admin,
   std::string const& project_id, std::string const& instance_id,
   std::string const& profile_id, std::string const& description) {
  std::string profile_name =
      cbt::AppProfileName(project_id, instance_id, profile_id);

  google::bigtable::admin::v2::AppProfile profile;
  profile.set_name(profile_name);
  profile.set_description(description);

  google::protobuf::FieldMask mask;
  mask.add_paths("description");

  future<StatusOr<google::bigtable::admin::v2::AppProfile>> profile_future =
      instance_admin.UpdateAppProfile(std::move(profile), std::move(mask));
  auto modified_profile = profile_future.get();
  if (!modified_profile) throw std::move(modified_profile).status();
  std::cout << "Updated AppProfile: " << modified_profile->DebugString()
            << "\n";
}
namespace cbt = ::google::cloud::bigtable;
namespace cbta = ::google::cloud::bigtable_admin;
using ::google::cloud::future;
using ::google::cloud::StatusOr;
[](cbta::BigtableInstanceAdminClient instance_admin,
   std::string const& project_id, std::string const& instance_id,
   std::string const& profile_id) {
  std::string profile_name =
      cbt::AppProfileName(project_id, instance_id, profile_id);

  google::bigtable::admin::v2::UpdateAppProfileRequest req;
  req.mutable_app_profile()->set_name(profile_name);
  req.mutable_app_profile()->mutable_multi_cluster_routing_use_any()->Clear();
  req.mutable_update_mask()->add_paths("multi_cluster_routing_use_any");
  req.set_ignore_warnings(true);

  future<StatusOr<google::bigtable::admin::v2::AppProfile>> profile_future =
      instance_admin.UpdateAppProfile(std::move(req));
  auto modified_profile = profile_future.get();
  if (!modified_profile) throw std::move(modified_profile).status();
  std::cout << "Updated AppProfile: " << modified_profile->DebugString()
            << "\n";
}

PHP

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

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

use Google\ApiCore\ApiException;
use Google\Cloud\Bigtable\Admin\V2\AppProfile;
use Google\Cloud\Bigtable\Admin\V2\AppProfile\SingleClusterRouting;
use Google\Cloud\Bigtable\Admin\V2\Client\BigtableInstanceAdminClient;
use Google\Cloud\Bigtable\Admin\V2\UpdateAppProfileRequest;
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.
        $updateAppProfileRequest = (new UpdateAppProfileRequest())
            ->setAppProfile($appProfile)
            ->setUpdateMask($updateMask)
            ->setIgnoreWarnings(true);
        $operationResponse = $instanceAdminClient->updateAppProfile($updateAppProfileRequest);

        $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.