Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Dec 17, 2015
1 parent 55f4fbc commit ce2954a
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ 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 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()
Expand Down
21 changes: 6 additions & 15 deletions 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 @@ -85,8 +89,7 @@ You can test against a temporary local Resource Manager by following these steps
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 = ResourceManagerOptions.builder()
.host("http://localhost:" + helper.port()).build().service();
ResourceManager resourceManager = LocalResourceManagerHelper.options().service();
```

3. Run your tests.
Expand All @@ -99,17 +102,5 @@ You can test against a temporary local Resource Manager by following these steps

This method will block 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
24 changes: 19 additions & 5 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 @@ -38,7 +52,7 @@ Google [Cloud Resource Manager][cloud-resourcemanager] provides a programmatic w
* Create new projects.
* Update existing projects.
* Delete projects.
* Undelete, or recover, projects that you don't want to delete.
* Undelete projects that you don't want to delete.

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

Expand Down Expand Up @@ -74,7 +88,7 @@ All you need to create a project is a globally unique project ID. You can also
import com.google.gcloud.resourcemanager.ProjectInfo;
```

Then add the following code to create a project (be sure to change `myProjectId` to be something unique).
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.
Expand Down Expand Up @@ -121,7 +135,7 @@ while (projectIterator.hasNext()) {

#### 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.
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public class ProjectInfo implements Serializable {
*/
public enum State {
/**
* Only used/useful for distinguishing unset values
* Only used/useful for distinguishing unset values.
*/
LIFECYCLE_STATE_UNSPECIFIED,

/**
* The normal and active state
* The normal and active state.
*/
ACTIVE,

Expand All @@ -66,7 +66,7 @@ public enum State {
DELETE_REQUESTED,

/**
* the process of deleting the project has begun. Reversing the deletion is no longer possible.
* The process of deleting the project has begun. Reversing the deletion is no longer possible.
*/
DELETE_IN_PROGRESS
}
Expand Down Expand Up @@ -105,7 +105,7 @@ com.google.api.services.cloudresourcemanager.model.ResourceId toPb() {
com.google.api.services.cloudresourcemanager.model.ResourceId resourceIdPb =
new com.google.api.services.cloudresourcemanager.model.ResourceId();
resourceIdPb.setId(id);
resourceIdPb.setType(type.toString().toLowerCase());
resourceIdPb.setType(type.toLowerCase());
return resourceIdPb;
}

Expand Down Expand Up @@ -141,9 +141,9 @@ private Builder() {
/**
* Set the user-assigned name of the project.
*
* This field is optional and can remain unset. Allowed characters are: lowercase and uppercase
* letters, numbers, hyphen, single-quote, double-quote, space, and exclamation point. This
* field can be changed after project creation.
* <p>This field is optional and can remain unset. Allowed characters are: lowercase and
* uppercase letters, numbers, hyphen, single-quote, double-quote, space, and exclamation point.
* This field can be changed after project creation.
*/
public Builder name(String name) {
this.name = firstNonNull(name, Data.<String>nullOf(String.class));
Expand All @@ -153,7 +153,7 @@ public Builder name(String name) {
/**
* Set the unique, user-assigned ID of the project.
*
* The ID must be 6 to 30 lowercase letters, digits, or hyphens. It must start with a letter.
* <p>The ID must be 6 to 30 lowercase letters, digits, or hyphens. It must start with a letter.
* Trailing hyphens are prohibited. This field cannot be changed after the server creates the
* project.
*/
Expand All @@ -165,7 +165,7 @@ public Builder projectId(String projectId) {
/**
* Add a label associated with this project.
*
* See {@link #labels} for label restrictions.
* <p>See {@link #labels} for label restrictions.
*/
public Builder addLabel(String key, String value) {
this.labels.put(key, value);
Expand All @@ -191,11 +191,11 @@ public Builder clearLabels() {
/**
* Set the labels associated with this project.
*
* Label keys must be between 1 and 63 characters long and must conform to the following regular
* expression: [a-z]([-a-z0-9]*[a-z0-9])?. Label values must be between 0 and 63 characters long
* and must conform to the regular expression ([a-z]([-a-z0-9]*[a-z0-9])?)?. No more than 256
* labels can be associated with a given resource. This field can be changed after project
* creation.
* <p>Label keys must be between 1 and 63 characters long and must conform to the following
* regular expression: [a-z]([-a-z0-9]*[a-z0-9])?. Label values must be between 0 and 63
* characters long and must conform to the regular expression ([a-z]([-a-z0-9]*[a-z0-9])?)?. No
* more than 256 labels can be associated with a given resource. This field can be changed after
* project creation.
*/
public Builder labels(Map<String, String> labels) {
this.labels = Maps.newHashMap(checkNotNull(labels));
Expand Down Expand Up @@ -240,7 +240,7 @@ public ProjectInfo build() {
/**
* Get the unique, user-assigned ID of the project.
*
* This field cannot be changed after the server creates the project.
* <p>This field cannot be changed after the server creates the project.
*/
public String projectId() {
return projectId;
Expand All @@ -249,7 +249,7 @@ public String projectId() {
/**
* Get the user-assigned name of the project.
*
* This field is optional, can remain unset, and can be changed after project creation.
* <p>This field is optional, can remain unset, and can be changed after project creation.
*/
public String name() {
return Data.isNull(name) ? null : name;
Expand All @@ -258,7 +258,7 @@ public String name() {
/**
* Get number uniquely identifying the project.
*
* This field is set by the server and is read-only.
* <p>This field is set by the server and is read-only.
*/
public Long projectNumber() {
return projectNumber;
Expand All @@ -274,7 +274,7 @@ public Map<String, String> labels() {
/**
* Get the project's lifecycle state.
*
* This is a read-only field. To change the lifecycle state of your project, use the
* <p>This is a read-only field. To change the lifecycle state of your project, use the
* {@code delete} or {@code undelete} method.
*/
public State state() {
Expand All @@ -288,7 +288,7 @@ ResourceId parent() {
/**
* Get the project's creation time (in milliseconds).
*
* This field is set by the server and is read-only.
* <p>This field is set by the server and is read-only.
*/
public Long createTimeMillis() {
return createTimeMillis;
Expand Down Expand Up @@ -332,9 +332,8 @@ com.google.api.services.cloudresourcemanager.model.Project toPb() {
}

static ProjectInfo fromPb(com.google.api.services.cloudresourcemanager.model.Project projectPb) {
ProjectInfo.Builder builder =
ProjectInfo.builder(projectPb.getProjectId()).projectNumber(projectPb.getProjectNumber());
if (projectPb.getName() != null) {
Builder builder = builder(projectPb.getProjectId()).projectNumber(projectPb.getProjectNumber());
if (projectPb.getName() != null && !projectPb.getName().equals("Unnamed")) {
builder.name(projectPb.getName());
}
if (projectPb.getLabels() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteStreams;
import com.google.gcloud.resourcemanager.ResourceManagerOptions;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
Expand Down Expand Up @@ -524,10 +525,10 @@ public static LocalResourceManagerHelper create() {
}

/**
* Returns the port that the LocalResourceManagerHelper listens to for requests.
* Returns a ResourceManagerOptions instance that sets the host to use the mock server.
*/
public int port() {
return port;
public ResourceManagerOptions options() {
return ResourceManagerOptions.builder().host("http://localhost:" + port).build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.google.gcloud.spi.ResourceManagerRpc.Option.FILTER;
import static com.google.gcloud.spi.ResourceManagerRpc.Option.PAGE_SIZE;
import static com.google.gcloud.spi.ResourceManagerRpc.Option.PAGE_TOKEN;
import static java.net.HttpURLConnection.HTTP_FORBIDDEN;

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
Expand Down Expand Up @@ -81,7 +82,7 @@ public Project get(String projectId, Map<Option, ?> options) throws ResourceMana
.execute();
} catch (IOException ex) {
ResourceManagerException translated = translate(ex);
if (translated.code() == 403) {
if (translated.code() == HTTP_FORBIDDEN) {
return null; // Project not found
} else {
throw translated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ public class LocalResourceManagerHelperTest {
private static final Map<ResourceManagerRpc.Option, ?> EMPTY_RPC_OPTIONS = ImmutableMap.of();
private static final LocalResourceManagerHelper RESOURCE_MANAGER_HELPER =
LocalResourceManagerHelper.create();
private static final ResourceManagerRpc rpc = new DefaultResourceManagerRpc(
ResourceManagerOptions.builder()
.host("http://localhost:" + RESOURCE_MANAGER_HELPER.port())
.build());
private static final ResourceManagerRpc rpc =
new DefaultResourceManagerRpc(RESOURCE_MANAGER_HELPER.options());
private static final com.google.api.services.cloudresourcemanager.model.Project PARTIAL_PROJECT =
new com.google.api.services.cloudresourcemanager.model.Project().setProjectId(
"partial-project");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class ProjectInfoTest {
.parent(PARENT)
.build();
private static final ProjectInfo PARTIAL_PROJECT_INFO = ProjectInfo.builder(PROJECT_ID).build();
private static final ProjectInfo UNNAMED_PROJECT_FROM_LIST =
PARTIAL_PROJECT_INFO.toBuilder().name("Unnamed").build();

@Test
public void testBuilder() {
Expand All @@ -71,8 +73,10 @@ public void testToBuilder() {

@Test
public void testToAndFromPb() {
assertTrue(FULL_PROJECT_INFO.toPb().getCreateTime().endsWith("Z"));
compareProjects(FULL_PROJECT_INFO, ProjectInfo.fromPb(FULL_PROJECT_INFO.toPb()));
compareProjects(PARTIAL_PROJECT_INFO, ProjectInfo.fromPb(PARTIAL_PROJECT_INFO.toPb()));
compareProjects(PARTIAL_PROJECT_INFO, ProjectInfo.fromPb(UNNAMED_PROJECT_FROM_LIST.toPb()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ public class ResourceManagerImplTest {

private static final LocalResourceManagerHelper RESOURCE_MANAGER_HELPER =
LocalResourceManagerHelper.create();
private static final ResourceManager RESOURCE_MANAGER = ResourceManagerOptions.builder()
.host("http://localhost:" + RESOURCE_MANAGER_HELPER.port())
.build()
.service();
private static final ResourceManager RESOURCE_MANAGER =
RESOURCE_MANAGER_HELPER.options().service();
private static final ProjectGetOption GET_FIELDS =
ProjectGetOption.fields(ProjectField.NAME, ProjectField.CREATE_TIME);
private static final ProjectListOption LIST_FIELDS =
Expand Down

0 comments on commit ce2954a

Please sign in to comment.