Search in sources :

Example 1 with ContainerAnalysisClient

use of com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient in project java-docs-samples by GoogleCloudPlatform.

the class Samples method updateNote.

// [END create_occurrence]
// [START update_note]
/**
 * Makes an update to an existing note
 *
 * @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 occurrence will be created under.
 * @throws Exception on errors while closing the client
 */
public static void updateNote(Note updated, String noteId, String projectId) throws Exception {
    try (ContainerAnalysisClient client = ContainerAnalysisClient.create()) {
        final String noteName = client.formatNoteName(projectId, noteId);
        client.updateNote(noteName, updated);
    }
}
Also used : ContainerAnalysisClient(com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient)

Example 2 with ContainerAnalysisClient

use of com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient in project java-docs-samples by GoogleCloudPlatform.

the class Samples method getOccurrencesForNote.

// [END discovery_info]
// [START occurrences_for_note]
/**
 * Retrieves all the occurrences associated with a specified note
 *
 * @param noteId the note's unique identifier
 * @param projectId the project's unique identifier
 * @return number of occurrences found
 * @throws Exception on errors while closing the client
 */
public static int getOccurrencesForNote(String noteId, String projectId) throws Exception {
    try (ContainerAnalysisClient client = ContainerAnalysisClient.create()) {
        final String parentNoteName = client.formatNoteName(projectId, noteId);
        int i = 0;
        ListNoteOccurrencesPagedResponse response = client.listNoteOccurrences(parentNoteName);
        for (Occurrence o : response.iterateAll()) {
            // print and count each occurrence found
            System.out.println(o.getName());
            i = i + 1;
        }
        return i;
    }
}
Also used : ListNoteOccurrencesPagedResponse(com.google.cloud.devtools.containeranalysis.v1alpha1.PagedResponseWrappers.ListNoteOccurrencesPagedResponse) ContainerAnalysisClient(com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient) Occurrence(com.google.containeranalysis.v1alpha1.Occurrence)

Example 3 with ContainerAnalysisClient

use of com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient in project java-docs-samples by GoogleCloudPlatform.

the class Samples method createNote.

// [START create_note]
/**
 * Creates and returns a new note
 *
 * @param noteId A user-specified identifier for the note.
 * @param projectId the GCP project the note will be created under
 * @return a Note object representing the new note
 * @throws Exception on errors while closing the client
 */
public static Note createNote(String noteId, String projectId) throws Exception {
    Note.Builder noteBuilder = Note.newBuilder();
    noteBuilder.setVulnerabilityType(VulnerabilityType.newBuilder().build());
    Note newNote = noteBuilder.build();
    try (ContainerAnalysisClient client = ContainerAnalysisClient.create()) {
        final String projectName = client.formatProjectName(projectId);
        return client.createNote(projectName, noteId, newNote);
    }
}
Also used : Note(com.google.containeranalysis.v1alpha1.Note) ContainerAnalysisClient(com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient)

Example 4 with ContainerAnalysisClient

use of com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient in project java-docs-samples by GoogleCloudPlatform.

the class Samples method deleteNote.

// [END update_occurrence]
// [START delete_note]
/**
 * Deletes an existing note
 *
 * @param noteId the identifier of the note to delete
 * @param projectId the GCP project the occurrence will be created under
 * @throws Exception on errors while closing the client
 */
public static void deleteNote(String noteId, String projectId) throws Exception {
    try (ContainerAnalysisClient client = ContainerAnalysisClient.create()) {
        final String noteName = client.formatNoteName(projectId, noteId);
        client.deleteNote(noteName);
    }
}
Also used : ContainerAnalysisClient(com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient)

Example 5 with ContainerAnalysisClient

use of com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient in project java-docs-samples by GoogleCloudPlatform.

the class Samples method getOccurrencesForImage.

// [END occurrences_for_note]
// [START occurrences_for_image]
/**
 * Retrieves all the occurrences associated with a specified image
 *
 * @param imageUrl the Container Registry URL associated with the image
 *                 example: "https://gcr.io/project/image@sha256:foo"
 * @param projectId the project's unique identifier
 * @return number of occurrences found
 * @throws Exception on errors while closing the client
 */
public static int getOccurrencesForImage(String imageUrl, String projectId) throws Exception {
    try (ContainerAnalysisClient client = ContainerAnalysisClient.create()) {
        final String filterStr = "resourceUrl=\"" + imageUrl + "\"";
        final String projectName = client.formatProjectName(projectId);
        int i = 0;
        // build the request
        ListOccurrencesRequest.Builder b = ListOccurrencesRequest.newBuilder();
        b.setFilter(filterStr);
        b.setParent(projectName);
        ListOccurrencesRequest req = b.build();
        // query for the requested occurrences
        ListOccurrencesPagedResponse response = client.listOccurrences(req);
        for (Occurrence o : response.iterateAll()) {
            // print and count each occurrence found
            System.out.println(o.getName());
            i = i + 1;
        }
        return i;
    }
}
Also used : ListOccurrencesRequest(com.google.containeranalysis.v1alpha1.ListOccurrencesRequest) ContainerAnalysisClient(com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient) Occurrence(com.google.containeranalysis.v1alpha1.Occurrence) ListOccurrencesPagedResponse(com.google.cloud.devtools.containeranalysis.v1alpha1.PagedResponseWrappers.ListOccurrencesPagedResponse)

Aggregations

ContainerAnalysisClient (com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient)7 Occurrence (com.google.containeranalysis.v1alpha1.Occurrence)4 ListOccurrencesPagedResponse (com.google.cloud.devtools.containeranalysis.v1alpha1.PagedResponseWrappers.ListOccurrencesPagedResponse)2 ListOccurrencesRequest (com.google.containeranalysis.v1alpha1.ListOccurrencesRequest)2 ListNoteOccurrencesPagedResponse (com.google.cloud.devtools.containeranalysis.v1alpha1.PagedResponseWrappers.ListNoteOccurrencesPagedResponse)1 Note (com.google.containeranalysis.v1alpha1.Note)1 VulnerabilityDetails (com.google.containeranalysis.v1alpha1.VulnerabilityType.VulnerabilityDetails)1