From 1a10c7bf2ae66d4be84dd27b62a06a6851993bdc Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Fri, 18 Dec 2015 10:15:39 -0800 Subject: [PATCH] Include delete action + minor edits --- gcloud-java-examples/README.md | 2 +- .../examples/ResourceManagerExample.java | 144 +++++++++++++----- .../gcloud/resourcemanager/package-info.java | 3 +- 3 files changed, 110 insertions(+), 39 deletions(-) diff --git a/gcloud-java-examples/README.md b/gcloud-java-examples/README.md index 75b8bb220ee0..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. 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`. +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) 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 index 497d4f07e081..e29aff99c50a 100644 --- 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 @@ -16,6 +16,7 @@ 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; @@ -23,18 +24,19 @@ 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, gets, and lists projects. - *

- * Steps needed for running the example:

    + * + *

    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="[create|get|list projectId]"}
    6. + * -Dexec.args="[list | [create | delete | get] projectId]"} *
    */ public class ResourceManagerExample { @@ -44,51 +46,88 @@ public class ResourceManagerExample { private interface ResourceManagerAction { void run(ResourceManager resourceManager, String... args); - String getRequiredParams(); + + String[] getRequiredParams(); + + String[] getOptionalParams(); } private static class CreateAction implements ResourceManagerAction { @Override public void run(ResourceManager resourceManager, String... args) { - if (args.length > 0) { - String projectId = args[0]; - ProjectInfo project = resourceManager.create(ProjectInfo.builder(projectId).build()); + if (args.length % 2 != 1) { + System.out.println(usage("create", ACTIONS.get("create"))); + return; + } + String projectId = args[0]; + Map labels = new HashMap<>(); + for (int i = 1; i < args.length; i += 2) { + labels.put(args[i], args[i + 1]); + } + 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.print("Are you sure [y/N]: "); + Scanner scanner = new Scanner(System.in); + if (scanner.nextLine().toLowerCase().equals("y")) { + resourceManager.delete(args[0]); + System.out.println("Successfully deleted project " + projectId + "."); } else { - System.out.println("Error: must supply a globally unique project ID for your new project."); + System.out.println("Will not delete project " + projectId + "."); } + scanner.close(); + } + + @Override + public String[] getRequiredParams() { + return new String[] {"project-id"}; } @Override - public String getRequiredParams() { - return "projectId"; + public String[] getOptionalParams() { + return new String[] {}; } } private static class GetAction implements ResourceManagerAction { @Override public void run(ResourceManager resourceManager, String... args) { - if (args.length > 0) { - 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); - } + 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.println( - "Error: must supply a project ID corresponding to a project for which you have viewing" - + " permissions. You can create a project and then call get using the same project ID" - + " if you have no other projects to use in this test."); + System.out.printf("Could not find project '%s'.%n", projectId); } } @Override - public String getRequiredParams() { - return "projectId"; + public String[] getRequiredParams() { + return new String[] {"project-id"}; + } + + @Override + public String[] getOptionalParams() { + return new String[] {}; } } @@ -102,20 +141,51 @@ public void run(ResourceManager resourceManager, String... args) { } @Override - public String getRequiredParams() { - return ""; + 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 "{projectId:" + project.projectId() + ", projectNumber:" + project.projectNumber() - + ", createTimeMillis:" + project.createTimeMillis() + ", state:" + project.state() + "}"; + return new StringBuffer() + .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 String usage(String actionName, ResourceManagerAction action) { + StringBuilder usage = new StringBuilder(); + usage.append(actionName); + String requiredParam = Joiner.on(" ").join(action.getRequiredParams()); + String optionalParam = Joiner.on(" ").join(action.getOptionalParams()); + if (!requiredParam.isEmpty()) { + usage.append(' ').append(requiredParam); + } + if (!optionalParam.isEmpty()) { + usage.append(" [").append(optionalParam).append("]"); + } + return usage.toString(); } public static void main(String... args) { @@ -124,11 +194,7 @@ public static void main(String... args) { if (action == null) { StringBuilder actionAndParams = new StringBuilder(); for (Map.Entry entry : ACTIONS.entrySet()) { - actionAndParams.append(entry.getKey()); - String param = entry.getValue().getRequiredParams(); - if (param != null && !param.isEmpty()) { - actionAndParams.append(' ').append(param); - } + actionAndParams.append(usage(entry.getKey(), entry.getValue())); actionAndParams.append('|'); } actionAndParams.setLength(actionAndParams.length() - 1); @@ -142,6 +208,10 @@ public static void main(String... args) { // ResourceManager resourceManager = LocalResourceManagerHelper.options().service(); ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service(); args = args.length > 1 ? Arrays.copyOfRange(args, 1, args.length) : new String[] {}; - action.run(resourceManager, args); + if (args.length < action.getRequiredParams().length) { + System.out.println("Usage: " + usage(actionName, action)); + } else { + action.run(resourceManager, args); + } } } 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 1c6ebed48d1c..ace65b7203fc 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 @@ -18,6 +18,7 @@ * A client to Google Cloud Resource Manager. * *

    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.
    @@ -30,7 +31,7 @@
      *   System.out.println(projectIterator.next().projectId());
      * }}
    * - * Remember that you must authenticate using the Google Cloud SDK. See more about + *

    Remember that you must authenticate using the Google Cloud SDK. See more about * providing * credentials here. *