Skip to content

Commit 6562a55

Browse files
committed
addressed PR comments
1 parent 934cbe2 commit 6562a55

File tree

1 file changed

+49
-44
lines changed
  • container-registry/container-analysis/src/main/java/com/example/containeranalysis

1 file changed

+49
-44
lines changed

container-registry/container-analysis/src/main/java/com/example/containeranalysis/Samples.java

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public class Samples {
5454
/**
5555
* Creates and returns a new vulnerability Note
5656
* @param client The Grafeas client used to perform the API requests.
57-
* @param noteId A user-specified identifier for the note.
58-
* @param projectId the GCP project the note will be created under
59-
* @return a Note object representing the new note
57+
* @param noteId A user-specified identifier for the Note.
58+
* @param projectId the GCP project the Note will be created under
59+
* @return the newly created Note object
6060
*/
6161
public static Note createNote(GrafeasV1Beta1Client client, String noteId, String projectId) {
6262
Note.Builder noteBuilder = Note.newBuilder();
@@ -76,28 +76,29 @@ public static Note createNote(GrafeasV1Beta1Client client, String noteId, String
7676
/**
7777
* Creates and returns a new Occurrence of a previously created vulnerability Note
7878
* @param client The Grafeas client used to perform the API requests.
79-
* @param imageUri the Container Registry URL associated with the image
79+
* @param imageUrl the Container Registry URL associated with the image
8080
* example: "https://gcr.io/project/image@sha256:foo"
81-
* @param parentNoteId the identifier of the note associated with this occurrence
82-
* @param projectId the GCP project the occurrence will be created under
83-
* @return an Occurrence object representing the new occurrence
81+
* @param noteId the identifier of the Note associated with this Occurrence
82+
* @param occProjectId the GCP project the Occurrence will be created under
83+
* @param noteProjectId the GCP project the associated Note belongs to
84+
* @return the newly created Occurrence object
8485
*/
85-
public static Occurrence createOccurrence(GrafeasV1Beta1Client client, String imageUri,
86-
String parentNoteId,String projectId) {
87-
final String parentNoteName = client.formatNoteName(projectId, parentNoteId);
88-
final String projectName = client.formatProjectName(projectId);
86+
public static Occurrence createOccurrence(GrafeasV1Beta1Client client, String imageUrl,
87+
String noteId, String occProjectId, String noteProjectId) {
88+
final String noteName = client.formatNoteName(noteProjectId, noteId);
89+
final String occProjectName = client.formatProjectName(occProjectId);
8990

9091
Occurrence.Builder occBuilder = Occurrence.newBuilder();
91-
occBuilder.setNoteName(parentNoteName);
92+
occBuilder.setNoteName(noteName);
9293
Details.Builder detailsBuilder = Details.newBuilder();
9394
// Details about the vulnerability instance can be added here
9495
occBuilder.setVulnerability(detailsBuilder);
9596
// Attach the occurrence to the associated image uri
9697
Resource.Builder resourceBuilder = Resource.newBuilder();
97-
resourceBuilder.setUri(imageUri);
98+
resourceBuilder.setUri(imageUrl);
9899
occBuilder.setResource(resourceBuilder);
99100
Occurrence newOcc = occBuilder.build();
100-
return client.createOccurrence(projectName, newOcc);
101+
return client.createOccurrence(occProjectName, newOcc);
101102
}
102103
// [END create_occurrence]
103104

@@ -106,43 +107,43 @@ public static Occurrence createOccurrence(GrafeasV1Beta1Client client, String im
106107
* Pushes an update to a Note that already exists on the server
107108
* @param client The Grafeas client used to perform the API requests.
108109
* @param updated a Note object representing the desired updates to push
109-
* @param noteId the identifier of the existing note
110+
* @param noteId the identifier of the existing Note
110111
* @param projectId the GCP project the Note belongs to
111112
*/
112-
public static void updateNote(GrafeasV1Beta1Client client, Note updated, String noteId,
113+
public static Note updateNote(GrafeasV1Beta1Client client, Note updated, String noteId,
113114
String projectId) {
114115
final String noteName = client.formatNoteName(projectId, noteId);
115116
UpdateNoteRequest request = UpdateNoteRequest.newBuilder()
116117
.setName(noteName)
117118
.setNote(updated)
118119
.build();
119-
client.updateNote(request);
120+
return client.updateNote(request);
120121
}
121122
// [END update_note]
122123

123124
// [START update_occurrence]
124125
/**
125126
* Pushes an update to an Occurrence that already exists on the server
126127
* @param client The Grafeas client used to perform the API requests.
127-
* @param occurrenceName the name of the occurrence to delete.
128-
* format: "projects/{projectId}/occurrences/{occurrence_id}"
128+
* @param occurrenceName the name of the Occurrence to delete.
129+
* format: "projects/[PROJECT_ID]/occurrences/[OCCURRENCE_ID]"
129130
* @param updated an Occurrence object representing the desired updates to push
130131
*/
131-
public static void updateOccurrence(GrafeasV1Beta1Client client, String occurrenceName,
132+
public static Occurrence updateOccurrence(GrafeasV1Beta1Client client, String occurrenceName,
132133
Occurrence updated) {
133134
UpdateOccurrenceRequest request = UpdateOccurrenceRequest.newBuilder()
134135
.setName(occurrenceName)
135136
.setOccurrence(updated)
136137
.build();
137-
client.updateOccurrence(request);
138+
return client.updateOccurrence(request);
138139
}
139140
// [END update_occurrence]
140141

141142
// [START delete_note]
142143
/**
143144
* Deletes an existing Note from the server
144145
* @param client The Grafeas client used to perform the API requests.
145-
* @param noteId the identifier of the note to delete
146+
* @param noteId the identifier of the Note to delete
146147
* @param projectId the GCP project the Note belongs to
147148
*/
148149
public static void deleteNote(GrafeasV1Beta1Client client, String noteId, String projectId) {
@@ -155,8 +156,8 @@ public static void deleteNote(GrafeasV1Beta1Client client, String noteId, String
155156
/**
156157
* Deletes an existing Occurrence from the server
157158
* @param client The Grafeas client used to perform the API requests.
158-
* @param occurrenceName the name of the occurrence to delete
159-
* format: "projects/{projectId}/occurrences/{occurrence_id}"
159+
* @param occurrenceName the name of the Occurrence to delete
160+
* format: "projects/[PROJECT_ID]/occurrences/[OCCURRENCE_ID]"
160161
*/
161162
public static void deleteOccurrence(GrafeasV1Beta1Client client, String occurrenceName) {
162163
client.deleteOccurrence(occurrenceName);
@@ -166,29 +167,33 @@ public static void deleteOccurrence(GrafeasV1Beta1Client client, String occurren
166167

167168
// [START get_occurrence]
168169
/**
169-
* Retrieves a specified Occurrence from the server
170+
* Retrieves and prints a specified Occurrence from the server
170171
* @param client The Grafeas client used to perform the API requests.
171-
* @param occurrenceName the name of the occurrence to delete
172-
* format: "projects/{projectId}/occurrences/{occurrence_id}"
172+
* @param occurrenceName the name of the Occurrence to delete
173+
* format: "projects/[PROJECT_ID]/occurrences/[OCCURRENCE_ID]"
173174
* @return the requested Occurrence object
174175
*/
175176
public static Occurrence getOccurrence(GrafeasV1Beta1Client client, String occurrenceName) {
176-
return client.getOccurrence(occurrenceName);
177+
Occurrence occ = client.getOccurrence(occurrenceName);
178+
System.out.println(occ);
179+
return occ;
177180
}
178181
// [END get_occurrence]
179182

180183
// [START get_note]
181184
/**
182-
* Retrieves a specified Note from the server
185+
* Retrieves and prints a specified Note from the server
183186
* @param client The Grafeas client used to perform the API requests.
184-
* @param noteId the note's unique identifier
187+
* @param noteId the Note's unique identifier
185188
* @param projectId the GCP project the Note belongs to
186189
* @return the requested Note object
187190
*/
188191
public static Note getNote(GrafeasV1Beta1Client client, String noteId, String projectId) {
189192
final String noteName = client.formatNoteName(projectId, noteId);
190193

191-
return client.getNote(noteName);
194+
Note n = client.getNote(noteName);
195+
System.out.println(n);
196+
return n;
192197
}
193198
// [END get_note]
194199

@@ -197,13 +202,13 @@ public static Note getNote(GrafeasV1Beta1Client client, String noteId, String pr
197202
* Retrieves and prints the Discovery Occurrence created for a specified image
198203
* The Discovery Occurrence contains information about the initial scan on the image
199204
* @param client The Grafeas client used to perform the API requests.
200-
* @param imageUri the Container Registry URL associated with the image
205+
* @param imageUrl the Container Registry URL associated with the image
201206
* example: "https://gcr.io/project/image@sha256:foo"
202207
* @param projectId the GCP project the image belongs to
203208
*/
204-
public static void getDiscoveryInfo(GrafeasV1Beta1Client client, String imageUri,
209+
public static void getDiscoveryInfo(GrafeasV1Beta1Client client, String imageUrl,
205210
String projectId) {
206-
String filterStr = "kind=\"DISCOVERY\" AND resourceUrl=\"" + imageUri + "\"";
211+
String filterStr = "kind=\"DISCOVERY\" AND resourceUrl=\"" + imageUrl + "\"";
207212
final String projectName = client.formatProjectName(projectId);
208213

209214
for (Occurrence o : client.listOccurrences(projectName, filterStr).iterateAll()) {
@@ -217,17 +222,17 @@ public static void getDiscoveryInfo(GrafeasV1Beta1Client client, String imageUri
217222
* Retrieves all the Occurrences associated with a specified Note
218223
* Here, all Occurrences are printed and counted
219224
* @param client The Grafeas client used to perform the API requests.
220-
* @param noteId the note's unique identifier
225+
* @param noteId the Note's unique identifier
221226
* @param projectId the GCP project the Note belongs to
222-
* @return number of occurrences found
227+
* @return number of Occurrences found
223228
*/
224229
public static int getOccurrencesForNote(GrafeasV1Beta1Client client, String noteId,
225230
String projectId) {
226-
final String parentNoteName = client.formatNoteName(projectId, noteId);
231+
final String noteName = client.formatNoteName(projectId, noteId);
227232
int i = 0;
228233

229234
ListNoteOccurrencesRequest request = ListNoteOccurrencesRequest.newBuilder()
230-
.setName(parentNoteName)
235+
.setName(noteName)
231236
.build();
232237
for (Occurrence o : client.listNoteOccurrences(request).iterateAll()) {
233238
// Write custom code to process each Occurrence here
@@ -244,14 +249,14 @@ public static int getOccurrencesForNote(GrafeasV1Beta1Client client, String note
244249
* Retrieves all the Occurrences associated with a specified image
245250
* Here, all Occurrences are simply printed and counted
246251
* @param client The Grafeas client used to perform the API requests.
247-
* @param imageUri the Container Registry URL associated with the image
252+
* @param imageUrl the Container Registry URL associated with the image
248253
* example: "https://gcr.io/project/image@sha256:foo"
249254
* @param projectId the GCP project to search for Occurrences in
250-
* @return number of occurrences found
255+
* @return number of Occurrences found
251256
*/
252-
public static int getOccurrencesForImage(GrafeasV1Beta1Client client, String imageUri,
257+
public static int getOccurrencesForImage(GrafeasV1Beta1Client client, String imageUrl,
253258
String projectId) {
254-
final String filterStr = "resourceUrl=\"" + imageUri + "\"";
259+
final String filterStr = "resourceUrl=\"" + imageUrl + "\"";
255260
final String projectName = client.formatProjectName(projectId);
256261
int i = 0;
257262

@@ -270,7 +275,7 @@ public static int getOccurrencesForImage(GrafeasV1Beta1Client client, String ima
270275
* @param subId the user-specified identifier for the Pub/Sub subscription
271276
* @param timeout the amount of time to listen for Pub/Sub messages (in seconds)
272277
* @param projectId the GCP project the Pub/Sub subscription belongs to
273-
* @return number of occurrence Pub/Sub messages received before exiting
278+
* @return number of Occurrence Pub/Sub messages received before exiting
274279
* @throws InterruptedException on errors with the subscription client
275280
*/
276281
public static int pubSub(String subId, int timeout, String projectId)
@@ -327,7 +332,7 @@ public synchronized void receiveMessage(PubsubMessage message, AckReplyConsumer
327332
public static Subscription createOccurrenceSubscription(String subId, String projectId)
328333
throws IOException, StatusRuntimeException {
329334
// This topic id will automatically receive messages when Occurrences are added or modified
330-
String topicId = "resource-notes-occurrences-v1alpha1";
335+
String topicId = "container-analysis-occurrences-v1beta1";
331336
SubscriptionAdminClient client = SubscriptionAdminClient.create();
332337
PushConfig config = PushConfig.getDefaultInstance();
333338
ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);

0 commit comments

Comments
 (0)