Skip to content

Commit e3a7f87

Browse files
committed
refactor: replace paged response types with ImmutableList in Security Command Center management snippets
1 parent b32aca8 commit e3a7f87

10 files changed

Lines changed: 55 additions & 66 deletions

security-command-center/snippets/src/main/java/management/api/ListDescendantEventThreatDetectionCustomModules.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
package management.api;
1818

1919
// [START securitycenter_list_descendant_event_threat_detection_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.EventThreatDetectionCustomModule;
2021
import com.google.cloud.securitycentermanagement.v1.ListDescendantEventThreatDetectionCustomModulesRequest;
2122
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListDescendantEventThreatDetectionCustomModulesPagedResponse;
23+
import com.google.common.collect.ImmutableList;
2324
import java.io.IOException;
2425

2526
public class ListDescendantEventThreatDetectionCustomModules {
@@ -31,7 +32,7 @@ public static void main(String[] args) throws IOException {
3132
listDescendantEventThreatDetectionCustomModules(projectId);
3233
}
3334

34-
public static ListDescendantEventThreatDetectionCustomModulesPagedResponse
35+
public static ImmutableList<EventThreatDetectionCustomModule>
3536
listDescendantEventThreatDetectionCustomModules(String projectId) throws IOException {
3637

3738
// Initialize client that will be used to send requests. This client only needs
@@ -46,10 +47,8 @@ public static void main(String[] args) throws IOException {
4647
.setParent(parent)
4748
.build();
4849

49-
ListDescendantEventThreatDetectionCustomModulesPagedResponse response =
50-
client.listDescendantEventThreatDetectionCustomModules(request);
51-
52-
return response;
50+
return ImmutableList.copyOf(
51+
client.listDescendantEventThreatDetectionCustomModules(request).iterateAll());
5352
}
5453
}
5554
}

security-command-center/snippets/src/main/java/management/api/ListDescendantSecurityHealthAnalyticsCustomModules.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
// [START securitycenter_list_descendant_security_health_analytics_custom_module]
2020
import com.google.cloud.securitycentermanagement.v1.ListDescendantSecurityHealthAnalyticsCustomModulesRequest;
2121
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListDescendantSecurityHealthAnalyticsCustomModulesPagedResponse;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityHealthAnalyticsCustomModule;
23+
import com.google.common.collect.ImmutableList;
2324
import java.io.IOException;
2425

2526
public class ListDescendantSecurityHealthAnalyticsCustomModules {
@@ -32,7 +33,7 @@ public static void main(String[] args) throws IOException {
3233
listDescendantSecurityHealthAnalyticsCustomModules(projectId);
3334
}
3435

35-
public static ListDescendantSecurityHealthAnalyticsCustomModulesPagedResponse
36+
public static ImmutableList<SecurityHealthAnalyticsCustomModule>
3637
listDescendantSecurityHealthAnalyticsCustomModules(String projectId) throws IOException {
3738

3839
// Initialize client that will be used to send requests. This client only needs
@@ -45,10 +46,8 @@ public static void main(String[] args) throws IOException {
4546
.setParent(String.format("projects/%s/locations/global", projectId))
4647
.build();
4748

48-
ListDescendantSecurityHealthAnalyticsCustomModulesPagedResponse response =
49-
client.listDescendantSecurityHealthAnalyticsCustomModules(request);
50-
51-
return response;
49+
return ImmutableList.copyOf(
50+
client.listDescendantSecurityHealthAnalyticsCustomModules(request).iterateAll());
5251
}
5352
}
5453
}

security-command-center/snippets/src/main/java/management/api/ListEffectiveEventThreatDetectionCustomModules.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
package management.api;
1818

1919
// [START securitycenter_list_effective_event_threat_detection_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.EffectiveEventThreatDetectionCustomModule;
2021
import com.google.cloud.securitycentermanagement.v1.ListEffectiveEventThreatDetectionCustomModulesRequest;
2122
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListEffectiveEventThreatDetectionCustomModulesPagedResponse;
23+
import com.google.common.collect.ImmutableList;
2324
import java.io.IOException;
2425

2526
public class ListEffectiveEventThreatDetectionCustomModules {
@@ -31,7 +32,7 @@ public static void main(String[] args) throws IOException {
3132
listEffectiveEventThreatDetectionCustomModules(projectId);
3233
}
3334

34-
public static ListEffectiveEventThreatDetectionCustomModulesPagedResponse
35+
public static ImmutableList<EffectiveEventThreatDetectionCustomModule>
3536
listEffectiveEventThreatDetectionCustomModules(String projectId) throws IOException {
3637

3738
// Initialize client that will be used to send requests. This client only needs
@@ -46,10 +47,8 @@ public static void main(String[] args) throws IOException {
4647
.setParent(parent)
4748
.build();
4849

49-
ListEffectiveEventThreatDetectionCustomModulesPagedResponse response =
50-
client.listEffectiveEventThreatDetectionCustomModules(request);
51-
52-
return response;
50+
return ImmutableList.copyOf(
51+
client.listEffectiveEventThreatDetectionCustomModules(request).iterateAll());
5352
}
5453
}
5554
}

security-command-center/snippets/src/main/java/management/api/ListEffectiveSecurityHealthAnalyticsCustomModules.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
package management.api;
1818

1919
// [START securitycenter_list_effective_security_health_analytics_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.EffectiveSecurityHealthAnalyticsCustomModule;
2021
import com.google.cloud.securitycentermanagement.v1.ListEffectiveSecurityHealthAnalyticsCustomModulesRequest;
2122
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListEffectiveSecurityHealthAnalyticsCustomModulesPagedResponse;
23+
import com.google.common.collect.ImmutableList;
2324
import java.io.IOException;
2425

2526
public class ListEffectiveSecurityHealthAnalyticsCustomModules {
@@ -32,7 +33,7 @@ public static void main(String[] args) throws IOException {
3233
listEffectiveSecurityHealthAnalyticsCustomModules(projectId);
3334
}
3435

35-
public static ListEffectiveSecurityHealthAnalyticsCustomModulesPagedResponse
36+
public static ImmutableList<EffectiveSecurityHealthAnalyticsCustomModule>
3637
listEffectiveSecurityHealthAnalyticsCustomModules(String projectId) throws IOException {
3738

3839
// Initialize client that will be used to send requests. This client only needs
@@ -45,10 +46,8 @@ public static void main(String[] args) throws IOException {
4546
.setParent(String.format("projects/%s/locations/global", projectId))
4647
.build();
4748

48-
ListEffectiveSecurityHealthAnalyticsCustomModulesPagedResponse response =
49-
client.listEffectiveSecurityHealthAnalyticsCustomModules(request);
50-
51-
return response;
49+
return ImmutableList.copyOf(
50+
client.listEffectiveSecurityHealthAnalyticsCustomModules(request).iterateAll());
5251
}
5352
}
5453
}

security-command-center/snippets/src/main/java/management/api/ListEventThreatDetectionCustomModules.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
package management.api;
1818

1919
// [START securitycenter_list_event_threat_detection_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.EventThreatDetectionCustomModule;
2021
import com.google.cloud.securitycentermanagement.v1.ListEventThreatDetectionCustomModulesRequest;
2122
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListEventThreatDetectionCustomModulesPagedResponse;
23+
import com.google.common.collect.ImmutableList;
2324
import java.io.IOException;
2425

2526
public class ListEventThreatDetectionCustomModules {
@@ -32,7 +33,7 @@ public static void main(String[] args) throws IOException {
3233
listEventThreatDetectionCustomModules(projectId);
3334
}
3435

35-
public static ListEventThreatDetectionCustomModulesPagedResponse
36+
public static ImmutableList<EventThreatDetectionCustomModule>
3637
listEventThreatDetectionCustomModules(String projectId) throws IOException {
3738

3839
// Initialize client that will be used to send requests. This client only needs
@@ -45,10 +46,8 @@ public static void main(String[] args) throws IOException {
4546
ListEventThreatDetectionCustomModulesRequest request =
4647
ListEventThreatDetectionCustomModulesRequest.newBuilder().setParent(parent).build();
4748

48-
ListEventThreatDetectionCustomModulesPagedResponse response =
49-
client.listEventThreatDetectionCustomModules(request);
50-
51-
return response;
49+
return ImmutableList.copyOf(
50+
client.listEventThreatDetectionCustomModules(request).iterateAll());
5251
}
5352
}
5453
}

security-command-center/snippets/src/main/java/management/api/ListSecurityCenterServices.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
// [START securitycenter_list_security_center_service]
2020
import com.google.cloud.securitycentermanagement.v1.ListSecurityCenterServicesRequest;
2121
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListSecurityCenterServicesPagedResponse;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterService;
23+
import com.google.common.collect.ImmutableList;
2324
import java.io.IOException;
2425

2526
public class ListSecurityCenterServices {
@@ -32,7 +33,7 @@ public static void main(String[] args) throws IOException {
3233
listSecurityCenterServices(projectId);
3334
}
3435

35-
public static ListSecurityCenterServicesPagedResponse listSecurityCenterServices(String projectId)
36+
public static ImmutableList<SecurityCenterService> listSecurityCenterServices(String projectId)
3637
throws IOException {
3738
// Initialize client that will be used to send requests. This client only needs
3839
// to be created
@@ -42,8 +43,7 @@ public static ListSecurityCenterServicesPagedResponse listSecurityCenterServices
4243
ListSecurityCenterServicesRequest.newBuilder()
4344
.setParent(String.format("projects/%s/locations/global", projectId))
4445
.build();
45-
ListSecurityCenterServicesPagedResponse response = client.listSecurityCenterServices(request);
46-
return response;
46+
return ImmutableList.copyOf(client.listSecurityCenterServices(request).iterateAll());
4747
}
4848
}
4949
}

security-command-center/snippets/src/main/java/management/api/ListSecurityHealthAnalyticsCustomModules.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
// [START securitycenter_list_security_health_analytics_custom_module]
2020
import com.google.cloud.securitycentermanagement.v1.ListSecurityHealthAnalyticsCustomModulesRequest;
2121
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListSecurityHealthAnalyticsCustomModulesPagedResponse;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityHealthAnalyticsCustomModule;
23+
import com.google.common.collect.ImmutableList;
2324
import java.io.IOException;
2425

2526
public class ListSecurityHealthAnalyticsCustomModules {
@@ -32,7 +33,7 @@ public static void main(String[] args) throws IOException {
3233
listSecurityHealthAnalyticsCustomModules(projectId);
3334
}
3435

35-
public static ListSecurityHealthAnalyticsCustomModulesPagedResponse
36+
public static ImmutableList<SecurityHealthAnalyticsCustomModule>
3637
listSecurityHealthAnalyticsCustomModules(String projectId) throws IOException {
3738
// Initialize client that will be used to send requests. This client only needs
3839
// to be created
@@ -44,10 +45,8 @@ public static void main(String[] args) throws IOException {
4445
.setParent(String.format("projects/%s/locations/global", projectId))
4546
.build();
4647

47-
ListSecurityHealthAnalyticsCustomModulesPagedResponse response =
48-
client.listSecurityHealthAnalyticsCustomModules(request);
49-
50-
return response;
48+
return ImmutableList.copyOf(
49+
client.listSecurityHealthAnalyticsCustomModules(request).iterateAll());
5150
}
5251
}
5352
}

security-command-center/snippets/src/test/java/management/api/EventThreatDetectionCustomModuleTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@
2424
import com.google.cloud.securitycentermanagement.v1.EffectiveEventThreatDetectionCustomModule;
2525
import com.google.cloud.securitycentermanagement.v1.EventThreatDetectionCustomModule;
2626
import com.google.cloud.securitycentermanagement.v1.EventThreatDetectionCustomModule.EnablementState;
27-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListDescendantEventThreatDetectionCustomModulesPagedResponse;
28-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListEffectiveEventThreatDetectionCustomModulesPagedResponse;
29-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListEventThreatDetectionCustomModulesPagedResponse;
3027
import com.google.cloud.securitycentermanagement.v1.ValidateEventThreatDetectionCustomModuleResponse;
3128
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
3229
import com.google.common.base.Strings;
30+
import com.google.common.collect.ImmutableList;
3331
import java.io.IOException;
3432
import java.util.ArrayList;
3533
import java.util.List;
@@ -70,10 +68,10 @@ public static void setUp() {
7068
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
7169
requireEnvVar("SCC_PROJECT_ID");
7270
try {
73-
ListEventThreatDetectionCustomModulesPagedResponse response =
71+
ImmutableList<EventThreatDetectionCustomModule> response =
7472
ListEventThreatDetectionCustomModules
7573
.listEventThreatDetectionCustomModules(PROJECT_ID);
76-
for (EventThreatDetectionCustomModule module : response.iterateAll()) {
74+
for (EventThreatDetectionCustomModule module : response) {
7775
if (module.getDisplayName() != null
7876
&& module.getDisplayName().startsWith("java_sample_etd_custom_module_test_")) {
7977
deleteCustomModule(PROJECT_ID, extractCustomModuleId(module.getName()));
@@ -148,10 +146,10 @@ public void testListEventThreatDetectionCustomModules() throws IOException {
148146
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
149147
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
150148
createdCustomModuleIds.add(extractCustomModuleId(createCustomModuleResponse.getName()));
151-
ListEventThreatDetectionCustomModulesPagedResponse response =
149+
ImmutableList<EventThreatDetectionCustomModule> response =
152150
ListEventThreatDetectionCustomModules.listEventThreatDetectionCustomModules(PROJECT_ID);
153151
assertTrue(
154-
StreamSupport.stream(response.iterateAll().spliterator(), false)
152+
response.stream()
155153
.anyMatch(module -> CUSTOM_MODULE_DISPLAY_NAME.equals(module.getDisplayName())));
156154
}
157155

@@ -207,11 +205,11 @@ public void testListEffectiveEventThreatDetectionCustomModules() throws IOExcept
207205
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
208206
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
209207
createdCustomModuleIds.add(extractCustomModuleId(createCustomModuleResponse.getName()));
210-
ListEffectiveEventThreatDetectionCustomModulesPagedResponse response =
208+
ImmutableList<EffectiveEventThreatDetectionCustomModule> response =
211209
ListEffectiveEventThreatDetectionCustomModules
212210
.listEffectiveEventThreatDetectionCustomModules(PROJECT_ID);
213211
assertTrue(
214-
StreamSupport.stream(response.iterateAll().spliterator(), false)
212+
response.stream()
215213
.anyMatch(module -> CUSTOM_MODULE_DISPLAY_NAME.equals(module.getDisplayName())));
216214
}
217215

@@ -221,11 +219,11 @@ public void testListDescendantEventThreatDetectionCustomModules() throws IOExcep
221219
CreateEventThreatDetectionCustomModule.createEventThreatDetectionCustomModule(
222220
PROJECT_ID, CUSTOM_MODULE_DISPLAY_NAME);
223221
createdCustomModuleIds.add(extractCustomModuleId(createCustomModuleResponse.getName()));
224-
ListDescendantEventThreatDetectionCustomModulesPagedResponse response =
222+
ImmutableList<EventThreatDetectionCustomModule> response =
225223
ListDescendantEventThreatDetectionCustomModules
226224
.listDescendantEventThreatDetectionCustomModules(PROJECT_ID);
227225
assertTrue(
228-
StreamSupport.stream(response.iterateAll().spliterator(), false)
226+
response.stream()
229227
.anyMatch(module -> CUSTOM_MODULE_DISPLAY_NAME.equals(module.getDisplayName())));
230228
}
231229

security-command-center/snippets/src/test/java/management/api/SecurityCenterServiceTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
import static org.junit.Assert.assertNotNull;
2222
import static org.junit.Assert.assertTrue;
2323

24-
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListSecurityCenterServicesPagedResponse;
2524
import com.google.cloud.securitycentermanagement.v1.SecurityCenterService;
2625
import com.google.cloud.securitycentermanagement.v1.SecurityCenterService.EnablementState;
26+
import com.google.common.collect.ImmutableList;
2727
import java.io.IOException;
28-
import java.util.stream.StreamSupport;
2928
import org.junit.BeforeClass;
3029
import org.junit.Test;
3130
import org.junit.runner.RunWith;
@@ -63,12 +62,12 @@ public void testGetSecurityCenterService() throws IOException {
6362

6463
@Test
6564
public void testListSecurityCenterServices() throws IOException {
66-
ListSecurityCenterServicesPagedResponse response =
65+
ImmutableList<SecurityCenterService> response =
6766
ListSecurityCenterServices.listSecurityCenterServices(PROJECT_ID);
6867
assertNotNull(response);
6968
// check whether the response contains the specified service
7069
assertTrue(
71-
StreamSupport.stream(response.iterateAll().spliterator(), false)
70+
response.stream()
7271
.anyMatch(service -> service.getName().contains(SERVICE)));
7372
}
7473

0 commit comments

Comments
 (0)