Skip to content

Commit ad8ea4d

Browse files
author
Ajay Kannan
committed
ResourceManagerImpl + docs
1 parent e35a1dd commit ad8ea4d

File tree

10 files changed

+806
-19
lines changed

10 files changed

+806
-19
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This client supports the following Google Cloud Platform services:
1414

1515
- [Google Cloud Datastore] (#google-cloud-datastore)
1616
- [Google Cloud Storage] (#google-cloud-storage)
17+
- [Google Cloud Resource Manager] (#google-cloud-resource-manager)
1718

1819
> Note: This client is a work-in-progress, and may occasionally
1920
> make backwards-incompatible changes.
@@ -182,6 +183,38 @@ if (blob == null) {
182183
}
183184
```
184185
186+
Google Cloud Resource Manager
187+
----------------------
188+
189+
- [API Documentation][resourcemanager-api]
190+
- [Official Documentation][cloud-resourcemanager-docs]
191+
192+
#### Preview
193+
194+
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).
195+
196+
```java
197+
import com.google.common.collect.ImmutableMap;
198+
import com.google.gcloud.resourcemanager.ProjectInfo;
199+
import com.google.gcloud.resourcemanager.ResourceManager;
200+
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
201+
202+
import java.util.Iterator;
203+
204+
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
205+
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());
208+
System.out.println("Updated the labels of project " + newProjectInfo.projectId()
209+
+ " to be " + newProjectInfo.labels() + System.lineSeparator());
210+
// List all the projects you have permission to view.
211+
Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();
212+
System.out.println("Projects I can view:");
213+
while (projectIterator.hasNext()) {
214+
System.out.println(projectIterator.next().projectId());
215+
}
216+
```
217+
185218
Troubleshooting
186219
---------------
187220
@@ -241,3 +274,6 @@ Apache 2.0 - See [LICENSE] for more information.
241274
[cloud-storage-create-bucket]: https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets
242275
[cloud-storage-activation]: https://cloud.google.com/storage/docs/signup
243276
[storage-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/storage/package-summary.html
277+
278+
[resourcemanager-api]:http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/resourcemanager/package-summary.html
279+
[cloud-resourcemanager-docs]:https://cloud.google.com/resource-manager/

TESTING.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,51 @@ Here is an example that clears the bucket created in Step 3 with a timeout of 5
6565
RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
6666
```
6767

68+
### Testing code that uses Resource Manager
69+
70+
#### On your machine
71+
72+
You can test against a temporary local Resource Manager by following these steps:
73+
74+
1. Before running your testing code, start the Resource Manager emulator `LocalResourceManagerHelper`. This can be done as follows:
75+
76+
```java
77+
import com.google.gcloud.resourcemanager.testing.LocalResourceManagerHelper;
78+
79+
LocalResourceManagerHelper helper = LocalResourceManagerHelper.create();
80+
helper.start();
81+
```
82+
83+
This will spawn a server thread that listens to `localhost` at an ephemeral port for Resource Manager requests.
84+
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:
86+
87+
```java
88+
ResourceManager resourceManager = ResourceManagerOptions.builder()
89+
.host("http://localhost:" + helper.port()).build().service();
90+
```
91+
92+
3. Run your tests.
93+
94+
4. Stop the Resource Manager emulator.
95+
96+
```java
97+
helper.stop();
98+
```
99+
100+
This method will block a short amount of time until the server thread has been terminated.
101+
102+
#### On a remote machine
103+
104+
You can test against a remote Resource Manager emulator as well. To do this, set the host to the hostname of the remote machine, like the example below.
105+
106+
```java
107+
ResourceManager resourceManager = ResourceManagerOptions.builder()
108+
.host("http://<hostname of machine>:<port>").build();
109+
```
110+
111+
Note that the remote Resource Manager emulator must be running before your tests are run.
112+
113+
68114

69115
[cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts

gcloud-java-resourcemanager/README.md

Lines changed: 138 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,153 @@ Example Application
2727
Authentication
2828
--------------
2929

30-
See the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) section in the base directory's README.
30+
Unlike other `gcloud-java` service libraries, `gcloud-java-resourcemanager` only accepts Google Cloud SDK credentials at this time. If you are having trouble authenticating, it may be that you have other types of credentials that override your Google Cloud SDK credentials. See more about Google Cloud SDK credentials and credential precedence in the global README's [Authentication section](https://github.com/GoogleCloudPlatform/gcloud-java#authentication).
3131

3232
About Google Cloud Resource Manager
3333
-----------------------------------
3434

35-
Google [Cloud Resource Manager][cloud-resourcemanager] provides a programmatic way to manage your Google Cloud Platform projects. Google Cloud Resource Manager is currently in beta and may occasionally make backwards incompatible changes.
35+
Google [Cloud Resource Manager][cloud-resourcemanager] provides a programmatic way to manage your Google Cloud Platform projects. With this API, you can do the following:
36+
37+
* Get a list of all projects associated with an account.
38+
* Create new projects.
39+
* Update existing projects.
40+
* Delete projects.
41+
* Undelete, or recover, projects that you don't want to delete.
42+
43+
Google Cloud Resource Manager is currently in beta and may occasionally make backwards incompatible changes.
3644

3745
Be sure to activate the Google Cloud Resource Manager API on the Developer's Console to use Resource Manager from your project.
3846

3947
See the ``gcloud-java`` API [Resource Manager documentation][resourcemanager-api] to learn how to interact
4048
with the Cloud Resource Manager using this client Library.
4149

42-
<!-- TODO(ajaykannan): add code snippet -->
50+
Getting Started
51+
---------------
52+
#### 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`.
54+
55+
> 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.
56+
57+
#### Installation and setup
58+
You'll need to obtain the `gcloud-java-resourcemanager` library. See the [Quickstart](#quickstart) section to add `gcloud-java-resourcemanager` as a dependency in your code.
59+
60+
#### Creating an authorized service object
61+
To make authenticated requests to Google Cloud Resource Manager, you must create a service object with Google Cloud SDK credentials. You can then make API calls by calling methods on the Resource Manager service object. The simplest way to authenticate is to use [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials). These credentials are automatically inferred from your environment, so you only need the following code to create your service object:
62+
63+
```java
64+
import com.google.gcloud.resourcemanager.ResourceManager;
65+
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
66+
67+
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
68+
```
69+
70+
#### 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:
72+
73+
```java
74+
import com.google.gcloud.resourcemanager.ProjectInfo;
75+
```
76+
77+
Then add the following code to create a project (be sure to change `myProjectId` to be something unique).
78+
79+
```java
80+
String myProjectId = "my-globally-unique-project-id"; // Change to a unique project ID.
81+
ProjectInfo myProject = resourceManager.create(ProjectInfo.builder(myProjectId).build());
82+
```
83+
84+
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).
85+
86+
#### 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:
88+
89+
```java
90+
ProjectInfo projectFromServer = resourceManager.get(myProjectId);
91+
```
92+
93+
#### Editing a project
94+
To edit a project, create a new `ProjectInfo` object and pass it in to the `ResourceManager.replace` method.
95+
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:
103+
104+
```java
105+
ProjectInfo newProjectInfo = resourceManager.replace(projectFromServer.toBuilder()
106+
.labels(ImmutableMap.of("launch-status", "in-development")).build());
107+
```
108+
109+
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.
110+
111+
#### Listing all projects
112+
Suppose that we want list of all projects for which we have read permissions. Add the following import:
113+
114+
```java
115+
import java.util.Iterator;
116+
```
117+
118+
Then add the following code to print a list of projects you can view:
119+
120+
```java
121+
Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();
122+
System.out.println("Projects I can view:");
123+
while (projectIterator.hasNext()) {
124+
System.out.println(projectIterator.next().projectId());
125+
}
126+
```
127+
128+
#### Complete source code
129+
130+
Here we put together all the code shown above into one program. This program assumes that you are running from your own desktop.
131+
132+
```java
133+
import com.google.common.collect.ImmutableMap;
134+
import com.google.gcloud.resourcemanager.ProjectInfo;
135+
import com.google.gcloud.resourcemanager.ResourceManager;
136+
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
137+
138+
import java.util.Iterator;
139+
140+
public class GcloudJavaResourceManagerExample {
141+
142+
public static void main(String[] args) {
143+
// Create Resource Manager service object.
144+
// By default, credentials are inferred from the runtime environment.
145+
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
146+
147+
// Create a project.
148+
String myProjectId = "my-globally-unique-project-id"; // Change to a unique project ID.
149+
ProjectInfo myProject = resourceManager.create(ProjectInfo.builder(myProjectId).build());
150+
151+
// Get a project from the server.
152+
ProjectInfo projectFromServer = resourceManager.get(myProjectId);
153+
System.out.println("Got project " + projectFromServer.projectId() + " from the server."
154+
+ System.lineSeparator());
155+
156+
// Update a project
157+
ProjectInfo newProjectInfo = resourceManager.replace(projectFromServer.toBuilder()
158+
.labels(ImmutableMap.of("launch-status", "in-development")).build());
159+
System.out.println("Updated the labels of project " + newProjectInfo.projectId()
160+
+ " to be " + newProjectInfo.labels() + System.lineSeparator());
161+
162+
// List all the projects you have permission to view.
163+
Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();
164+
System.out.println("Projects I can view:");
165+
while (projectIterator.hasNext()) {
166+
System.out.println(projectIterator.next().projectId());
167+
}
168+
}
169+
}
170+
```
43171

44172
Java Versions
45173
-------------
46174

47175
Java 7 or above is required for using this client.
48176

49-
<!-- TODO(ajaykannan): add this in once the RemoteGCRMHelper class is functional -->
50-
51177
Versioning
52178
----------
53179

@@ -57,6 +183,13 @@ It is currently in major version zero (``0.y.z``), which means that anything
57183
may change at any time and the public API should not be considered
58184
stable.
59185

186+
Testing
187+
-------
188+
189+
This library has tools to help write tests for code that uses Resource Manager.
190+
191+
See [TESTING] to read more about testing.
192+
60193
Contributing
61194
------------
62195

gcloud-java-resourcemanager/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
<artifactId>google-api-services-cloudresourcemanager</artifactId>
2828
<version>v1beta1-rev6-1.19.0</version>
2929
<scope>compile</scope>
30+
<exclusions>
31+
<exclusion>
32+
<groupId>com.google.guava</groupId>
33+
<artifactId>guava-jdk5</artifactId>
34+
</exclusion>
35+
</exclusions>
3036
</dependency>
3137
<dependency>
3238
<groupId>junit</groupId>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
/**
3333
* A Google Cloud Resource Manager project metadata object.
34-
*
3534
* A Project is a high-level Google Cloud Platform entity. It is a container for ACLs, APIs,
3635
* AppEngine Apps, VMs, and other Google Cloud Platform resources.
3736
*/
@@ -324,7 +323,7 @@ com.google.api.services.cloudresourcemanager.model.Project toPb() {
324323
projectPb.setLifecycleState(state.toString());
325324
}
326325
if (createTimeMillis != null) {
327-
projectPb.setCreateTime(ISODateTimeFormat.dateTime().print(createTimeMillis));
326+
projectPb.setCreateTime(ISODateTimeFormat.dateTime().withZoneUTC().print(createTimeMillis));
328327
}
329328
if (parent != null) {
330329
projectPb.setParent(parent.toPb());

0 commit comments

Comments
 (0)