Skip to content

Commit

Permalink
Merge pull request #457 from ajkannan/resource-manager-impl
Browse files Browse the repository at this point in the history
ResourceManagerImpl + docs
  • Loading branch information
aozarov committed Dec 17, 2015
2 parents e35a1dd + ce2954a commit 2650901
Show file tree
Hide file tree
Showing 13 changed files with 865 additions and 79 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This client supports the following Google Cloud Platform services:

- [Google Cloud Datastore] (#google-cloud-datastore)
- [Google Cloud Storage] (#google-cloud-storage)
- [Google Cloud Resource Manager] (#google-cloud-resource-manager)

> Note: This client is a work-in-progress, and may occasionally
> make backwards-incompatible changes.
Expand Down Expand Up @@ -182,6 +183,37 @@ if (blob == null) {
}
```
Google Cloud Resource Manager
----------------------
- [API Documentation][resourcemanager-api]
- [Official Documentation][cloud-resourcemanager-docs]
#### Preview
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).
```java
import com.google.gcloud.resourcemanager.ProjectInfo;
import com.google.gcloud.resourcemanager.ResourceManager;
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
import java.util.Iterator;
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
ProjectInfo myProject = resourceManager.get("some-project-id"); // Use an existing project's ID
ProjectInfo newProjectInfo = resourceManager.replace(myProject.toBuilder()
.addLabel("launch-status", "in-development").build());
System.out.println("Updated the labels of project " + newProjectInfo.projectId()
+ " to be " + newProjectInfo.labels());
// List all the projects you have permission to view.
Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();
System.out.println("Projects I can view:");
while (projectIterator.hasNext()) {
System.out.println(projectIterator.next().projectId());
}
```
Troubleshooting
---------------
Expand Down Expand Up @@ -241,3 +273,6 @@ Apache 2.0 - See [LICENSE] for more information.
[cloud-storage-create-bucket]: https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets
[cloud-storage-activation]: https://cloud.google.com/storage/docs/signup
[storage-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/storage/package-summary.html
[resourcemanager-api]:http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/resourcemanager/package-summary.html
[cloud-resourcemanager-docs]:https://cloud.google.com/resource-manager/
39 changes: 38 additions & 1 deletion TESTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## gcloud-java tools for testing

This library provides tools to help write tests for code that uses gcloud-java services.
This library provides tools to help write tests for code that uses the following gcloud-java services:

- [Datastore] (#testing-code-that-uses-datastore)
- [Storage] (#testing-code-that-uses-storage)
- [Resource Manager] (#testing-code-that-uses-resource-manager)

### Testing code that uses Datastore

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

### Testing code that uses Resource Manager

#### On your machine

You can test against a temporary local Resource Manager by following these steps:

1. Before running your testing code, start the Resource Manager emulator `LocalResourceManagerHelper`. This can be done as follows:

```java
import com.google.gcloud.resourcemanager.testing.LocalResourceManagerHelper;

LocalResourceManagerHelper helper = LocalResourceManagerHelper.create();
helper.start();
```

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

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

```java
ResourceManager resourceManager = LocalResourceManagerHelper.options().service();
```

3. Run your tests.

4. Stop the Resource Manager emulator.

```java
helper.stop();
```

This method will block until the server thread has been terminated.


[cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts
153 changes: 146 additions & 7 deletions gcloud-java-resourcemanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,22 @@ Java idiomatic client for [Google Cloud Resource Manager] (https://cloud.google.
Quickstart
----------
This library is currently under development and will be available soon!
<!--TODO(ajaykannan): add in pom.xml snippet once gcloud-java-resourcemanager becomes available on maven -->
If you are using Maven, add this to your pom.xml file
```xml
<dependency>
<groupId>com.google.gcloud</groupId>
<artifactId>gcloud-java-resourcemanager</artifactId>
<version>0.1.0</version>
</dependency>
```
If you are using Gradle, add this to your dependencies
```Groovy
compile 'com.google.gcloud:gcloud-java-resourcemanager:jar:0.1.0'
```
If you are using SBT, add this to your dependencies
```Scala
libraryDependencies += "com.google.gcloud" % "gcloud-java-resourcemanager" % "0.1.0"
```

<!-- TODO(ajaykannan): once the API becomes usable, make an example application
Example Application
Expand All @@ -27,27 +41,145 @@ Example Application
Authentication
--------------

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

About Google Cloud Resource Manager
-----------------------------------

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.
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:

* Get a list of all projects associated with an account.
* Create new projects.
* Update existing projects.
* Delete projects.
* Undelete projects that you don't want to delete.

Google Cloud Resource Manager is currently in beta and may occasionally make backwards incompatible changes.

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

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

<!-- TODO(ajaykannan): add code snippet -->
Getting Started
---------------
#### Prerequisites
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`.

> 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.
#### Installation and setup
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.

#### Creating an authorized service object
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:

```java
import com.google.gcloud.resourcemanager.ResourceManager;
import com.google.gcloud.resourcemanager.ResourceManagerOptions;

ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
```

#### Creating a project
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:

```java
import com.google.gcloud.resourcemanager.ProjectInfo;
```

Then add the following code to create a project (be sure to change `myProjectId` to your own unique project ID).

```java
String myProjectId = "my-globally-unique-project-id"; // Change to a unique project ID.
ProjectInfo myProject = resourceManager.create(ProjectInfo.builder(myProjectId).build());
```

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).

#### Getting a specific project
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:

```java
ProjectInfo projectFromServer = resourceManager.get(myProjectId);
```

#### Editing a project
To edit a project, create a new `ProjectInfo` object and pass it in to the `ResourceManager.replace` method.

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:

```java
ProjectInfo newProjectInfo = resourceManager.replace(projectFromServer.toBuilder()
.addLabel("launch-status", "in-development").build());
```

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.

#### Listing all projects
Suppose that we want a list of all projects for which we have read permissions. Add the following import:

```java
import java.util.Iterator;
```

Then add the following code to print a list of projects you can view:

```java
Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();
System.out.println("Projects I can view:");
while (projectIterator.hasNext()) {
System.out.println(projectIterator.next().projectId());
}
```

#### Complete source code

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.

```java
import com.google.gcloud.resourcemanager.ProjectInfo;
import com.google.gcloud.resourcemanager.ResourceManager;
import com.google.gcloud.resourcemanager.ResourceManagerOptions;

import java.util.Iterator;

public class GcloudJavaResourceManagerExample {

public static void main(String[] args) {
// Create Resource Manager service object.
// By default, credentials are inferred from the runtime environment.
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();

// Create a project.
String myProjectId = "my-globally-unique-project-id"; // Change to a unique project ID.
ProjectInfo myProject = resourceManager.create(ProjectInfo.builder(myProjectId).build());

// Get a project from the server.
ProjectInfo projectFromServer = resourceManager.get(myProjectId);
System.out.println("Got project " + projectFromServer.projectId() + " from the server.");

// Update a project
ProjectInfo newProjectInfo = resourceManager.replace(myProject.toBuilder()
.addLabel("launch-status", "in-development").build());
System.out.println("Updated the labels of project " + newProjectInfo.projectId()
+ " to be " + newProjectInfo.labels());

// List all the projects you have permission to view.
Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();
System.out.println("Projects I can view:");
while (projectIterator.hasNext()) {
System.out.println(projectIterator.next().projectId());
}
}
}
```

Java Versions
-------------

Java 7 or above is required for using this client.

<!-- TODO(ajaykannan): add this in once the RemoteGCRMHelper class is functional -->

Versioning
----------

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

Testing
-------

This library has tools to help write tests for code that uses Resource Manager.

See [TESTING] to read more about testing.

Contributing
------------

Expand Down
6 changes: 6 additions & 0 deletions gcloud-java-resourcemanager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
<artifactId>google-api-services-cloudresourcemanager</artifactId>
<version>v1beta1-rev6-1.19.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava-jdk5</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
Loading

0 comments on commit 2650901

Please sign in to comment.