Skip to content

Commit 6a8b3af

Browse files
committed
updated Java code to address comments
1 parent f8b4dc9 commit 6a8b3af

File tree

2 files changed

+66
-32
lines changed

2 files changed

+66
-32
lines changed

javav2/example_code/inspector/src/main/java/com/java/inspector/HelloInspector.java

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import software.amazon.awssdk.services.inspector2.model.ListUsageTotalsResponse;
1919
import software.amazon.awssdk.services.inspector2.model.UsageTotal;
2020
import software.amazon.awssdk.services.inspector2.model.Inspector2Exception;
21+
import software.amazon.awssdk.services.inspector2.paginators.ListFindingsIterable;
2122
import software.amazon.awssdk.services.inspector2.paginators.ListUsageTotalsIterable;
2223
import java.util.List;
2324
import java.util.ArrayList;
@@ -75,19 +76,19 @@ public static void checkAccountStatus(Inspector2Client inspectorClient) {
7576

7677
List<AccountState> accounts = response.accounts();
7778
if (accounts == null || accounts.isEmpty()) {
78-
System.out.println(" No account information returned.");
79+
logger.info(" No account information returned.");
7980
return;
8081
}
8182

8283
for (AccountState account : accounts) {
83-
System.out.println(" Account: " + account.accountId());
84+
logger.info(" Account: " + account.accountId());
8485
ResourceState resources = account.resourceState();
8586
if (resources == null) {
8687
System.out.println(" No resource state data available.");
8788
continue;
8889
}
8990

90-
System.out.println(" Resource States:");
91+
logger.info(" Resource States:");
9192
printState("EC2", resources.ec2());
9293
printState("ECR", resources.ecr());
9394
printState("Lambda", resources.lambda());
@@ -102,45 +103,79 @@ public static void checkAccountStatus(Inspector2Client inspectorClient) {
102103

103104
public static void printState(String name, State state) {
104105
if (state == null) {
105-
System.out.println(" - " + name + ": (no data)");
106+
logger.info(" - {} : (no data)", name);
106107
return;
107108
}
108109
String err = state.errorMessage() != null ? " (Error: " + state.errorMessage() + ")" : "";
109-
System.out.println(" - " + name + ": " + state.status() + err);
110+
logger.info(" - {}: {}{}", name, state.status(), err);
110111
}
111112

112113
/**
113-
* Retrieves and prints the most recent findings from the Inspector2 service.
114+
* Lists recent findings from AWS Inspector2 using the synchronous client with a paginator.
114115
*
115-
* @param inspectorClient the Inspector2Client used to interact with the AWS Inspector2 service
116+
* @param inspectorClient an instance of {@link Inspector2Client} used to call AWS Inspector2
117+
* @throws Inspector2Exception if there is an error communicating with the Inspector2 service
118+
*/
119+
/**
120+
* Lists up to 10 recent findings from AWS Inspector2 using the synchronous client.
121+
*
122+
* <p>This method retrieves findings in pages and logs details for each finding,
123+
* including title, severity, status, and last observed time. Only the first
124+
* 10 findings are logged, even if more exist.
125+
*
126+
* @param inspectorClient an instance of {@link Inspector2Client} used to call AWS Inspector2
127+
* @throws Inspector2Exception if there is an error communicating with the Inspector2 service
116128
*/
117129
public static void listRecentFindings(Inspector2Client inspectorClient) {
130+
final int MAX_FINDINGS = 10;
131+
int totalLogged = 0;
132+
118133
try {
134+
// Build initial request with page size
119135
ListFindingsRequest request = ListFindingsRequest.builder()
120-
.maxResults(10)
136+
.maxResults(MAX_FINDINGS)
121137
.build();
122138

123-
ListFindingsResponse response = inspectorClient.listFindings(request);
124-
List<Finding> findings = response.findings();
139+
// Paginator returns an iterable over responses
140+
ListFindingsIterable responses = inspectorClient.listFindingsPaginator(request);
141+
142+
for (ListFindingsResponse response : responses) {
143+
List<Finding> findings = response.findings();
144+
if (findings == null || findings.isEmpty()) {
145+
continue;
146+
}
125147

126-
if (findings == null || findings.isEmpty()) {
127-
System.out.println(" No findings found.");
128-
} else {
129-
System.out.println(" Found " + findings.size() + " recent finding(s):");
130148
for (Finding finding : findings) {
131-
System.out.println(" Title: " + finding.title());
132-
System.out.println(" Severity: " + finding.severity());
133-
System.out.println(" Status: " + finding.status());
134-
System.out.println(" Last Observed: " + finding.lastObservedAt());
135-
System.out.println();
149+
if (totalLogged >= MAX_FINDINGS) {
150+
break;
151+
}
152+
153+
logger.info(" Title: {}", finding.title());
154+
logger.info(" Severity: {}", finding.severity());
155+
logger.info(" Status: {}", finding.status());
156+
logger.info(" Last Observed: {}", finding.lastObservedAt());
157+
logger.info("");
158+
159+
totalLogged++;
160+
}
161+
162+
if (totalLogged >= MAX_FINDINGS) {
163+
break;
136164
}
137165
}
138166

167+
if (totalLogged == 0) {
168+
logger.info(" No findings found.");
169+
} else {
170+
logger.info(" Displayed {} recent finding(s).", totalLogged);
171+
}
172+
139173
} catch (Inspector2Exception e) {
140-
System.err.println(" Error listing findings: " + e.awsErrorDetails().errorMessage());
174+
logger.info(" Error listing findings: {}", e.awsErrorDetails().errorMessage());
141175
}
142176
}
143177

178+
144179
/**
145180
* Displays the usage totals for the Inspector2 service.
146181
*
@@ -152,7 +187,7 @@ public static void listRecentFindings(Inspector2Client inspectorClient) {
152187
*/
153188
public static void showUsageTotals(Inspector2Client inspectorClient) {
154189
try {
155-
System.out.println("Listing usage totals using paginator...");
190+
logger.info("Listing usage totals using paginator...");
156191
ListUsageTotalsRequest request = ListUsageTotalsRequest.builder()
157192
.maxResults(10)
158193
.build();
@@ -171,27 +206,27 @@ public static void showUsageTotals(Inspector2Client inspectorClient) {
171206

172207
// Display results.
173208
if (allTotals.isEmpty()) {
174-
System.out.println(" No usage data available yet.");
175-
System.out.println(" Usage data appears after Inspector has been active for some time.");
209+
logger.info(" No usage data available yet.");
210+
logger.info(" Usage data appears after Inspector has been active for some time.");
176211
} else {
177-
System.out.println(" Usage Totals (Last 30 days):");
212+
logger.info(" Usage Totals (Last 30 days):");
178213
for (UsageTotal total : allTotals) {
179-
System.out.println(" Account: " + total.accountId());
214+
logger.info(" Account: {}" , total.accountId());
180215
if (total.usage() != null && !total.usage().isEmpty()) {
181216
total.usage().forEach(u -> {
182-
System.out.println(" - " + u.type() + ": " + u.total());
217+
logger.info(" - {}: {}", u.type(), u.total());
218+
183219
if (u.estimatedMonthlyCost() != null) {
184-
System.out.println(" Estimated Monthly Cost: " +
185-
u.estimatedMonthlyCost() + " " + u.currency());
220+
logger.info(" Estimated Monthly Cost: {} {}", u.estimatedMonthlyCost(), u.currency());
186221
}
187222
});
188223
}
189-
System.out.println();
224+
logger.info("");
190225
}
191226
}
192227

193228
} catch (Inspector2Exception e) {
194-
System.err.println(" Error getting usage totals: " + e.awsErrorDetails().errorMessage());
229+
logger.info(" Error getting usage totals: {}" , e.awsErrorDetails().errorMessage());
195230
throw e;
196231
} catch (Exception e) {
197232
throw new RuntimeException("Unexpected error while listing usage totals: " + e.getMessage(), e);

javav2/example_code/inspector/src/main/java/com/java/inspector/InspectorActions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,12 @@ public CompletableFuture<String> createLowSeverityFilterAsync(
557557
);
558558
}
559559
})
560-
// Extract and return the ARN of the created filter
560+
// Extract and return the ARN of the created filter.
561561
.thenApply(response -> response.arn());
562562
}
563563
// snippet-end:[inspector.java2.create.filter.main]
564564

565565
// snippet-start:[inspector.java2.list_findings.main]
566-
567566
/**
568567
* Fetches Inspector2 findings asynchronously using a paginator and returns
569568
* a summary string when complete.

0 commit comments

Comments
 (0)