use of com.google.cloud.secretmanager.v1.SecretName 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.SecretName in project java-secretmanager by googleapis.
the class DeleteSecret method deleteSecret.
// Delete an existing secret with the given name.
public void deleteSecret(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 secret name.
SecretName secretName = SecretName.of(projectId, secretId);
// Create the secret.
client.deleteSecret(secretName);
System.out.printf("Deleted secret %s\n", secretId);
}
}
use of com.google.cloud.secretmanager.v1.SecretName in project java-secretmanager by googleapis.
the class IamGrantAccess method iamGrantAccess.
// Grant a member access to a particular secret.
public void iamGrantAccess(String projectId, String secretId, String member) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
// Build the name from the version.
SecretName secretName = SecretName.of(projectId, secretId);
// Request the current IAM policy.
Policy currentPolicy = client.getIamPolicy(GetIamPolicyRequest.newBuilder().setResource(secretName.toString()).build());
// Build the new binding.
Binding binding = Binding.newBuilder().setRole("roles/secretmanager.secretAccessor").addMembers(member).build();
// Create a new IAM policy from the current policy, adding the binding.
Policy newPolicy = Policy.newBuilder().mergeFrom(currentPolicy).addBindings(binding).build();
// Save the updated IAM policy.
client.setIamPolicy(SetIamPolicyRequest.newBuilder().setResource(secretName.toString()).setPolicy(newPolicy).build());
System.out.printf("Updated IAM policy for %s\n", secretId);
}
}
use of com.google.cloud.secretmanager.v1.SecretName in project java-secretmanager by googleapis.
the class ListSecretVersions method listSecretVersions.
// List all secret versions for a secret.
public void listSecretVersions(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.
SecretName projectName = SecretName.of(projectId, secretId);
// Get all versions.
ListSecretVersionsPagedResponse pagedResponse = client.listSecretVersions(projectName);
// List all versions and their state.
pagedResponse.iterateAll().forEach(version -> {
System.out.printf("Secret version %s, %s\n", version.getName(), version.getState());
});
}
}
use of com.google.cloud.secretmanager.v1.SecretName in project java-secretmanager by googleapis.
the class QuickstartIT method afterAll.
@AfterClass
public static void afterAll() throws Exception {
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
try (SecretManagerServiceClient client = SecretManagerServiceClient.create()) {
// Delete the secret created by quickstart
SecretName name = SecretName.of(PROJECT_ID, SECRET_ID);
DeleteSecretRequest deleteRequest = DeleteSecretRequest.newBuilder().setName(name.toString()).build();
client.deleteSecret(deleteRequest);
}
}
Aggregations