|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package privateca; |
| 17 | + |
| 18 | +// [START privateca_update_ca_label] |
| 19 | + |
| 20 | +import com.google.api.core.ApiFuture; |
| 21 | +import com.google.cloud.security.privateca.v1.CertificateAuthority; |
| 22 | +import com.google.cloud.security.privateca.v1.CertificateAuthorityName; |
| 23 | +import com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient; |
| 24 | +import com.google.cloud.security.privateca.v1.UpdateCertificateAuthorityRequest; |
| 25 | +import com.google.longrunning.Operation; |
| 26 | +import com.google.protobuf.FieldMask; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.concurrent.ExecutionException; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import java.util.concurrent.TimeoutException; |
| 31 | + |
| 32 | +public class UpdateCertificateAuthority { |
| 33 | + |
| 34 | + public static void main(String[] args) |
| 35 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 36 | + // TODO(developer): Replace these variables before running the sample. |
| 37 | + // location: For a list of locations, see: |
| 38 | + // https://cloud.google.com/certificate-authority-service/docs/locations |
| 39 | + // pool_Id: Set it to the CA Pool under which the CA should be created. |
| 40 | + // certificateAuthorityName: Unique name for the CA. |
| 41 | + String project = "your-project-id"; |
| 42 | + String location = "ca-location"; |
| 43 | + String pool_Id = "ca-pool-id"; |
| 44 | + String certificateAuthorityName = "certificate-authority-name"; |
| 45 | + |
| 46 | + updateCaLabel(project, location, pool_Id, certificateAuthorityName); |
| 47 | + } |
| 48 | + |
| 49 | + // Updates the labels in a certificate authority. |
| 50 | + public static void updateCaLabel( |
| 51 | + String project, String location, String pool_Id, String certificateAuthorityName) |
| 52 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 53 | + /* Initialize client that will be used to send requests. This client only needs to be created |
| 54 | + once, and can be reused for multiple requests. After completing all of your requests, call |
| 55 | + the `certificateAuthorityServiceClient.close()` method on the client to safely |
| 56 | + clean up any remaining background resources. */ |
| 57 | + try (CertificateAuthorityServiceClient certificateAuthorityServiceClient = |
| 58 | + CertificateAuthorityServiceClient.create()) { |
| 59 | + |
| 60 | + // Set the parent path and the new labels. |
| 61 | + String certificateAuthorityParent = |
| 62 | + CertificateAuthorityName.of(project, location, pool_Id, certificateAuthorityName) |
| 63 | + .toString(); |
| 64 | + CertificateAuthority certificateAuthority = |
| 65 | + CertificateAuthority.newBuilder() |
| 66 | + .setName(certificateAuthorityParent) |
| 67 | + .putLabels("env", "test") |
| 68 | + .build(); |
| 69 | + |
| 70 | + // Create a request to update the CA. |
| 71 | + UpdateCertificateAuthorityRequest request = |
| 72 | + UpdateCertificateAuthorityRequest.newBuilder() |
| 73 | + .setCertificateAuthority(certificateAuthority) |
| 74 | + .setUpdateMask(FieldMask.newBuilder().addPaths("labels").build()) |
| 75 | + .build(); |
| 76 | + |
| 77 | + // Update the CA and wait for the operation to complete. |
| 78 | + ApiFuture<Operation> futureCall = |
| 79 | + certificateAuthorityServiceClient |
| 80 | + .updateCertificateAuthorityCallable() |
| 81 | + .futureCall(request); |
| 82 | + Operation operation = futureCall.get(60, TimeUnit.SECONDS); |
| 83 | + |
| 84 | + // Check for errors. |
| 85 | + if (operation.hasError()) { |
| 86 | + System.out.println("Error in updating labels ! " + operation.getError()); |
| 87 | + } |
| 88 | + |
| 89 | + // Get the updated CA and check if it contains the new label. |
| 90 | + CertificateAuthority response = |
| 91 | + certificateAuthorityServiceClient.getCertificateAuthority(certificateAuthorityParent); |
| 92 | + if (response.getLabelsMap().containsKey("env") |
| 93 | + && response.getLabelsMap().get("env").equalsIgnoreCase("test")) { |
| 94 | + System.out.println("Successfully updated the labels ! "); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | +// [END privateca_update_ca_label] |
0 commit comments