Search in sources :

Example 6 with Occurrence

use of com.google.containeranalysis.v1alpha1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class Samples method getDiscoveryInfo.

// [END get_note]
// [START discovery_info]
/**
 * Retrieves the Discovery occurrence created for a specified image
 * This occurrence contains information about the initial scan on the image
 *
 * @param imageUrl the Container Registry URL associated with the image
 *                 example: "https://gcr.io/project/image@sha256:foo"
 * @param projectId the GCP project the occurrence will be created under
 * @throws Exception on errors while closing the client
 */
public static void getDiscoveryInfo(String imageUrl, String projectId) throws Exception {
    String filterStr = "kind=\"DISCOVERY\" AND resourceUrl=\"" + imageUrl + "\"";
    try (ContainerAnalysisClient client = ContainerAnalysisClient.create()) {
        final String projectName = client.formatProjectName(projectId);
        ListOccurrencesRequest.Builder req = ListOccurrencesRequest.newBuilder();
        req.setFilter(filterStr).setParent(projectName);
        ListOccurrencesPagedResponse response = client.listOccurrences(req.build());
        for (Occurrence o : response.iterateAll()) {
            System.out.println(o);
        }
    }
}
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)

Example 7 with Occurrence

use of com.google.containeranalysis.v1alpha1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class SamplesTests method testUpdateOccurrence.

@Test
public void testUpdateOccurrence() throws Exception {
    String typeId = "newType";
    Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
    Occurrence.Builder b = Occurrence.newBuilder(o);
    VulnerabilityDetails.Builder v = VulnerabilityDetails.newBuilder();
    v.setType(typeId);
    b.setVulnerabilityDetails(v.build());
    Samples.updateOccurrence(o.getName(), b.build());
    Occurrence o2 = Samples.getOccurrence(o.getName());
    assertEquals(typeId, o2.getVulnerabilityDetails().getType());
    // clean up
    Samples.deleteOccurrence(o2.getName());
}
Also used : VulnerabilityDetails(com.google.containeranalysis.v1alpha1.VulnerabilityType.VulnerabilityDetails) Occurrence(com.google.containeranalysis.v1alpha1.Occurrence) Test(org.junit.Test)

Example 8 with Occurrence

use of com.google.containeranalysis.v1alpha1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class SamplesTests method testCreateOccurrence.

@Test
public void testCreateOccurrence() throws Exception {
    Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
    Occurrence retrieved = Samples.getOccurrence(o.getName());
    assertEquals(o.getName(), retrieved.getName());
    // clean up
    Samples.deleteOccurrence(o.getName());
}
Also used : Occurrence(com.google.containeranalysis.v1alpha1.Occurrence) Test(org.junit.Test)

Example 9 with Occurrence

use of com.google.containeranalysis.v1alpha1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class SamplesTests method testDeleteOccurrence.

@Test
public void testDeleteOccurrence() throws Exception {
    Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
    String occName = o.getName();
    Samples.deleteOccurrence(occName);
    try {
        Samples.getOccurrence(occName);
        // getOccurrence should fail, because occurrence was deleted
        fail("failed to delete occurrence");
    } catch (Exception e) {
    // test passes
    }
}
Also used : Occurrence(com.google.containeranalysis.v1alpha1.Occurrence) Test(org.junit.Test)

Example 10 with Occurrence

use of com.google.containeranalysis.v1alpha1.Occurrence in project java-docs-samples by GoogleCloudPlatform.

the class SamplesTests method testOccurrencesForImage.

@Test
public void testOccurrencesForImage() throws Exception {
    int newCount;
    int tries = 0;
    int origCount = Samples.getOccurrencesForImage(imageUrl, PROJECT_ID);
    final Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
    do {
        newCount = Samples.getOccurrencesForImage(imageUrl, PROJECT_ID);
        sleep(SLEEP_TIME);
    } while (newCount != 1 && tries < TRY_LIMIT);
    assertEquals(1, newCount);
    assertEquals(0, origCount);
    // clean up
    Samples.deleteOccurrence(o.getName());
}
Also used : Occurrence(com.google.containeranalysis.v1alpha1.Occurrence) Test(org.junit.Test)

Aggregations

Occurrence (com.google.containeranalysis.v1alpha1.Occurrence)10 Test (org.junit.Test)6 ContainerAnalysisClient (com.google.cloud.devtools.containeranalysis.v1alpha1.ContainerAnalysisClient)4 ListOccurrencesPagedResponse (com.google.cloud.devtools.containeranalysis.v1alpha1.PagedResponseWrappers.ListOccurrencesPagedResponse)2 ListOccurrencesRequest (com.google.containeranalysis.v1alpha1.ListOccurrencesRequest)2 VulnerabilityDetails (com.google.containeranalysis.v1alpha1.VulnerabilityType.VulnerabilityDetails)2 ListNoteOccurrencesPagedResponse (com.google.cloud.devtools.containeranalysis.v1alpha1.PagedResponseWrappers.ListNoteOccurrencesPagedResponse)1 Subscriber (com.google.cloud.pubsub.v1.Subscriber)1 SubscriptionAdminClient (com.google.cloud.pubsub.v1.SubscriptionAdminClient)1 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)1