16
16
17
17
package com .google .gcloud .examples ;
18
18
19
+ import com .google .common .base .Joiner ;
19
20
import com .google .gcloud .resourcemanager .ProjectInfo ;
20
21
import com .google .gcloud .resourcemanager .ResourceManager ;
21
22
import com .google .gcloud .resourcemanager .ResourceManagerOptions ;
22
23
23
24
import java .util .Arrays ;
24
25
import java .util .HashMap ;
25
26
import java .util .Map ;
27
+ import java .util .Scanner ;
26
28
27
29
/**
28
30
* An example of using the Google Cloud Resource Manager.
29
31
* <p>
30
- * This example creates, gets, and lists projects.
32
+ * This example creates, deletes, gets, and lists projects.
31
33
* <p>
32
34
* Steps needed for running the example:<ol>
33
35
* <li>login using gcloud SDK - {@code gcloud auth login}.</li>
34
36
* <li>compile using maven - {@code mvn compile}</li>
35
37
* <li>run using maven - {@code mvn exec:java
36
38
* -Dexec.mainClass="com.google.gcloud.examples.ResourceManagerExample"
37
- * -Dexec.args="[create| get| list projectId]"}</li>
39
+ * -Dexec.args="[create | [delete | get | list] projectId]"}</li>
38
40
* </ol>
39
41
*/
40
42
public class ResourceManagerExample {
@@ -44,51 +46,88 @@ public class ResourceManagerExample {
44
46
45
47
private interface ResourceManagerAction {
46
48
void run (ResourceManager resourceManager , String ... args );
47
- String getRequiredParams ();
49
+
50
+ String [] getRequiredParams ();
51
+
52
+ String [] getOptionalParams ();
48
53
}
49
54
50
55
private static class CreateAction implements ResourceManagerAction {
51
56
@ Override
52
57
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 ());
56
69
System .out .printf (
57
70
"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 + "." );
58
93
} 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 + " ." );
60
95
}
96
+ scanner .close ();
61
97
}
62
98
63
99
@ 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 [] {"" };
66
107
}
67
108
}
68
109
69
110
private static class GetAction implements ResourceManagerAction {
70
111
@ Override
71
112
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 ));
81
118
} 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 );
86
120
}
87
121
}
88
122
89
123
@ 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 [] {"" };
92
131
}
93
132
}
94
133
@@ -102,20 +141,51 @@ public void run(ResourceManager resourceManager, String... args) {
102
141
}
103
142
104
143
@ 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 [] {};
107
151
}
108
152
}
109
153
110
154
static {
111
155
ACTIONS .put ("create" , new CreateAction ());
156
+ ACTIONS .put ("delete" , new DeleteAction ());
112
157
ACTIONS .put ("get" , new GetAction ());
113
158
ACTIONS .put ("list" , new ListAction ());
114
159
}
115
160
116
161
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 ();
119
189
}
120
190
121
191
public static void main (String ... args ) {
@@ -124,11 +194,7 @@ public static void main(String... args) {
124
194
if (action == null ) {
125
195
StringBuilder actionAndParams = new StringBuilder ();
126
196
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 ()));
132
198
actionAndParams .append ('|' );
133
199
}
134
200
actionAndParams .setLength (actionAndParams .length () - 1 );
@@ -142,6 +208,10 @@ public static void main(String... args) {
142
208
// ResourceManager resourceManager = LocalResourceManagerHelper.options().service();
143
209
ResourceManager resourceManager = ResourceManagerOptions .defaultInstance ().service ();
144
210
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
+ }
146
216
}
147
217
}
0 commit comments