use of com.google.cloud.secretmanager.v1.SecretManagerServiceClient 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.SecretManagerServiceClient 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.SecretManagerServiceClient 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.SecretManagerServiceClient 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.SecretManagerServiceClient in project java-secretmanager by googleapis.
the class NativeImageSecretManagerSampleIT method afterAll.
@AfterClass
public static void afterAll() throws Exception {
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
// Delete the secret created by quickstart
SecretName name = SecretName.of(PROJECT_ID, NATIVE_TEST_SECRET_ID);
DeleteSecretRequest deleteRequest = DeleteSecretRequest.newBuilder().setName(name.toString()).build();
client.deleteSecret(deleteRequest);
}
}
Aggregations