Search in sources :

Example 6 with CertificateAuthority

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 ! ");
        }
    }
}
Also used : CertificateAuthorityServiceClient(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient) CertificateAuthority(com.google.cloud.security.privateca.v1.CertificateAuthority) UpdateCertificateAuthorityRequest(com.google.cloud.security.privateca.v1.UpdateCertificateAuthorityRequest) Operation(com.google.longrunning.Operation)

Example 7 with CertificateAuthority

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()));
}
Also used : ListCertificateAuthoritiesPagedResponse(com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient.ListCertificateAuthoritiesPagedResponse) AbstractMessage(com.google.protobuf.AbstractMessage) Test(org.junit.Test)

Aggregations

CertificateAuthority (com.google.cloud.security.privateca.v1.CertificateAuthority)5 CertificateAuthorityServiceClient (com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient)5 Test (org.junit.Test)4 Operation (com.google.longrunning.Operation)3 KeyVersionSpec (com.google.cloud.security.privateca.v1.CertificateAuthority.KeyVersionSpec)2 ListCertificateAuthoritiesPagedResponse (com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient.ListCertificateAuthoritiesPagedResponse)2 SubjectConfig (com.google.cloud.security.privateca.v1.CertificateConfig.SubjectConfig)2 CreateCertificateAuthorityRequest (com.google.cloud.security.privateca.v1.CreateCertificateAuthorityRequest)2 X509Parameters (com.google.cloud.security.privateca.v1.X509Parameters)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 UpdateCertificateAuthorityRequest (com.google.cloud.security.privateca.v1.UpdateCertificateAuthorityRequest)1 ByteString (com.google.protobuf.ByteString)1