use of com.google.cloud.security.privateca.v1.CertificateAuthority 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 ! ");
}
}
}
use of com.google.cloud.security.privateca.v1.CertificateAuthority in project java-security-private-ca by googleapis.
the class CertificateAuthorityServiceClientTest method listCertificateAuthoritiesTest.
@Test
public void listCertificateAuthoritiesTest() throws Exception {
CertificateAuthority responsesElement = CertificateAuthority.newBuilder().build();
ListCertificateAuthoritiesResponse expectedResponse = ListCertificateAuthoritiesResponse.newBuilder().setNextPageToken("").addAllCertificateAuthorities(Arrays.asList(responsesElement)).build();
mockCertificateAuthorityService.addResponse(expectedResponse);
CaPoolName parent = CaPoolName.of("[PROJECT]", "[LOCATION]", "[CA_POOL]");
ListCertificateAuthoritiesPagedResponse pagedListResponse = client.listCertificateAuthorities(parent);
List<CertificateAuthority> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getCertificateAuthoritiesList().get(0), resources.get(0));
List<AbstractMessage> actualRequests = mockCertificateAuthorityService.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListCertificateAuthoritiesRequest actualRequest = ((ListCertificateAuthoritiesRequest) actualRequests.get(0));
Assert.assertEquals(parent.toString(), actualRequest.getParent());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations