Skip to content

Commit 55f4fbc

Browse files
author
Ajay Kannan
committed
Fix docs and return null if project not found by get.
1 parent ad8ea4d commit 55f4fbc

File tree

9 files changed

+80
-87
lines changed

9 files changed

+80
-87
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ Google Cloud Resource Manager
194194
Here is a code snippet showing a simple usage example. Note that you must supply Google SDK credentials for this service, not other forms of authentication listed in the [Authentication section](#authentication).
195195
196196
```java
197-
import com.google.common.collect.ImmutableMap;
198197
import com.google.gcloud.resourcemanager.ProjectInfo;
199198
import com.google.gcloud.resourcemanager.ResourceManager;
200199
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
@@ -203,10 +202,10 @@ import java.util.Iterator;
203202
204203
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
205204
ProjectInfo myProject = resourceManager.get("some-project-id-that-I-own");
206-
ProjectInfo newProjectInfo = resourceManager.replace(projectFromServer.toBuilder()
207-
.labels(ImmutableMap.of("launch-status", "in-development")).build());
205+
ProjectInfo newProjectInfo = resourceManager.replace(myProject.toBuilder()
206+
.addLabel("launch-status", "in-development").build());
208207
System.out.println("Updated the labels of project " + newProjectInfo.projectId()
209-
+ " to be " + newProjectInfo.labels() + System.lineSeparator());
208+
+ " to be " + newProjectInfo.labels());
210209
// List all the projects you have permission to view.
211210
Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();
212211
System.out.println("Projects I can view:");

TESTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ You can test against a temporary local Resource Manager by following these steps
8282

8383
This will spawn a server thread that listens to `localhost` at an ephemeral port for Resource Manager requests.
8484

85-
2. In your program, create and use a Resource Manager service object whose host is set host to `localhost` at the appropriate port. For example:
85+
2. In your program, create and use a Resource Manager service object whose host is set to `localhost` at the appropriate port. For example:
8686

8787
```java
8888
ResourceManager resourceManager = ResourceManagerOptions.builder()
@@ -97,7 +97,7 @@ You can test against a temporary local Resource Manager by following these steps
9797
helper.stop();
9898
```
9999

100-
This method will block a short amount of time until the server thread has been terminated.
100+
This method will block until the server thread has been terminated.
101101

102102
#### On a remote machine
103103

gcloud-java-resourcemanager/README.md

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ with the Cloud Resource Manager using this client Library.
5050
Getting Started
5151
---------------
5252
#### Prerequisites
53-
You will also need to set up the local development environment by [installing the Google Cloud SDK](https://cloud.google.com/sdk/) and running the following commands in command line: `gcloud auth login`.
53+
You will need to set up the local development environment by [installing the Google Cloud SDK](https://cloud.google.com/sdk/) and running the following command in command line: `gcloud auth login`.
5454

5555
> Note: You don't need a project ID to use this service. If you have a project ID set in the Google Cloud SDK, you can unset it by typing `gcloud config unset project` in command line.
5656
@@ -68,7 +68,7 @@ ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().servi
6868
```
6969

7070
#### Creating a project
71-
All you need to create a project is a globally unique project ID. You can also optionally attach a non-unique name and labels to your project. Read more about naming guidelines for project IDs, names, and labels [here](https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects). To create a project, add the following imports at the top of your file:
71+
All you need to create a project is a globally unique project ID. You can also optionally attach a non-unique name and labels to your project. Read more about naming guidelines for project IDs, names, and labels [here](https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects). To create a project, add the following import at the top of your file:
7272

7373
```java
7474
import com.google.gcloud.resourcemanager.ProjectInfo;
@@ -84,7 +84,7 @@ ProjectInfo myProject = resourceManager.create(ProjectInfo.builder(myProjectId).
8484
Note that the return value from `create` is a `ProjectInfo` that includes additional read-only information, like creation time, project number, and lifecycle state. Read more about these fields on the [Projects page](https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects).
8585

8686
#### Getting a specific project
87-
You can load a project if you know it's project ID and have read permissions to the project. For example, say we wanted to get the project we just created. We can do the following:
87+
You can load a project if you know it's project ID and have read permissions to the project. For example, to get the project we just created we can do the following:
8888

8989
```java
9090
ProjectInfo projectFromServer = resourceManager.get(myProjectId);
@@ -93,23 +93,17 @@ ProjectInfo projectFromServer = resourceManager.get(myProjectId);
9393
#### Editing a project
9494
To edit a project, create a new `ProjectInfo` object and pass it in to the `ResourceManager.replace` method.
9595

96-
Suppose that you want to add a label for the newly created project to denote that it's launch status is "in development". Import the following:
97-
98-
```java
99-
import com.google.common.collect.ImmutableMap;
100-
```
101-
102-
Then add the following code to your program:
96+
For example, to add a label for the newly created project to denote that it's launch status is "in development", add the following code:
10397

10498
```java
10599
ProjectInfo newProjectInfo = resourceManager.replace(projectFromServer.toBuilder()
106-
.labels(ImmutableMap.of("launch-status", "in-development")).build());
100+
.addLabel("launch-status", "in-development").build());
107101
```
108102

109103
Note that the values of the project you pass in to `replace` overwrite the server's values for non-read-only fields, namely `projectName` and `labels`. For example, if you create a project with `projectName` "some-project-name" and subsequently call replace using a `ProjectInfo` object that didn't set the `projectName`, then the server will unset the project's name. The server ignores any attempted changes to the read-only fields `projectNumber`, `lifecycleState`, and `createTime`. The `projectId` cannot change.
110104

111105
#### Listing all projects
112-
Suppose that we want list of all projects for which we have read permissions. Add the following import:
106+
Suppose that we want a list of all projects for which we have read permissions. Add the following import:
113107

114108
```java
115109
import java.util.Iterator;
@@ -130,7 +124,6 @@ while (projectIterator.hasNext()) {
130124
Here we put together all the code shown above into one program. This program assumes that you are running from your own desktop.
131125

132126
```java
133-
import com.google.common.collect.ImmutableMap;
134127
import com.google.gcloud.resourcemanager.ProjectInfo;
135128
import com.google.gcloud.resourcemanager.ResourceManager;
136129
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
@@ -150,14 +143,13 @@ public class GcloudJavaResourceManagerExample {
150143

151144
// Get a project from the server.
152145
ProjectInfo projectFromServer = resourceManager.get(myProjectId);
153-
System.out.println("Got project " + projectFromServer.projectId() + " from the server."
154-
+ System.lineSeparator());
146+
System.out.println("Got project " + projectFromServer.projectId() + " from the server.");
155147

156148
// Update a project
157-
ProjectInfo newProjectInfo = resourceManager.replace(projectFromServer.toBuilder()
158-
.labels(ImmutableMap.of("launch-status", "in-development")).build());
149+
ProjectInfo newProjectInfo = resourceManager.replace(myProject.toBuilder()
150+
.addLabel("launch-status", "in-development").build());
159151
System.out.println("Updated the labels of project " + newProjectInfo.projectId()
160-
+ " to be " + newProjectInfo.labels() + System.lineSeparator());
152+
+ " to be " + newProjectInfo.labels());
161153

162154
// List all the projects you have permission to view.
163155
Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface ResourceManager extends Service<ResourceManagerOptions> {
3636
/**
3737
* The fields of a project.
3838
*
39-
* These values can be used to specify the fields to include in a partial response when calling
39+
* <p>These values can be used to specify the fields to include in a partial response when calling
4040
* {@link ResourceManager#get} or {@link ResourceManager#list}. Project ID is always returned,
4141
* even if not specified.
4242
*/
@@ -82,7 +82,7 @@ private ProjectGetOption(ResourceManagerRpc.Option option, Object value) {
8282
/**
8383
* Returns an option to specify the project's fields to be returned by the RPC call.
8484
*
85-
* If this option is not provided all project fields are returned.
85+
* <p>If this option is not provided all project fields are returned.
8686
* {@code ProjectGetOption.fields} can be used to specify only the fields of interest. Project
8787
* ID is always returned, even if not specified. {@link ProjectField} provides a list of fields
8888
* that can be used.
@@ -106,17 +106,17 @@ private ProjectListOption(ResourceManagerRpc.Option option, Object value) {
106106
/**
107107
* Returns an option to specify a filter.
108108
*
109-
* Filter rules are case insensitive. The fields eligible for filtering are:
109+
* <p>Filter rules are case insensitive. The fields eligible for filtering are:
110110
* <ul>
111111
* <li>name
112112
* <li>project ID
113113
* <li>labels.key, where key is the name of a label
114114
* </ul>
115115
*
116-
* You can specify multiple filters by adding a space between each filter. Multiple filters
116+
* <p>You can specify multiple filters by adding a space between each filter. Multiple filters
117117
* are composed using "and".
118118
*
119-
* Some examples of filters:
119+
* <p>Some examples of filters:
120120
* <ul>
121121
* <li> name:* The project has a name.
122122
* <li> name:Howl The project's name is Howl or howl.
@@ -135,7 +135,7 @@ public static ProjectListOption filter(String filter) {
135135
/**
136136
* Returns an option to specify a page token.
137137
*
138-
* The page token (returned from a previous call to list) indicates from where listing should
138+
* <p>The page token (returned from a previous call to list) indicates from where listing should
139139
* continue.
140140
*/
141141
public static ProjectListOption pageToken(String pageToken) {
@@ -145,7 +145,7 @@ public static ProjectListOption pageToken(String pageToken) {
145145
/**
146146
* The maximum number of projects to return per RPC.
147147
*
148-
* The server can return fewer projects than requested. When there are more results than the
148+
* <p>The server can return fewer projects than requested. When there are more results than the
149149
* page size, the server will return a page token that can be used to fetch other results.
150150
* Note: pagination is not yet supported; the server currently ignores this field and returns
151151
* all results.
@@ -157,7 +157,7 @@ public static ProjectListOption pageSize(int pageSize) {
157157
/**
158158
* Returns an option to specify the project's fields to be returned by the RPC call.
159159
*
160-
* If this option is not provided all project fields are returned.
160+
* <p>If this option is not provided all project fields are returned.
161161
* {@code ProjectListOption.fields} can be used to specify only the fields of interest. Project
162162
* ID is always returned, even if not specified. {@link ProjectField} provides a list of fields
163163
* that can be used.
@@ -172,7 +172,7 @@ public static ProjectListOption fields(ProjectField... fields) {
172172
/**
173173
* Create a new project.
174174
*
175-
* Initially, the project resource is owned by its creator exclusively. The creator can later
175+
* <p>Initially, the project resource is owned by its creator exclusively. The creator can later
176176
* grant permission to others to read or update the project. Several APIs are activated
177177
* automatically for the project, including Google Cloud Storage.
178178
*
@@ -189,7 +189,7 @@ public static ProjectListOption fields(ProjectField... fields) {
189189
/**
190190
* Marks the project identified by the specified project ID for deletion.
191191
*
192-
* This method will only affect the project if the following criteria are met:
192+
* <p>This method will only affect the project if the following criteria are met:
193193
* <ul>
194194
* <li>The project does not have a billing account associated with it.
195195
* <li>The project has a lifecycle state of {@link ProjectInfo.State#ACTIVE}.
@@ -213,7 +213,8 @@ public static ProjectListOption fields(ProjectField... fields) {
213213
/**
214214
* Retrieves the project identified by the specified project ID.
215215
*
216-
* The caller must have read permissions for this project.
216+
* <p>The caller must have read permissions for this project. Returns {@code null} if project
217+
* not found.
217218
*
218219
* @see <a
219220
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/get">Cloud
@@ -225,7 +226,8 @@ public static ProjectListOption fields(ProjectField... fields) {
225226
/**
226227
* Lists the projects visible to the current user.
227228
*
228-
* This method returns projects in an unspecified order. New projects do not necessarily appear at
229+
* <p>This method returns projects in an unspecified order. New projects do not necessarily appear
230+
* at
229231
* the end of the list. Use {@link ProjectListOption} to filter this list, set page size, and set
230232
* page tokens. Note that pagination is currently not implemented by the Cloud Resource Manager
231233
* API.
@@ -241,7 +243,7 @@ public static ProjectListOption fields(ProjectField... fields) {
241243
/**
242244
* Replaces the attributes of the project.
243245
*
244-
* The caller must have modify permissions for this project.
246+
* <p>The caller must have modify permissions for this project.
245247
*
246248
* @see <a
247249
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update">Cloud
@@ -254,7 +256,7 @@ public static ProjectListOption fields(ProjectField... fields) {
254256
/**
255257
* Restores the project identified by the specified project ID.
256258
*
257-
* You can only use this method for a project that has a lifecycle state of
259+
* <p>You can only use this method for a project that has a lifecycle state of
258260
* {@link ProjectInfo.State#DELETE_REQUESTED}. After deletion starts, as indicated by a lifecycle
259261
* state of {@link ProjectInfo.State#DELETE_IN_PROGRESS}, the project cannot be restored. The
260262
* caller must have modify permissions for this project.

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class ResourceManagerImpl
4242

4343
private static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() {
4444

45+
private static final long serialVersionUID = 2091576149969931704L;
46+
4547
@Override
4648
public RetryResult afterEval(Exception exception, RetryResult retryResult) {
4749
return Interceptor.RetryResult.CONTINUE_EVALUATION;
@@ -69,14 +71,13 @@ public RetryResult beforeEval(Exception exception) {
6971
}
7072

7173
@Override
72-
public ProjectInfo create(ProjectInfo project) {
73-
final com.google.api.services.cloudresourcemanager.model.Project projectPb = project.toPb();
74+
public ProjectInfo create(final ProjectInfo project) {
7475
try {
7576
return ProjectInfo.fromPb(runWithRetries(
7677
new Callable<com.google.api.services.cloudresourcemanager.model.Project>() {
7778
@Override
7879
public com.google.api.services.cloudresourcemanager.model.Project call() {
79-
return resourceManagerRpc.create(projectPb);
80+
return resourceManagerRpc.create(project.toPb());
8081
}
8182
}, options().retryParams(), EXCEPTION_HANDLER));
8283
} catch (RetryHelperException e) {
@@ -103,20 +104,24 @@ public Void call() {
103104
public ProjectInfo get(final String projectId, ProjectGetOption... options) {
104105
final Map<ResourceManagerRpc.Option, ?> optionsMap = optionMap(options);
105106
try {
106-
return ProjectInfo.fromPb(runWithRetries(
107+
com.google.api.services.cloudresourcemanager.model.Project answer = runWithRetries(
107108
new Callable<com.google.api.services.cloudresourcemanager.model.Project>() {
108109
@Override
109110
public com.google.api.services.cloudresourcemanager.model.Project call() {
110111
return resourceManagerRpc.get(projectId, optionsMap);
111112
}
112-
}, options().retryParams(), EXCEPTION_HANDLER));
113+
}, options().retryParams(), EXCEPTION_HANDLER);
114+
return answer == null ? null : ProjectInfo.fromPb(answer);
113115
} catch (RetryHelperException e) {
114116
throw ResourceManagerException.translateAndThrow(e);
115117
}
116118
}
117119

118120
private abstract static class BasePageFetcher<T extends Serializable>
119121
implements PageImpl.NextPageFetcher<T> {
122+
123+
private static final long serialVersionUID = -5560906434575940205L;
124+
120125
protected final Map<ResourceManagerRpc.Option, ?> requestOptions;
121126
protected final ResourceManagerOptions serviceOptions;
122127

@@ -137,6 +142,9 @@ private abstract static class BasePageFetcher<T extends Serializable>
137142
}
138143

139144
private static class ProjectPageFetcher extends BasePageFetcher<ProjectInfo> {
145+
146+
private static final long serialVersionUID = -533306655445189098L;
147+
140148
ProjectPageFetcher(ResourceManagerOptions serviceOptions, String cursor,
141149
Map<ResourceManagerRpc.Option, ?> optionMap) {
142150
super(serviceOptions, cursor, optionMap);
@@ -153,18 +161,15 @@ public Page<ProjectInfo> list(ProjectListOption... options) {
153161
return listProjects(options(), optionMap(options));
154162
}
155163

156-
private static Page<ProjectInfo> listProjects(
157-
final ResourceManagerOptions serviceOptions,
164+
private static Page<ProjectInfo> listProjects(final ResourceManagerOptions serviceOptions,
158165
final Map<ResourceManagerRpc.Option, ?> optionsMap) {
159166
try {
160167
Tuple<String, Iterable<com.google.api.services.cloudresourcemanager.model.Project>> result =
161-
runWithRetries(
162-
new Callable<Tuple<String, Iterable<com.google.api.services.cloudresourcemanager.model
163-
.Project>>>() {
168+
runWithRetries(new Callable<Tuple<String,
169+
Iterable<com.google.api.services.cloudresourcemanager.model.Project>>>() {
164170
@Override
165-
public Tuple<String, Iterable<com.google.api.services.cloudresourcemanager.model
166-
.Project>>
167-
call() {
171+
public Tuple<String,
172+
Iterable<com.google.api.services.cloudresourcemanager.model.Project>> call() {
168173
return serviceOptions.rpc().list(optionsMap);
169174
}
170175
},
@@ -190,14 +195,13 @@ public ProjectInfo apply(
190195
}
191196

192197
@Override
193-
public ProjectInfo replace(ProjectInfo newProject) {
194-
final com.google.api.services.cloudresourcemanager.model.Project projectPb = newProject.toPb();
198+
public ProjectInfo replace(final ProjectInfo newProject) {
195199
try {
196200
return ProjectInfo.fromPb(runWithRetries(
197201
new Callable<com.google.api.services.cloudresourcemanager.model.Project>() {
198202
@Override
199203
public com.google.api.services.cloudresourcemanager.model.Project call() {
200-
return resourceManagerRpc.replace(projectPb);
204+
return resourceManagerRpc.replace(newProject.toPb());
201205
}
202206
}, options().retryParams(), EXCEPTION_HANDLER));
203207
} catch (RetryHelperException e) {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ public class ResourceManagerOptions
2929

3030
private static final long serialVersionUID = 538303101192527452L;
3131
private static final String GCRM_SCOPE = "https://www.googleapis.com/auth/cloud-platform";
32-
private static final String READ_ONLY_GCRM_SCOPE =
33-
"https://www.googleapis.com/auth/cloud-platform.read-only";
34-
private static final Set<String> SCOPES = ImmutableSet.of(GCRM_SCOPE, READ_ONLY_GCRM_SCOPE);
32+
private static final Set<String> SCOPES = ImmutableSet.of(GCRM_SCOPE);
3533
private static final String DEFAULT_HOST = "https://cloudresourcemanager.googleapis.com";
3634

3735
public static class DefaultResourceManagerFactory implements ResourceManagerFactory {

0 commit comments

Comments
 (0)