Skip to content

Commit

Permalink
ResourceManagerImpl + docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Dec 15, 2015
1 parent e35a1dd commit ad8ea4d
Show file tree
Hide file tree
Showing 10 changed files with 806 additions and 19 deletions.
36 changes: 36 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,38 @@ 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.common.collect.ImmutableMap;
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-that-I-own");
ProjectInfo newProjectInfo = resourceManager.replace(projectFromServer.toBuilder()
.labels(ImmutableMap.of("launch-status", "in-development")).build());
System.out.println("Updated the labels of project " + newProjectInfo.projectId()
+ " to be " + newProjectInfo.labels() + System.lineSeparator());
// 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 +274,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/
46 changes: 46 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,51 @@ 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 host to `localhost` at the appropriate port. For example:

```java
ResourceManager resourceManager = ResourceManagerOptions.builder()
.host("http://localhost:" + helper.port()).build().service();
```

3. Run your tests.

4. Stop the Resource Manager emulator.

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

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

#### On a remote machine

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.

```java
ResourceManager resourceManager = ResourceManagerOptions.builder()
.host("http://<hostname of machine>:<port>").build();
```

Note that the remote Resource Manager emulator must be running before your tests are run.



[cloud-platform-storage-authentication]:https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts
143 changes: 138 additions & 5 deletions gcloud-java-resourcemanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,153 @@ 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, or recover, 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 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`.

> 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 imports 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 be something unique).

```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, say we wanted 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.

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:

```java
import com.google.common.collect.ImmutableMap;
```

Then add the following code to your program:

```java
ProjectInfo newProjectInfo = resourceManager.replace(projectFromServer.toBuilder()
.labels(ImmutableMap.of("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 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.

```java
import com.google.common.collect.ImmutableMap;
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."
+ System.lineSeparator());

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

// 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 +183,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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

/**
* A Google Cloud Resource Manager project metadata object.
*
* A Project is a high-level Google Cloud Platform entity. It is a container for ACLs, APIs,
* AppEngine Apps, VMs, and other Google Cloud Platform resources.
*/
Expand Down Expand Up @@ -324,7 +323,7 @@ com.google.api.services.cloudresourcemanager.model.Project toPb() {
projectPb.setLifecycleState(state.toString());
}
if (createTimeMillis != null) {
projectPb.setCreateTime(ISODateTimeFormat.dateTime().print(createTimeMillis));
projectPb.setCreateTime(ISODateTimeFormat.dateTime().withZoneUTC().print(createTimeMillis));
}
if (parent != null) {
projectPb.setParent(parent.toPb());
Expand Down
Loading

0 comments on commit ad8ea4d

Please sign in to comment.