Skip to content

Commit

Permalink
Remove parent and resource ID, add fields options
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Nov 25, 2015
1 parent 9f7fc1a commit 6de6ecf
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public class ProjectInfo implements Serializable {
private final Long number;
private final State state;
private final Long createTimeMillis;
private final ResourceId parent;

/**
* The project lifecycle state.
Expand Down Expand Up @@ -80,7 +79,6 @@ public static class Builder {
private Long number;
private State state;
private Long createTimeMillis;
private ResourceId parent;

Builder() {
labels = new HashMap<>();
Expand Down Expand Up @@ -165,20 +163,6 @@ Builder createTimeMillis(Long createTimeMillis) {
return this;
}

/**
* Set the parent of the project.
*
* If this field is left unset in a project creation request, the server will set this field by
* default to the creator of the project. The parent cannot be changed after the server creates
* the project. When calling {@link ResourceManager#replace}, be sure to set the parent of the
* new ProjectInfo instance. Leaving the parent unset or setting it to null in a replace request
* will cause an error.
*/
public Builder parent(ResourceId parent) {
this.parent = parent;
return this;
}

public ProjectInfo build() {
return new ProjectInfo(this);
}
Expand All @@ -191,7 +175,6 @@ public ProjectInfo build() {
this.number = builder.number;
this.state = builder.state;
this.createTimeMillis = builder.createTimeMillis;
this.parent = builder.parent;
}

/**
Expand Down Expand Up @@ -247,23 +230,14 @@ public Long createTimeMillis() {
return createTimeMillis;
}

/**
* Get the parent of the project.
*
* The parent cannot be changed after the server creates the project.
*/
public ResourceId parent() {
return parent;
}

@Override
public boolean equals(Object obj) {
return obj instanceof ProjectInfo && Objects.equals(toPb(), ((ProjectInfo) obj).toPb());
}

@Override
public int hashCode() {
return Objects.hash(name, id, labels, number, state, createTimeMillis, parent);
return Objects.hash(name, id, labels, number, state, createTimeMillis);
}

public static Builder builder(String id) {
Expand All @@ -277,8 +251,7 @@ public Builder toBuilder() {
.labels(labels)
.number(number)
.state(state)
.createTimeMillis(createTimeMillis)
.parent(parent);
.createTimeMillis(createTimeMillis);
}

com.google.api.services.cloudresourcemanager.model.Project toPb() {
Expand All @@ -294,9 +267,6 @@ com.google.api.services.cloudresourcemanager.model.Project toPb() {
if (createTimeMillis != null) {
projectPb.setCreateTime(ISODateTimeFormat.dateTime().print(createTimeMillis));
}
if (parent != null) {
projectPb.setParent(parent.toPb());
}
return projectPb;
}

Expand All @@ -306,9 +276,6 @@ static ProjectInfo fromPb(com.google.api.services.cloudresourcemanager.model.Pro
if (projectPb.getLabels() != null) {
builder.labels(projectPb.getLabels());
}
if (projectPb.getParent() != null) {
builder.parent(ResourceId.fromPb(projectPb.getParent()));
}
return builder.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@

package com.google.gcloud.resourcemanager;

import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
import com.google.gcloud.Page;
import com.google.gcloud.Service;
import com.google.gcloud.spi.ResourceManagerRpc;

import java.util.HashSet;

/**
* An interface for Google Cloud Resource Manager.
*
Expand All @@ -30,25 +34,66 @@ public interface ResourceManager extends Service<ResourceManagerOptions> {
public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";

/**
* Class for specifying project list options.
* The fields of a project.
*
* These values can be used to specify the fields to include of in a partial response when calling
* {@link ResourceManager#get} or {@link ResourceManager#list}. Project ID is always returned,
* even if not specified.
*/
public class ProjectListOption extends Option {
enum ProjectField {
ID("projectId"),
NAME("name"),
LABELS("labels"),
NUMBER("projectNumber"),
STATE("lifecycleState"),
CREATE_TIME("createTime");

private final String selector;

ProjectField(String selector) {
this.selector = selector;
}

private static final long serialVersionUID = 7888768979702012328L;
public String selector() {
return selector;
}

private ProjectListOption(ResourceManagerRpc.Option option, Object value) {
static String selector(ProjectField... fields) {
HashSet<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
fieldStrings.add(ID.selector());
for (ProjectField field : fields) {
fieldStrings.add(field.selector());
}
return Joiner.on(',').join(fieldStrings);
}
}

public class ProjectGetOption extends Option {
private ProjectGetOption(ResourceManagerRpc.Option option, Object value) {
super(option, value);
}

/**
* Returns an option to specify a page token.
* Returns an option to specify the project's fields to be returned by the RPC call.
*
* The page token (returned from a previous call to list) indicates from where listing should
* continue. Pagination is not yet supported; the server ignores this field. Optional.
* If this option is not provided all project fields are returned.
* {@code ProjectListOption.fields} can be used to specify only the fields of interest.
* {@link ProjectField} provides a list of fields that can be used.
*/
public static ProjectListOption pageToken(String pageToken) {
// return new ProjectListOption(ResourceManagerRpc.Option.PAGE_TOKEN, pageToken);
throw new UnsupportedOperationException("paging for project lists is not implemented yet.");
public static ProjectGetOption fields(ProjectField... fields) {
return new ProjectGetOption(ResourceManagerRpc.Option.FIELDS, ProjectField.selector(fields));
}
}

/**
* Class for specifying project list options.
*/
public class ProjectListOption extends Option {

private static final long serialVersionUID = 7888768979702012328L;

private ProjectListOption(ResourceManagerRpc.Option option, Object value) {
super(option, value);
}

/**
Expand Down Expand Up @@ -80,15 +125,16 @@ public static ProjectListOption filter(String filter) {
}

/**
* The maximum number of projects to return in the response.
* Returns an option to specify the project's fields to be returned by the RPC call.
*
* The server can return fewer projects than requested. If unspecified, server picks an
* appropriate default. Note: pagination is not yet supported; the server ignores this field.
* Optional.
* If this option is not provided all project fields are returned.
* {@code ProjectListOption.fields} can be used to specify only the fields of interest.
* {@link ProjectField} provides a list of fields that can be used.
*/
public static ProjectListOption pageSize(int pageSize) {
// return new ProjectListOption(ResourceManagerRpc.Option.PAGE_SIZE, pageSize);
throw new UnsupportedOperationException("paging for project lists is not implemented yet.");
public static ProjectListOption fields(ProjectField... fields) {
StringBuilder builder = new StringBuilder();
builder.append("projects(").append(ProjectField.selector(fields)).append(")");
return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, builder.toString());
}
}

Expand Down Expand Up @@ -143,7 +189,7 @@ public static ProjectListOption pageSize(int pageSize) {
* Cloud Resource Manager get</a>
* @throws ResourceManagerException upon failure
*/
ProjectInfo get(String projectId);
ProjectInfo get(String projectId, ProjectGetOption... options);

/**
* Lists the projects visible to the current user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public interface ResourceManagerRpc {

enum Option {
FILTER("filter"),
PAGE_SIZE("maxResults"),
PAGE_TOKEN("pageToken");
FIELDS("fields");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ public class ProjectInfoTest {
private static final Long NUMBER = 123L;
private static final Long CREATE_TIME_MILLIS = 123456789L;
private static final ProjectInfo.State STATE = ProjectInfo.State.DELETE_REQUESTED;
private static final ResourceId PARENT = ResourceId.of("owner-id", "organization");
private static final ProjectInfo FULL_PROJECT_INFO =
ProjectInfo.builder(ID)
.name(NAME)
.labels(LABELS)
.number(NUMBER)
.createTimeMillis(CREATE_TIME_MILLIS)
.state(STATE)
.parent(PARENT)
.build();
private static final ProjectInfo PARTIAL_PROJECT_INFO = ProjectInfo.builder(ID).build();

Expand All @@ -54,15 +52,13 @@ public void testBuilder() {
assertEquals(NUMBER, FULL_PROJECT_INFO.number());
assertEquals(CREATE_TIME_MILLIS, FULL_PROJECT_INFO.createTimeMillis());
assertEquals(STATE, FULL_PROJECT_INFO.state());
assertEquals(PARENT, FULL_PROJECT_INFO.parent());

assertEquals(ID, PARTIAL_PROJECT_INFO.id());
assertEquals(null, PARTIAL_PROJECT_INFO.name());
assertTrue(PARTIAL_PROJECT_INFO.labels().isEmpty());
assertEquals(null, PARTIAL_PROJECT_INFO.number());
assertEquals(null, PARTIAL_PROJECT_INFO.createTimeMillis());
assertEquals(null, PARTIAL_PROJECT_INFO.state());
assertEquals(null, PARTIAL_PROJECT_INFO.parent());
}

@Test
Expand All @@ -87,7 +83,6 @@ public void testEquals() {
.number(NUMBER)
.createTimeMillis(CREATE_TIME_MILLIS)
.state(STATE)
.parent(PARENT)
.build());
compareProjects(PARTIAL_PROJECT_INFO, ProjectInfo.builder(ID).build());
assertNotEquals(FULL_PROJECT_INFO, PARTIAL_PROJECT_INFO);
Expand All @@ -101,7 +96,6 @@ private void compareProjects(ProjectInfo expected, ProjectInfo value) {
assertEquals(expected.number(), value.number());
assertEquals(expected.createTimeMillis(), value.createTimeMillis());
assertEquals(expected.state(), value.state());
assertEquals(expected.parent(), value.parent());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ public class ProjectTest {
private static final Long NUMBER = 123L;
private static final Long CREATE_TIME_MILLIS = 123456789L;
private static final ProjectInfo.State STATE = ProjectInfo.State.DELETE_REQUESTED;
private static final ResourceId PARENT = ResourceId.of("owner-id", "organization");
private static final ProjectInfo PROJECT_INFO =
ProjectInfo.builder(ID)
.name(NAME)
.labels(LABELS)
.number(NUMBER)
.createTimeMillis(CREATE_TIME_MILLIS)
.state(STATE)
.parent(PARENT)
.build();

private ResourceManager resourceManager;
Expand Down
Loading

0 comments on commit 6de6ecf

Please sign in to comment.