Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Usage Reports): Update query param for getting reports snapshotlist #216

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,12 @@ public ServiceCall<SnapshotList> getReportsSnapshot(GetReportsSnapshotOptions ge
if (getReportsSnapshotOptions.dateTo() != null) {
builder.query("date_to", String.valueOf(getReportsSnapshotOptions.dateTo()));
}
if (getReportsSnapshotOptions.limit() != null) {
builder.query("_limit", String.valueOf(getReportsSnapshotOptions.limit()));
}
if (getReportsSnapshotOptions.start() != null) {
builder.query("_start", String.valueOf(getReportsSnapshotOptions.start()));
}
ResponseConverter<SnapshotList> responseConverter =
ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<SnapshotList>() { }.getType());
return createServiceCall(builder.build(), responseConverter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class GetReportsSnapshotOptions extends GenericModel {
protected String month;
protected Long dateFrom;
protected Long dateTo;
protected Long limit;
protected String start;

/**
* Builder.
Expand All @@ -32,6 +34,8 @@ public static class Builder {
private String month;
private Long dateFrom;
private Long dateTo;
private Long limit;
private String start;

/**
* Instantiates a new Builder from an existing GetReportsSnapshotOptions instance.
Expand All @@ -43,6 +47,8 @@ private Builder(GetReportsSnapshotOptions getReportsSnapshotOptions) {
this.month = getReportsSnapshotOptions.month;
this.dateFrom = getReportsSnapshotOptions.dateFrom;
this.dateTo = getReportsSnapshotOptions.dateTo;
this.limit = getReportsSnapshotOptions.limit;
this.start = getReportsSnapshotOptions.start;
}

/**
Expand Down Expand Up @@ -114,6 +120,28 @@ public Builder dateTo(long dateTo) {
this.dateTo = dateTo;
return this;
}

/**
* Set the limit.
*
* @param limit the limit
* @return the GetReportsSnapshotOptions builder
*/
public Builder limit(long limit) {
this.limit = limit;
return this;
}

/**
* Set the start.
*
* @param start the start
* @return the GetReportsSnapshotOptions builder
*/
public Builder start(String start) {
this.start = start;
return this;
}
}

protected GetReportsSnapshotOptions() { }
Expand All @@ -127,6 +155,8 @@ protected GetReportsSnapshotOptions(Builder builder) {
month = builder.month;
dateFrom = builder.dateFrom;
dateTo = builder.dateTo;
limit = builder.limit;
start = builder.start;
}

/**
Expand Down Expand Up @@ -181,5 +211,27 @@ public Long dateFrom() {
public Long dateTo() {
return dateTo;
}

/**
* Gets the limit.
*
* Number of usage records returned. The default value is 30. Maximum value is 200.
*
* @return the limit
*/
public Long limit() {
return limit;
}

/**
* Gets the start.
*
* The offset from which the records must be fetched. Offset information is included in the response.
*
* @return the start
*/
public String start() {
return start;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* (C) Copyright IBM Corp. 2023.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.ibm.cloud.platform_services.usage_reports.v4.model;

import com.ibm.cloud.platform_services.usage_reports.v4.UsageReports;
import com.ibm.cloud.sdk.core.util.UrlHelper;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

/**
* GetReportsSnapshotPager can be used to simplify the use of the "getReportsSnapshot" method.
*/
public class GetReportsSnapshotPager {
private static class PageContext {
private String next;
public String getNext() {
return next;
}
public void setNext(String next) {
this.next = next;
}
}

protected boolean hasNext;
protected GetReportsSnapshotOptions options;
protected UsageReports client;
protected PageContext pageContext;

// Hide the default ctor.
protected GetReportsSnapshotPager() { }

/**
* Constructs a new GetReportsSnapshotPager instance with the specified client and options model instance.
* @param client the UsageReports instance to be used to invoke the "getReportsSnapshot" method
* @param options the GetReportsSnapshotOptions instance to be used to invoke the "getReportsSnapshot" method
*/
public GetReportsSnapshotPager(UsageReports client, GetReportsSnapshotOptions options) {
if (options.start() != null) {
throw new IllegalArgumentException("The options 'start' field should not be set");
}

this.hasNext = true;
this.client = client;
this.options = options.newBuilder().build();
this.pageContext = new PageContext();
}

/**
* Returns true if there are more results to be retrieved.
* @return boolean
*/
public boolean hasNext() {
return hasNext;
}

/**
* Returns the next page of results.
* @return a List&lt;SnapshotListSnapshotsItem&gt; that contains the next page of results
*/
public List<SnapshotListSnapshotsItem> getNext() {
if (!hasNext()) {
throw new NoSuchElementException("No more results available");
}

GetReportsSnapshotOptions.Builder builder = this.options.newBuilder();
if (this.pageContext.getNext() != null) {
builder.start(this.pageContext.getNext());
}
this.options = builder.build();

SnapshotList result = client.getReportsSnapshot(options).execute().getResult();

String next = null;
if (result.getNext() != null) {
String queryParam = UrlHelper.getQueryParam(result.getNext().getHref(), "_start");
if (queryParam != null) {
next = queryParam;
}
}
this.pageContext.setNext(next);
if (next == null) {
this.hasNext = false;
}

return result.getSnapshots();
}

/**
* Returns all results by invoking getNext() repeatedly until all pages of results have been retrieved.
* @return a List&lt;SnapshotListSnapshotsItem&gt; containing all results returned by the "getReportsSnapshot" method
*/
public List<SnapshotListSnapshotsItem> getAll() {
List<SnapshotListSnapshotsItem> results = new ArrayList<>();
while (hasNext()) {
List<SnapshotListSnapshotsItem> nextPage = getNext();
results.addAll(nextPage);
}
return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class SnapshotListNext extends GenericModel {

protected String href;
protected String offset;

protected SnapshotListNext() { }

Expand All @@ -31,5 +32,14 @@ protected SnapshotListNext() { }
public String getHref() {
return href;
}

/**
* Gets the offset.
*
* @return the offset
*/
public String getOffset() {
return offset;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.ibm.cloud.platform_services.usage_reports.v4.model.GetOrgUsageOptions;
import com.ibm.cloud.platform_services.usage_reports.v4.model.GetReportsSnapshotConfigOptions;
import com.ibm.cloud.platform_services.usage_reports.v4.model.GetReportsSnapshotOptions;
import com.ibm.cloud.platform_services.usage_reports.v4.model.GetReportsSnapshotPager;
import com.ibm.cloud.platform_services.usage_reports.v4.model.GetResourceGroupUsageOptions;
import com.ibm.cloud.platform_services.usage_reports.v4.model.GetResourceUsageAccountOptions;
import com.ibm.cloud.platform_services.usage_reports.v4.model.GetResourceUsageAccountPager;
Expand All @@ -34,6 +35,7 @@
import com.ibm.cloud.platform_services.usage_reports.v4.model.ResourceGroupUsage;
import com.ibm.cloud.platform_services.usage_reports.v4.model.SnapshotConfig;
import com.ibm.cloud.platform_services.usage_reports.v4.model.SnapshotList;
import com.ibm.cloud.platform_services.usage_reports.v4.model.SnapshotListSnapshotsItem;
import com.ibm.cloud.platform_services.usage_reports.v4.model.UpdateReportsSnapshotConfigOptions;
import com.ibm.cloud.platform_services.usage_reports.v4.utils.TestUtilities;
import com.ibm.cloud.sdk.core.http.Response;
Expand Down Expand Up @@ -959,7 +961,7 @@ public void testDeleteReportsSnapshotConfigNoOptions() throws Throwable {
@Test
public void testGetReportsSnapshotWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"count\": 3, \"first\": {\"href\": \"/v1/billing-reports-snapshots?_limit=10&account_id=272b9a4f73e11030d0ba037daee47a35&date_from=-Infinity&date_to=Infinity&month=2023-06\"}, \"next\": {\"href\": \"/v1/billing-reports-snapshots?_limit=10&account_id=272b9a4f73e11030d0ba037daee47a35&date_from=-Infinity&date_to=Infinity&month=2023-06\"}, \"snapshots\": [{\"account_id\": \"abc\", \"month\": \"2023-06\", \"account_type\": \"account\", \"expected_processed_at\": 1687470383610, \"state\": \"enabled\", \"billing_period\": {\"start\": \"2023-06-01T00:00:00.000Z\", \"end\": \"2023-06-30T23:59:59.999Z\"}, \"snapshot_id\": \"1685577600000\", \"charset\": \"UTF-8\", \"compression\": \"GZIP\", \"content_type\": \"text/csv\", \"bucket\": \"bucket_name\", \"version\": \"1.0\", \"created_on\": \"2023-06-22T21:47:28.297Z\", \"report_types\": [{\"type\": \"account_summary\", \"version\": \"1.0\"}], \"files\": [{\"report_types\": \"account_summary\", \"location\": \"june/2023-06/1685577600000/2023-06-account-summary-272b9a4f73e11030d0ba037daee47a35.csv.gz\", \"account_id\": \"abc\"}], \"processed_at\": 1687470448297}]}";
String mockResponseBody = "{\"count\": 3, \"first\": {\"href\": \"/v1/billing-reports-snapshots?_limit=10&account_id=272b9a4f73e11030d0ba037daee47a35&date_from=-Infinity&date_to=Infinity&month=2023-06\"}, \"next\": {\"href\": \"/v1/billing-reports-snapshots?_limit=10&account_id=272b9a4f73e11030d0ba037daee47a35&date_from=-Infinity&date_to=Infinity&month=2023-06\", \"offset\": \"g1AAAAHyeJzLYWBgYMtgTmHQSklKzi9KdUhJMtRLytVNTtZNSU3JTE4sSU0xMjTUS87JL01JzCvRy0styQHqYUpSAJJJ-v___88C892cKtZ\"}, \"snapshots\": [{\"account_id\": \"abc\", \"month\": \"2023-06\", \"account_type\": \"account\", \"expected_processed_at\": 1687470383610, \"state\": \"enabled\", \"billing_period\": {\"start\": \"2023-06-01T00:00:00.000Z\", \"end\": \"2023-06-30T23:59:59.999Z\"}, \"snapshot_id\": \"1685577600000\", \"charset\": \"UTF-8\", \"compression\": \"GZIP\", \"content_type\": \"text/csv\", \"bucket\": \"bucket_name\", \"version\": \"1.0\", \"created_on\": \"2023-06-22T21:47:28.297Z\", \"report_types\": [{\"type\": \"account_summary\", \"version\": \"1.0\"}], \"files\": [{\"report_types\": \"account_summary\", \"location\": \"june/2023-06/1685577600000/2023-06-account-summary-272b9a4f73e11030d0ba037daee47a35.csv.gz\", \"account_id\": \"abc\"}], \"processed_at\": 1687470448297}]}";
String getReportsSnapshotPath = "/v1/billing-reports-snapshots";
server.enqueue(new MockResponse()
.setHeader("Content-type", "application/json")
Expand All @@ -972,6 +974,8 @@ public void testGetReportsSnapshotWOptions() throws Throwable {
.month("2023-02")
.dateFrom(Long.valueOf("1675209600000"))
.dateTo(Long.valueOf("1675987200000"))
.limit(Long.valueOf("30"))
.start("testString")
.build();

// Invoke getReportsSnapshot() with a valid options model and verify the result
Expand All @@ -994,6 +998,8 @@ public void testGetReportsSnapshotWOptions() throws Throwable {
assertEquals(query.get("month"), "2023-02");
assertEquals(Long.valueOf(query.get("date_from")), Long.valueOf("1675209600000"));
assertEquals(Long.valueOf(query.get("date_to")), Long.valueOf("1675987200000"));
assertEquals(Long.valueOf(query.get("_limit")), Long.valueOf("30"));
assertEquals(query.get("_start"), "testString");
}

// Test the getReportsSnapshot operation with and without retries enabled
Expand All @@ -1013,6 +1019,76 @@ public void testGetReportsSnapshotNoOptions() throws Throwable {
usageReportsService.getReportsSnapshot(null).execute();
}

// Test the getReportsSnapshot operation using the GetReportsSnapshotPager.getNext() method
@Test
public void testGetReportsSnapshotWithPagerGetNext() throws Throwable {
// Set up the two-page mock response.
String mockResponsePage1 = "{\"snapshots\":[{\"account_id\":\"abc\",\"month\":\"2023-06\",\"account_type\":\"account\",\"expected_processed_at\":1687470383610,\"state\":\"enabled\",\"billing_period\":{\"start\":\"2023-06-01T00:00:00.000Z\",\"end\":\"2023-06-30T23:59:59.999Z\"},\"snapshot_id\":\"1685577600000\",\"charset\":\"UTF-8\",\"compression\":\"GZIP\",\"content_type\":\"text/csv\",\"bucket\":\"bucket_name\",\"version\":\"1.0\",\"created_on\":\"2023-06-22T21:47:28.297Z\",\"report_types\":[{\"type\":\"account_summary\",\"version\":\"1.0\"}],\"files\":[{\"report_types\":\"account_summary\",\"location\":\"june/2023-06/1685577600000/2023-06-account-summary-272b9a4f73e11030d0ba037daee47a35.csv.gz\",\"account_id\":\"abc\"}],\"processed_at\":1687470448297}],\"next\":{\"href\":\"https://myhost.com/somePath?_start=1\"},\"total_count\":2,\"limit\":1}";
String mockResponsePage2 = "{\"snapshots\":[{\"account_id\":\"abc\",\"month\":\"2023-06\",\"account_type\":\"account\",\"expected_processed_at\":1687470383610,\"state\":\"enabled\",\"billing_period\":{\"start\":\"2023-06-01T00:00:00.000Z\",\"end\":\"2023-06-30T23:59:59.999Z\"},\"snapshot_id\":\"1685577600000\",\"charset\":\"UTF-8\",\"compression\":\"GZIP\",\"content_type\":\"text/csv\",\"bucket\":\"bucket_name\",\"version\":\"1.0\",\"created_on\":\"2023-06-22T21:47:28.297Z\",\"report_types\":[{\"type\":\"account_summary\",\"version\":\"1.0\"}],\"files\":[{\"report_types\":\"account_summary\",\"location\":\"june/2023-06/1685577600000/2023-06-account-summary-272b9a4f73e11030d0ba037daee47a35.csv.gz\",\"account_id\":\"abc\"}],\"processed_at\":1687470448297}],\"total_count\":2,\"limit\":1}";
server.enqueue(new MockResponse()
.setHeader("Content-type", "application/json")
.setResponseCode(200)
.setBody(mockResponsePage1));
server.enqueue(new MockResponse()
.setHeader("Content-type", "application/json")
.setResponseCode(200)
.setBody(mockResponsePage2));
server.enqueue(new MockResponse()
.setHeader("Content-type", "application/json")
.setResponseCode(400)
.setBody("{\"message\": \"No more results available!\"}"));

GetReportsSnapshotOptions getReportsSnapshotOptions = new GetReportsSnapshotOptions.Builder()
.accountId("abc")
.month("2023-02")
.dateFrom(Long.valueOf("1675209600000"))
.dateTo(Long.valueOf("1675987200000"))
.limit(Long.valueOf("30"))
.build();

List<SnapshotListSnapshotsItem> allResults = new ArrayList<>();
GetReportsSnapshotPager pager = new GetReportsSnapshotPager(usageReportsService, getReportsSnapshotOptions);
while (pager.hasNext()) {
List<SnapshotListSnapshotsItem> nextPage = pager.getNext();
assertNotNull(nextPage);
allResults.addAll(nextPage);
}
assertEquals(allResults.size(), 2);
}

// Test the getReportsSnapshot operation using the GetReportsSnapshotPager.getAll() method
@Test
public void testGetReportsSnapshotWithPagerGetAll() throws Throwable {
// Set up the two-page mock response.
String mockResponsePage1 = "{\"snapshots\":[{\"account_id\":\"abc\",\"month\":\"2023-06\",\"account_type\":\"account\",\"expected_processed_at\":1687470383610,\"state\":\"enabled\",\"billing_period\":{\"start\":\"2023-06-01T00:00:00.000Z\",\"end\":\"2023-06-30T23:59:59.999Z\"},\"snapshot_id\":\"1685577600000\",\"charset\":\"UTF-8\",\"compression\":\"GZIP\",\"content_type\":\"text/csv\",\"bucket\":\"bucket_name\",\"version\":\"1.0\",\"created_on\":\"2023-06-22T21:47:28.297Z\",\"report_types\":[{\"type\":\"account_summary\",\"version\":\"1.0\"}],\"files\":[{\"report_types\":\"account_summary\",\"location\":\"june/2023-06/1685577600000/2023-06-account-summary-272b9a4f73e11030d0ba037daee47a35.csv.gz\",\"account_id\":\"abc\"}],\"processed_at\":1687470448297}],\"next\":{\"href\":\"https://myhost.com/somePath?_start=1\"},\"total_count\":2,\"limit\":1}";
String mockResponsePage2 = "{\"snapshots\":[{\"account_id\":\"abc\",\"month\":\"2023-06\",\"account_type\":\"account\",\"expected_processed_at\":1687470383610,\"state\":\"enabled\",\"billing_period\":{\"start\":\"2023-06-01T00:00:00.000Z\",\"end\":\"2023-06-30T23:59:59.999Z\"},\"snapshot_id\":\"1685577600000\",\"charset\":\"UTF-8\",\"compression\":\"GZIP\",\"content_type\":\"text/csv\",\"bucket\":\"bucket_name\",\"version\":\"1.0\",\"created_on\":\"2023-06-22T21:47:28.297Z\",\"report_types\":[{\"type\":\"account_summary\",\"version\":\"1.0\"}],\"files\":[{\"report_types\":\"account_summary\",\"location\":\"june/2023-06/1685577600000/2023-06-account-summary-272b9a4f73e11030d0ba037daee47a35.csv.gz\",\"account_id\":\"abc\"}],\"processed_at\":1687470448297}],\"total_count\":2,\"limit\":1}";
server.enqueue(new MockResponse()
.setHeader("Content-type", "application/json")
.setResponseCode(200)
.setBody(mockResponsePage1));
server.enqueue(new MockResponse()
.setHeader("Content-type", "application/json")
.setResponseCode(200)
.setBody(mockResponsePage2));
server.enqueue(new MockResponse()
.setHeader("Content-type", "application/json")
.setResponseCode(400)
.setBody("{\"message\": \"No more results available!\"}"));

GetReportsSnapshotOptions getReportsSnapshotOptions = new GetReportsSnapshotOptions.Builder()
.accountId("abc")
.month("2023-02")
.dateFrom(Long.valueOf("1675209600000"))
.dateTo(Long.valueOf("1675987200000"))
.limit(Long.valueOf("30"))
.build();

GetReportsSnapshotPager pager = new GetReportsSnapshotPager(usageReportsService, getReportsSnapshotOptions);
List<SnapshotListSnapshotsItem> allResults = pager.getAll();
assertNotNull(allResults);
assertEquals(allResults.size(), 2);
}

// Perform setup needed before each test method
@BeforeMethod
public void beforeEachTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

package com.ibm.cloud.platform_services.usage_reports.v4.model;

import com.ibm.cloud.platform_services.usage_reports.v4.model.GetReportsSnapshotOptions;
import com.ibm.cloud.platform_services.usage_reports.v4.utils.TestUtilities;
import com.ibm.cloud.sdk.core.service.model.FileWithMetadata;
import java.io.InputStream;
Expand All @@ -36,11 +35,15 @@ public void testGetReportsSnapshotOptions() throws Throwable {
.month("2023-02")
.dateFrom(Long.valueOf("1675209600000"))
.dateTo(Long.valueOf("1675987200000"))
.limit(Long.valueOf("30"))
.start("testString")
.build();
assertEquals(getReportsSnapshotOptionsModel.accountId(), "abc");
assertEquals(getReportsSnapshotOptionsModel.month(), "2023-02");
assertEquals(getReportsSnapshotOptionsModel.dateFrom(), Long.valueOf("1675209600000"));
assertEquals(getReportsSnapshotOptionsModel.dateTo(), Long.valueOf("1675987200000"));
assertEquals(getReportsSnapshotOptionsModel.limit(), Long.valueOf("30"));
assertEquals(getReportsSnapshotOptionsModel.start(), "testString");
}

@Test(expectedExceptions = IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

package com.ibm.cloud.platform_services.usage_reports.v4.model;

import com.ibm.cloud.platform_services.usage_reports.v4.model.GetResourceUsageAccountOptions;
import com.ibm.cloud.platform_services.usage_reports.v4.utils.TestUtilities;
import com.ibm.cloud.sdk.core.service.model.FileWithMetadata;
import java.io.InputStream;
Expand Down
Loading