public static NoteName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "NoteName.parse: formattedString not in valid format"); return of(matchMap.get("project"), matchMap.get("note")); }
public static List<String> toStringList(List<NoteName> values) { List<String> list = new ArrayList<String>(values.size()); for (NoteName value : values) { if (value == null) { list.add(""); } else { list.add(value.toString()); } } return list; }
public static IamResourceName parse(String resourceNameString) { if (NoteName.isParsableFrom(resourceNameString)) { return NoteName.parse(resourceNameString); } if (OccurrenceName.isParsableFrom(resourceNameString)) { return OccurrenceName.parse(resourceNameString); } return UntypedIamResourceName.parse(resourceNameString); } }
public static String format(String project, String note) { return newBuilder().setProject(project).setNote(note).build().toString(); }
/** * Pushes an update to a Note that already exists on the server * @param client The Grafeas client used to perform the API requests. * @param updated a Note object representing the desired updates to push * @param noteId the identifier of the existing Note * @param projectId the GCP project the Note belongs to */ public static Note updateNote(GrafeasV1Beta1Client client, Note updated, String noteId, String projectId) { final NoteName noteName = NoteName.of(projectId, noteId); UpdateNoteRequest request = UpdateNoteRequest.newBuilder() .setName(noteName.toString()) .setNote(updated) .build(); return client.updateNote(request); } // [END update_note]
@Test @SuppressWarnings("all") public void deleteNoteTest() { Empty expectedResponse = Empty.newBuilder().build(); mockGrafeasV1Beta1.addResponse(expectedResponse); NoteName name = NoteName.of("[PROJECT]", "[NOTE]"); client.deleteNote(name); List<GeneratedMessageV3> actualRequests = mockGrafeasV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); DeleteNoteRequest actualRequest = (DeleteNoteRequest) actualRequests.get(0); Assert.assertEquals(name, NoteName.parse(actualRequest.getName())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
public static List<NoteName> parseList(List<String> formattedStrings) { List<NoteName> list = new ArrayList<>(formattedStrings.size()); for (String formattedString : formattedStrings) { list.add(parse(formattedString)); } return list; }
public NoteName build() { return new NoteName(this); } }
public String getFieldValue(String fieldName) { return getFieldValuesMap().get(fieldName); }
public static NoteName of(String project, String note) { return newBuilder().setProject(project).setNote(note).build(); }
/** * Retrieves all the Occurrences associated with a specified Note * Here, all Occurrences are printed and counted * @param client The Grafeas client used to perform the API requests. * @param noteId the Note's unique identifier * @param projectId the GCP project the Note belongs to * @return number of Occurrences found */ public static int getOccurrencesForNote(GrafeasV1Beta1Client client, String noteId, String projectId) { final NoteName noteName = NoteName.of(projectId, noteId); int i = 0; ListNoteOccurrencesRequest request = ListNoteOccurrencesRequest.newBuilder() .setName(noteName.toString()) .build(); for (Occurrence o : client.listNoteOccurrences(request).iterateAll()) { // Write custom code to process each Occurrence here System.out.println(o.getName()); i = i + 1; } return i; } // [END occurrences_for_note]
@Test @SuppressWarnings("all") public void listNoteOccurrencesTest() { String nextPageToken = ""; Occurrence occurrencesElement = Occurrence.newBuilder().build(); List<Occurrence> occurrences = Arrays.asList(occurrencesElement); ListNoteOccurrencesResponse expectedResponse = ListNoteOccurrencesResponse.newBuilder() .setNextPageToken(nextPageToken) .addAllOccurrences(occurrences) .build(); mockGrafeasV1Beta1.addResponse(expectedResponse); NoteName name = NoteName.of("[PROJECT]", "[NOTE]"); String filter = "filter-1274492040"; ListNoteOccurrencesPagedResponse pagedListResponse = client.listNoteOccurrences(name, filter); List<Occurrence> resources = Lists.newArrayList(pagedListResponse.iterateAll()); Assert.assertEquals(1, resources.size()); Assert.assertEquals(expectedResponse.getOccurrencesList().get(0), resources.get(0)); List<GeneratedMessageV3> actualRequests = mockGrafeasV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); ListNoteOccurrencesRequest actualRequest = (ListNoteOccurrencesRequest) actualRequests.get(0); Assert.assertEquals(name, NoteName.parse(actualRequest.getName())); Assert.assertEquals(filter, actualRequest.getFilter()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
public static String format(String project, String note) { return newBuilder().setProject(project).setNote(note).build().toString(); }
public static List<NoteName> parseList(List<String> formattedStrings) { List<NoteName> list = new ArrayList<>(formattedStrings.size()); for (String formattedString : formattedStrings) { list.add(parse(formattedString)); } return list; }
public NoteName build() { return new NoteName(this); } }
public String getFieldValue(String fieldName) { return getFieldValuesMap().get(fieldName); }
public static NoteName of(String project, String note) { return newBuilder().setProject(project).setNote(note).build(); }
@Test @SuppressWarnings("all") public void testIamPermissionsExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockContainerAnalysisV1Beta1.addException(exception); try { IamResourceName resource = NoteName.of("[PROJECT]", "[NOTE]"); List<String> permissions = new ArrayList<>(); client.testIamPermissions(resource, permissions); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
/** * Creates and returns a new Occurrence of a previously created vulnerability Note * @param client The Grafeas client used to perform the API requests. * @param imageUrl the Container Registry URL associated with the image * example: "https://gcr.io/project/image@sha256:foo" * @param noteId the identifier of the Note associated with this Occurrence * @param occProjectId the GCP project the Occurrence will be created under * @param noteProjectId the GCP project the associated Note belongs to * @return the newly created Occurrence object */ public static Occurrence createOccurrence(GrafeasV1Beta1Client client, String imageUrl, String noteId, String occProjectId, String noteProjectId) { final NoteName noteName = NoteName.of(noteProjectId, noteId); final String occProjectName = ProjectName.format(occProjectId); Occurrence.Builder occBuilder = Occurrence.newBuilder(); occBuilder.setNoteName(noteName.toString()); Details.Builder detailsBuilder = Details.newBuilder(); // Details about the vulnerability instance can be added here occBuilder.setVulnerability(detailsBuilder); // Attach the occurrence to the associated image uri Resource.Builder resourceBuilder = Resource.newBuilder(); resourceBuilder.setUri(imageUrl); occBuilder.setResource(resourceBuilder); Occurrence newOcc = occBuilder.build(); return client.createOccurrence(occProjectName, newOcc); } // [END create_occurrence]
@Test @SuppressWarnings("all") public void getNoteTest() { String name2 = "name2-1052831874"; String shortDescription = "shortDescription-235369287"; String longDescription = "longDescription-1747792199"; Note expectedResponse = Note.newBuilder() .setName(name2) .setShortDescription(shortDescription) .setLongDescription(longDescription) .build(); mockGrafeasV1Beta1.addResponse(expectedResponse); NoteName name = NoteName.of("[PROJECT]", "[NOTE]"); Note actualResponse = client.getNote(name); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockGrafeasV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); GetNoteRequest actualRequest = (GetNoteRequest) actualRequests.get(0); Assert.assertEquals(name, NoteName.parse(actualRequest.getName())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }