55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 *
8- * http ://www.apache.org/licenses/LICENSE-2.0
8+ * https ://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
1515 */
1616package com .google .cloud .examples .securitycenter .snippets ;
1717
18- import com .google .cloud .securitycenter .v1beta1 .ListAssetsRequest ;
19- import com .google .cloud .securitycenter .v1beta1 .ListAssetsResponse .ListAssetsResult ;
20- import com .google .cloud .securitycenter .v1beta1 .OrganizationName ;
21- import com .google .cloud .securitycenter .v1beta1 .SecurityCenterClient ;
22- import com .google .cloud .securitycenter .v1beta1 .SecurityCenterClient .ListAssetsPagedResponse ;
18+ import com .google .api .gax .longrunning .OperationFuture ;
19+ import com .google .api .gax .rpc .ResourceExhaustedException ;
20+ import com .google .cloud .securitycenter .v1 .GroupAssetsRequest ;
21+ import com .google .cloud .securitycenter .v1 .GroupResult ;
22+ import com .google .cloud .securitycenter .v1 .ListAssetsRequest ;
23+ import com .google .cloud .securitycenter .v1 .ListAssetsResponse .ListAssetsResult ;
24+ import com .google .cloud .securitycenter .v1 .OrganizationName ;
25+ import com .google .cloud .securitycenter .v1 .SecurityCenterClient ;
26+ import com .google .cloud .securitycenter .v1 .SecurityCenterClient .GroupAssetsPagedResponse ;
27+ import com .google .cloud .securitycenter .v1 .SecurityCenterClient .ListAssetsPagedResponse ;
2328import com .google .common .base .MoreObjects ;
2429import com .google .common .base .Preconditions ;
2530import com .google .common .collect .ImmutableList ;
31+ import com .google .protobuf .Empty ;
2632import java .io .IOException ;
2733import org .threeten .bp .Duration ;
2834import org .threeten .bp .Instant ;
@@ -40,15 +46,15 @@ private AssetSnippets() {}
4046 static ImmutableList <ListAssetsResult > listAssets (OrganizationName organizationName ) {
4147 try (SecurityCenterClient client = SecurityCenterClient .create ()) {
4248 // Start setting up a request for to search for all assets in an organization.
43- // OrganizationName organizationName = OrganizationName.of("123234324");
49+ // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/ "123234324");
4450 ListAssetsRequest .Builder request =
4551 ListAssetsRequest .newBuilder ().setParent (organizationName .toString ());
4652
4753 // Call the API.
4854 ListAssetsPagedResponse response = client .listAssets (request .build ());
4955
5056 // This creates one list for all assets. If your organization has a large number of assets
51- // this can cause out of memory issues. You can process them batches by returning
57+ // this can cause out of memory issues. You can process them incrementally by returning
5258 // the Iterable returned response.iterateAll() directly.
5359 ImmutableList <ListAssetsResult > results = ImmutableList .copyOf (response .iterateAll ());
5460 System .out .println ("All assets:" );
@@ -69,22 +75,21 @@ static ImmutableList<ListAssetsResult> listAssets(OrganizationName organizationN
6975 static ImmutableList <ListAssetsResult > listAssetsWithFilter (OrganizationName organizationName ) {
7076 try (SecurityCenterClient client = SecurityCenterClient .create ()) {
7177 // Start setting up a request for to search for all assets in an organization.
72- // OrganizationName organizationName = OrganizationName.of("123234324");
73- ListAssetsRequest request =
78+ // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/ "123234324");
79+ ListAssetsRequest . Builder request =
7480 ListAssetsRequest .newBuilder ()
7581 .setParent (organizationName .toString ())
7682 .setFilter (
77- "security_center_properties.resource_type=\" google.cloud.resourcemanager.Project\" " )
78- .build ();
83+ "security_center_properties.resource_type=\" google.cloud.resourcemanager.Project\" " );
7984
8085 // Call the API.
81- ListAssetsPagedResponse response = client .listAssets (request );
86+ ListAssetsPagedResponse response = client .listAssets (request . build () );
8287
8388 // This creates one list for all assets. If your organization has a large number of assets
84- // this can cause out of memory issues. You can process them batches by returning
89+ // this can cause out of memory issues. You can process them incrementally by returning
8590 // the Iterable returned response.iterateAll() directly.
8691 ImmutableList <ListAssetsResult > results = ImmutableList .copyOf (response .iterateAll ());
87- System .out .println ("Projects :" );
92+ System .out .println ("Project assets :" );
8893 System .out .println (results );
8994 return results ;
9095 } catch (IOException e ) {
@@ -104,7 +109,7 @@ static ImmutableList<ListAssetsResult> listAssetsAsOfYesterday(
104109 OrganizationName organizationName , Instant asOf ) {
105110 try (SecurityCenterClient client = SecurityCenterClient .create ()) {
106111 // Start setting up a request for to search for all assets in an organization.
107- // OrganizationName organizationName = OrganizationName.of("123234324");
112+ // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/ "123234324");
108113
109114 // Initialize the builder with the organization and filter
110115 ListAssetsRequest .Builder request =
@@ -121,7 +126,7 @@ static ImmutableList<ListAssetsResult> listAssetsAsOfYesterday(
121126 ListAssetsPagedResponse response = client .listAssets (request .build ());
122127
123128 // This creates one list for all assets. If your organization has a large number of assets
124- // this can cause out of memory issues. You can process them batches by returning
129+ // this can cause out of memory issues. You can process them incrementally by returning
125130 // the Iterable returned response.iterateAll() directly.
126131 ImmutableList <ListAssetsResult > results = ImmutableList .copyOf (response .iterateAll ());
127132 System .out .println ("Projects:" );
@@ -146,7 +151,7 @@ static ImmutableList<ListAssetsResult> listAssetAndStatusChanges(
146151 try (SecurityCenterClient client = SecurityCenterClient .create ()) {
147152
148153 // Start setting up a request for to search for all assets in an organization.
149- // OrganizationName organizationName = OrganizationName.of("123234324");
154+ // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/ "123234324");
150155 ListAssetsRequest .Builder request =
151156 ListAssetsRequest .newBuilder ()
152157 .setParent (organizationName .toString ())
@@ -165,7 +170,7 @@ static ImmutableList<ListAssetsResult> listAssetAndStatusChanges(
165170 ListAssetsPagedResponse response = client .listAssets (request .build ());
166171
167172 // This creates one list for all assets. If your organization has a large number of assets
168- // this can cause out of memory issues. You can process them batches by returning
173+ // this can cause out of memory issues. You can process them incrementally by returning
169174 // the Iterable returned response.iterateAll() directly.
170175 ImmutableList <ListAssetsResult > results = ImmutableList .copyOf (response .iterateAll ());
171176 System .out .println ("Projects:" );
@@ -177,6 +182,129 @@ static ImmutableList<ListAssetsResult> listAssetAndStatusChanges(
177182 }
178183 // [END list_asset_changes_status_changes]
179184
185+ /**
186+ * Groups all assets by their specified properties (e.g. type) for an organization.
187+ *
188+ * @param organizationName The organization to group assets for.
189+ */
190+ // [START group_all_assets]
191+ static ImmutableList <GroupResult > groupAssets (OrganizationName organizationName ) {
192+ try (SecurityCenterClient client = SecurityCenterClient .create ()) {
193+ // Start setting up a request for to group all assets by type in an organization.
194+ // OrganizationName organizationName = OrganizationName.of("123234324");
195+ GroupAssetsRequest .Builder request =
196+ GroupAssetsRequest .newBuilder ()
197+ .setGroupBy ("security_center_properties.resource_type" )
198+ .setParent (organizationName .toString ());
199+
200+ // Call the API.
201+ GroupAssetsPagedResponse response = client .groupAssets (request .build ());
202+
203+ // This creates one list for all assets. If your organization has a large number of assets
204+ // this can cause out of memory issues. You can process them batches by returning
205+ // the Iterable returned response.iterateAll() directly.
206+ ImmutableList <GroupResult > results = ImmutableList .copyOf (response .iterateAll ());
207+ System .out .println ("All assets:" );
208+ System .out .println (results );
209+ return results ;
210+ } catch (IOException e ) {
211+ throw new RuntimeException ("Couldn't create client." , e );
212+ }
213+ }
214+ // [END group_all_assets]
215+
216+ /**
217+ * Filters all assets by their specified properties and groups them by specified properties for an
218+ * organization.
219+ *
220+ * @param organizationName The organization to group assets for.
221+ */
222+ // [START group_all_assets_with_filter]
223+ static ImmutableList <GroupResult > groupAssetsWithFilter (OrganizationName organizationName ) {
224+ try (SecurityCenterClient client = SecurityCenterClient .create ()) {
225+ // Start setting up a request for to filter all assets by type and group them by project in an
226+ // organization.
227+ // OrganizationName organizationName = OrganizationName.of("123234324");
228+ GroupAssetsRequest .Builder request =
229+ GroupAssetsRequest .newBuilder ()
230+ .setFilter (
231+ "security_center_properties.resource_type=\" google.cloud.resourcemanager.Project\" " )
232+ .setGroupBy ("security_center_properties.resource_project" )
233+ .setParent (organizationName .toString ());
234+
235+ // Call the API.
236+ GroupAssetsPagedResponse response = client .groupAssets (request .build ());
237+
238+ // This creates one list for all assets. If your organization has a large number of assets
239+ // this can cause out of memory issues. You can process them batches by returning
240+ // the Iterable returned response.iterateAll() directly.
241+ ImmutableList <GroupResult > results = ImmutableList .copyOf (response .iterateAll ());
242+ System .out .println ("All assets:" );
243+ System .out .println (results );
244+ return results ;
245+ } catch (IOException e ) {
246+ throw new RuntimeException ("Couldn't create client." , e );
247+ }
248+ }
249+ // [END group_all_assets_with_filter]
250+
251+ /**
252+ * Groups all assets by their state_changes (ADDED/DELETED/ACTIVE) during a period of time for an
253+ * organization.
254+ *
255+ * @param organizationName The organization to group assets for.
256+ */
257+ // [START group_all_assets_with_compare_duration]
258+ static ImmutableList <GroupResult > groupAssetsWithCompareDuration (
259+ OrganizationName organizationName , Duration duration ) {
260+ try (SecurityCenterClient client = SecurityCenterClient .create ()) {
261+ // Start setting up a request for to group all assets during a period of time in an
262+ // organization.
263+ // OrganizationName organizationName = OrganizationName.of("123234324");
264+ GroupAssetsRequest .Builder request =
265+ GroupAssetsRequest .newBuilder ()
266+ .setGroupBy ("state_change" )
267+ .setParent (organizationName .toString ());
268+ request
269+ .getCompareDurationBuilder ()
270+ .setSeconds (duration .getSeconds ())
271+ .setNanos (duration .getNano ());
272+
273+ // Call the API.
274+ GroupAssetsPagedResponse response = client .groupAssets (request .build ());
275+
276+ // This creates one list for all assets. If your organization has a large number of assets
277+ // this can cause out of memory issues. You can process them batches by returning
278+ // the Iterable returned response.iterateAll() directly.
279+ ImmutableList <GroupResult > results = ImmutableList .copyOf (response .iterateAll ());
280+ System .out .println ("All assets:" );
281+ System .out .println (results );
282+ return results ;
283+ } catch (IOException e ) {
284+ throw new RuntimeException ("Couldn't create client." , e );
285+ }
286+ }
287+ // [END group_all_assets_with_compare_duration]
288+
289+ // [START run_asset_discovery]
290+ static void runAssetDiscovery (OrganizationName organizationName ) {
291+ try (SecurityCenterClient client = SecurityCenterClient .create ()) {
292+ // Call the API. Note calls to runAssetDiscovery are throttled if too many requests
293+ // are made.
294+ OperationFuture <Empty , Empty > result = client .runAssetDiscoveryAsync (organizationName );
295+
296+ // Uncomment this line to wait for a certain amount of time for the asset discovery run
297+ // to complete.
298+ // result.get(130, TimeUnit.SECONDS);
299+ System .out .println ("Asset discovery runs asynchronously." );
300+ } catch (IOException e ) {
301+ throw new RuntimeException ("Couldn't create client." , e );
302+ } catch (ResourceExhaustedException e ) {
303+ System .out .println ("Asset discovery run already in progress." );
304+ }
305+ }
306+ // [END run_asset_discovery]
307+
180308 public static void main (String ... args ) {
181309 String org_id = System .getenv ("ORGANIZATION_ID" );
182310 if (args .length > 0 ) {
0 commit comments