diff --git a/gcloud-java-examples/README.md b/gcloud-java-examples/README.md index 2ccec3e81571..c8ea1f6f0d00 100644 --- a/gcloud-java-examples/README.md +++ b/gcloud-java-examples/README.md @@ -33,7 +33,7 @@ To run examples from your command line: 1. Login using gcloud SDK (`gcloud auth login` in command line) -2. Set your current project using `gcloud config set project PROJECT_ID` +2. Set your current project using `gcloud config set project PROJECT_ID`. This step is not necessary for `ResourceManagerExample`. 3. Compile using Maven (`mvn compile` in command line from your base project directory) @@ -56,7 +56,16 @@ To run examples from your command line: $mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="list " $mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="download test.txt" $mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="delete test.txt" -``` + ``` + + Here's an example run of `ResourceManagerExample`. + + Be sure to change the placeholder project ID "my-project-id" with your own globally unique project ID. + ``` + $mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample" -Dexec.args="create my-project-id" + $mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample" -Dexec.args="list" + $mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample" -Dexec.args="get my-project-id" + ``` Troubleshooting --------------- diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/ResourceManagerExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/ResourceManagerExample.java new file mode 100644 index 000000000000..cd0217c34dcb --- /dev/null +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/ResourceManagerExample.java @@ -0,0 +1,222 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud.examples; + +import com.google.common.base.Joiner; +import com.google.gcloud.resourcemanager.ProjectInfo; +import com.google.gcloud.resourcemanager.ResourceManager; +import com.google.gcloud.resourcemanager.ResourceManagerOptions; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +/** + * An example of using the Google Cloud Resource Manager. + * + *

This example creates, deletes, gets, and lists projects. + * + *

Steps needed for running the example:

    + *
  1. login using gcloud SDK - {@code gcloud auth login}.
  2. + *
  3. compile using maven - {@code mvn compile}
  4. + *
  5. run using maven - {@code mvn exec:java + * -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample" + * -Dexec.args="[list | [create | delete | get] projectId]"}
  6. + *
+ */ +public class ResourceManagerExample { + + private static final String DEFAULT_ACTION = "list"; + private static final Map ACTIONS = new HashMap<>(); + + private interface ResourceManagerAction { + void run(ResourceManager resourceManager, String... args); + + String[] getRequiredParams(); + + String[] getOptionalParams(); + } + + private static class CreateAction implements ResourceManagerAction { + @Override + public void run(ResourceManager resourceManager, String... args) { + String projectId = args[0]; + Map labels = new HashMap<>(); + for (int i = 1; i < args.length; i += 2) { + if (i + 1 < args.length) { + labels.put(args[i], args[i + 1]); + } else { + labels.put(args[i], ""); + } + } + ProjectInfo project = + resourceManager.create(ProjectInfo.builder(projectId).labels(labels).build()); + System.out.printf( + "Successfully created project '%s': %s.%n", projectId, projectDetails(project)); + } + + @Override + public String[] getRequiredParams() { + return new String[] {"project-id"}; + } + + @Override + public String[] getOptionalParams() { + return new String[] {"label-key-1", "label-value-1", "label-key-2", "label-value-2", "..."}; + } + } + + private static class DeleteAction implements ResourceManagerAction { + @Override + public void run(ResourceManager resourceManager, String... args) { + String projectId = args[0]; + System.out.printf("Going to delete project \"%s\". Are you sure [y/N]: ", projectId); + Scanner scanner = new Scanner(System.in); + if (scanner.nextLine().toLowerCase().equals("y")) { + resourceManager.delete(projectId); + System.out.println("Successfully deleted project " + projectId + "."); + } else { + System.out.println("Will not delete project " + projectId + "."); + } + scanner.close(); + } + + @Override + public String[] getRequiredParams() { + return new String[] {"project-id"}; + } + + @Override + public String[] getOptionalParams() { + return new String[] {}; + } + } + + private static class GetAction implements ResourceManagerAction { + @Override + public void run(ResourceManager resourceManager, String... args) { + String projectId = args[0]; + ProjectInfo project = resourceManager.get(projectId); + if (project != null) { + System.out.printf( + "Successfully got project '%s': %s.%n", projectId, projectDetails(project)); + } else { + System.out.printf("Could not find project '%s'.%n", projectId); + } + } + + @Override + public String[] getRequiredParams() { + return new String[] {"project-id"}; + } + + @Override + public String[] getOptionalParams() { + return new String[] {}; + } + } + + private static class ListAction implements ResourceManagerAction { + @Override + public void run(ResourceManager resourceManager, String... args) { + System.out.println("Projects you can view:"); + for (ProjectInfo project : resourceManager.list().values()) { + System.out.println(projectDetails(project)); + } + } + + @Override + public String[] getRequiredParams() { + return new String[] {}; + } + + @Override + public String[] getOptionalParams() { + return new String[] {}; + } + } + + static { + ACTIONS.put("create", new CreateAction()); + ACTIONS.put("delete", new DeleteAction()); + ACTIONS.put("get", new GetAction()); + ACTIONS.put("list", new ListAction()); + } + + private static String projectDetails(ProjectInfo project) { + return new StringBuilder() + .append("{projectId:") + .append(project.projectId()) + .append(", projectNumber:") + .append(project.projectNumber()) + .append(", createTimeMillis:") + .append(project.createTimeMillis()) + .append(", state:") + .append(project.state()) + .append(", labels:") + .append(project.labels()) + .append("}") + .toString(); + } + + private static void addUsage( + String actionName, ResourceManagerAction action, StringBuilder usage) { + usage.append(actionName); + Joiner joiner = Joiner.on(" "); + String[] requiredParams = action.getRequiredParams(); + String[] optionalParams = action.getOptionalParams(); + if (requiredParams.length > 0) { + usage.append(' '); + joiner.appendTo(usage, requiredParams); + } + if (optionalParams.length > 0) { + usage.append(" ["); + joiner.appendTo(usage, optionalParams); + usage.append(']'); + } + } + + public static void main(String... args) { + String actionName = args.length > 0 ? args[0].toLowerCase() : DEFAULT_ACTION; + ResourceManagerAction action = ACTIONS.get(actionName); + if (action == null) { + StringBuilder actionAndParams = new StringBuilder(); + for (Map.Entry entry : ACTIONS.entrySet()) { + addUsage(entry.getKey(), entry.getValue(), actionAndParams); + actionAndParams.append('|'); + } + actionAndParams.setLength(actionAndParams.length() - 1); + System.out.printf( + "Usage: %s [%s]%n", ResourceManagerExample.class.getSimpleName(), actionAndParams); + return; + } + + // If you want to access a local Resource Manager emulator (after creating and starting the + // LocalResourceManagerHelper), use the following code instead: + // ResourceManager resourceManager = LocalResourceManagerHelper.options().service(); + ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service(); + args = args.length > 1 ? Arrays.copyOfRange(args, 1, args.length) : new String[] {}; + if (args.length < action.getRequiredParams().length) { + StringBuilder usage = new StringBuilder(); + addUsage(actionName, action, usage); + System.out.println("Usage: " + usage); + } else { + action.run(resourceManager, args); + } + } +} diff --git a/gcloud-java-resourcemanager/README.md b/gcloud-java-resourcemanager/README.md index 4e6ebecbbe5c..fc8b6e0893c1 100644 --- a/gcloud-java-resourcemanager/README.md +++ b/gcloud-java-resourcemanager/README.md @@ -5,12 +5,10 @@ Java idiomatic client for [Google Cloud Resource Manager] (https://cloud.google. [![Build Status](https://travis-ci.org/GoogleCloudPlatform/gcloud-java.svg?branch=master)](https://travis-ci.org/GoogleCloudPlatform/gcloud-java) [![Coverage Status](https://coveralls.io/repos/GoogleCloudPlatform/gcloud-java/badge.svg?branch=master)](https://coveralls.io/r/GoogleCloudPlatform/gcloud-java?branch=master) - - +[![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) - [Homepage] (https://googlecloudplatform.github.io/gcloud-java/) - - +- [API Documentation] (http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/resourcemanager/package-summary.html) > Note: This client is a work-in-progress, and may occasionally > make backwards-incompatible changes. @@ -34,9 +32,9 @@ If you are using SBT, add this to your dependencies libraryDependencies += "com.google.gcloud" % "gcloud-java-resourcemanager" % "0.1.0" ``` - +-------------------- +[`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). Authentication -------------- diff --git a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/package-info.java b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/package-info.java index 3beaa0967443..0c9b0e5a4059 100644 --- a/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/package-info.java +++ b/gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/package-info.java @@ -16,7 +16,24 @@ /** * A client to Google Cloud Resource Manager. - * //TODO(ajaykannan): add code example + * + *

Here's a simple usage example for using gcloud-java-resourcemanager: + *

{@code
+ * ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
+ * String myProjectId = "my-globally-unique-project-id"; // Change to a unique project ID.
+ * ProjectInfo myProject = resourceManager.create(ProjectInfo.builder(myProjectId).build());
+ * ProjectInfo newProjectInfo = resourceManager.replace(myProject.toBuilder()
+ *     .addLabel("launch-status", "in-development").build());
+ * Iterator projectIterator = resourceManager.list().iterateAll();
+ * System.out.println("Projects I can view:");
+ * while (projectIterator.hasNext()) {
+ *   System.out.println(projectIterator.next().projectId());
+ * }}
+ * + *

Remember that you must authenticate using the Google Cloud SDK. See more about + * providing + * credentials here. + * * @see Google Cloud Resource Manager */