use of com.google.cloud.security.privateca.v1.DeleteCertificateAuthorityRequest in project java-security-private-ca by googleapis.
the class DeleteCertificateAuthority method deleteCertificateAuthority.
// Delete the Certificate Authority from the specified CA pool.
// Before deletion, the CA must be disabled and must not contain any active certificates.
public static void deleteCertificateAuthority(String project, String location, String pool_Id, String certificateAuthorityName) throws IOException, ExecutionException, InterruptedException {
// clean up any remaining background resources.
try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = CertificateAuthorityServiceClient.create()) {
// Create the Certificate Authority Name.
CertificateAuthorityName certificateAuthorityNameParent = CertificateAuthorityName.newBuilder().setProject(project).setLocation(location).setCaPool(pool_Id).setCertificateAuthority(certificateAuthorityName).build();
// Check if the CA is enabled.
State caState = certificateAuthorityServiceClient.getCertificateAuthority(certificateAuthorityNameParent).getState();
if (caState == State.ENABLED) {
System.out.println("Please disable the Certificate Authority before deletion ! Current state: " + caState);
return;
}
// Create the DeleteCertificateAuthorityRequest.
// Setting the setIgnoreActiveCertificates() to true, will delete the CA
// even if it contains active certificates. Care should be taken to re-anchor
// the certificates to new CA before deleting.
DeleteCertificateAuthorityRequest deleteCertificateAuthorityRequest = DeleteCertificateAuthorityRequest.newBuilder().setName(certificateAuthorityNameParent.toString()).setIgnoreActiveCertificates(false).build();
// Delete the Certificate Authority.
ApiFuture<Operation> futureCall = certificateAuthorityServiceClient.deleteCertificateAuthorityCallable().futureCall(deleteCertificateAuthorityRequest);
Operation response = futureCall.get();
if (response.hasError()) {
System.out.println("Error while deleting Certificate Authority !" + response.getError());
return;
}
// Check if the CA has been deleted.
caState = certificateAuthorityServiceClient.getCertificateAuthority(certificateAuthorityNameParent).getState();
if (caState == State.DELETED) {
System.out.println("Successfully deleted Certificate Authority : " + certificateAuthorityName);
} else {
System.out.println("Unable to delete Certificate Authority. Please try again ! Current state: " + caState);
}
}
}
Aggregations