33
44package com .java .inspector ;
55
6- import software .amazon .awssdk .auth .credentials .ProfileCredentialsProvider ;
76import software .amazon .awssdk .regions .Region ;
87import software .amazon .awssdk .services .inspector2 .Inspector2Client ;
98import software .amazon .awssdk .services .inspector2 .model .BatchGetAccountStatusRequest ;
1817import software .amazon .awssdk .services .inspector2 .model .ListUsageTotalsResponse ;
1918import software .amazon .awssdk .services .inspector2 .model .UsageTotal ;
2019import software .amazon .awssdk .services .inspector2 .model .Inspector2Exception ;
21-
20+ import software . amazon . awssdk . services . inspector2 . paginators . ListUsageTotalsIterable ;
2221import java .util .List ;
22+ import java .util .ArrayList ;
2323
2424// snippet-start:[inspector.java2.hello.main]
25+ /**
26+ * Before running this Java V2 code example, set up your development
27+ * environment, including your credentials.
28+ *
29+ * For more information, see the following documentation topic:
30+ *
31+ * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
32+ */
2533public class HelloInspector {
2634
2735 public static void main (String [] args ) {
28- System .out .println ("🔍 Hello Amazon Inspector!" );
36+ System .out .println (" Hello Amazon Inspector!" );
2937 Region region = Region .US_EAST_1 ;
3038
3139 try (Inspector2Client inspectorClient = Inspector2Client .builder ()
3240 .region (region )
3341 .build ()) {
3442
35- System .out .println ("🔍 Checking Inspector account status..." );
43+ System .out .println ("Checking Inspector account status..." );
3644 checkAccountStatus (inspectorClient );
3745 System .out .println ();
3846
39- System .out .println ("📋 Checking for recent findings..." );
47+ System .out .println ("Checking for recent findings..." );
4048 listRecentFindings (inspectorClient );
4149 System .out .println ();
4250
43- System .out .println ("💰 Checking usage totals..." );
51+ System .out .println ("Checking usage totals..." );
4452 showUsageTotals (inspectorClient );
4553 System .out .println ();
4654
47- System .out .println ("🎉 Hello Inspector example completed successfully!" );
55+ System .out .println ("Hello Inspector example completed successfully!" );
4856
4957 } catch (Inspector2Exception e ) {
50- System .err .println ("❌ Error: " + e .getMessage ());
51- System .err .println ("🔧 Troubleshooting:" );
58+ System .err .println (" Error: " + e .getMessage ());
59+ System .err .println (" Troubleshooting:" );
5260 System .err .println ("1. Verify AWS credentials are configured" );
5361 System .err .println ("2. Check IAM permissions for Inspector2" );
5462 System .err .println ("3. Ensure Inspector2 is enabled in your account" );
5563 System .err .println ("4. Verify you're using a supported region" );
5664 }
5765 }
5866
59- // ✅ Fixed version using BatchGetAccountStatus
60- private static void checkAccountStatus (Inspector2Client inspectorClient ) {
67+ /**
68+ * Checks the account status using the provided Inspector2Client.
69+ * This method sends a request to retrieve the account status and prints the details of each account's resource states.
70+ *
71+ * @param inspectorClient The Inspector2Client used to interact with the AWS Inspector service.
72+ */
73+ public static void checkAccountStatus (Inspector2Client inspectorClient ) {
6174 try {
6275 BatchGetAccountStatusRequest request = BatchGetAccountStatusRequest .builder ().build ();
6376 BatchGetAccountStatusResponse response = inspectorClient .batchGetAccountStatus (request );
6477
6578 List <AccountState > accounts = response .accounts ();
6679 if (accounts == null || accounts .isEmpty ()) {
67- System .out .println ("❌ No account information returned." );
80+ System .out .println (" No account information returned." );
6881 return ;
6982 }
7083
7184 for (AccountState account : accounts ) {
72- System .out .println ("✅ Account: " + account .accountId ());
85+ System .out .println (" Account: " + account .accountId ());
7386 ResourceState resources = account .resourceState ();
7487 if (resources == null ) {
7588 System .out .println (" No resource state data available." );
@@ -85,11 +98,11 @@ private static void checkAccountStatus(Inspector2Client inspectorClient) {
8598 }
8699
87100 } catch (Inspector2Exception e ) {
88- System .err .println ("❌ Failed to retrieve account status: " + e .awsErrorDetails ().errorMessage ());
101+ System .err .println (" Failed to retrieve account status: " + e .awsErrorDetails ().errorMessage ());
89102 }
90103 }
91104
92- private static void printState (String name , State state ) {
105+ public static void printState (String name , State state ) {
93106 if (state == null ) {
94107 System .out .println (" - " + name + ": (no data)" );
95108 return ;
@@ -98,7 +111,12 @@ private static void printState(String name, State state) {
98111 System .out .println (" - " + name + ": " + state .status () + err );
99112 }
100113
101- private static void listRecentFindings (Inspector2Client inspectorClient ) {
114+ /**
115+ * Retrieves and prints the most recent findings from the Inspector2 service.
116+ *
117+ * @param inspectorClient the Inspector2Client used to interact with the AWS Inspector2 service
118+ */
119+ public static void listRecentFindings (Inspector2Client inspectorClient ) {
102120 try {
103121 ListFindingsRequest request = ListFindingsRequest .builder ()
104122 .maxResults (10 )
@@ -108,9 +126,9 @@ private static void listRecentFindings(Inspector2Client inspectorClient) {
108126 List <Finding > findings = response .findings ();
109127
110128 if (findings == null || findings .isEmpty ()) {
111- System .out .println ("📭 No findings found." );
129+ System .out .println (" No findings found." );
112130 } else {
113- System .out .println ("✅ Found " + findings .size () + " recent finding(s):" );
131+ System .out .println (" Found " + findings .size () + " recent finding(s):" );
114132 for (Finding finding : findings ) {
115133 System .out .println (" Title: " + finding .title ());
116134 System .out .println (" Severity: " + finding .severity ());
@@ -121,34 +139,64 @@ private static void listRecentFindings(Inspector2Client inspectorClient) {
121139 }
122140
123141 } catch (Inspector2Exception e ) {
124- System .err .println ("❌ Error listing findings: " + e .awsErrorDetails ().errorMessage ());
142+ System .err .println (" Error listing findings: " + e .awsErrorDetails ().errorMessage ());
125143 }
126144 }
127145
128- private static void showUsageTotals (Inspector2Client inspectorClient ) {
146+ /**
147+ * Displays the usage totals for the Inspector2 service.
148+ *
149+ * @param inspectorClient the {@code Inspector2Client} used to make the API call to
150+ * retrieve the usage totals.
151+ *
152+ * @throws Inspector2Exception if there is an error while retrieving the usage totals.
153+ * The error message is printed to the standard error output.
154+ */
155+ public static void showUsageTotals (Inspector2Client inspectorClient ) {
129156 try {
157+ System .out .println ("Listing usage totals using paginator..." );
130158 ListUsageTotalsRequest request = ListUsageTotalsRequest .builder ()
131159 .maxResults (10 )
132160 .build ();
133161
134- ListUsageTotalsResponse response = inspectorClient .listUsageTotals (request );
135- List <UsageTotal > totals = response .totals ();
162+ // Create paginator.
163+ ListUsageTotalsIterable paginator = inspectorClient .listUsageTotalsPaginator (request );
164+ List <UsageTotal > allTotals = new ArrayList <>();
165+
166+ // Iterate through all pages.
167+ for (ListUsageTotalsResponse response : paginator ) {
168+ List <UsageTotal > totals = response .totals ();
169+ if (totals != null && !totals .isEmpty ()) {
170+ allTotals .addAll (totals );
171+ }
172+ }
136173
137- if (totals == null || totals .isEmpty ()) {
138- System .out .println ("📊 No usage data available yet." );
174+ // Display results.
175+ if (allTotals .isEmpty ()) {
176+ System .out .println (" No usage data available yet." );
177+ System .out .println (" Usage data appears after Inspector has been active for some time." );
139178 } else {
140- System .out .println ("✅ Usage Totals (Last 30 days):" );
141- for (UsageTotal total : totals ) {
179+ System .out .println (" Usage Totals (Last 30 days):" );
180+ for (UsageTotal total : allTotals ) {
142181 System .out .println (" Account: " + total .accountId ());
143- total .usage ().forEach (u ->
144- System .out .println (" - " + u .type () + ": " + u .total () +
145- " (Est. Monthly: " + u .estimatedMonthlyCost () + " " + u .currency () + ")" ));
182+ if (total .usage () != null && !total .usage ().isEmpty ()) {
183+ total .usage ().forEach (u -> {
184+ System .out .println (" - " + u .type () + ": " + u .total ());
185+ if (u .estimatedMonthlyCost () != null ) {
186+ System .out .println (" Estimated Monthly Cost: " +
187+ u .estimatedMonthlyCost () + " " + u .currency ());
188+ }
189+ });
190+ }
146191 System .out .println ();
147192 }
148193 }
149194
150195 } catch (Inspector2Exception e ) {
151- System .err .println ("❌ Error getting usage totals: " + e .awsErrorDetails ().errorMessage ());
196+ System .err .println (" Error getting usage totals: " + e .awsErrorDetails ().errorMessage ());
197+ throw e ;
198+ } catch (Exception e ) {
199+ throw new RuntimeException ("Unexpected error while listing usage totals: " + e .getMessage (), e );
152200 }
153201 }
154202}
0 commit comments