Search in sources :

Example 1 with NoteName

use of com.google.containeranalysis.v1beta1.NoteName in project java-docs-samples by GoogleCloudPlatform.

the class CreateOccurrence method createOccurrence.

// Creates and returns a new vulnerability Occurrence associated with an existing Note
public static Occurrence createOccurrence(String resourceUrl, String noteId, String occProjectId, String noteProjectId) throws IOException, InterruptedException {
    // String resourceUrl = "https://gcr.io/project/image@sha256:123";
    // String noteId = "my-note";
    // String occProjectId = "my-project-id";
    // String noteProjectId = "my-project-id";
    final NoteName noteName = NoteName.of(noteProjectId, noteId);
    final String occProjectName = ProjectName.format(occProjectId);
    Occurrence newOcc = Occurrence.newBuilder().setNoteName(noteName.toString()).setResourceUri(resourceUrl).setVulnerability(VulnerabilityOccurrence.newBuilder().addPackageIssue(PackageIssue.newBuilder().setAffectedCpeUri("your-uri-here").setAffectedPackage("your-package-here").setAffectedVersion(Version.newBuilder().setKind(Version.VersionKind.MINIMUM)).setFixedVersion(Version.newBuilder().setKind(Version.VersionKind.MAXIMUM)))).build();
    // Initialize client that will be used to send requests. After completing all of your requests,
    // call the "close" method on the client to safely clean up any remaining background resources.
    GrafeasClient client = ContainerAnalysisClient.create().getGrafeasClient();
    Occurrence result = client.createOccurrence(occProjectName, newOcc);
    return result;
}
Also used : GrafeasClient(io.grafeas.v1.GrafeasClient) NoteName(io.grafeas.v1.NoteName) Occurrence(io.grafeas.v1.Occurrence) VulnerabilityOccurrence(io.grafeas.v1.VulnerabilityOccurrence)

Example 2 with NoteName

use of com.google.containeranalysis.v1beta1.NoteName in project java-docs-samples by GoogleCloudPlatform.

the class GetNote method getNote.

// Retrieves and prints a specified Note from the server
public static Note getNote(String noteId, String projectId) throws IOException, InterruptedException {
    // String noteId = "my-note";
    // String projectId = "my-project-id";
    final NoteName noteName = NoteName.of(projectId, noteId);
    // Initialize client that will be used to send requests. After completing all of your requests,
    // call the "close" method on the client to safely clean up any remaining background resources.
    GrafeasClient client = ContainerAnalysisClient.create().getGrafeasClient();
    Note n = client.getNote(noteName);
    System.out.println(n);
    return n;
}
Also used : GrafeasClient(io.grafeas.v1.GrafeasClient) Note(io.grafeas.v1.Note) NoteName(io.grafeas.v1.NoteName)

Example 3 with NoteName

use of com.google.containeranalysis.v1beta1.NoteName in project java-docs-samples by GoogleCloudPlatform.

the class OccurrencesForNote method getOccurrencesForNote.

// Retrieves all the Occurrences associated with a specified Note
// Here, all Occurrences are printed and counted
public static int getOccurrencesForNote(String noteId, String projectId) throws IOException, InterruptedException {
    // String noteId = "my-note";
    // String projectId = "my-project-id";
    final NoteName noteName = NoteName.of(projectId, noteId);
    ListNoteOccurrencesRequest request = ListNoteOccurrencesRequest.newBuilder().setName(noteName.toString()).build();
    // Initialize client that will be used to send requests. After completing all of your requests,
    // call the "close" method on the client to safely clean up any remaining background resources.
    GrafeasClient client = ContainerAnalysisClient.create().getGrafeasClient();
    int i = 0;
    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;
}
Also used : GrafeasClient(io.grafeas.v1.GrafeasClient) NoteName(io.grafeas.v1.NoteName) Occurrence(io.grafeas.v1.Occurrence) ListNoteOccurrencesRequest(io.grafeas.v1.ListNoteOccurrencesRequest)

Example 4 with NoteName

use of com.google.containeranalysis.v1beta1.NoteName in project java-containeranalysis by googleapis.

the class GrafeasV1Beta1ClientTest method listNoteOccurrencesExceptionTest.

@Test
@SuppressWarnings("all")
public void listNoteOccurrencesExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
    mockGrafeasV1Beta1.addException(exception);
    try {
        NoteName name = NoteName.of("[PROJECT]", "[NOTE]");
        String filter = "filter-1274492040";
        client.listNoteOccurrences(name, filter);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) NoteName(com.google.containeranalysis.v1beta1.NoteName) Test(org.junit.Test)

Example 5 with NoteName

use of com.google.containeranalysis.v1beta1.NoteName in project java-containeranalysis by googleapis.

the class GrafeasV1Beta1ClientTest method getOccurrenceTest.

@Test
@SuppressWarnings("all")
public void getOccurrenceTest() {
    String name2 = "name2-1052831874";
    String noteName = "noteName1780787896";
    String remediation = "remediation779381797";
    Occurrence expectedResponse = Occurrence.newBuilder().setName(name2).setNoteName(noteName).setRemediation(remediation).build();
    mockGrafeasV1Beta1.addResponse(expectedResponse);
    OccurrenceName name = OccurrenceName.of("[PROJECT]", "[OCCURRENCE]");
    Occurrence actualResponse = client.getOccurrence(name);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockGrafeasV1Beta1.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetOccurrenceRequest actualRequest = (GetOccurrenceRequest) actualRequests.get(0);
    Assert.assertEquals(name, OccurrenceName.parse(actualRequest.getName()));
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) OccurrenceName(com.google.containeranalysis.v1beta1.OccurrenceName) GetOccurrenceRequest(io.grafeas.v1beta1.GetOccurrenceRequest) Occurrence(io.grafeas.v1beta1.Occurrence) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 NoteName (com.google.containeranalysis.v1beta1.NoteName)8 AbstractMessage (com.google.protobuf.AbstractMessage)7 GrafeasClient (io.grafeas.v1.GrafeasClient)5 NoteName (io.grafeas.v1.NoteName)5 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)4 Occurrence (io.grafeas.v1beta1.Occurrence)4 StatusRuntimeException (io.grpc.StatusRuntimeException)4 FieldMask (com.google.protobuf.FieldMask)3 Occurrence (io.grafeas.v1.Occurrence)3 Note (io.grafeas.v1beta1.Note)3 OccurrenceName (com.google.containeranalysis.v1beta1.OccurrenceName)2 Note (io.grafeas.v1.Note)2 VulnerabilityOccurrence (io.grafeas.v1.VulnerabilityOccurrence)2 ListNoteOccurrencesPagedResponse (com.google.cloud.devtools.containeranalysis.v1beta1.GrafeasV1Beta1Client.ListNoteOccurrencesPagedResponse)1 ProjectName (com.google.containeranalysis.v1beta1.ProjectName)1 Empty (com.google.protobuf.Empty)1 DiscoveryNote (io.grafeas.v1.DiscoveryNote)1 DiscoveryOccurrence (io.grafeas.v1.DiscoveryOccurrence)1 AnalysisStatus (io.grafeas.v1.DiscoveryOccurrence.AnalysisStatus)1