|
| 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 | +} |
0 commit comments