diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ProjectInfo.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ProjectInfo.java index 8dbc41d691a4..4e45e1274dbf 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ProjectInfo.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ProjectInfo.java @@ -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. @@ -80,7 +79,6 @@ public static class Builder { private Long number; private State state; private Long createTimeMillis; - private ResourceId parent; Builder() { labels = new HashMap<>(); @@ -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); } @@ -191,7 +175,6 @@ public ProjectInfo build() { this.number = builder.number; this.state = builder.state; this.createTimeMillis = builder.createTimeMillis; - this.parent = builder.parent; } /** @@ -247,15 +230,6 @@ 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()); @@ -263,7 +237,7 @@ public boolean equals(Object obj) { @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) { @@ -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() { @@ -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; } @@ -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(); } } diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceId.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceId.java deleted file mode 100644 index 77d0cfe58866..000000000000 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceId.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2015 Google Inc. All Rights Reserved. - * - * 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.google.gcloud.resourcemanager; - -import static com.google.common.base.Preconditions.checkNotNull; - -import java.io.Serializable; -import java.util.Objects; - -/** - * Represents a Google Cloud Resource Manager Resource ID. - * - * A 'resource' in Google Cloud Platform is a generic term for something you (a developer) may want - * to interact with through one of our API's. Some examples are an AppEngine app, a Compute Engine - * instance, and Cloud SQL database. - */ -public class ResourceId implements Serializable { - - private static final long serialVersionUID = 7928469304338358885L; - private final String id; - private final String type; - - private ResourceId(String id, String type) { - this.id = checkNotNull(id); - this.type = checkNotNull(type); - } - - /** - * Required field for the type-specific ID. - * - * This should correspond to the ID used in the type-specific APIs. - */ - public String id() { - return id; - } - - /** - * Required field representing the resource type this ID is for. - * - * At present, the only valid type is "organization". - */ - public String type() { - return type; - } - - @Override - public boolean equals(Object obj) { - return obj instanceof ResourceId && Objects.equals(toPb(), ((ResourceId) obj).toPb()); - } - - @Override - public int hashCode() { - return Objects.hash(id, type); - } - - /** - * Create a new Resource ID with the given ID and resource type. - * - * The ID should correspond to the ID used in the type-specific APIs. At present, the only valid - * type is "organization". - */ - public static ResourceId of(String id, String type) { - return new ResourceId(id, type); - } - - com.google.api.services.cloudresourcemanager.model.ResourceId toPb() { - com.google.api.services.cloudresourcemanager.model.ResourceId resourceIdPb = - new com.google.api.services.cloudresourcemanager.model.ResourceId(); - resourceIdPb.setId(id); - resourceIdPb.setType(type.toString().toLowerCase()); - return resourceIdPb; - } - - static ResourceId fromPb( - com.google.api.services.cloudresourcemanager.model.ResourceId resourceIdPb) { - return new ResourceId(resourceIdPb.getId(), resourceIdPb.getType()); - } -} diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java index a0405ef5117e..07003c078575 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java @@ -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. * @@ -30,25 +34,66 @@ public interface ResourceManager extends Service { 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 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); } /** @@ -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()); } } @@ -143,7 +189,7 @@ public static ProjectListOption pageSize(int pageSize) { * Cloud Resource Manager get * @throws ResourceManagerException upon failure */ - ProjectInfo get(String projectId); + ProjectInfo get(String projectId, ProjectGetOption... options); /** * Lists the projects visible to the current user. diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java index 854994109e3d..f7e330639399 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java @@ -25,8 +25,7 @@ public interface ResourceManagerRpc { enum Option { FILTER("filter"), - PAGE_SIZE("maxResults"), - PAGE_TOKEN("pageToken"); + FIELDS("fields"); private final String value; diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectInfoTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectInfoTest.java index ef4d213721de..e11962d94273 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectInfoTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectInfoTest.java @@ -34,7 +34,6 @@ 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) @@ -42,7 +41,6 @@ public class ProjectInfoTest { .number(NUMBER) .createTimeMillis(CREATE_TIME_MILLIS) .state(STATE) - .parent(PARENT) .build(); private static final ProjectInfo PARTIAL_PROJECT_INFO = ProjectInfo.builder(ID).build(); @@ -54,7 +52,6 @@ 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()); @@ -62,7 +59,6 @@ public void testBuilder() { 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 @@ -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); @@ -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()); } } diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java index 9201448f60f8..ddb543752ac7 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java @@ -39,7 +39,6 @@ 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) @@ -47,7 +46,6 @@ public class ProjectTest { .number(NUMBER) .createTimeMillis(CREATE_TIME_MILLIS) .state(STATE) - .parent(PARENT) .build(); private ResourceManager resourceManager; diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceIdTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceIdTest.java deleted file mode 100644 index 9fed10759931..000000000000 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ResourceIdTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2015 Google Inc. All Rights Reserved. - * - * 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.google.gcloud.resourcemanager; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; - -import org.junit.Test; - -public class ResourceIdTest { - - private static final String ID = "id"; - private static final String TYPE = "organization"; - private static final ResourceId RESOURCE_ID = ResourceId.of(ID, TYPE); - - @Test - public void testOf() { - assertEquals(ID, RESOURCE_ID.id()); - assertEquals(TYPE, RESOURCE_ID.type()); - } - - @Test - public void testEquals() { - assertEquals(RESOURCE_ID, ResourceId.of(ID, TYPE)); - assertEquals(ID, RESOURCE_ID.id()); - assertEquals(TYPE, RESOURCE_ID.type()); - assertNotEquals(ResourceId.of("another-ID", TYPE), RESOURCE_ID); - } - - @Test - public void testToAndFromPb() { - ResourceId copy = ResourceId.fromPb(RESOURCE_ID.toPb()); - assertEquals(RESOURCE_ID, copy); - assertEquals(ID, copy.id()); - assertEquals(TYPE, copy.type()); - } -} diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index 0929ea18c94a..5b981ce4c77a 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -36,7 +36,6 @@ public class SerializationTest { - private static final ResourceId RESOURCE_ID = ResourceId.of("some id", "organization"); private static final ProjectInfo PARTIAL_PROJECT_INFO = ProjectInfo.builder("id1").build(); private static final ProjectInfo FULL_PROJECT_INFO = ProjectInfo.builder("id") @@ -45,7 +44,6 @@ public class SerializationTest { .number(123L) .state(ProjectInfo.State.ACTIVE) .createTimeMillis(1234L) - .parent(RESOURCE_ID) .build(); private static final PageImpl PAGE_RESULT = new PageImpl<>(null, "c", Collections.singletonList(PARTIAL_PROJECT_INFO)); @@ -67,7 +65,7 @@ public void testServiceOptions() throws Exception { @Test public void testModelAndRequests() throws Exception { - Serializable[] objects = {RESOURCE_ID, PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PAGE_RESULT}; + Serializable[] objects = {PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PAGE_RESULT}; for (Serializable obj : objects) { Object copy = serializeAndDeserialize(obj); assertEquals(obj, obj);