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());
}
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;
}
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);
}
}
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;
}
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());
}
}
Aggregations