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

Add pageToken to list fields option for storage and resourcemanager #668

Merged
merged 4 commits into from
Feb 25, 2016
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 @@ -162,7 +162,7 @@ public static ProjectListOption pageSize(int pageSize) {
*/
public static ProjectListOption fields(ProjectField... fields) {
StringBuilder builder = new StringBuilder();
builder.append("projects(").append(ProjectField.selector(fields)).append(")");
builder.append("projects(").append(ProjectField.selector(fields)).append("),nextPageToken");
return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, builder.toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

/**
Expand All @@ -56,6 +58,9 @@ public class LocalResourceManagerHelper {
private static final URI BASE_CONTEXT;
private static final Set<String> SUPPORTED_COMPRESSION_ENCODINGS =
ImmutableSet.of("gzip", "x-gzip");
private static final Pattern LIST_FIELDS_PATTERN =
Pattern.compile("(.*?)projects\\((.*?)\\)(.*?)");
private static final String[] NO_FIELDS = {};

static {
try {
Expand Down Expand Up @@ -236,10 +241,15 @@ private static Map<String, Object> parseListOptions(String query) throws IOExcep
String[] argEntry = arg.split("=");
switch (argEntry[0]) {
case "fields":
// List fields are in the form "projects(field1, field2, ...)"
options.put(
"fields",
argEntry[1].substring("projects(".length(), argEntry[1].length() - 1).split(","));
// List fields are in the form "projects(field1, field2, ...),nextPageToken"
Matcher matcher = LIST_FIELDS_PATTERN.matcher(argEntry[1]);
if (matcher.matches()) {
options.put("projectFields", matcher.group(2).split(","));
options.put("listFields", (matcher.group(1) + matcher.group(3)).split(","));
} else {
options.put("projectFields", NO_FIELDS);
options.put("listFields", argEntry[1].split(","));
}
break;
case "filter":
options.put("filter", argEntry[1].split(" "));
Expand Down Expand Up @@ -362,7 +372,7 @@ Response list(Map<String, Object> options) {
if (filters != null && !isValidFilter(filters)) {
return Error.INVALID_ARGUMENT.response("Could not parse the filter.");
}
String[] fields = (String[]) options.get("fields");
String[] projectFields = (String[]) options.get("projectFields");
int count = 0;
String pageToken = (String) options.get("pageToken");
Integer pageSize = (Integer) options.get("pageSize");
Expand All @@ -380,19 +390,28 @@ Response list(Map<String, Object> options) {
if (includeProject) {
count++;
try {
projectsSerialized.add(jsonFactory.toString(extractFields(p, fields)));
projectsSerialized.add(jsonFactory.toString(extractFields(p, projectFields)));
} catch (IOException e) {
return Error.INTERNAL_ERROR.response(
"Error when serializing project " + p.getProjectId());
}
}
}
String[] listFields = (String[]) options.get("listFields");
StringBuilder responseBody = new StringBuilder();
responseBody.append("{\"projects\": [");
Joiner.on(",").appendTo(responseBody, projectsSerialized);
responseBody.append(']');
if (nextPageToken != null) {
responseBody.append(", \"nextPageToken\": \"");
responseBody.append('{');
// If fields parameter is set but no project field is selected we must return no projects.
if (!(projectFields != null && projectFields.length == 0)) {
responseBody.append("\"projects\": [");
Joiner.on(",").appendTo(responseBody, projectsSerialized);
responseBody.append(']');
}
if (nextPageToken != null && (listFields == null
|| ImmutableSet.copyOf(listFields).contains("nextPageToken"))) {
if (responseBody.length() > 1) {
responseBody.append(',');
}
responseBody.append("\"nextPageToken\": \"");
responseBody.append(nextPageToken);
responseBody.append('"');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ public void testListPaging() {
@Test
public void testListFieldOptions() {
Map<ResourceManagerRpc.Option, Object> rpcOptions = new HashMap<>();
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "projects(projectId,name,labels)");
rpcOptions.put(ResourceManagerRpc.Option.FIELDS,
"projects(projectId,name,labels),nextPageToken");
rpc.create(PROJECT_WITH_PARENT);
Tuple<String, Iterable<com.google.api.services.cloudresourcemanager.model.Project>> projects =
rpc.list(rpcOptions);
Expand All @@ -349,6 +350,81 @@ public void testListFieldOptions() {
assertNull(returnedProject.getCreateTime());
}

@Test
public void testListPageTokenFieldOptions() {
Map<ResourceManagerRpc.Option, Object> rpcOptions = new HashMap<>();
rpcOptions.put(ResourceManagerRpc.Option.PAGE_SIZE, 1);
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "nextPageToken,projects(projectId,name)");
rpc.create(PARTIAL_PROJECT);
rpc.create(COMPLETE_PROJECT);
Tuple<String, Iterable<com.google.api.services.cloudresourcemanager.model.Project>> projects =
rpc.list(rpcOptions);
assertNotNull(projects.x());
Iterator<com.google.api.services.cloudresourcemanager.model.Project> iterator =
projects.y().iterator();
com.google.api.services.cloudresourcemanager.model.Project returnedProject = iterator.next();
assertEquals(COMPLETE_PROJECT.getProjectId(), returnedProject.getProjectId());
assertEquals(COMPLETE_PROJECT.getName(), returnedProject.getName());
assertNull(returnedProject.getLabels());
assertNull(returnedProject.getParent());
assertNull(returnedProject.getProjectNumber());
assertNull(returnedProject.getLifecycleState());
assertNull(returnedProject.getCreateTime());
assertFalse(iterator.hasNext());
rpcOptions.put(ResourceManagerRpc.Option.PAGE_TOKEN, projects.x());
projects = rpc.list(rpcOptions);
iterator = projects.y().iterator();
returnedProject = iterator.next();
assertEquals(PARTIAL_PROJECT.getProjectId(), returnedProject.getProjectId());
assertEquals(PARTIAL_PROJECT.getName(), returnedProject.getName());
assertNull(returnedProject.getLabels());
assertNull(returnedProject.getParent());
assertNull(returnedProject.getProjectNumber());
assertNull(returnedProject.getLifecycleState());
assertNull(returnedProject.getCreateTime());
assertNull(projects.x());
}

@Test
public void testListNoPageTokenFieldOptions() {
Map<ResourceManagerRpc.Option, Object> rpcOptions = new HashMap<>();
rpcOptions.put(ResourceManagerRpc.Option.PAGE_SIZE, 1);
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "projects(projectId,name)");
rpc.create(PARTIAL_PROJECT);
rpc.create(COMPLETE_PROJECT);
Tuple<String, Iterable<com.google.api.services.cloudresourcemanager.model.Project>> projects =
rpc.list(rpcOptions);
assertNull(projects.x());
Iterator<com.google.api.services.cloudresourcemanager.model.Project> iterator =
projects.y().iterator();
com.google.api.services.cloudresourcemanager.model.Project returnedProject = iterator.next();
assertEquals(COMPLETE_PROJECT.getProjectId(), returnedProject.getProjectId());
assertEquals(COMPLETE_PROJECT.getName(), returnedProject.getName());
assertNull(returnedProject.getLabels());
assertNull(returnedProject.getParent());
assertNull(returnedProject.getProjectNumber());
assertNull(returnedProject.getLifecycleState());
assertNull(returnedProject.getCreateTime());
assertFalse(iterator.hasNext());
}

@Test
public void testListPageTokenNoFieldsOptions() {
Map<ResourceManagerRpc.Option, Object> rpcOptions = new HashMap<>();
rpcOptions.put(ResourceManagerRpc.Option.PAGE_SIZE, 1);
rpcOptions.put(ResourceManagerRpc.Option.FIELDS, "nextPageToken");
rpc.create(PARTIAL_PROJECT);
rpc.create(COMPLETE_PROJECT);
Tuple<String, Iterable<com.google.api.services.cloudresourcemanager.model.Project>> projects =
rpc.list(rpcOptions);
assertNotNull(projects.x());
assertNull(projects.y());
rpcOptions.put(ResourceManagerRpc.Option.PAGE_TOKEN, projects.x());
projects = rpc.list(rpcOptions);
assertNull(projects.x());
assertNull(projects.y());
}

@Test
public void testListFilterOptions() {
Map<ResourceManagerRpc.Option, Object> rpcFilterOptions = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,38 @@ public void testListFieldOptions() {
assertSame(RESOURCE_MANAGER, returnedProject.resourceManager());
}

@Test
public void testListPagingWithFieldOptions() {
RESOURCE_MANAGER.create(PARTIAL_PROJECT);
RESOURCE_MANAGER.create(COMPLETE_PROJECT);
Page<Project> projects = RESOURCE_MANAGER.list(LIST_FIELDS, ProjectListOption.pageSize(1));
assertNotNull(projects.nextPageCursor());
Iterator<Project> iterator = projects.values().iterator();
Project returnedProject = iterator.next();
assertEquals(COMPLETE_PROJECT.projectId(), returnedProject.projectId());
assertEquals(COMPLETE_PROJECT.name(), returnedProject.name());
assertEquals(COMPLETE_PROJECT.labels(), returnedProject.labels());
assertNull(returnedProject.parent());
assertNull(returnedProject.projectNumber());
assertNull(returnedProject.state());
assertNull(returnedProject.createTimeMillis());
assertSame(RESOURCE_MANAGER, returnedProject.resourceManager());
assertFalse(iterator.hasNext());
projects = projects.nextPage();
iterator = projects.values().iterator();
returnedProject = iterator.next();
assertEquals(PARTIAL_PROJECT.projectId(), returnedProject.projectId());
assertEquals(PARTIAL_PROJECT.name(), returnedProject.name());
assertEquals(PARTIAL_PROJECT.labels(), returnedProject.labels());
assertNull(returnedProject.parent());
assertNull(returnedProject.projectNumber());
assertNull(returnedProject.state());
assertNull(returnedProject.createTimeMillis());
assertSame(RESOURCE_MANAGER, returnedProject.resourceManager());
assertFalse(iterator.hasNext());
assertNull(projects.nextPageCursor());
}

@Test
public void testListFilterOptions() {
ProjectInfo matchingProject = ProjectInfo.builder("matching-project")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public static BucketListOption prefix(String prefix) {
*/
public static BucketListOption fields(BucketField... fields) {
StringBuilder builder = new StringBuilder();
builder.append("items(").append(BucketField.selector(fields)).append(")");
builder.append("items(").append(BucketField.selector(fields)).append("),nextPageToken");
return new BucketListOption(StorageRpc.Option.FIELDS, builder.toString());
}
}
Expand Down Expand Up @@ -708,7 +708,7 @@ public static BlobListOption recursive(boolean recursive) {
*/
public static BlobListOption fields(BlobField... fields) {
StringBuilder builder = new StringBuilder();
builder.append("items(").append(BlobField.selector(fields)).append(")");
builder.append("items(").append(BlobField.selector(fields)).append("),nextPageToken");
return new BlobListOption(StorageRpc.Option.FIELDS, builder.toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ public void testListBucketsWithSelectedFields() {
assertTrue(selector.contains("name"));
assertTrue(selector.contains("acl"));
assertTrue(selector.contains("location"));
assertEquals(24, selector.length());
assertTrue(selector.contains("nextPageToken"));
assertEquals(38, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.values(), Bucket.class));
}
Expand All @@ -606,7 +607,8 @@ public void testListBucketsWithEmptyFields() {
String selector = (String) capturedOptions.getValue().get(BLOB_LIST_FIELDS.rpcOption());
assertTrue(selector.contains("items"));
assertTrue(selector.contains("name"));
assertEquals(11, selector.length());
assertTrue(selector.contains("nextPageToken"));
assertEquals(25, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.values(), Bucket.class));
}
Expand Down Expand Up @@ -678,7 +680,8 @@ public void testListBlobsWithSelectedFields() {
assertTrue(selector.contains("name"));
assertTrue(selector.contains("contentType"));
assertTrue(selector.contains("md5Hash"));
assertEquals(38, selector.length());
assertTrue(selector.contains("nextPageToken"));
assertEquals(52, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class));
}
Expand Down Expand Up @@ -706,7 +709,8 @@ public void testListBlobsWithEmptyFields() {
assertTrue(selector.contains("items"));
assertTrue(selector.contains("bucket"));
assertTrue(selector.contains("name"));
assertEquals(18, selector.length());
assertTrue(selector.contains("nextPageToken"));
assertEquals(32, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class));
}
Expand Down