Skip to content

Commit 66d8aa6

Browse files
gwhitehawkMiroslava Sotakovaparthea
authored andcommitted
docs(samples): Add filtered listing samples (#209)
* docs(samples): Add filtered listing samples * remove unused import * remove unused import * ci: opt in to use multiple projects Co-authored-by: Miroslava Sotakova <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent eca0873 commit 66d8aa6

File tree

4 files changed

+168
-3
lines changed

4 files changed

+168
-3
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 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+
"""
16+
command line application and sample code for listing secret versions of a
17+
secret.
18+
"""
19+
20+
21+
# [START secretmanager_list_secret_versions_with_filter]
22+
def list_secret_versions_with_filter(project_id, secret_id, filter_str="state:ENABLED"):
23+
"""
24+
List all secret versions in the given secret and their metadata.
25+
26+
Args:
27+
project_id: Parent project id
28+
secret_id: Parent secret id
29+
filter_str: Secret version filter, constructing according to
30+
https://cloud.google.com/secret-manager/docs/filtering
31+
"""
32+
33+
# Import the Secret Manager client library.
34+
from google.cloud import secretmanager
35+
36+
# Create the Secret Manager client.
37+
client = secretmanager.SecretManagerServiceClient()
38+
39+
# Build the resource name of the parent secret.
40+
parent = client.secret_path(project_id, secret_id)
41+
42+
# List all secret versions.
43+
for version in client.list_secret_versions(request={"parent": parent, "filter": filter_str}):
44+
print("Found secret version: {}".format(version.name))
45+
46+
47+
# [END secretmanager_list_secret_versions_with_filter]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 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+
"""
16+
command line application and sample code for listing secrets in a project.
17+
"""
18+
19+
20+
# [START secretmanager_list_secrets_with_filter]
21+
def list_secrets_with_filter(project_id, filter_str):
22+
"""
23+
List all secrets in the given project.
24+
25+
Args:
26+
project_id: Parent project id
27+
filter_str: Secret filter, constructing according to
28+
https://cloud.google.com/secret-manager/docs/filtering
29+
"""
30+
31+
# Import the Secret Manager client library.
32+
from google.cloud import secretmanager
33+
34+
# Create the Secret Manager client.
35+
client = secretmanager.SecretManagerServiceClient()
36+
37+
# Build the resource name of the parent project.
38+
parent = f"projects/{project_id}"
39+
40+
# List all secrets.
41+
for secret in client.list_secrets(request={"parent": parent, "filter": filter_str}):
42+
print("Found secret: {}".format(secret.name))
43+
44+
45+
# [END secretmanager_list_secrets_with_filter]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Default TEST_CONFIG_OVERRIDE for python repos.
16+
17+
# You can copy this file into your directory, then it will be inported from
18+
# the noxfile.py.
19+
20+
# The source of truth:
21+
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
22+
23+
TEST_CONFIG_OVERRIDE = {
24+
# You can opt out from the test for specific Python versions.
25+
"ignored_versions": ["2.7"],
26+
# Old samples are opted out of enforcing Python type hints
27+
# All new samples should feature them
28+
"enforce_type_hints": False,
29+
# An envvar key for determining the project id to use. Change it
30+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
31+
# build specific Cloud project. You can also use your own string
32+
# to use your own Cloud project.
33+
# "gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
34+
"gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT",
35+
# A dictionary you want to inject into your test. Don't put any
36+
# secrets here. These values will override predefined values.
37+
"envs": {},
38+
}

secretmanager/snippets/snippets_test.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
from iam_grant_access import iam_grant_access
3737
from iam_revoke_access import iam_revoke_access
3838
from list_secret_versions import list_secret_versions
39+
from list_secret_versions_with_filter import list_secret_versions_with_filter
3940
from list_secrets import list_secrets
41+
from list_secrets_with_filter import list_secrets_with_filter
4042
from quickstart import quickstart
4143
from update_secret import update_secret
4244
from update_secret_with_etag import update_secret_with_etag
@@ -219,21 +221,54 @@ def test_iam_revoke_access(client, secret, iam_user):
219221

220222
def test_list_secret_versions(capsys, secret_version, another_secret_version):
221223
project_id, secret_id, version_id, _ = secret_version
224+
version_1 = get_secret_version(project_id, secret_id, version_id)
222225
_, _, another_version_id, _ = another_secret_version
226+
version_2 = get_secret_version(project_id, secret_id, another_version_id)
223227
list_secret_versions(project_id, secret_id)
224228

225229
out, _ = capsys.readouterr()
226230
assert secret_id in out
227-
assert version_id in out
228-
assert another_version_id in out
231+
assert "Found secret version: {}".format(version_1.name) in out
232+
assert "Found secret version: {}".format(version_2.name) in out
233+
234+
235+
def test_list_secret_versions_with_filter(capsys, secret_version, another_secret_version):
236+
project_id, secret_id, version_id, _ = secret_version
237+
enabled = get_secret_version(project_id, secret_id, version_id)
238+
_, _, another_version_id, _ = another_secret_version
239+
disabled = disable_secret_version(project_id, secret_id, another_version_id)
240+
assert disabled.state == secretmanager.SecretVersion.State.DISABLED
241+
list_secret_versions_with_filter(project_id, secret_id, "state:ENABLED")
242+
243+
out, _ = capsys.readouterr()
244+
assert secret_id in out
245+
assert "Found secret version: {}".format(enabled.name) in out
246+
assert "Found secret version: {}".format(disabled.name) not in out
229247

230248

231249
def test_list_secrets(capsys, secret):
232250
project_id, secret_id, _ = secret
251+
secret = get_secret(project_id, secret_id)
233252
list_secrets(project_id)
234253

235254
out, _ = capsys.readouterr()
236-
assert secret_id in out
255+
assert "Found secret: {}".format(secret.name) in out
256+
257+
258+
def test_list_secrets_with_filter(capsys, secret):
259+
project_id, secret_id, _ = secret
260+
unlabeled = get_secret(project_id, secret_id)
261+
list_secrets_with_filter(project_id, "labels.secretmanager:rocks")
262+
263+
out, _ = capsys.readouterr()
264+
assert "Found secret: {}".format(unlabeled.name) not in out
265+
266+
labeled = update_secret(project_id, secret_id)
267+
assert labeled.labels["secretmanager"] == "rocks"
268+
list_secrets_with_filter(project_id, "labels.secretmanager:rocks")
269+
270+
out, _ = capsys.readouterr()
271+
assert "Found secret: {}".format(labeled.name) in out
237272

238273

239274
def test_update_secret(secret):

0 commit comments

Comments
 (0)