Skip to content

samples(StorageControl): Add sample for anywhereCache #2143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions storagecontrol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ to [browse the source][google-cloud-php-source] and [report issues][google-clou
## Licensing

* See [LICENSE](../../LICENSE)

## Note

Tests for Anywhere cache Sample are not included due to the long operation times typical for cache operations.
2 changes: 1 addition & 1 deletion storagecontrol/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"google/cloud-storage-control": "1.3.0"
},
"require-dev": {
"google/cloud-storage": "^1.41.3"
"google/cloud-storage": "^1.48.1"
}
}
67 changes: 67 additions & 0 deletions storagecontrol/src/create_anywhere_cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
*/

namespace Google\Cloud\Samples\StorageControl;

# [START storage_control_create_anywhere_cache]
use Google\Cloud\Storage\Control\V2\AnywhereCache;
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\CreateAnywhereCacheRequest;

/**
* Creates an Anywhere Cache instance.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $zone The zone in which the cache instance is running.
* (e.g. 'us-east1-b')
*/
function create_anywhere_cache(string $bucketName, string $zone): void
{
$storageControlClient = new StorageControlClient();

// Set project to "_" to signify global bucket
$formattedName = $storageControlClient->bucketName('_', $bucketName);

$anywhereCache = new AnywhereCache([
'zone' => $zone,
]);

$request = new CreateAnywhereCacheRequest([
'parent' => $formattedName,
'anywhere_cache' => $anywhereCache,
]);

// Start a create operation and block until it completes. Real applications
// may want to setup a callback, wait on a coroutine, or poll until it
// completes.
$response = $storageControlClient->createAnywhereCache($request);

printf('Created anywhere cache: %s', $response->getName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these responses are operations (because they are longrunning), so this is incorrect.

I would suggest either calling the $response variable an $operation, or waiting for it to complete like this:

$operation->pollUntilComplete();
$cache = $operation->getResult();

printf('Created anywhere cache: %s', $cache->getName());

Copy link
Contributor Author

@thiyaguk09 thiyaguk09 Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retrieve the cache name using the operation pollUntilComplete method.

   $operation = $storageControlClient->createAnywhereCache($request);
   $operation->pollUntilComplete([
       'totalPollTimeoutMillis' => 5400000,
       'initialPollDelayMillis' => 1000, // Start with 1 second delay
       'pollDelayMultiplier' => 2,      // Double delay each time
       'maxPollDelayMillis' => 60000,   // Max 60 seconds delay between polls
   ]);

   /** @var AnywhereCache $anywhereCacheResult */
   $anywhereCacheResult = $operation->getResult();
   printf('Created anywhere cache: %s' . PHP_EOL, $anywhereCacheResult->getName());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess would be the only required configuration of the above is totalPollTimeoutMillis, but the others may be nice to have there as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bshaffer I hope the test cases are working well. Is it okay to approve them, or do we need to add more?

}
# [END storage_control_create_anywhere_cache]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
58 changes: 58 additions & 0 deletions storagecontrol/src/disable_anywhere_cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
*/

namespace Google\Cloud\Samples\StorageControl;

# [START storage_control_disable_anywhere_cache]
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\DisableAnywhereCacheRequest;

/**
* Disables an Anywhere Cache instance.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $anywhereCacheId Uniquely identifies the cache.
* (e.g. 'us-east1-b')
*/
function disable_anywhere_cache(string $bucketName, string $anywhereCacheId): void
{
$storageControlClient = new StorageControlClient();

// Set project to "_" to signify global bucket
$formattedName = $storageControlClient->anywhereCacheName('_', $bucketName, $anywhereCacheId);

$request = new DisableAnywhereCacheRequest([
'name' => $formattedName
]);

$response = $storageControlClient->disableAnywhereCache($request);

printf('Disabled anywhere cache: %s', $response->getName());
}
# [END storage_control_disable_anywhere_cache]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
58 changes: 58 additions & 0 deletions storagecontrol/src/get_anywhere_cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
*/

namespace Google\Cloud\Samples\StorageControl;

# [START storage_control_get_anywhere_cache]
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\GetAnywhereCacheRequest;

/**
* Gets an Anywhere Cache instance.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $anywhereCacheId Uniquely identifies the cache.
* (e.g. 'us-east1-b')
*/
function get_anywhere_cache(string $bucketName, string $anywhereCacheId): void
{
$storageControlClient = new StorageControlClient();

// Set project to "_" to signify global bucket
$formattedName = $storageControlClient->anywhereCacheName('_', $bucketName, $anywhereCacheId);

$request = new GetAnywhereCacheRequest([
'name' => $formattedName,
]);

$response = $storageControlClient->getAnywhereCache($request);

printf('Got anywhere cache: %s', $response->getName());
}
# [END storage_control_get_anywhere_cache]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
58 changes: 58 additions & 0 deletions storagecontrol/src/list_anywhere_caches.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
*/

namespace Google\Cloud\Samples\StorageControl;

# [START storage_control_list_anywhere_caches]
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\ListAnywhereCachesRequest;

/**
* Lists Anywhere Cache instances for a given bucket.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
*/
function list_anywhere_caches(string $bucketName): void
{
$storageControlClient = new StorageControlClient();

// Set project to "_" to signify global bucket
$formattedName = $storageControlClient->bucketName('_', $bucketName);

$request = new ListAnywhereCachesRequest([
'parent' => $formattedName,
]);

$response = $storageControlClient->listAnywhereCaches($request);

foreach ($response as $anywhereCache) {
printf('Anywhere cache name: %s' . PHP_EOL, $anywhereCache->getName());
}
}
# [END storage_control_list_anywhere_caches]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
58 changes: 58 additions & 0 deletions storagecontrol/src/pause_anywhere_cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
*/

namespace Google\Cloud\Samples\StorageControl;

# [START storage_control_pause_anywhere_cache]
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\PauseAnywhereCacheRequest;

/**
* Pauses an Anywhere Cache instance.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $anywhereCacheId Uniquely identifies the cache.
* (e.g. 'us-east1-b')
*/
function pause_anywhere_cache(string $bucketName, string $anywhereCacheId): void
{
$storageControlClient = new StorageControlClient();

// Set project to "_" to signify global bucket
$formattedName = $storageControlClient->anywhereCacheName('_', $bucketName, $anywhereCacheId);

$request = new PauseAnywhereCacheRequest([
'name' => $formattedName,
]);

$response = $storageControlClient->pauseAnywhereCache($request);

printf('Paused anywhere cache: %s', $response->getName());
}
# [END storage_control_pause_anywhere_cache]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
58 changes: 58 additions & 0 deletions storagecontrol/src/resume_anywhere_cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storagecontrol/README.md
*/

namespace Google\Cloud\Samples\StorageControl;

# [START storage_control_resume_anywhere_cache]
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\ResumeAnywhereCacheRequest;

/**
* Resumes a disabled or paused Anywhere Cache instance.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $anywhereCacheId Uniquely identifies the cache.
* (e.g. 'us-east1-b')
*/
function resume_anywhere_cache(string $bucketName, string $anywhereCacheId): void
{
$storageControlClient = new StorageControlClient();

// Set project to "_" to signify global bucket
$formattedName = $storageControlClient->anywhereCacheName('_', $bucketName, $anywhereCacheId);

$request = new ResumeAnywhereCacheRequest([
'name' => $formattedName
]);

$response = $storageControlClient->resumeAnywhereCache($request);

printf('Resumed anywhere cache: %s', $response->getName());
}
# [END storage_control_resume_anywhere_cache]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Loading