use of com.google.cloud.security.privateca.v1.UpdateCertificateAuthorityRequest in project java-security-private-ca by googleapis.
the class UpdateCertificateAuthority method updateCaLabel.
// Updates the labels in a certificate authority.
public static void updateCaLabel(String project, String location, String pool_Id, String certificateAuthorityName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
/* Initialize client that will be used to send requests. This client only needs to be created
once, and can be reused for multiple requests. After completing all of your requests, call
the `certificateAuthorityServiceClient.close()` method on the client to safely
clean up any remaining background resources. */
try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
// Set the parent path and the new labels.
String certificateAuthorityParent = CertificateAuthorityName.of(project, location, pool_Id, certificateAuthorityName).toString();
CertificateAuthority certificateAuthority = CertificateAuthority.newBuilder().setName(certificateAuthorityParent).putLabels("env", "test").build();
// Create a request to update the CA.
UpdateCertificateAuthorityRequest request = UpdateCertificateAuthorityRequest.newBuilder().setCertificateAuthority(certificateAuthority).setUpdateMask(FieldMask.newBuilder().addPaths("labels").build()).build();
// Update the CA and wait for the operation to complete.
ApiFuture<Operation> futureCall = certificateAuthorityServiceClient.updateCertificateAuthorityCallable().futureCall(request);
Operation operation = futureCall.get(60, TimeUnit.SECONDS);
// Check for errors.
if (operation.hasError()) {
System.out.println("Error in updating labels ! " + operation.getError());
}
// Get the updated CA and check if it contains the new label.
CertificateAuthority response = certificateAuthorityServiceClient.getCertificateAuthority(certificateAuthorityParent);
if (response.getLabelsMap().containsKey("env") && response.getLabelsMap().get("env").equalsIgnoreCase("test")) {
System.out.println("Successfully updated the labels ! ");
}
}
}
Aggregations