public static ProjectName parse(String formattedString) { if (formattedString.isEmpty()) { return null; } Map<String, String> matchMap = PATH_TEMPLATE.validatedMatch( formattedString, "ProjectName.parse: formattedString not in valid format"); return of(matchMap.get("project")); }
public static List<ProjectName> parseList(List<String> formattedStrings) { List<ProjectName> list = new ArrayList<>(formattedStrings.size()); for (String formattedString : formattedStrings) { list.add(parse(formattedString)); } return list; }
public static List<String> toStringList(List<ProjectName> values) { List<String> list = new ArrayList<String>(values.size()); for (ProjectName value : values) { if (value == null) { list.add(""); } else { list.add(value.toString()); } } return list; }
public static String format(String project) { return newBuilder().setProject(project).build().toString(); }
@Test @SuppressWarnings("all") public void batchCreateNotesTest() { BatchCreateNotesResponse expectedResponse = BatchCreateNotesResponse.newBuilder().build(); mockGrafeasV1Beta1.addResponse(expectedResponse); ProjectName parent = ProjectName.of("[PROJECT]"); Map<String, Note> notes = new HashMap<>(); BatchCreateNotesResponse actualResponse = client.batchCreateNotes(parent, notes); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockGrafeasV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); BatchCreateNotesRequest actualRequest = (BatchCreateNotesRequest) actualRequests.get(0); Assert.assertEquals(parent, ProjectName.parse(actualRequest.getParent())); Assert.assertEquals(notes, actualRequest.getNotesMap()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * Retrieves and prints the Discovery Occurrence created for a specified image * The Discovery Occurrence contains information about the initial scan on the image * @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 projectId the GCP project the image belongs to */ public static void getDiscoveryInfo(GrafeasV1Beta1Client client, String imageUrl, String projectId) { String filterStr = "kind=\"DISCOVERY\" AND resourceUrl=\"" + imageUrl + "\""; final String projectName = ProjectName.format(projectId); for (Occurrence o : client.listOccurrences(projectName, filterStr).iterateAll()) { System.out.println(o); } } // [END discovery_info]
public ProjectName build() { return new ProjectName(this); } }
public static ProjectName of(String project) { return newBuilder().setProject(project).build(); }
public String getFieldValue(String fieldName) { return getFieldValuesMap().get(fieldName); }
@Test @SuppressWarnings("all") public void batchCreateOccurrencesTest() { BatchCreateOccurrencesResponse expectedResponse = BatchCreateOccurrencesResponse.newBuilder().build(); mockGrafeasV1Beta1.addResponse(expectedResponse); ProjectName parent = ProjectName.of("[PROJECT]"); List<Occurrence> occurrences = new ArrayList<>(); BatchCreateOccurrencesResponse actualResponse = client.batchCreateOccurrences(parent, occurrences); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockGrafeasV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); BatchCreateOccurrencesRequest actualRequest = (BatchCreateOccurrencesRequest) actualRequests.get(0); Assert.assertEquals(parent, ProjectName.parse(actualRequest.getParent())); Assert.assertEquals(occurrences, actualRequest.getOccurrencesList()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
public static String format(String project) { return newBuilder().setProject(project).build().toString(); }
/** * Retrieves all the Occurrences associated with a specified image * Here, all Occurrences are simply printed and counted * @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 projectId the GCP project to search for Occurrences in * @return number of Occurrences found */ public static int getOccurrencesForImage(GrafeasV1Beta1Client client, String imageUrl, String projectId) { final String filterStr = "resourceUrl=\"" + imageUrl + "\""; final String projectName = ProjectName.format(projectId); int i = 0; for (Occurrence o : client.listOccurrences(projectName, filterStr).iterateAll()) { // Write custom code to process each Occurrence here System.out.println(o.getName()); i = i + 1; } return i; } // [END occurrences_for_image]
public ProjectName build() { return new ProjectName(this); } }
public static ProjectName of(String project) { return newBuilder().setProject(project).build(); }
public String getFieldValue(String fieldName) { return getFieldValuesMap().get(fieldName); }
@Test @SuppressWarnings("all") public void listNotesTest() { String nextPageToken = ""; Note notesElement = Note.newBuilder().build(); List<Note> notes = Arrays.asList(notesElement); ListNotesResponse expectedResponse = ListNotesResponse.newBuilder().setNextPageToken(nextPageToken).addAllNotes(notes).build(); mockGrafeasV1Beta1.addResponse(expectedResponse); ProjectName parent = ProjectName.of("[PROJECT]"); String filter = "filter-1274492040"; ListNotesPagedResponse pagedListResponse = client.listNotes(parent, filter); List<Note> resources = Lists.newArrayList(pagedListResponse.iterateAll()); Assert.assertEquals(1, resources.size()); Assert.assertEquals(expectedResponse.getNotesList().get(0), resources.get(0)); List<GeneratedMessageV3> actualRequests = mockGrafeasV1Beta1.getRequests(); Assert.assertEquals(1, actualRequests.size()); ListNotesRequest actualRequest = (ListNotesRequest) actualRequests.get(0); Assert.assertEquals(parent, ProjectName.parse(actualRequest.getParent())); Assert.assertEquals(filter, actualRequest.getFilter()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void batchCreateOccurrencesExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockGrafeasV1Beta1.addException(exception); try { ProjectName parent = ProjectName.of("[PROJECT]"); List<Occurrence> occurrences = new ArrayList<>(); client.batchCreateOccurrences(parent, occurrences); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
/** * Lists notes for the specified project. * * <p>Sample code: * * <pre><code> * try (GrafeasV1Beta1Client grafeasV1Beta1Client = GrafeasV1Beta1Client.create()) { * ProjectName parent = ProjectName.of("[PROJECT]"); * String filter = ""; * for (Note element : grafeasV1Beta1Client.listNotes(parent, filter).iterateAll()) { * // doThingsWith(element); * } * } * </code></pre> * * @param parent The name of the project to list notes for in the form of `projects/[PROJECT_ID]`. * @param filter The filter expression. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final ListNotesPagedResponse listNotes(ProjectName parent, String filter) { ListNotesRequest request = ListNotesRequest.newBuilder() .setParent(parent == null ? null : parent.toString()) .setFilter(filter) .build(); return listNotes(request); }
public static List<ProjectName> parseList(List<String> formattedStrings) { List<ProjectName> list = new ArrayList<>(formattedStrings.size()); for (String formattedString : formattedStrings) { list.add(parse(formattedString)); } return list; }
/** * Creates and returns a new vulnerability Note * @param client The Grafeas client used to perform the API requests. * @param noteId A user-specified identifier for the Note. * @param projectId the GCP project the Note will be created under * @return the newly created Note object */ public static Note createNote(GrafeasV1Beta1Client client, String noteId, String projectId) { Note.Builder noteBuilder = Note.newBuilder(); Vulnerability.Builder vulBuilder = Vulnerability.newBuilder(); // Details about the your vulnerability can be added here // Example: vulBuilder.setSeverity(Severity.CRITICAL); noteBuilder.setVulnerability(vulBuilder); Note newNote = noteBuilder.build(); final String projectName = ProjectName.format(projectId); return client.createNote(projectName, noteId, newNote); } // [END create_note]