use of com.google.cloud.devtools.containeranalysis.v1.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);
}
}
use of com.google.cloud.devtools.containeranalysis.v1.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;
}
}
use of com.google.cloud.devtools.containeranalysis.v1.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);
}
}
use of com.google.cloud.devtools.containeranalysis.v1.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);
}
}
use of com.google.cloud.devtools.containeranalysis.v1.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;
}
}
Aggregations