public static LocationName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "LocationName.parse: formattedString not in valid format"); return of(matchMap.get("project"), matchMap.get("location")); }
public static List<String> toStringList(List<LocationName> values) { List<String> list = new ArrayList<String>(values.size()); for (LocationName value : values) { if (value == null) { list.add(""); } else { list.add(value.toString()); } } return list; }
public static List<LocationName> parseList(List<String> formattedStrings) { List<LocationName> list = new ArrayList<>(formattedStrings.size()); for (String formattedString : formattedStrings) { list.add(parse(formattedString)); } return list; }
public static String format(String project, String location) { return newBuilder().setProject(project).setLocation(location).build().toString(); }
@Test @SuppressWarnings("all") public void listKeyRingsTest() { String nextPageToken = ""; int totalSize = 705419236; KeyRing keyRingsElement = KeyRing.newBuilder().build(); List<KeyRing> keyRings = Arrays.asList(keyRingsElement); ListKeyRingsResponse expectedResponse = ListKeyRingsResponse.newBuilder() .setNextPageToken(nextPageToken) .setTotalSize(totalSize) .addAllKeyRings(keyRings) .build(); mockKeyManagementService.addResponse(expectedResponse); LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); ListKeyRingsPagedResponse pagedListResponse = client.listKeyRings(parent); List<KeyRing> resources = Lists.newArrayList(pagedListResponse.iterateAll()); Assert.assertEquals(1, resources.size()); Assert.assertEquals(expectedResponse.getKeyRingsList().get(0), resources.get(0)); List<GeneratedMessageV3> actualRequests = mockKeyManagementService.getRequests(); Assert.assertEquals(1, actualRequests.size()); ListKeyRingsRequest actualRequest = (ListKeyRingsRequest) actualRequests.get(0); Assert.assertEquals(parent, LocationName.parse(actualRequest.getParent())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * Lists all the key rings in the given project. */ public static List<KeyRing> listKeyRings(String projectId, String locationId) throws IOException { // Create the Cloud KMS client. try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { // The resource name of the location to search. String parent = LocationName.format(projectId, locationId); // Retrieve a paginated list of KeyRings ListKeyRingsPagedResponse response = client.listKeyRings(parent); ArrayList<KeyRing> results = new ArrayList<>(); // Iterate over all KeyRings (subsequent pages are retrieved automatically) for (KeyRing keyRing : response.iterateAll()) { results.add(keyRing); } return results; } }
public static LocationName of(String project, String location) { return newBuilder().setProject(project).setLocation(location).build(); }
public LocationName build() { return new LocationName(this); } }
public String getFieldValue(String fieldName) { return getFieldValuesMap().get(fieldName); }
@Test @SuppressWarnings("all") public void createKeyRingTest() { KeyRingName name = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); KeyRing expectedResponse = KeyRing.newBuilder().setName(name.toString()).build(); mockKeyManagementService.addResponse(expectedResponse); LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); String keyRingId = "keyRingId-2056646742"; KeyRing keyRing = KeyRing.newBuilder().build(); KeyRing actualResponse = client.createKeyRing(parent, keyRingId, keyRing); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockKeyManagementService.getRequests(); Assert.assertEquals(1, actualRequests.size()); CreateKeyRingRequest actualRequest = (CreateKeyRingRequest) actualRequests.get(0); Assert.assertEquals(parent, LocationName.parse(actualRequest.getParent())); Assert.assertEquals(keyRingId, actualRequest.getKeyRingId()); Assert.assertEquals(keyRing, actualRequest.getKeyRing()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
public static String format(String project, String location) { return newBuilder().setProject(project).setLocation(location).build().toString(); }
public static void main(String... args) throws Exception { String projectId = args[0]; // The location of the Key Rings String location = args[1]; // Create the KeyManagementServiceClient using try-with-resources to manage client cleanup. try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { // The resource name of the location to search String locationPath = LocationName.format(projectId, location); // Make the RPC call ListKeyRingsPagedResponse response = client.listKeyRings(locationPath); // Iterate over all KeyRings (which may cause more result pages to be loaded automatically) for (KeyRing keyRing : response.iterateAll()) { System.out.println("Found KeyRing: " + keyRing.getName()); } } } }
public static LocationName of(String project, String location) { return newBuilder().setProject(project).setLocation(location).build(); }
public LocationName build() { return new LocationName(this); } }
public String getFieldValue(String fieldName) { return getFieldValuesMap().get(fieldName); }
/** * Lists [KeyRings][google.cloud.kms.v1.KeyRing]. * * <p>Sample code: * * <pre><code> * try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) { * LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); * for (KeyRing element : keyManagementServiceClient.listKeyRings(parent).iterateAll()) { * // doThingsWith(element); * } * } * </code></pre> * * @param parent Required. The resource name of the location associated with the * [KeyRings][google.cloud.kms.v1.KeyRing], in the format `projects/*/locations/*`. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final ListKeyRingsPagedResponse listKeyRings(LocationName parent) { ListKeyRingsRequest request = ListKeyRingsRequest.newBuilder() .setParent(parent == null ? null : parent.toString()) .build(); return listKeyRings(request); }
@Test @SuppressWarnings("all") public void listKeyRingsExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockKeyManagementService.addException(exception); try { LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); client.listKeyRings(parent); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
public static List<LocationName> parseList(List<String> formattedStrings) { List<LocationName> list = new ArrayList<>(formattedStrings.size()); for (String formattedString : formattedStrings) { list.add(parse(formattedString)); } return list; }
/** * Creates a new key ring with the given id. */ public static KeyRing createKeyRing(String projectId, String locationId, String keyRingId) throws IOException { // Create the Cloud KMS client. try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { // The resource name of the location associated with the KeyRing. String parent = LocationName.format(projectId, locationId); // Create the KeyRing for your project. KeyRing keyRing = client.createKeyRing(parent, keyRingId, KeyRing.newBuilder().build()); return keyRing; } } // [END kms_create_keyring]