Skip to content

Commit 37b5efd

Browse files
author
Ajay Kannan
committed
Adding ResourceManagerExample, update docs
1 parent a3bbbd2 commit 37b5efd

4 files changed

Lines changed: 180 additions & 9 deletions

File tree

gcloud-java-examples/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ To run examples from your command line:
3333

3434
1. Login using gcloud SDK (`gcloud auth login` in command line)
3535

36-
2. Set your current project using `gcloud config set project PROJECT_ID`
36+
2. If you are running an example other than `ResourceManagerExample`, set your current project using `gcloud config set project PROJECT_ID`. If you are running `ResourceManagerExample`, unset your project by using `gcloud config unset project`.
3737

3838
3. Compile using Maven (`mvn compile` in command line from your base project directory)
3939

@@ -56,7 +56,16 @@ To run examples from your command line:
5656
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="list <bucket_name>"
5757
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="download <bucket_name> test.txt"
5858
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="delete <bucket_name> test.txt"
59-
```
59+
```
60+
61+
Here's an example run of `ResourceManagerExample`.
62+
63+
Be sure to change the placeholder project ID "my-project-id" with your own globally unique project ID.
64+
```
65+
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample" -Dexec.args="create my-project-id"
66+
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample" -Dexec.args="list"
67+
$mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample" -Dexec.args="get my-project-id"
68+
```
6069

6170
Troubleshooting
6271
---------------
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.examples;
18+
19+
import com.google.gcloud.resourcemanager.ProjectInfo;
20+
import com.google.gcloud.resourcemanager.ResourceManager;
21+
import com.google.gcloud.resourcemanager.ResourceManagerOptions;
22+
23+
import java.util.Arrays;
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
/**
28+
* An example of using the Google Cloud Resource Manager.
29+
* <p>
30+
* This example creates, gets, and lists projects.
31+
* <p>
32+
* Steps needed for running the example:<ol>
33+
* <li>login using gcloud SDK - {@code gcloud auth login}.</li>
34+
* <li>compile using maven - {@code mvn compile}</li>
35+
* <li>run using maven - {@code mvn exec:java
36+
* -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample"
37+
* -Dexec.args="[create|get|list projectId]"}</li>
38+
* </ol>
39+
*/
40+
public class ResourceManagerExample {
41+
42+
private static final String DEFAULT_ACTION = "list";
43+
private static final Map<String, ResourceManagerAction> ACTIONS = new HashMap<>();
44+
45+
private interface ResourceManagerAction {
46+
void run(ResourceManager resourceManager, String... args);
47+
String getRequiredParams();
48+
}
49+
50+
private static class CreateAction implements ResourceManagerAction {
51+
@Override
52+
public void run(ResourceManager resourceManager, String... args) {
53+
if (args.length > 0) {
54+
String projectId = args[0];
55+
ProjectInfo project = resourceManager.create(ProjectInfo.builder(projectId).build());
56+
System.out.printf(
57+
"Successfully created project '%s': %s.%n", projectId, projectDetails(project));
58+
} else {
59+
System.out.println("Error: must supply a globally unique project ID for your new project.");
60+
}
61+
}
62+
63+
@Override
64+
public String getRequiredParams() {
65+
return "projectId";
66+
}
67+
}
68+
69+
private static class GetAction implements ResourceManagerAction {
70+
@Override
71+
public void run(ResourceManager resourceManager, String... args) {
72+
if (args.length > 0) {
73+
String projectId = args[0];
74+
ProjectInfo project = resourceManager.get(projectId);
75+
if (project != null) {
76+
System.out.printf(
77+
"Successfully got project '%s': %s.%n", projectId, projectDetails(project));
78+
} else {
79+
System.out.printf("Could not find project '%s'.%n", projectId);
80+
}
81+
} else {
82+
System.out.println(
83+
"Error: must supply a project ID corresponding to a project for which you have viewing"
84+
+ " permissions. You can create a project and then call get using the same project ID"
85+
+ " if you have no other projects to use in this test.");
86+
}
87+
}
88+
89+
@Override
90+
public String getRequiredParams() {
91+
return "projectId";
92+
}
93+
}
94+
95+
private static class ListAction implements ResourceManagerAction {
96+
@Override
97+
public void run(ResourceManager resourceManager, String... args) {
98+
System.out.println("Projects you can view:");
99+
for (ProjectInfo project : resourceManager.list().values()) {
100+
System.out.println(projectDetails(project));
101+
}
102+
}
103+
104+
@Override
105+
public String getRequiredParams() {
106+
return "";
107+
}
108+
}
109+
110+
static {
111+
ACTIONS.put("create", new CreateAction());
112+
ACTIONS.put("get", new GetAction());
113+
ACTIONS.put("list", new ListAction());
114+
}
115+
116+
private static String projectDetails(ProjectInfo project) {
117+
return "{projectId:" + project.projectId() + ", projectNumber:" + project.projectNumber()
118+
+ ", createTimeMillis:" + project.createTimeMillis() + ", state:" + project.state() + "}";
119+
}
120+
121+
public static void main(String... args) {
122+
String actionName = args.length > 0 ? args[0].toLowerCase() : DEFAULT_ACTION;
123+
ResourceManagerAction action = ACTIONS.get(actionName);
124+
if (action == null) {
125+
StringBuilder actionAndParams = new StringBuilder();
126+
for (Map.Entry<String, ResourceManagerAction> entry : ACTIONS.entrySet()) {
127+
actionAndParams.append(entry.getKey());
128+
String param = entry.getValue().getRequiredParams();
129+
if (param != null && !param.isEmpty()) {
130+
actionAndParams.append(' ').append(param);
131+
}
132+
actionAndParams.append('|');
133+
}
134+
actionAndParams.setLength(actionAndParams.length() - 1);
135+
System.out.printf(
136+
"Usage: %s [%s]%n", ResourceManagerExample.class.getSimpleName(), actionAndParams);
137+
return;
138+
}
139+
140+
// If you want to access a local Resource Manager emulator (after creating and starting the
141+
// LocalResourceManagerHelper), use the following code instead:
142+
// ResourceManager resourceManager = LocalResourceManagerHelper.options().service();
143+
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
144+
args = args.length > 1 ? Arrays.copyOfRange(args, 1, args.length) : new String[] {};
145+
action.run(resourceManager, args);
146+
}
147+
}

gcloud-java-resourcemanager/README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ Java idiomatic client for [Google Cloud Resource Manager] (https://cloud.google.
55

66
[![Build Status](https://travis-ci.org/GoogleCloudPlatform/gcloud-java.svg?branch=master)](https://travis-ci.org/GoogleCloudPlatform/gcloud-java)
77
[![Coverage Status](https://coveralls.io/repos/GoogleCloudPlatform/gcloud-java/badge.svg?branch=master)](https://coveralls.io/r/GoogleCloudPlatform/gcloud-java?branch=master)
8-
9-
<!-- TODO(ajaykannan): add in the maven shield once the artifact is pushed to maven -->
8+
[![Maven](https://img.shields.io/maven-central/v/com.google.gcloud/gcloud-java-resourcemanager.svg)]( https://img.shields.io/maven-central/v/com.google.gcloud/gcloud-java-resourcemanager.svg)
109

1110
- [Homepage] (https://googlecloudplatform.github.io/gcloud-java/)
12-
13-
<!-- TODO(ajaykannan): add in a link to javadocs once the site has been generated with resource manager docs included -->
11+
- [API Documentation] (http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/resourcemanager/package-summary.html)
1412

1513
> Note: This client is a work-in-progress, and may occasionally
1614
> make backwards-incompatible changes.
@@ -34,9 +32,9 @@ If you are using SBT, add this to your dependencies
3432
libraryDependencies += "com.google.gcloud" % "gcloud-java-resourcemanager" % "0.1.0"
3533
```
3634

37-
<!-- TODO(ajaykannan): once the API becomes usable, make an example application
3835
Example Application
39-
-------------------- -->
36+
--------------------
37+
[`ResourceManagerExample`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/ResourceManagerExample.java) is a simple command line interface for the Cloud Resource Manager. Read more about using the application on the [`gcloud-java-examples` docs page](http://googlecloudplatform.github.io/gcloud-java/apidocs/?com/google/gcloud/examples/ResourceManagerExample.html).
4038

4139
Authentication
4240
--------------

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/package-info.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,24 @@
1616

1717
/**
1818
* A client to Google Cloud Resource Manager.
19-
* //TODO(ajaykannan): add code example
19+
*
20+
* <p>Here's a simple usage example for using gcloud-java-resourcemanager:
21+
* <pre>{@code
22+
* ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
23+
* String myProjectId = "my-globally-unique-project-id"; // Change to a unique project ID.
24+
* ProjectInfo myProject = resourceManager.create(ProjectInfo.builder(myProjectId).build());
25+
* ProjectInfo newProjectInfo = resourceManager.replace(myProject.toBuilder()
26+
* .addLabel("launch-status", "in-development").build());
27+
* Iterator<ProjectInfo> projectIterator = resourceManager.list().iterateAll();
28+
* System.out.println("Projects I can view:");
29+
* while (projectIterator.hasNext()) {
30+
* System.out.println(projectIterator.next().projectId());
31+
* }}</pre>
32+
*
33+
* Remember that you must authenticate using the Google Cloud SDK. See more about
34+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java#specifying-a-project-id">providing
35+
* credentials here</a>.
36+
*
2037
* @see <a href="https://cloud.google.com/resource-manager/">Google Cloud Resource Manager</a>
2138
*/
2239

0 commit comments

Comments
 (0)