1616
1717package com .google .gcloud .examples ;
1818
19+ import com .google .common .base .Joiner ;
1920import com .google .gcloud .resourcemanager .ProjectInfo ;
2021import com .google .gcloud .resourcemanager .ResourceManager ;
2122import com .google .gcloud .resourcemanager .ResourceManagerOptions ;
2223
2324import java .util .Arrays ;
2425import java .util .HashMap ;
2526import java .util .Map ;
27+ import java .util .Scanner ;
2628
2729/**
2830 * An example of using the Google Cloud Resource Manager.
2931 * <p>
30- * This example creates, gets, and lists projects.
32+ * This example creates, deletes, gets, and lists projects.
3133 * <p>
3234 * Steps needed for running the example:<ol>
3335 * <li>login using gcloud SDK - {@code gcloud auth login}.</li>
3436 * <li>compile using maven - {@code mvn compile}</li>
3537 * <li>run using maven - {@code mvn exec:java
3638 * -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample"
37- * -Dexec.args="[create| get| list projectId]"}</li>
39+ * -Dexec.args="[create | [delete | get | list] projectId]"}</li>
3840 * </ol>
3941 */
4042public class ResourceManagerExample {
@@ -44,51 +46,88 @@ public class ResourceManagerExample {
4446
4547 private interface ResourceManagerAction {
4648 void run (ResourceManager resourceManager , String ... args );
47- String getRequiredParams ();
49+
50+ String [] getRequiredParams ();
51+
52+ String [] getOptionalParams ();
4853 }
4954
5055 private static class CreateAction implements ResourceManagerAction {
5156 @ Override
5257 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 ());
58+ if (args .length % 2 != 1 ) {
59+ System .out .println (usage ("create" , ACTIONS .get ("create" )));
60+ return ;
61+ }
62+ String projectId = args [0 ];
63+ Map <String , String > labels = new HashMap <>();
64+ for (int i = 1 ; i < args .length ; i +=2 ) {
65+ labels .put (args [i ], args [i +1 ]);
66+ }
67+ ProjectInfo project =
68+ resourceManager .create (ProjectInfo .builder (projectId ).labels (labels ).build ());
5669 System .out .printf (
5770 "Successfully created project '%s': %s.%n" , projectId , projectDetails (project ));
71+ }
72+
73+ @ Override
74+ public String [] getRequiredParams () {
75+ return new String [] {"project-id" };
76+ }
77+
78+ @ Override
79+ public String [] getOptionalParams () {
80+ return new String [] {"label-key-1" , "label-value-1" , "label-key-2" , "label-value-2" , "..." };
81+ }
82+ }
83+
84+ private static class DeleteAction implements ResourceManagerAction {
85+ @ Override
86+ public void run (ResourceManager resourceManager , String ... args ) {
87+ String projectId = args [0 ];
88+ System .out .print ("Are you sure [y/N]: " );
89+ Scanner scanner = new Scanner (System .in );
90+ if (scanner .nextLine ().toLowerCase ().equals ("y" )) {
91+ resourceManager .delete (args [0 ]);
92+ System .out .println ("Successfully deleted project " + projectId + "." );
5893 } else {
59- System .out .println ("Error: must supply a globally unique project ID for your new project ." );
94+ System .out .println ("Will not delete project " + projectId + " ." );
6095 }
96+ scanner .close ();
6197 }
6298
6399 @ Override
64- public String getRequiredParams () {
65- return "projectId" ;
100+ public String [] getRequiredParams () {
101+ return new String [] {"project-id" };
102+ }
103+
104+ @ Override
105+ public String [] getOptionalParams () {
106+ return new String [] {"" };
66107 }
67108 }
68109
69110 private static class GetAction implements ResourceManagerAction {
70111 @ Override
71112 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- }
113+ String projectId = args [0 ];
114+ ProjectInfo project = resourceManager .get (projectId );
115+ if (project != null ) {
116+ System .out .printf (
117+ "Successfully got project '%s': %s.%n" , projectId , projectDetails (project ));
81118 } 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." );
119+ System .out .printf ("Could not find project '%s'.%n" , projectId );
86120 }
87121 }
88122
89123 @ Override
90- public String getRequiredParams () {
91- return "projectId" ;
124+ public String [] getRequiredParams () {
125+ return new String [] {"project-id" };
126+ }
127+
128+ @ Override
129+ public String [] getOptionalParams () {
130+ return new String [] {"" };
92131 }
93132 }
94133
@@ -102,20 +141,51 @@ public void run(ResourceManager resourceManager, String... args) {
102141 }
103142
104143 @ Override
105- public String getRequiredParams () {
106- return "" ;
144+ public String [] getRequiredParams () {
145+ return new String [] {};
146+ }
147+
148+ @ Override
149+ public String [] getOptionalParams () {
150+ return new String [] {};
107151 }
108152 }
109153
110154 static {
111155 ACTIONS .put ("create" , new CreateAction ());
156+ ACTIONS .put ("delete" , new DeleteAction ());
112157 ACTIONS .put ("get" , new GetAction ());
113158 ACTIONS .put ("list" , new ListAction ());
114159 }
115160
116161 private static String projectDetails (ProjectInfo project ) {
117- return "{projectId:" + project .projectId () + ", projectNumber:" + project .projectNumber ()
118- + ", createTimeMillis:" + project .createTimeMillis () + ", state:" + project .state () + "}" ;
162+ return new StringBuffer ()
163+ .append ("{projectId:" )
164+ .append (project .projectId ())
165+ .append (", projectNumber:" )
166+ .append (project .projectNumber ())
167+ .append (", createTimeMillis:" )
168+ .append (project .createTimeMillis ())
169+ .append (", state:" )
170+ .append (project .state ())
171+ .append (", labels:" )
172+ .append (project .labels ())
173+ .append ("}" )
174+ .toString ();
175+ }
176+
177+ private static String usage (String actionName , ResourceManagerAction action ) {
178+ StringBuilder usage = new StringBuilder ();
179+ usage .append (actionName );
180+ String requiredParam = Joiner .on (" " ).join (action .getRequiredParams ());
181+ String optionalParam = Joiner .on (" " ).join (action .getOptionalParams ());
182+ if (!requiredParam .isEmpty ()) {
183+ usage .append (' ' ).append (requiredParam );
184+ }
185+ if (!optionalParam .isEmpty ()) {
186+ usage .append (" [" ).append (optionalParam ).append ("]" );
187+ }
188+ return usage .toString ();
119189 }
120190
121191 public static void main (String ... args ) {
@@ -124,11 +194,7 @@ public static void main(String... args) {
124194 if (action == null ) {
125195 StringBuilder actionAndParams = new StringBuilder ();
126196 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- }
197+ actionAndParams .append (usage (entry .getKey (), entry .getValue ()));
132198 actionAndParams .append ('|' );
133199 }
134200 actionAndParams .setLength (actionAndParams .length () - 1 );
@@ -142,6 +208,10 @@ public static void main(String... args) {
142208 // ResourceManager resourceManager = LocalResourceManagerHelper.options().service();
143209 ResourceManager resourceManager = ResourceManagerOptions .defaultInstance ().service ();
144210 args = args .length > 1 ? Arrays .copyOfRange (args , 1 , args .length ) : new String [] {};
145- action .run (resourceManager , args );
211+ if (args .length < action .getRequiredParams ().length ) {
212+ System .out .println ("Usage: " + usage (actionName , action ));
213+ } else {
214+ action .run (resourceManager , args );
215+ }
146216 }
147217}
0 commit comments