Skip to content

Commit 6e94946

Browse files
gwhitehawkMiroslava Sotakova
andauthored
Added SecretManager filtered-listing samples (GoogleCloudPlatform#5852)
Co-authored-by: Miroslava Sotakova <mirka@google.com>
1 parent df82348 commit 6e94946

4 files changed

Lines changed: 153 additions & 2 deletions

File tree

secretmanager/src/main/java/secretmanager/ListSecretVersions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public static void listSecretVersions(String projectId, String secretId) throws
3838
// the "close" method on the client to safely clean up any remaining background resources.
3939
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
4040
// Build the parent name.
41-
SecretName projectName = SecretName.of(projectId, secretId);
41+
SecretName secretName = SecretName.of(projectId, secretId);
4242

4343
// Get all versions.
44-
ListSecretVersionsPagedResponse pagedResponse = client.listSecretVersions(projectName);
44+
ListSecretVersionsPagedResponse pagedResponse = client.listSecretVersions(secretName);
4545

4646
// List all versions and their state.
4747
pagedResponse
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_list_secret_versions_with_filter]
20+
import com.google.cloud.secretmanager.v1.ListSecretVersionsRequest;
21+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
22+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse;
23+
import com.google.cloud.secretmanager.v1.SecretName;
24+
import java.io.IOException;
25+
26+
public class ListSecretVersionsWithFilter {
27+
28+
public static void listSecretVersions() throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "your-project-id";
31+
String secretId = "your-secret-id";
32+
// Follow https://cloud.google.com/secret-manager/docs/filtering
33+
// for filter syntax and examples.
34+
String filter = "create_time>2021-01-01T00:00:00Z";
35+
listSecretVersions(projectId, secretId, filter);
36+
}
37+
38+
// List all secret versions for a secret.
39+
public static void listSecretVersions(String projectId, String secretId, String filter)
40+
throws IOException {
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests. After completing all of your requests, call
43+
// the "close" method on the client to safely clean up any remaining background resources.
44+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
45+
// Build the parent name.
46+
SecretName secretName = SecretName.of(projectId, secretId);
47+
48+
// Get filtered versions.
49+
ListSecretVersionsRequest request =
50+
ListSecretVersionsRequest.newBuilder()
51+
.setParent(secretName.toString())
52+
.setFilter(filter)
53+
.build();
54+
55+
ListSecretVersionsPagedResponse pagedResponse = client.listSecretVersions(request);
56+
57+
// List all versions and their state.
58+
pagedResponse
59+
.iterateAll()
60+
.forEach(
61+
version -> {
62+
System.out.printf("Secret version %s, %s\n", version.getName(), version.getState());
63+
});
64+
}
65+
}
66+
}
67+
// [END secretmanager_list_secret_versions_with_filter]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package secretmanager;
18+
19+
// [START secretmanager_list_secrets_with_filter]
20+
import com.google.cloud.secretmanager.v1.ListSecretsRequest;
21+
import com.google.cloud.secretmanager.v1.ProjectName;
22+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
23+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse;
24+
import java.io.IOException;
25+
26+
public class ListSecretsWithFilter {
27+
28+
public static void listSecrets() throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "your-project-id";
31+
// Follow https://cloud.google.com/secret-manager/docs/filtering
32+
// for filter syntax and examples.
33+
String filter = "name:your-secret-substring AND expire_time<2022-01-01T00:00:00Z";
34+
listSecrets(projectId, filter);
35+
}
36+
37+
// List all secrets for a project
38+
public static void listSecrets(String projectId, String filter) throws IOException {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
42+
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
43+
// Build the parent name.
44+
ProjectName projectName = ProjectName.of(projectId);
45+
46+
// Get filtered secrets.
47+
ListSecretsRequest request =
48+
ListSecretsRequest.newBuilder()
49+
.setParent(projectName.toString())
50+
.setFilter(filter)
51+
.build();
52+
53+
ListSecretsPagedResponse pagedResponse = client.listSecrets(request);
54+
55+
// List all secrets.
56+
pagedResponse
57+
.iterateAll()
58+
.forEach(
59+
secret -> {
60+
System.out.printf("Secret %s\n", secret.getName());
61+
});
62+
}
63+
}
64+
}
65+
// [END secretmanager_list_secrets_with_filter]

secretmanager/src/test/java/secretmanager/SnippetsIT.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ public void testListSecretVersions() throws IOException {
324324
assertThat(stdOut.toString()).contains("Secret version");
325325
}
326326

327+
@Test
328+
public void testListSecretVersionsWithFilter() throws IOException {
329+
SecretName name = SecretName.parse(TEST_SECRET_WITH_VERSIONS.getName());
330+
ListSecretVersionsWithFilter.listSecretVersions(
331+
name.getProject(), name.getSecret(), "name:1");
332+
333+
assertThat(stdOut.toString()).contains("Secret version");
334+
}
335+
327336
@Test
328337
public void testListSecrets() throws IOException {
329338
SecretName name = SecretName.parse(TEST_SECRET.getName());
@@ -333,6 +342,16 @@ public void testListSecrets() throws IOException {
333342
assertThat(stdOut.toString()).contains(name.getSecret());
334343
}
335344

345+
@Test
346+
public void testListSecretsWithFilter() throws IOException {
347+
SecretName name = SecretName.parse(TEST_SECRET.getName());
348+
ListSecretsWithFilter.listSecrets(
349+
name.getProject(), String.format("name:%s", name.getSecret()));
350+
351+
assertThat(stdOut.toString()).contains("Secret projects/");
352+
assertThat(stdOut.toString()).contains(name.getSecret());
353+
}
354+
336355
@Test
337356
public void testUpdateSecret() throws IOException {
338357
SecretName name = SecretName.parse(TEST_SECRET.getName());

0 commit comments

Comments
 (0)