Skip to content

Commit a770e12

Browse files
author
Ajay Kannan
committed
Remove parent and resource ID, add fields options
1 parent d800d1f commit a770e12

8 files changed

Lines changed: 73 additions & 207 deletions

File tree

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ProjectInfo.java

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class ProjectInfo implements Serializable {
3838
private final Long number;
3939
private final State state;
4040
private final Long createTimeMillis;
41-
private final ResourceId parent;
4241

4342
/**
4443
* The project lifecycle state.
@@ -80,7 +79,6 @@ public static class Builder {
8079
private Long number;
8180
private State state;
8281
private Long createTimeMillis;
83-
private ResourceId parent;
8482

8583
Builder() {
8684
labels = new HashMap<>();
@@ -165,20 +163,6 @@ Builder createTimeMillis(Long createTimeMillis) {
165163
return this;
166164
}
167165

168-
/**
169-
* Set the parent of the project.
170-
*
171-
* If this field is left unset in a project creation request, the server will set this field by
172-
* default to the creator of the project. The parent cannot be changed after the server creates
173-
* the project. When calling {@link ResourceManager#replace}, be sure to set the parent of the
174-
* new ProjectInfo instance. Leaving the parent unset or setting it to null in a replace request
175-
* will cause an error.
176-
*/
177-
public Builder parent(ResourceId parent) {
178-
this.parent = parent;
179-
return this;
180-
}
181-
182166
public ProjectInfo build() {
183167
return new ProjectInfo(this);
184168
}
@@ -191,7 +175,6 @@ public ProjectInfo build() {
191175
this.number = builder.number;
192176
this.state = builder.state;
193177
this.createTimeMillis = builder.createTimeMillis;
194-
this.parent = builder.parent;
195178
}
196179

197180
/**
@@ -247,23 +230,14 @@ public Long createTimeMillis() {
247230
return createTimeMillis;
248231
}
249232

250-
/**
251-
* Get the parent of the project.
252-
*
253-
* The parent cannot be changed after the server creates the project.
254-
*/
255-
public ResourceId parent() {
256-
return parent;
257-
}
258-
259233
@Override
260234
public boolean equals(Object obj) {
261235
return obj instanceof ProjectInfo && Objects.equals(toPb(), ((ProjectInfo) obj).toPb());
262236
}
263237

264238
@Override
265239
public int hashCode() {
266-
return Objects.hash(name, id, labels, number, state, createTimeMillis, parent);
240+
return Objects.hash(name, id, labels, number, state, createTimeMillis);
267241
}
268242

269243
public static Builder builder(String id) {
@@ -277,8 +251,7 @@ public Builder toBuilder() {
277251
.labels(labels)
278252
.number(number)
279253
.state(state)
280-
.createTimeMillis(createTimeMillis)
281-
.parent(parent);
254+
.createTimeMillis(createTimeMillis);
282255
}
283256

284257
com.google.api.services.cloudresourcemanager.model.Project toPb() {
@@ -294,9 +267,6 @@ com.google.api.services.cloudresourcemanager.model.Project toPb() {
294267
if (createTimeMillis != null) {
295268
projectPb.setCreateTime(ISODateTimeFormat.dateTime().print(createTimeMillis));
296269
}
297-
if (parent != null) {
298-
projectPb.setParent(parent.toPb());
299-
}
300270
return projectPb;
301271
}
302272

@@ -306,9 +276,6 @@ static ProjectInfo fromPb(com.google.api.services.cloudresourcemanager.model.Pro
306276
if (projectPb.getLabels() != null) {
307277
builder.labels(projectPb.getLabels());
308278
}
309-
if (projectPb.getParent() != null) {
310-
builder.parent(ResourceId.fromPb(projectPb.getParent()));
311-
}
312279
return builder.build();
313280
}
314281
}

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceId.java

Lines changed: 0 additions & 90 deletions
This file was deleted.

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616

1717
package com.google.gcloud.resourcemanager;
1818

19+
import com.google.common.base.Joiner;
20+
import com.google.common.collect.Sets;
1921
import com.google.gcloud.Page;
2022
import com.google.gcloud.Service;
2123
import com.google.gcloud.spi.ResourceManagerRpc;
2224

25+
import java.util.HashSet;
26+
2327
/**
2428
* An interface for Google Cloud Resource Manager.
2529
*
@@ -30,25 +34,70 @@ public interface ResourceManager extends Service<ResourceManagerOptions> {
3034
public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
3135

3236
/**
33-
* Class for specifying project list options.
37+
* The fields of a project.
38+
*
39+
* These values can be used to specify the fields to include of in a partial response when calling
40+
* {@link ResourceManager#get} or {@link ResourceManager#list}. Project ID is always returned,
41+
* even if not specified.
3442
*/
35-
public class ProjectListOption extends Option {
43+
enum ProjectField {
44+
ID("projectId"),
45+
NAME("name"),
46+
LABELS("labels"),
47+
NUMBER("projectNumber"),
48+
STATE("lifecycleState"),
49+
CREATE_TIME("createTime");
50+
51+
private final String selector;
52+
53+
ProjectField(String selector) {
54+
this.selector = selector;
55+
}
3656

37-
private static final long serialVersionUID = 7888768979702012328L;
57+
public String selector() {
58+
return selector;
59+
}
3860

39-
private ProjectListOption(ResourceManagerRpc.Option option, Object value) {
61+
static String selector(ProjectField... fields) {
62+
HashSet<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
63+
fieldStrings.add(ID.selector());
64+
for (ProjectField field : fields) {
65+
fieldStrings.add(field.selector());
66+
}
67+
return Joiner.on(',').join(fieldStrings);
68+
}
69+
}
70+
71+
public class ProjectGetOption extends Option {
72+
73+
private static final long serialVersionUID = 270185129961146874L;
74+
75+
private ProjectGetOption(ResourceManagerRpc.Option option, Object value) {
4076
super(option, value);
4177
}
4278

4379
/**
44-
* Returns an option to specify a page token.
80+
* Returns an option to specify the project's fields to be returned by the RPC call.
4581
*
46-
* The page token (returned from a previous call to list) indicates from where listing should
47-
* continue. Pagination is not yet supported; the server ignores this field. Optional.
82+
* If this option is not provided all project fields are returned.
83+
* {@code ProjectListOption.fields} can be used to specify only the fields of interest. Project
84+
* ID is always returned, even if not specified. {@link ProjectField} provides a list of fields
85+
* that can be used.
4886
*/
49-
public static ProjectListOption pageToken(String pageToken) {
50-
// return new ProjectListOption(ResourceManagerRpc.Option.PAGE_TOKEN, pageToken);
51-
throw new UnsupportedOperationException("paging for project lists is not implemented yet.");
87+
public static ProjectGetOption fields(ProjectField... fields) {
88+
return new ProjectGetOption(ResourceManagerRpc.Option.FIELDS, ProjectField.selector(fields));
89+
}
90+
}
91+
92+
/**
93+
* Class for specifying project list options.
94+
*/
95+
public class ProjectListOption extends Option {
96+
97+
private static final long serialVersionUID = 7888768979702012328L;
98+
99+
private ProjectListOption(ResourceManagerRpc.Option option, Object value) {
100+
super(option, value);
52101
}
53102

54103
/**
@@ -80,15 +129,17 @@ public static ProjectListOption filter(String filter) {
80129
}
81130

82131
/**
83-
* The maximum number of projects to return in the response.
132+
* Returns an option to specify the project's fields to be returned by the RPC call.
84133
*
85-
* The server can return fewer projects than requested. If unspecified, server picks an
86-
* appropriate default. Note: pagination is not yet supported; the server ignores this field.
87-
* Optional.
134+
* If this option is not provided all project fields are returned.
135+
* {@code ProjectListOption.fields} can be used to specify only the fields of interest. Project
136+
* ID is always returned, even if not specified. {@link ProjectField} provides a list of fields
137+
* that can be used.
88138
*/
89-
public static ProjectListOption pageSize(int pageSize) {
90-
// return new ProjectListOption(ResourceManagerRpc.Option.PAGE_SIZE, pageSize);
91-
throw new UnsupportedOperationException("paging for project lists is not implemented yet.");
139+
public static ProjectListOption fields(ProjectField... fields) {
140+
StringBuilder builder = new StringBuilder();
141+
builder.append("projects(").append(ProjectField.selector(fields)).append(")");
142+
return new ProjectListOption(ResourceManagerRpc.Option.FIELDS, builder.toString());
92143
}
93144
}
94145

@@ -143,7 +194,7 @@ public static ProjectListOption pageSize(int pageSize) {
143194
* Cloud Resource Manager get</a>
144195
* @throws ResourceManagerException upon failure
145196
*/
146-
ProjectInfo get(String projectId);
197+
ProjectInfo get(String projectId, ProjectGetOption... options);
147198

148199
/**
149200
* Lists the projects visible to the current user.

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/ResourceManagerRpc.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public interface ResourceManagerRpc {
2525

2626
enum Option {
2727
FILTER("filter"),
28-
PAGE_SIZE("maxResults"),
29-
PAGE_TOKEN("pageToken");
28+
FIELDS("fields");
3029

3130
private final String value;
3231

gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectInfoTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,13 @@ public class ProjectInfoTest {
3434
private static final Long NUMBER = 123L;
3535
private static final Long CREATE_TIME_MILLIS = 123456789L;
3636
private static final ProjectInfo.State STATE = ProjectInfo.State.DELETE_REQUESTED;
37-
private static final ResourceId PARENT = ResourceId.of("owner-id", "organization");
3837
private static final ProjectInfo FULL_PROJECT_INFO =
3938
ProjectInfo.builder(ID)
4039
.name(NAME)
4140
.labels(LABELS)
4241
.number(NUMBER)
4342
.createTimeMillis(CREATE_TIME_MILLIS)
4443
.state(STATE)
45-
.parent(PARENT)
4644
.build();
4745
private static final ProjectInfo PARTIAL_PROJECT_INFO = ProjectInfo.builder(ID).build();
4846

@@ -54,15 +52,13 @@ public void testBuilder() {
5452
assertEquals(NUMBER, FULL_PROJECT_INFO.number());
5553
assertEquals(CREATE_TIME_MILLIS, FULL_PROJECT_INFO.createTimeMillis());
5654
assertEquals(STATE, FULL_PROJECT_INFO.state());
57-
assertEquals(PARENT, FULL_PROJECT_INFO.parent());
5855

5956
assertEquals(ID, PARTIAL_PROJECT_INFO.id());
6057
assertEquals(null, PARTIAL_PROJECT_INFO.name());
6158
assertTrue(PARTIAL_PROJECT_INFO.labels().isEmpty());
6259
assertEquals(null, PARTIAL_PROJECT_INFO.number());
6360
assertEquals(null, PARTIAL_PROJECT_INFO.createTimeMillis());
6461
assertEquals(null, PARTIAL_PROJECT_INFO.state());
65-
assertEquals(null, PARTIAL_PROJECT_INFO.parent());
6662
}
6763

6864
@Test
@@ -87,7 +83,6 @@ public void testEquals() {
8783
.number(NUMBER)
8884
.createTimeMillis(CREATE_TIME_MILLIS)
8985
.state(STATE)
90-
.parent(PARENT)
9186
.build());
9287
compareProjects(PARTIAL_PROJECT_INFO, ProjectInfo.builder(ID).build());
9388
assertNotEquals(FULL_PROJECT_INFO, PARTIAL_PROJECT_INFO);
@@ -101,7 +96,6 @@ private void compareProjects(ProjectInfo expected, ProjectInfo value) {
10196
assertEquals(expected.number(), value.number());
10297
assertEquals(expected.createTimeMillis(), value.createTimeMillis());
10398
assertEquals(expected.state(), value.state());
104-
assertEquals(expected.parent(), value.parent());
10599
}
106100
}
107101

gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/ProjectTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ public class ProjectTest {
3939
private static final Long NUMBER = 123L;
4040
private static final Long CREATE_TIME_MILLIS = 123456789L;
4141
private static final ProjectInfo.State STATE = ProjectInfo.State.DELETE_REQUESTED;
42-
private static final ResourceId PARENT = ResourceId.of("owner-id", "organization");
4342
private static final ProjectInfo PROJECT_INFO =
4443
ProjectInfo.builder(ID)
4544
.name(NAME)
4645
.labels(LABELS)
4746
.number(NUMBER)
4847
.createTimeMillis(CREATE_TIME_MILLIS)
4948
.state(STATE)
50-
.parent(PARENT)
5149
.build();
5250

5351
private ResourceManager resourceManager;

0 commit comments

Comments
 (0)