Search in sources :

Example 36 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class NativeImageSecretManagerSample method createSecret.

static void createSecret(SecretManagerServiceClient client, String projectId, String secretId) {
    Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder().build()).build()).build();
    ProjectName projectName = ProjectName.of(projectId);
    Secret createdSecret = client.createSecret(projectName, secretId, secret);
    System.out.println("Created secret: " + createdSecret.getName());
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) ProjectName(com.google.cloud.secretmanager.v1.ProjectName)

Example 37 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class NativeImageSecretManagerSample method addSecretVersion.

static SecretVersion addSecretVersion(SecretManagerServiceClient client, String projectId, String secretId) {
    SecretName secretName = SecretName.of(projectId, secretId);
    SecretPayload payload = SecretPayload.newBuilder().setData(ByteString.copyFromUtf8("Hello World")).build();
    SecretVersion version = client.addSecretVersion(secretName, payload);
    System.out.println("Added Secret Version: " + version.getName());
    return version;
}
Also used : SecretName(com.google.cloud.secretmanager.v1.SecretName) SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretPayload(com.google.cloud.secretmanager.v1.SecretPayload)

Example 38 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class NativeImageSecretManagerSample method main.

/**
 * Runs the Secret Manager sample application.
 */
public static void main(String[] args) throws IOException {
    String projectId = ServiceOptions.getDefaultProjectId();
    try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
        if (!hasSecret(client, projectId, NATIVE_TEST_SECRET_ID)) {
            createSecret(client, projectId, NATIVE_TEST_SECRET_ID);
        } else {
            System.out.println("Project already has secret: " + NATIVE_TEST_SECRET_ID);
        }
        SecretVersion version = addSecretVersion(client, projectId, NATIVE_TEST_SECRET_ID);
        printSecretVersion(client, version);
    }
}
Also used : SecretVersion(com.google.cloud.secretmanager.v1.SecretVersion) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient) ByteString(com.google.protobuf.ByteString)

Example 39 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class NativeImageSecretManagerSample method hasSecret.

static boolean hasSecret(SecretManagerServiceClient client, String projectId, String secretId) {
    ProjectName projectName = ProjectName.of(projectId);
    ListSecretsPagedResponse pagedResponse = client.listSecrets(projectName);
    for (Secret secret : pagedResponse.iterateAll()) {
        String otherSecretId = extractSecretId(secret);
        if (secretId.equals(otherSecretId)) {
            return true;
        }
    }
    return false;
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) ProjectName(com.google.cloud.secretmanager.v1.ProjectName) ListSecretsPagedResponse(com.google.cloud.secretmanager.v1.SecretManagerServiceClient.ListSecretsPagedResponse) ByteString(com.google.protobuf.ByteString)

Example 40 with Secret

use of com.google.cloud.secretmanager.v1.Secret in project java-secretmanager by googleapis.

the class CreateSecret method createSecret.

// Add a new version to the existing secret.
public void createSecret(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 parent name from the project.
        ProjectName projectName = ProjectName.of(projectId);
        // Build the secret to create.
        Secret secret = Secret.newBuilder().setReplication(Replication.newBuilder().setAutomatic(Replication.Automatic.newBuilder().build()).build()).build();
        // Create the secret.
        Secret createdSecret = client.createSecret(projectName, secretId, secret);
        System.out.printf("Created secret %s\n", createdSecret.getName());
    }
}
Also used : Secret(com.google.cloud.secretmanager.v1.Secret) ProjectName(com.google.cloud.secretmanager.v1.ProjectName) SecretManagerServiceClient(com.google.cloud.secretmanager.v1.SecretManagerServiceClient)

Aggregations

SecretManagerServiceClient (com.google.cloud.secretmanager.v1.SecretManagerServiceClient)51 SecretName (com.google.cloud.secretmanager.v1.SecretName)44 Test (org.junit.Test)42 SecretVersionName (com.google.cloud.secretmanager.v1.SecretVersionName)38 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 AccessSecretVersionResponse (com.google.cloud.secretmanager.v1.AccessSecretVersionResponse)9 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 Test (org.junit.jupiter.api.Test)5 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