use of io.grafeas.v1beta1.Occurrence 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;
}
}
use of io.grafeas.v1beta1.Occurrence 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;
}
}
use of io.grafeas.v1beta1.Occurrence in project java-docs-samples by GoogleCloudPlatform.
the class SamplesTests method testOccurrencesForNote.
@Test
public void testOccurrencesForNote() throws Exception {
int newCount;
int tries = 0;
int origCount = Samples.getOccurrencesForNote(noteId, PROJECT_ID);
final Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
do {
newCount = Samples.getOccurrencesForNote(noteId, PROJECT_ID);
sleep(SLEEP_TIME);
} while (newCount != 1 && tries < TRY_LIMIT);
assertEquals(0, origCount);
assertEquals(1, newCount);
// clean up
Samples.deleteOccurrence(o.getName());
}
use of io.grafeas.v1beta1.Occurrence in project java-docs-samples by GoogleCloudPlatform.
the class SamplesTests method testPubSub.
@Test
public void testPubSub() throws Exception {
int newCount;
int tries;
String subscriptionId = "drydockOccurrences";
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);
Samples.createOccurrenceSubscription(subscriptionId, PROJECT_ID);
Subscriber subscriber = null;
Samples.MessageReceiverExample receiver = new Samples.MessageReceiverExample();
try {
subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
subscriber.startAsync().awaitRunning();
// sleep so any messages in the queue can go through and be counted before we start the test
sleep(SLEEP_TIME);
// set the initial state of our counter
int startVal = receiver.messageCount + 1;
// now, we can test adding 3 more occurrences
int endVal = startVal + 3;
for (int i = startVal; i <= endVal; i++) {
Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
System.out.println("CREATED: " + o.getName());
tries = 0;
do {
newCount = receiver.messageCount;
sleep(SLEEP_TIME);
tries += 1;
} while (newCount != i && tries < TRY_LIMIT);
System.out.println(receiver.messageCount + " : " + i);
assertEquals(i, receiver.messageCount);
Samples.deleteOccurrence(o.getName());
}
} catch (Exception e) {
fail("exception thrown");
} finally {
if (subscriber != null) {
subscriber.stopAsync();
}
// delete subscription now that we're done with it
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
subscriptionAdminClient.deleteSubscription(subscriptionName);
}
}
}
use of io.grafeas.v1beta1.Occurrence in project java-docs-samples by GoogleCloudPlatform.
the class SamplesTest method testPubSub.
@Test
public void testPubSub() throws Exception {
// create new topic and subscription if needed
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
String topicId = "container-analysis-occurrences-v1";
ProjectTopicName topicName = ProjectTopicName.of(PROJECT_ID, topicId);
topicAdminClient.createTopic(topicName);
} catch (AlreadyExistsException e) {
System.out.println("Topic already exists");
}
ProjectSubscriptionName subName = ProjectSubscriptionName.of(PROJECT_ID, subId);
try {
Subscriptions.createOccurrenceSubscription(subId, PROJECT_ID);
} catch (AlreadyExistsException e) {
System.out.println("subscription " + subId + " already exists");
}
Subscriber subscriber = null;
Subscriptions.MessageReceiverExample receiver = new Subscriptions.MessageReceiverExample();
subscriber = Subscriber.newBuilder(subName, receiver).build();
subscriber.startAsync().awaitRunning();
// sleep so any messages in the queue can go through and be counted before we start the test
sleep(SLEEP_TIME * 3);
// set the initial state of our counter
int startVal = receiver.messageCount + 1;
// add 3 new occurrences
for (int i = 0; i < 3; i++) {
Occurrence o = CreateOccurrence.createOccurrence(imageUrl, noteId, PROJECT_ID, PROJECT_ID);
System.out.println("CREATED: " + o.getName());
String[] nameArr = o.getName().split("/");
String occId = nameArr[nameArr.length - 1];
DeleteOccurrence.deleteOccurrence(occId, PROJECT_ID);
}
// verify the pubsub channel has new entries
int newCount;
int tries = 0;
do {
newCount = receiver.messageCount;
sleep(SLEEP_TIME);
tries += 1;
} while (newCount <= startVal && tries < TRY_LIMIT);
assertTrue(receiver.messageCount > startVal);
if (subscriber != null) {
subscriber.stopAsync();
}
}
Aggregations