2525import com .google .monitoring .v3 .ListUptimeCheckConfigsRequest ;
2626import com .google .monitoring .v3 .ListUptimeCheckIpsRequest ;
2727import com .google .monitoring .v3 .ProjectName ;
28+ import com .google .monitoring .v3 .UpdateUptimeCheckConfigRequest ;
2829import com .google .monitoring .v3 .UptimeCheckConfig ;
2930import com .google .monitoring .v3 .UptimeCheckConfig .HttpCheck ;
3031import com .google .monitoring .v3 .UptimeCheckConfigName ;
3132import com .google .monitoring .v3 .UptimeCheckIp ;
3233import com .google .protobuf .Duration ;
34+ import com .google .protobuf .FieldMask ;
3335
3436import java .io .IOException ;
3537import java .util .Optional ;
@@ -64,11 +66,19 @@ public class UptimeSample {
6466 .argName ("HOST_NAME" )
6567 .required (false )
6668 .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 ();
6776
6877 private static final Options OPTIONS = new Options ()
6978 .addOption (PROJECT_ID_OPTION )
7079 .addOption (DISPLAY_NAME_OPTION )
71- .addOption (HOST_NAME_OPTION );
80+ .addOption (HOST_NAME_OPTION )
81+ .addOption (PATH_NAME_OPTION );
7282
7383 private static final CommandLineParser PARSER = new DefaultParser ();
7484
@@ -100,7 +110,16 @@ public static void main(String... args) throws IOException {
100110 createUptimeCheck (
101111 projectId ,
102112 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 (), "/" )
104123 );
105124 break ;
106125 case "list" :
@@ -125,8 +144,8 @@ public static void main(String... args) throws IOException {
125144 }
126145
127146 // [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 {
130149 CreateUptimeCheckConfigRequest request = CreateUptimeCheckConfigRequest
131150 .newBuilder ()
132151 .setParent (ProjectName .format (projectId ))
@@ -139,7 +158,7 @@ private static void createUptimeCheck(String projectId, String displayName, Stri
139158 .putLabels ("host" , hostName ))
140159 .setHttpCheck (HttpCheck
141160 .newBuilder ()
142- .setPath ("/" )
161+ .setPath (pathName )
143162 .setPort (80 ))
144163 .setTimeout (Duration .newBuilder ().setSeconds (10 ))
145164 .setPeriod (Duration .newBuilder ().setSeconds (300 )))
@@ -154,6 +173,40 @@ private static void createUptimeCheck(String projectId, String displayName, Stri
154173 }
155174 // [END monitoring_uptime_check_create]]
156175
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+
157210 // [START monitoring_uptime_check_list_configs]]
158211 private static void listUptimeChecks (String projectId ) throws IOException {
159212 ListUptimeCheckConfigsRequest request = ListUptimeCheckConfigsRequest
0 commit comments