Skip to content

Commit 4c3d2bf

Browse files
committed
feat: add support for application list pagination
Application list results are now paginated. Add properties and helper classes for accessing application list pages. Signed-off-by: Subin Shekhar <[email protected]>
1 parent 6aee320 commit 4c3d2bf

File tree

11 files changed

+446
-10
lines changed

11 files changed

+446
-10
lines changed

modules/examples/src/main/java/com/ibm/cloud/iaesdk/ibm_analytics_engine_api/v3/IbmAnalyticsEngineApiExamples.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313

1414
package com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3;
1515

16-
import com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.model.ApplicationCollection;
16+
import com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.model.Application;
1717
import com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.model.ApplicationGetResponse;
1818
import com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.model.ApplicationGetStateResponse;
1919
import com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.model.ApplicationRequestApplicationDetails;
2020
import com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.model.ApplicationResponse;
21+
import com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.model.ApplicationsPager;
2122
import com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.model.ConfigurePlatformLoggingOptions;
2223
import com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.model.CreateApplicationOptions;
2324
import com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.model.CurrentResourceConsumptionResponse;
@@ -53,6 +54,9 @@
5354
import com.ibm.cloud.sdk.core.http.Response;
5455
import com.ibm.cloud.sdk.core.service.exception.ServiceResponseException;
5556
import com.ibm.cloud.sdk.core.util.CredentialUtils;
57+
import com.ibm.cloud.sdk.core.util.GsonSingleton;
58+
import java.util.ArrayList;
59+
import java.util.List;
5660
import java.util.Map;
5761
import org.slf4j.Logger;
5862
import org.slf4j.LoggerFactory;
@@ -237,6 +241,7 @@ public static void main(String[] args) throws Exception {
237241
// begin-replace_instance_default_runtime
238242
ReplaceInstanceDefaultRuntimeOptions replaceInstanceDefaultRuntimeOptions = new ReplaceInstanceDefaultRuntimeOptions.Builder()
239243
.instanceId("e64c907a-e82f-46fd-addc-ccfafbd28b09")
244+
.sparkVersion("3.3")
240245
.build();
241246

242247
Response<Runtime> response = ibmAnalyticsEngineApiService.replaceInstanceDefaultRuntime(replaceInstanceDefaultRuntimeOptions).execute();
@@ -280,12 +285,21 @@ public static void main(String[] args) throws Exception {
280285
// begin-list_applications
281286
ListApplicationsOptions listApplicationsOptions = new ListApplicationsOptions.Builder()
282287
.instanceId("e64c907a-e82f-46fd-addc-ccfafbd28b09")
288+
.addState("accepted")
289+
.addState("running")
290+
.addState("finished")
291+
.addState("failed")
292+
.limit(Long.valueOf("10"))
283293
.build();
284294

285-
Response<ApplicationCollection> response = ibmAnalyticsEngineApiService.listApplications(listApplicationsOptions).execute();
286-
ApplicationCollection applicationCollection = response.getResult();
295+
ApplicationsPager pager = new ApplicationsPager(ibmAnalyticsEngineApiService, listApplicationsOptions);
296+
List<Application> allResults = new ArrayList<>();
297+
while (pager.hasNext()) {
298+
List<Application> nextPage = pager.getNext();
299+
allResults.addAll(nextPage);
300+
}
287301

288-
System.out.println(applicationCollection);
302+
System.out.println(GsonSingleton.getGson().toJson(allResults));
289303
// end-list_applications
290304
} catch (ServiceResponseException e) {
291305
logger.error(String.format("Service returned status code %s: %s%nError details: %s",
@@ -367,6 +381,7 @@ public static void main(String[] args) throws Exception {
367381
// begin-replace_log_forwarding_config
368382
ReplaceLogForwardingConfigOptions replaceLogForwardingConfigOptions = new ReplaceLogForwardingConfigOptions.Builder()
369383
.instanceId("e64c907a-e82f-46fd-addc-ccfafbd28b09")
384+
.enabled(true)
370385
.build();
371386

372387
Response<LogForwardingConfigResponse> response = ibmAnalyticsEngineApiService.replaceLogForwardingConfig(replaceLogForwardingConfigOptions).execute();
@@ -401,6 +416,7 @@ public static void main(String[] args) throws Exception {
401416
// begin-configure_platform_logging
402417
ConfigurePlatformLoggingOptions configurePlatformLoggingOptions = new ConfigurePlatformLoggingOptions.Builder()
403418
.instanceGuid("e64c907a-e82f-46fd-addc-ccfafbd28b09")
419+
.enable(true)
404420
.build();
405421

406422
Response<LoggingConfigurationResponse> response = ibmAnalyticsEngineApiService.configurePlatformLogging(configurePlatformLoggingOptions).execute();

modules/ibm-analytics-engine-api/src/main/java/com/ibm/cloud/iaesdk/ibm_analytics_engine_api/v3/IbmAnalyticsEngineApi.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,12 @@ public ServiceCall<ApplicationCollection> listApplications(ListApplicationsOptio
452452
if (listApplicationsOptions.state() != null) {
453453
builder.query("state", RequestUtils.join(listApplicationsOptions.state(), ","));
454454
}
455+
if (listApplicationsOptions.limit() != null) {
456+
builder.query("limit", String.valueOf(listApplicationsOptions.limit()));
457+
}
458+
if (listApplicationsOptions.start() != null) {
459+
builder.query("start", String.valueOf(listApplicationsOptions.start()));
460+
}
455461
ResponseConverter<ApplicationCollection> responseConverter =
456462
ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<ApplicationCollection>() { }.getType());
457463
return createServiceCall(builder.build(), responseConverter);

modules/ibm-analytics-engine-api/src/main/java/com/ibm/cloud/iaesdk/ibm_analytics_engine_api/v3/model/ApplicationCollection.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
import com.ibm.cloud.sdk.core.service.model.GenericModel;
1818

1919
/**
20-
* An array of application details.
20+
* A paginated collection of applications.
2121
*/
2222
public class ApplicationCollection extends GenericModel {
2323

2424
protected List<Application> applications;
25+
protected PageLink first;
26+
protected PageLink next;
27+
protected PageLink previous;
28+
protected Long limit;
2529

2630
protected ApplicationCollection() { }
2731

@@ -35,5 +39,49 @@ protected ApplicationCollection() { }
3539
public List<Application> getApplications() {
3640
return applications;
3741
}
42+
43+
/**
44+
* Gets the first.
45+
*
46+
* A reference to a page in a paginated collection.
47+
*
48+
* @return the first
49+
*/
50+
public PageLink getFirst() {
51+
return first;
52+
}
53+
54+
/**
55+
* Gets the next.
56+
*
57+
* A reference to a page in a paginated collection.
58+
*
59+
* @return the next
60+
*/
61+
public PageLink getNext() {
62+
return next;
63+
}
64+
65+
/**
66+
* Gets the previous.
67+
*
68+
* A reference to a page in a paginated collection.
69+
*
70+
* @return the previous
71+
*/
72+
public PageLink getPrevious() {
73+
return previous;
74+
}
75+
76+
/**
77+
* Gets the limit.
78+
*
79+
* The maximum number of results in this page of the collection.
80+
*
81+
* @return the limit
82+
*/
83+
public Long getLimit() {
84+
return limit;
85+
}
3886
}
3987

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* (C) Copyright IBM Corp. 2023.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.model;
14+
15+
import com.ibm.cloud.iaesdk.ibm_analytics_engine_api.v3.IbmAnalyticsEngineApi;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.NoSuchElementException;
19+
20+
/**
21+
* ApplicationsPager can be used to simplify the use of the "listApplications" method.
22+
*/
23+
public class ApplicationsPager {
24+
private static class PageContext {
25+
private String next;
26+
public String getNext() {
27+
return next;
28+
}
29+
public void setNext(String next) {
30+
this.next = next;
31+
}
32+
}
33+
34+
protected boolean hasNext;
35+
protected ListApplicationsOptions options;
36+
protected IbmAnalyticsEngineApi client;
37+
protected PageContext pageContext;
38+
39+
// Hide the default ctor.
40+
protected ApplicationsPager() { }
41+
42+
/**
43+
* Constructs a new ApplicationsPager instance with the specified client and options model instance.
44+
* @param client the IbmAnalyticsEngineApi instance to be used to invoke the "listApplications" method
45+
* @param options the ListApplicationsOptions instance to be used to invoke the "listApplications" method
46+
*/
47+
public ApplicationsPager(IbmAnalyticsEngineApi client, ListApplicationsOptions options) {
48+
if (options.start() != null) {
49+
throw new IllegalArgumentException("The options 'start' field should not be set");
50+
}
51+
52+
this.hasNext = true;
53+
this.client = client;
54+
this.options = options.newBuilder().build();
55+
this.pageContext = new PageContext();
56+
}
57+
58+
/**
59+
* Returns true if there are more results to be retrieved.
60+
* @return boolean
61+
*/
62+
public boolean hasNext() {
63+
return hasNext;
64+
}
65+
66+
/**
67+
* Returns the next page of results.
68+
* @return a List&lt;Application&gt; that contains the next page of results
69+
*/
70+
public List<Application> getNext() {
71+
if (!hasNext()) {
72+
throw new NoSuchElementException("No more results available");
73+
}
74+
75+
ListApplicationsOptions.Builder builder = this.options.newBuilder();
76+
if (this.pageContext.getNext() != null) {
77+
builder.start(this.pageContext.getNext());
78+
}
79+
this.options = builder.build();
80+
81+
ApplicationCollection result = client.listApplications(options).execute().getResult();
82+
83+
String next = null;
84+
if (result.getNext() != null) {
85+
next = result.getNext().getStart();
86+
}
87+
this.pageContext.setNext(next);
88+
if (next == null) {
89+
this.hasNext = false;
90+
}
91+
92+
return result.getApplications();
93+
}
94+
95+
/**
96+
* Returns all results by invoking getNext() repeatedly until all pages of results have been retrieved.
97+
* @return a List&lt;Application&gt; containing all results returned by the "listApplications" method
98+
*/
99+
public List<Application> getAll() {
100+
List<Application> results = new ArrayList<>();
101+
while (hasNext()) {
102+
List<Application> nextPage = getNext();
103+
results.addAll(nextPage);
104+
}
105+
return results;
106+
}
107+
}

modules/ibm-analytics-engine-api/src/main/java/com/ibm/cloud/iaesdk/ibm_analytics_engine_api/v3/model/ListApplicationsOptions.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ public interface State {
4444

4545
protected String instanceId;
4646
protected List<String> state;
47+
protected Long limit;
48+
protected String start;
4749

4850
/**
4951
* Builder.
5052
*/
5153
public static class Builder {
5254
private String instanceId;
5355
private List<String> state;
56+
private Long limit;
57+
private String start;
5458

5559
/**
5660
* Instantiates a new Builder from an existing ListApplicationsOptions instance.
@@ -60,6 +64,8 @@ public static class Builder {
6064
private Builder(ListApplicationsOptions listApplicationsOptions) {
6165
this.instanceId = listApplicationsOptions.instanceId;
6266
this.state = listApplicationsOptions.state;
67+
this.limit = listApplicationsOptions.limit;
68+
this.start = listApplicationsOptions.start;
6369
}
6470

6571
/**
@@ -124,6 +130,28 @@ public Builder state(List<String> state) {
124130
this.state = state;
125131
return this;
126132
}
133+
134+
/**
135+
* Set the limit.
136+
*
137+
* @param limit the limit
138+
* @return the ListApplicationsOptions builder
139+
*/
140+
public Builder limit(long limit) {
141+
this.limit = limit;
142+
return this;
143+
}
144+
145+
/**
146+
* Set the start.
147+
*
148+
* @param start the start
149+
* @return the ListApplicationsOptions builder
150+
*/
151+
public Builder start(String start) {
152+
this.start = start;
153+
return this;
154+
}
127155
}
128156

129157
protected ListApplicationsOptions() { }
@@ -133,6 +161,8 @@ protected ListApplicationsOptions(Builder builder) {
133161
"instanceId cannot be empty");
134162
instanceId = builder.instanceId;
135163
state = builder.state;
164+
limit = builder.limit;
165+
start = builder.start;
136166
}
137167

138168
/**
@@ -165,5 +195,27 @@ public String instanceId() {
165195
public List<String> state() {
166196
return state;
167197
}
198+
199+
/**
200+
* Gets the limit.
201+
*
202+
* Number of application entries to be included in the response.
203+
*
204+
* @return the limit
205+
*/
206+
public Long limit() {
207+
return limit;
208+
}
209+
210+
/**
211+
* Gets the start.
212+
*
213+
* Token used to fetch the next or the previous page of the applications list.
214+
*
215+
* @return the start
216+
*/
217+
public String start() {
218+
return start;
219+
}
168220
}
169221

0 commit comments

Comments
 (0)