Search in sources :

Example 66 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-docs-samples by GoogleCloudPlatform.

the class ListSecretVersionsWithFilter method listSecretVersions.

// List all secret versions for a secret.
public static void listSecretVersions(String projectId, String secretId, String filter) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the parent name.
        SecretName secretName = SecretName.of(projectId, secretId);
        // Get filtered versions.
        ListSecretVersionsRequest request = ListSecretVersionsRequest.newBuilder().setParent(secretName.toString()).setFilter(filter).build();
        ListSecretVersionsPagedResponse pagedResponse = client.listSecretVersions(request);
        // List all versions and their state.
        pagedResponse.iterateAll().forEach(version -> {
            System.out.printf("Secret version %s, %s\n", version.getName(), version.getState());
        });
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) ListSecretVersionsPagedResponse(com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) ListSecretVersionsRequest(com.google.cloud.secretmanager.v1.ListSecretVersionsRequest)

Example 67 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-docs-samples by GoogleCloudPlatform.

the class ListSecretsWithFilter method listSecrets.

// List all secrets for a project
public static void listSecrets(String projectId, String filter) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the parent name.
        ProjectName projectName = ProjectName.of(projectId);
        // Get filtered secrets.
        ListSecretsRequest request = ListSecretsRequest.newBuilder().setParent(projectName.toString()).setFilter(filter).build();
        ListSecretsPagedResponse pagedResponse = client.listSecrets(request);
        // List all secrets.
        pagedResponse.iterateAll().forEach(secret -> {
            System.out.printf("Secret %s\n", secret.getName());
        });
    }
}
Also used : ProjectName(com.google.cloud.secretmanager.v1.ProjectName) ListSecretsPagedResponse(com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse) ListSecretsRequest(com.google.cloud.secretmanager.v1.ListSecretsRequest) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Example 68 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-docs-samples by GoogleCloudPlatform.

the class UpdateSecret method updateSecret.

// Update an existing secret.
public static void updateSecret(String projectId, String secretId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the name.
        SecretName secretName = SecretName.of(projectId, secretId);
        // Build the updated secret.
        Secret secret = Secret.newBuilder().setName(secretName.toString()).putLabels("secretmanager", "rocks").build();
        // Build the field mask.
        FieldMask fieldMask = FieldMaskUtil.fromString("labels");
        // Update the secret.
        Secret updatedSecret = client.updateSecret(secret, fieldMask);
        System.out.printf("Updated secret %s\n", updatedSecret.getName());
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) Secret(com.google.cloud.secretmanager.v1.Secret) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) FieldMask(com.google.protobuf.FieldMask)

Example 69 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-docs-samples by GoogleCloudPlatform.

the class UpdateSecretWithEtag method updateSecret.

// Update an existing secret.
public static void updateSecret(String projectId, String secretId, String etag) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        // Build the name.
        SecretName secretName = SecretName.of(projectId, secretId);
        // Build the updated secret.
        Secret secret = Secret.newBuilder().setName(secretName.toString()).setEtag(etag).putLabels("secretmanager", "rocks").build();
        // Build the field mask.
        FieldMask fieldMask = FieldMaskUtil.fromString("labels");
        // Update the secret.
        Secret updatedSecret = client.updateSecret(secret, fieldMask);
        System.out.printf("Updated secret %s\n", updatedSecret.getName());
    }
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) Secret(com.google.cloud.secretmanager.v1.Secret) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) FieldMask(com.google.protobuf.FieldMask)

Example 70 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-docs-samples by GoogleCloudPlatform.

the class SnippetsIT method testListSecrets.

@Test
public void testListSecrets() throws IOException {
    SecretName name = SecretName.parse(TEST_SECRET.getName());
    ListSecrets.listSecrets(name.getProject());
    assertThat(stdOut.toString()).contains("Secret projects/");
    assertThat(stdOut.toString()).contains(name.getSecret());
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) Test(org.junit.Test)

Aggregations

SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)52 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)45 SecretName (com.google.cloud.secretmanager.v1.SecretName)44 Test (org.junit.Test)42 SecretVersion (com.google.cloud.secretmanager.v1.SecretVersion)22 Secret (com.google.cloud.secretmanager.v1.Secret)21 ProjectName (com.google.cloud.secretmanager.v1.ProjectName)16 ByteString (com.google.protobuf.ByteString)13 Test (org.junit.jupiter.api.Test)13 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)10 ListSecretsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse)7 CreateSecretRequest (com.google.cloud.secretmanager.v1.CreateSecretRequest)6 SecretPayload (com.google.cloud.secretmanager.v1.SecretPayload)6 AddSecretVersionRequest (com.google.cloud.secretmanager.v1.AddSecretVersionRequest)4 DeleteSecretRequest (com.google.cloud.secretmanager.v1.DeleteSecretRequest)4 ListSecretVersionsPagedResponse (com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretVersionsPagedResponse)4 Binding (com.google.iam.v1.Binding)4 Policy (com.google.iam.v1.Policy)4 ListSecretsRequest (com.google.cloud.secretmanager.v1.ListSecretsRequest)3 Secret (com.google.cloud.secretmanager.v1beta1.Secret)3