Skip to content

Commit ac81cd0

Browse files
feat(secretmanager): Add samples for listing secrets and versions with filters
1 parent 57f7e42 commit ac81cd0

File tree

6 files changed

+284
-0
lines changed

6 files changed

+284
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/*
3+
* Copyright 2026 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_list_regional_secret_versions_with_filter]
29+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
30+
use Google\Cloud\SecretManager\V1\ListSecretVersionsRequest;
31+
32+
/**
33+
* List secret versions in a location using a filter.
34+
*
35+
* @param string $projectId Your Google Cloud project id (e.g. 'my-project')
36+
* @param string $locationId Your secret location (e.g. 'us-central1')
37+
* @param string $secretId Secret id (e.g. 'my-secret')
38+
* @param string $filter Filter string (see Secret Manager filtering docs)
39+
*/
40+
function list_regional_secret_versions_with_filter(string $projectId, string $locationId, string $secretId, string $filter): void
41+
{
42+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
43+
$client = new SecretManagerServiceClient($options);
44+
45+
$parent = $client->projectLocationSecretName($projectId, $locationId, $secretId);
46+
47+
$request = ListSecretVersionsRequest::build($parent)->setFilter($filter);
48+
49+
foreach ($client->listSecretVersions($request) as $version) {
50+
printf('Found secret version %s' . PHP_EOL, $version->getName());
51+
}
52+
}
53+
// [END secretmanager_list_regional_secret_versions_with_filter]
54+
55+
// The following 2 lines are only needed to execute the samples on the CLI
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/*
3+
* Copyright 2026 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_list_regional_secrets_with_filter]
29+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
30+
use Google\Cloud\SecretManager\V1\ListSecretsRequest;
31+
32+
/**
33+
* List secrets in a location using a filter.
34+
*
35+
* @param string $projectId Your Google Cloud project id (e.g. 'my-project')
36+
* @param string $locationId Your secret location (e.g. 'us-central1')
37+
* @param string $filter Filter string (see Secret Manager filtering docs)
38+
*/
39+
function list_regional_secrets_with_filter(string $projectId, string $locationId, string $filter): void
40+
{
41+
$options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"];
42+
$client = new SecretManagerServiceClient($options);
43+
44+
$parent = $client->locationName($projectId, $locationId);
45+
46+
$request = ListSecretsRequest::build($parent)->setFilter($filter);
47+
48+
foreach ($client->listSecrets($request) as $secret) {
49+
printf('Found secret %s' . PHP_EOL, $secret->getName());
50+
}
51+
}
52+
// [END secretmanager_list_regional_secrets_with_filter]
53+
54+
// The following 2 lines are only needed to execute the samples on the CLI
55+
require_once __DIR__ . '/../../testing/sample_helpers.php';
56+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/*
3+
* Copyright 2026 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_list_secret_versions_with_filter]
29+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
30+
use Google\Cloud\SecretManager\V1\ListSecretVersionsRequest;
31+
32+
/**
33+
* List secret versions for a secret using a filter.
34+
*
35+
* @param string $projectId Your Google Cloud project id (e.g. 'my-project')
36+
* @param string $secretId Your secret id (e.g. 'my-secret')
37+
* @param string $filter Filter string (see Secret Manager filtering docs)
38+
*/
39+
function list_secret_versions_with_filter(string $projectId, string $secretId, string $filter): void
40+
{
41+
$client = new SecretManagerServiceClient();
42+
43+
$parent = $client->secretName($projectId, $secretId);
44+
45+
$request = ListSecretVersionsRequest::build($parent)->setFilter($filter);
46+
47+
foreach ($client->listSecretVersions($request) as $version) {
48+
printf('Found secret version %s' . PHP_EOL, $version->getName());
49+
}
50+
}
51+
// [END secretmanager_list_secret_versions_with_filter]
52+
53+
// The following 2 lines are only needed to execute the samples on the CLI
54+
require_once __DIR__ . '/../../testing/sample_helpers.php';
55+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/*
3+
* Copyright 2026 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/*
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md
22+
*/
23+
24+
declare(strict_types=1);
25+
26+
namespace Google\Cloud\Samples\SecretManager;
27+
28+
// [START secretmanager_list_secrets_with_filter]
29+
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
30+
use Google\Cloud\SecretManager\V1\ListSecretsRequest;
31+
32+
/**
33+
* List secrets in a project using a filter.
34+
*
35+
* @param string $projectId Your Google Cloud project id (e.g. 'my-project')
36+
* @param string $filter Filter string (see Secret Manager filtering docs)
37+
*/
38+
function list_secrets_with_filter(string $projectId, string $filter): void
39+
{
40+
$client = new SecretManagerServiceClient();
41+
42+
$parent = $client->projectName($projectId);
43+
44+
$request = ListSecretsRequest::build($parent)->setFilter($filter);
45+
46+
foreach ($client->listSecrets($request) as $secret) {
47+
printf('Found secret %s' . PHP_EOL, $secret->getName());
48+
}
49+
}
50+
// [END secretmanager_list_secrets_with_filter]
51+
52+
// The following 2 lines are only needed to execute the samples on the CLI
53+
require_once __DIR__ . '/../../testing/sample_helpers.php';
54+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

secretmanager/test/regionalsecretmanagerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,23 @@ public function testIamRevokeAccess()
405405
$this->assertStringContainsString('Updated IAM policy', $output);
406406
}
407407

408+
public function testListSecretVersionsWithFilter()
409+
{
410+
$name = self::$client->parseName(self::$testSecretWithVersions->getName());
411+
412+
// Filter for enabled versions.
413+
$filter = 'state = ENABLED';
414+
415+
$output = $this->runFunctionSnippet('list_regional_secret_versions_with_filter', [
416+
$name['project'],
417+
$name['location'],
418+
$name['secret'],
419+
$filter,
420+
]);
421+
422+
$this->assertStringContainsString('Found secret version', $output);
423+
}
424+
408425
public function testListSecretVersions()
409426
{
410427
$name = self::$client->parseName(self::$testSecretWithVersions->getName());
@@ -418,6 +435,21 @@ public function testListSecretVersions()
418435
$this->assertStringContainsString('secret version', $output);
419436
}
420437

438+
public function testListSecretsWithFilter()
439+
{
440+
$name = self::$client->parseName(self::$testSecret->getName());
441+
442+
$filter = 'name:' . $name['secret'];
443+
444+
$output = $this->runFunctionSnippet('list_regional_secrets_with_filter', [
445+
$name['project'],
446+
$name['location'],
447+
$filter,
448+
]);
449+
450+
$this->assertStringContainsString('Found secret', $output);
451+
}
452+
421453
public function testListSecrets()
422454
{
423455
$name = self::$client->parseName(self::$testSecret->getName());

secretmanager/test/secretmanagerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,22 @@ public function testIamRevokeAccess()
414414
$this->assertStringContainsString('Updated IAM policy', $output);
415415
}
416416

417+
public function testListSecretVersionsWithFilter()
418+
{
419+
$name = self::$client->parseName(self::$testSecretWithVersions->getName());
420+
421+
// Filter for enabled versions.
422+
$filter = 'state = ENABLED';
423+
424+
$output = $this->runFunctionSnippet('list_secret_versions_with_filter', [
425+
$name['project'],
426+
$name['secret'],
427+
$filter,
428+
]);
429+
430+
$this->assertStringContainsString('Found secret version', $output);
431+
}
432+
417433
public function testListSecretVersions()
418434
{
419435
$name = self::$client->parseName(self::$testSecretWithVersions->getName());
@@ -426,6 +442,20 @@ public function testListSecretVersions()
426442
$this->assertStringContainsString('secret version', $output);
427443
}
428444

445+
public function testListSecretsWithFilter()
446+
{
447+
$name = self::$client->parseName(self::$testSecret->getName());
448+
449+
$filter = 'name:' . $name['secret'];
450+
451+
$output = $this->runFunctionSnippet('list_secrets_with_filter', [
452+
$name['project'],
453+
$filter,
454+
]);
455+
456+
$this->assertStringContainsString('Found secret', $output);
457+
}
458+
429459
public function testListSecrets()
430460
{
431461
$name = self::$client->parseName(self::$testSecret->getName());

0 commit comments

Comments
 (0)