-
Notifications
You must be signed in to change notification settings - Fork 1k
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
thiyaguk09
wants to merge
9
commits into
GoogleCloudPlatform:main
Choose a base branch
from
thiyaguk09:anywhere-cache-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d997da3
Feat(StorageControl): Add sample for anywhereCache
thiyaguk09 326dfd6
Merge branch 'main' into anywhere-cache-samples
thiyaguk09 ced4bef
Merge branch 'main' into anywhere-cache-samples
thiyaguk09 b0fd0f1
Docs: Update README file
thiyaguk09 e4c8403
add test, wait for LROs
bshaffer e487857
Add test cases
thiyaguk09 91efa59
Bug fix
thiyaguk09 1b103f0
Lint fix
thiyaguk09 72cc20a
Docs: Update README file
thiyaguk09 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?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. | ||
$operation = $storageControlClient->createAnywhereCache($request); | ||
|
||
printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName()); | ||
$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()); | ||
} | ||
# [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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.