25
25
import com .google .monitoring .v3 .ListUptimeCheckConfigsRequest ;
26
26
import com .google .monitoring .v3 .ListUptimeCheckIpsRequest ;
27
27
import com .google .monitoring .v3 .ProjectName ;
28
+ import com .google .monitoring .v3 .UpdateUptimeCheckConfigRequest ;
28
29
import com .google .monitoring .v3 .UptimeCheckConfig ;
29
30
import com .google .monitoring .v3 .UptimeCheckConfig .HttpCheck ;
30
31
import com .google .monitoring .v3 .UptimeCheckConfigName ;
31
32
import com .google .monitoring .v3 .UptimeCheckIp ;
32
33
import com .google .protobuf .Duration ;
34
+ import com .google .protobuf .FieldMask ;
33
35
34
36
import java .io .IOException ;
35
37
import java .util .Optional ;
@@ -64,11 +66,19 @@ public class UptimeSample {
64
66
.argName ("HOST_NAME" )
65
67
.required (false )
66
68
.build ();
69
+ private static final Option PATH_NAME_OPTION = Option .builder ("a" )
70
+ .longOpt ("pathname" )
71
+ .desc ("[create/update]: Path name of uptime check to create/update." )
72
+ .hasArg ()
73
+ .argName ("HOST_NAME" )
74
+ .required (false )
75
+ .build ();
67
76
68
77
private static final Options OPTIONS = new Options ()
69
78
.addOption (PROJECT_ID_OPTION )
70
79
.addOption (DISPLAY_NAME_OPTION )
71
- .addOption (HOST_NAME_OPTION );
80
+ .addOption (HOST_NAME_OPTION )
81
+ .addOption (PATH_NAME_OPTION );
72
82
73
83
private static final CommandLineParser PARSER = new DefaultParser ();
74
84
@@ -100,7 +110,16 @@ public static void main(String... args) throws IOException {
100
110
createUptimeCheck (
101
111
projectId ,
102
112
cl .getOptionValue (DISPLAY_NAME_OPTION .getOpt (), "new uptime check" ),
103
- cl .getOptionValue (HOST_NAME_OPTION .getOpt (), "example.com" )
113
+ cl .getOptionValue (HOST_NAME_OPTION .getOpt (), "example.com" ),
114
+ cl .getOptionValue (PATH_NAME_OPTION .getOpt (), "/" )
115
+ );
116
+ break ;
117
+ case "update" :
118
+ updateUptimeCheck (
119
+ projectId ,
120
+ cl .getOptionValue (DISPLAY_NAME_OPTION .getOpt (), "new uptime check" ),
121
+ cl .getOptionValue (HOST_NAME_OPTION .getOpt (), "example.com" ),
122
+ cl .getOptionValue (PATH_NAME_OPTION .getOpt (), "/" )
104
123
);
105
124
break ;
106
125
case "list" :
@@ -125,8 +144,8 @@ public static void main(String... args) throws IOException {
125
144
}
126
145
127
146
// [START monitoring_uptime_check_create]]
128
- private static void createUptimeCheck (String projectId , String displayName , String hostName )
129
- throws IOException {
147
+ private static void createUptimeCheck (
148
+ String projectId , String displayName , String hostName , String pathName ) throws IOException {
130
149
CreateUptimeCheckConfigRequest request = CreateUptimeCheckConfigRequest
131
150
.newBuilder ()
132
151
.setParent (ProjectName .format (projectId ))
@@ -139,7 +158,7 @@ private static void createUptimeCheck(String projectId, String displayName, Stri
139
158
.putLabels ("host" , hostName ))
140
159
.setHttpCheck (HttpCheck
141
160
.newBuilder ()
142
- .setPath ("/" )
161
+ .setPath (pathName )
143
162
.setPort (80 ))
144
163
.setTimeout (Duration .newBuilder ().setSeconds (10 ))
145
164
.setPeriod (Duration .newBuilder ().setSeconds (300 )))
@@ -154,6 +173,40 @@ private static void createUptimeCheck(String projectId, String displayName, Stri
154
173
}
155
174
// [END monitoring_uptime_check_create]]
156
175
176
+ // [START monitoring_uptime_check_update]]
177
+ private static void updateUptimeCheck (
178
+ String projectId , String displayName , String hostName , String pathName ) throws IOException {
179
+ String fullCheckName = UptimeCheckConfigName .format (projectId , displayName );
180
+
181
+ UpdateUptimeCheckConfigRequest request = UpdateUptimeCheckConfigRequest
182
+ .newBuilder ()
183
+ .setUpdateMask (FieldMask
184
+ .newBuilder ()
185
+ .addPaths ("http_check.path" ))
186
+ .setUptimeCheckConfig (UptimeCheckConfig
187
+ .newBuilder ()
188
+ .setName (fullCheckName )
189
+ .setMonitoredResource (MonitoredResource
190
+ .newBuilder ()
191
+ .setType ("uptime_url" )
192
+ .putLabels ("host" , hostName ))
193
+ .setHttpCheck (HttpCheck
194
+ .newBuilder ()
195
+ .setPath (pathName )
196
+ .setPort (80 ))
197
+ .setTimeout (Duration .newBuilder ().setSeconds (10 ))
198
+ .setPeriod (Duration .newBuilder ().setSeconds (300 )))
199
+ .build ();
200
+ try (UptimeCheckServiceClient client = UptimeCheckServiceClient .create ()) {
201
+ UptimeCheckConfig config = client .updateUptimeCheckConfig (request );
202
+ System .out .println ("Uptime check updated: \n " + config .toString ());
203
+ } catch (Exception e ) {
204
+ usage ("Exception updating uptime check: " + e .toString ());
205
+ throw e ;
206
+ }
207
+ }
208
+ // [END monitoring_uptime_check_update]]
209
+
157
210
// [START monitoring_uptime_check_list_configs]]
158
211
private static void listUptimeChecks (String projectId ) throws IOException {
159
212
ListUptimeCheckConfigsRequest request = ListUptimeCheckConfigsRequest
0 commit comments