42
42
import java .io .FileReader ;
43
43
import java .io .FileWriter ;
44
44
import java .io .IOException ;
45
- import java .io .PrintStream ;
46
45
import java .util .List ;
47
46
import java .util .Map ;
48
47
import java .util .Optional ;
@@ -105,7 +104,8 @@ public class AlertSample {
105
104
"backup" , BACKUP_OPTIONS ,
106
105
"restore" , BACKUP_OPTIONS ,
107
106
"replace-channels" , REPLACE_CHANNELS_OPTIONS ,
108
- "enable" , ENABLE_OPTIONS
107
+ "enable" , ENABLE_OPTIONS ,
108
+ "disable" , ENABLE_OPTIONS
109
109
);
110
110
111
111
private static final CommandLineParser PARSER = new DefaultParser ();
@@ -122,26 +122,23 @@ public class AlertSample {
122
122
123
123
private AlertPolicyServiceClient alertPolicyClient ;
124
124
private NotificationChannelServiceClient notificationChannelClient ;
125
- private PrintStream outputStream ;
126
125
private Gson gson = new Gson ();
127
126
128
127
private AlertSample () throws IOException {
129
- this (AlertPolicyServiceClient .create (), NotificationChannelServiceClient .create (), System . out );
128
+ this (AlertPolicyServiceClient .create (), NotificationChannelServiceClient .create ());
130
129
}
131
130
132
131
AlertSample (AlertPolicyServiceClient alertPolicyClient ,
133
- NotificationChannelServiceClient notificationChannelClient ,
134
- PrintStream os ) {
132
+ NotificationChannelServiceClient notificationChannelClient ) {
135
133
this .alertPolicyClient = checkNotNull (alertPolicyClient );
136
134
this .notificationChannelClient = notificationChannelClient ;
137
- outputStream = checkNotNull (os );
138
135
}
139
136
140
137
public static void main (String [] args ) throws IOException {
141
138
AlertSample sample = createAlertSample ();
142
139
143
140
if (args .length == 0 ) {
144
- usage (System . out , null );
141
+ usage (null );
145
142
return ;
146
143
}
147
144
String command = args [0 ];
@@ -151,16 +148,15 @@ public static void main(String[] args) throws IOException {
151
148
152
149
String projectId = cl .hasOption (PROJECT_ID_OPTION .getOpt ())
153
150
? cl .getOptionValue (PROJECT_ID_OPTION .getOpt ())
154
- : System .getenv ("GOOGLE_PROJECT_ID " );
151
+ : System .getenv ("GOOGLE_CLOUD_PROJECT " );
155
152
156
153
if (Strings .isNullOrEmpty (projectId )) {
157
154
projectId = System .getenv ("DEVSHELL_PROJECT_ID" );
158
155
}
159
156
160
157
if (Strings .isNullOrEmpty (projectId )) {
161
- usage (sample .outputStream ,
162
- "Error: --project-id arg required unless provided by the GOOGLE_PROJECT_ID "
163
- + "or DEVSHELL_PROJECT_ID environment variables." );
158
+ usage ("Error: --project-id arg required unless provided by the GOOGLE_CLOUD_PROJECT "
159
+ + "or DEVSHELL_PROJECT_ID environment variables." );
164
160
return ;
165
161
}
166
162
@@ -190,7 +186,7 @@ public static void main(String[] args) throws IOException {
190
186
cl .getOptionValue (FILTER_OPTION .getOpt ()), false );
191
187
break ;
192
188
default :
193
- usage (sample . outputStream , null );
189
+ usage (null );
194
190
}
195
191
}
196
192
@@ -199,7 +195,7 @@ private static CommandLine parseCommandLine(String[] args, Options expectedOptio
199
195
try {
200
196
cl = PARSER .parse (expectedOptions , args );
201
197
} catch (ParseException pe ) {
202
- usage (System . out , "Exception parsing command line arguments." );
198
+ usage ("Exception parsing command line arguments." );
203
199
throw new RuntimeException ("Exception parsing command line arguments." , pe );
204
200
}
205
201
return cl ;
@@ -210,7 +206,7 @@ private static AlertSample createAlertSample() throws IOException {
210
206
try {
211
207
sample = new AlertSample ();
212
208
} catch (Exception e ) {
213
- usage (System . out , "Exception creating alert sample." );
209
+ usage ("Exception creating alert sample." );
214
210
throw e ;
215
211
}
216
212
return sample ;
@@ -221,10 +217,19 @@ void listAlertPolicies(String projectId) {
221
217
ListAlertPoliciesPagedResponse response = alertPolicyClient .listAlertPolicies (ProjectName .of (
222
218
projectId ));
223
219
220
+ System .out .println ("Alert Policies:" );
224
221
for (AlertPolicy policy : response .iterateAll ()) {
225
- outputStream .println (policy .getDisplayName ());
222
+ System .out .println (
223
+ String .format ("\n Policy %s\n alert-id: %s" , policy .getDisplayName (), policy .getName ()));
224
+ int channels = policy .getNotificationChannelsCount ();
225
+ if (channels > 0 ) {
226
+ System .out .println ("notification-channels:" );
227
+ for (int i = 0 ; i < channels ; i ++) {
228
+ System .out .println ("\t " + policy .getNotificationChannels (i ));
229
+ }
230
+ }
226
231
if (policy .hasDocumentation () && policy .getDocumentation ().getContent () != null ) {
227
- outputStream .println (policy .getDocumentation ().getContent ());
232
+ System . out .println (policy .getDocumentation ().getContent ());
228
233
}
229
234
}
230
235
}
@@ -235,7 +240,7 @@ void backupPolicies(String projectId, String filePath) throws IOException {
235
240
List <AlertPolicy > alertPolicies = getAlertPolicies (projectId );
236
241
List <NotificationChannel > notificationChannels = getNotificationChannels (projectId );
237
242
writePoliciesBackupFile (projectId , filePath , alertPolicies , notificationChannels );
238
- outputStream .println (String .format ("Saved policies to %s" , filePath ));
243
+ System . out .println (String .format ("Saved policies to %s" , filePath ));
239
244
}
240
245
241
246
private List <AlertPolicy > getAlertPolicies (String projectId ) {
@@ -351,7 +356,7 @@ private void restoreRevisedPolicies(String projectId,
351
356
policy .toBuilder ().clearName ().build ());
352
357
}
353
358
}
354
- outputStream .println (String .format ("Restored %s" , policy .getName ()));
359
+ System . out .println (String .format ("Restored %s" , policy .getName ()));
355
360
}
356
361
}
357
362
// [END monitoring_alert_create_policy]
@@ -400,7 +405,7 @@ private Map<String, String> restoreNotificationChannels(String projectId,
400
405
}
401
406
// [END monitoring_alert_create_channel]
402
407
// [END monitoring_alert_update_channel]
403
-
408
+
404
409
private JsonObject getPolicyJsonContents (String filePath , BufferedReader content , Gson gson ) {
405
410
try {
406
411
return gson .fromJson (content , JsonObject .class );
@@ -421,7 +426,7 @@ void replaceChannels(String projectId, String alertPolicyId, String[] channelIds
421
426
}
422
427
AlertPolicy result = alertPolicyClient .updateAlertPolicy (
423
428
FieldMask .newBuilder ().addPaths ("notification_channels" ).build (), policyBuilder .build ());
424
- outputStream .println (String .format ("Updated %s" , result .getName ()));
429
+ System . out .println (String .format ("Updated %s" , result .getName ()));
425
430
}
426
431
// [END monitoring_alert_replace_channels]
427
432
@@ -432,38 +437,37 @@ void enablePolicies(String projectId,
432
437
boolean enable ) {
433
438
ListAlertPoliciesPagedResponse response = alertPolicyClient
434
439
.listAlertPolicies (ListAlertPoliciesRequest .newBuilder ()
435
- .setName (projectId )
440
+ .setName (ProjectName . of ( projectId ). toString () )
436
441
.setFilter (filter )
437
442
.build ());
438
443
439
444
for (AlertPolicy policy : response .iterateAll ()) {
440
445
if (policy .getEnabled ().getValue () == enable ) {
441
- outputStream .println (String .format ("Policy %s is already %b." , policy .getName (), enable ));
446
+ System .out .println (String .format (
447
+ "Policy %s is already %b." , policy .getName (), enable ? "enabled" : "disabled" ));
442
448
continue ;
443
449
}
444
450
AlertPolicy updatedPolicy = AlertPolicy
445
451
.newBuilder ()
446
- .setName (AlertPolicyName . of ( projectId , policy .getName ()). toString ())
452
+ .setName (policy .getName ())
447
453
.setEnabled (BoolValue .newBuilder ().setValue (enable ))
448
454
.build ();
449
455
AlertPolicy result = alertPolicyClient .updateAlertPolicy (
450
456
FieldMask .newBuilder ().addPaths ("enabled" ).build (), updatedPolicy );
451
- outputStream .println (String .format (
452
- "%s %s" , result .getName (), result .getEnabled ().getValue () ? "Enabled" : "Disabled" ));
457
+ System .out .println (String .format (
458
+ "%s %s" ,
459
+ result .getDisplayName (),
460
+ result .getEnabled ().getValue () ? "enabled" : "disabled" ));
453
461
}
454
462
455
463
}
456
464
// [END monitoring_alert_enable_policies]
457
465
// [END monitoring_alert_disable_policies]
458
466
459
- private static void usage (PrintStream ps ) {
460
- usage (ps , null );
461
- }
462
-
463
- private static void usage (PrintStream ps , String message ) {
464
- Optional .ofNullable (message ).ifPresent (ps ::println );
465
- ps .println ("Usage:" );
466
- ps .printf (
467
+ private static void usage (String message ) {
468
+ Optional .ofNullable (message ).ifPresent (System .out ::println );
469
+ System .out .println ("Usage:" );
470
+ System .out .printf (
467
471
"\t java %s \" <command>\" \" <args>\" \n "
468
472
+ "Args:\n "
469
473
+ "\t %s\n "
0 commit comments