Skip to content

Commit 2650901

Browse files
committed
Merge pull request #457 from ajkannan/resource-manager-impl
ResourceManagerImpl + docs
2 parents e35a1dd + ce2954a commit 2650901

13 files changed

Lines changed: 865 additions & 79 deletions

File tree

README.md

Lines changed: 35 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,37 @@ 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.gcloud.resourcemanager.ProjectInfo;
198+
import com.google.gcloud.resourcemanager.ResourceManager;
199+
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
200+
201+
import java.util.Iterator;
202+
203+
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
204+
ProjectInfo myProject = resourceManager.get("some-project-id"); // Use an existing project's ID
205+
ProjectInfo newProjectInfo = resourceManager.replace(myProject.toBuilder()
206+
.addLabel("launch-status", "in-development").build());
207+
System.out.println("Updated the labels of project " + newProjectInfo.projectId()
208+
+ " to be " + newProjectInfo.labels());
209+
// List all the projects you have permission to view.
210+
Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();
211+
System.out.println("Projects I can view:");
212+
while (projectIterator.hasNext()) {
213+
System.out.println(projectIterator.next().projectId());
214+
}
215+
```
216+
185217
Troubleshooting
186218
---------------
187219
@@ -241,3 +273,6 @@ Apache 2.0 - See [LICENSE] for more information.
241273
[cloud-storage-create-bucket]: https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets
242274
[cloud-storage-activation]: https://cloud.google.com/storage/docs/signup
243275
[storage-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/storage/package-summary.html
276+
277+
[resourcemanager-api]:http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/resourcemanager/package-summary.html
278+
[cloud-resourcemanager-docs]:https://cloud.google.com/resource-manager/

TESTING.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
## gcloud-java tools for testing
22

3-
This library provides tools to help write tests for code that uses gcloud-java services.
3+
This library provides tools to help write tests for code that uses the following gcloud-java services:
4+
5+
- [Datastore] (#testing-code-that-uses-datastore)
6+
- [Storage] (#testing-code-that-uses-storage)
7+
- [Resource Manager] (#testing-code-that-uses-resource-manager)
48

59
### Testing code that uses Datastore
610

@@ -65,5 +69,38 @@ Here is an example that clears the bucket created in Step 3 with a timeout of 5
6569
RemoteGcsHelper.forceDelete(storage, bucket, 5, TimeUnit.SECONDS);
6670
```
6771

72+
### Testing code that uses Resource Manager
73+
74+
#### On your machine
75+
76+
You can test against a temporary local Resource Manager by following these steps:
77+
78+
1. Before running your testing code, start the Resource Manager emulator `LocalResourceManagerHelper`. This can be done as follows:
79+
80+
```java
81+
import com.google.gcloud.resourcemanager.testing.LocalResourceManagerHelper;
82+
83+
LocalResourceManagerHelper helper = LocalResourceManagerHelper.create();
84+
helper.start();
85+
```
86+
87+
This will spawn a server thread that listens to `localhost` at an ephemeral port for Resource Manager requests.
88+
89+
2. In your program, create and use a Resource Manager service object whose host is set to `localhost` at the appropriate port. For example:
90+
91+
```java
92+
ResourceManager resourceManager = LocalResourceManagerHelper.options().service();
93+
```
94+
95+
3. Run your tests.
96+
97+
4. Stop the Resource Manager emulator.
98+
99+
```java
100+
helper.stop();
101+
```
102+
103+
This method will block until the server thread has been terminated.
104+
68105

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

gcloud-java-resourcemanager/README.md

Lines changed: 146 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,22 @@ Java idiomatic client for [Google Cloud Resource Manager] (https://cloud.google.
1717
1818
Quickstart
1919
----------
20-
This library is currently under development and will be available soon!
21-
<!--TODO(ajaykannan): add in pom.xml snippet once gcloud-java-resourcemanager becomes available on maven -->
20+
If you are using Maven, add this to your pom.xml file
21+
```xml
22+
<dependency>
23+
<groupId>com.google.gcloud</groupId>
24+
<artifactId>gcloud-java-resourcemanager</artifactId>
25+
<version>0.1.0</version>
26+
</dependency>
27+
```
28+
If you are using Gradle, add this to your dependencies
29+
```Groovy
30+
compile 'com.google.gcloud:gcloud-java-resourcemanager:jar:0.1.0'
31+
```
32+
If you are using SBT, add this to your dependencies
33+
```Scala
34+
libraryDependencies += "com.google.gcloud" % "gcloud-java-resourcemanager" % "0.1.0"
35+
```
2236

2337
<!-- TODO(ajaykannan): once the API becomes usable, make an example application
2438
Example Application
@@ -27,27 +41,145 @@ Example Application
2741
Authentication
2842
--------------
2943

30-
See the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) section in the base directory's README.
44+
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).
3145

3246
About Google Cloud Resource Manager
3347
-----------------------------------
3448

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.
49+
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:
50+
51+
* Get a list of all projects associated with an account.
52+
* Create new projects.
53+
* Update existing projects.
54+
* Delete projects.
55+
* Undelete projects that you don't want to delete.
56+
57+
Google Cloud Resource Manager is currently in beta and may occasionally make backwards incompatible changes.
3658

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

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

42-
<!-- TODO(ajaykannan): add code snippet -->
64+
Getting Started
65+
---------------
66+
#### Prerequisites
67+
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`.
68+
69+
> 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.
70+
71+
#### Installation and setup
72+
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.
73+
74+
#### Creating an authorized service object
75+
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:
76+
77+
```java
78+
import com.google.gcloud.resourcemanager.ResourceManager;
79+
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
80+
81+
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
82+
```
83+
84+
#### Creating a project
85+
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:
86+
87+
```java
88+
import com.google.gcloud.resourcemanager.ProjectInfo;
89+
```
90+
91+
Then add the following code to create a project (be sure to change `myProjectId` to your own unique project ID).
92+
93+
```java
94+
String myProjectId = "my-globally-unique-project-id"; // Change to a unique project ID.
95+
ProjectInfo myProject = resourceManager.create(ProjectInfo.builder(myProjectId).build());
96+
```
97+
98+
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).
99+
100+
#### Getting a specific project
101+
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:
102+
103+
```java
104+
ProjectInfo projectFromServer = resourceManager.get(myProjectId);
105+
```
106+
107+
#### Editing a project
108+
To edit a project, create a new `ProjectInfo` object and pass it in to the `ResourceManager.replace` method.
109+
110+
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:
111+
112+
```java
113+
ProjectInfo newProjectInfo = resourceManager.replace(projectFromServer.toBuilder()
114+
.addLabel("launch-status", "in-development").build());
115+
```
116+
117+
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.
118+
119+
#### Listing all projects
120+
Suppose that we want a list of all projects for which we have read permissions. Add the following import:
121+
122+
```java
123+
import java.util.Iterator;
124+
```
125+
126+
Then add the following code to print a list of projects you can view:
127+
128+
```java
129+
Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();
130+
System.out.println("Projects I can view:");
131+
while (projectIterator.hasNext()) {
132+
System.out.println(projectIterator.next().projectId());
133+
}
134+
```
135+
136+
#### Complete source code
137+
138+
Here we put together all the code shown above into one program. This program assumes that you are running from your own desktop and used the Google Cloud SDK to authenticate yourself.
139+
140+
```java
141+
import com.google.gcloud.resourcemanager.ProjectInfo;
142+
import com.google.gcloud.resourcemanager.ResourceManager;
143+
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
144+
145+
import java.util.Iterator;
146+
147+
public class GcloudJavaResourceManagerExample {
148+
149+
public static void main(String[] args) {
150+
// Create Resource Manager service object.
151+
// By default, credentials are inferred from the runtime environment.
152+
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
153+
154+
// Create a project.
155+
String myProjectId = "my-globally-unique-project-id"; // Change to a unique project ID.
156+
ProjectInfo myProject = resourceManager.create(ProjectInfo.builder(myProjectId).build());
157+
158+
// Get a project from the server.
159+
ProjectInfo projectFromServer = resourceManager.get(myProjectId);
160+
System.out.println("Got project " + projectFromServer.projectId() + " from the server.");
161+
162+
// Update a project
163+
ProjectInfo newProjectInfo = resourceManager.replace(myProject.toBuilder()
164+
.addLabel("launch-status", "in-development").build());
165+
System.out.println("Updated the labels of project " + newProjectInfo.projectId()
166+
+ " to be " + newProjectInfo.labels());
167+
168+
// List all the projects you have permission to view.
169+
Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();
170+
System.out.println("Projects I can view:");
171+
while (projectIterator.hasNext()) {
172+
System.out.println(projectIterator.next().projectId());
173+
}
174+
}
175+
}
176+
```
43177

44178
Java Versions
45179
-------------
46180

47181
Java 7 or above is required for using this client.
48182

49-
<!-- TODO(ajaykannan): add this in once the RemoteGCRMHelper class is functional -->
50-
51183
Versioning
52184
----------
53185

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

192+
Testing
193+
-------
194+
195+
This library has tools to help write tests for code that uses Resource Manager.
196+
197+
See [TESTING] to read more about testing.
198+
60199
Contributing
61200
------------
62201

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>

0 commit comments

Comments
 (0)