Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
fix: fix samples and samples tests for UptimeCheck. (#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeli0 committed Aug 10, 2022
1 parent 00dbc83 commit 7143f96
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
Expand Up @@ -29,7 +29,6 @@
import com.google.monitoring.v3.UpdateUptimeCheckConfigRequest;
import com.google.monitoring.v3.UptimeCheckConfig;
import com.google.monitoring.v3.UptimeCheckConfig.HttpCheck;
import com.google.monitoring.v3.UptimeCheckConfigName;
import com.google.monitoring.v3.UptimeCheckIp;
import com.google.protobuf.Duration;
import com.google.protobuf.FieldMask;
Expand Down Expand Up @@ -119,7 +118,6 @@ public static void main(String... args) throws IOException {
break;
case "update":
updateUptimeCheck(
projectId,
cl.getOptionValue(DISPLAY_NAME_OPTION.getOpt(), "new uptime check"),
cl.getOptionValue(HOST_NAME_OPTION.getOpt(), "example.com"),
cl.getOptionValue(PATH_NAME_OPTION.getOpt(), "/"));
Expand All @@ -131,12 +129,11 @@ public static void main(String... args) throws IOException {
listUptimeCheckIps();
break;
case "get":
getUptimeCheckConfig(
projectId, cl.getOptionValue(DISPLAY_NAME_OPTION.getOpt(), "new uptime check"));
getUptimeCheckConfig(cl.getOptionValue(DISPLAY_NAME_OPTION.getOpt(), "new uptime check"));
break;
case "delete":
deleteUptimeCheckConfig(
projectId, cl.getOptionValue(DISPLAY_NAME_OPTION.getOpt(), "new uptime check"));
cl.getOptionValue(DISPLAY_NAME_OPTION.getOpt(), "new uptime check"));
break;
default:
usage(null);
Expand All @@ -162,7 +159,7 @@ private static void createUptimeCheck(
.build();
try (UptimeCheckServiceClient client = UptimeCheckServiceClient.create()) {
UptimeCheckConfig config = client.createUptimeCheckConfig(request);
System.out.println("Uptime check created: " + config.getDisplayName());
System.out.println("Uptime check created: " + config.getName());
} catch (Exception e) {
usage("Exception creating uptime check: " + e.toString());
throw e;
Expand All @@ -171,16 +168,15 @@ private static void createUptimeCheck(
// [END monitoring_uptime_check_create]]

// [START monitoring_uptime_check_update]]
private static void updateUptimeCheck(
String projectId, String displayName, String hostName, String pathName) throws IOException {
String fullCheckName = UptimeCheckConfigName.format(projectId, displayName);
private static void updateUptimeCheck(String checkName, String hostName, String pathName)
throws IOException {

UpdateUptimeCheckConfigRequest request =
UpdateUptimeCheckConfigRequest.newBuilder()
.setUpdateMask(FieldMask.newBuilder().addPaths("http_check.path"))
.setUptimeCheckConfig(
UptimeCheckConfig.newBuilder()
.setName(fullCheckName)
.setName(checkName)
.setMonitoredResource(
MonitoredResource.newBuilder()
.setType("uptime_url")
Expand Down Expand Up @@ -231,7 +227,7 @@ private static void listUptimeCheckIps() throws IOException {
// [END monitoring_uptime_check_list_ips]]

// [START monitoring_uptime_check_get]]
private static void getUptimeCheckConfig(String projectId, String checkName) throws IOException {
private static void getUptimeCheckConfig(String checkName) throws IOException {
// Create UptimeCheckServiceSettings instance for add retry mechanism
UptimeCheckServiceSettings.Builder uptimeCheckServiceSettingsBuilder =
UptimeCheckServiceSettings.newBuilder();
Expand All @@ -257,13 +253,11 @@ private static void getUptimeCheckConfig(String projectId, String checkName) thr
// create UptimeCheckServiceClient with retry setting
try (UptimeCheckServiceClient client =
UptimeCheckServiceClient.create(uptimeCheckServiceSettings)) {
String fullCheckName = UptimeCheckConfigName.format(projectId, checkName);
UptimeCheckConfig config = client.getUptimeCheckConfig(fullCheckName);
UptimeCheckConfig config = client.getUptimeCheckConfig(checkName);
if (config != null) {
System.out.println(config.toString());
} else {
System.out.println(
"No uptime check config found with name " + checkName + " in project " + projectId);
System.out.println("No uptime check config found with ID " + checkName);
}
} catch (Exception e) {
usage("Exception getting uptime check: " + e.toString());
Expand All @@ -273,10 +267,9 @@ private static void getUptimeCheckConfig(String projectId, String checkName) thr
// [END monitoring_uptime_check_get]]

// [START monitoring_uptime_check_delete]]
private static void deleteUptimeCheckConfig(String projectId, String checkName)
throws IOException {
private static void deleteUptimeCheckConfig(String checkName) throws IOException {
try (UptimeCheckServiceClient client = UptimeCheckServiceClient.create()) {
client.deleteUptimeCheckConfig(UptimeCheckConfigName.format(projectId, checkName));
client.deleteUptimeCheckConfig(checkName);
} catch (Exception e) {
usage("Exception deleting uptime check: " + e.toString());
throw e;
Expand Down
Expand Up @@ -38,6 +38,7 @@ public class UptimeIT {
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;
private static String checkName;

private static UptimeCheckConfig config =
UptimeCheckConfig.newBuilder()
Expand All @@ -62,18 +63,20 @@ public void tearDown() {
@Test
public void test1_CreateUptimeCheck() throws Exception {
UptimeSample.main("create", "-n", config.getDisplayName(), "-o", "test.example.com", "-a", "/");
assertThat(bout.toString()).contains("Uptime check created: " + config.getDisplayName());
String actual = bout.toString();
assertThat(actual).contains(config.getDisplayName());
checkName = actual.split(":")[1].trim();
}

@Test
public void test2_UpdateUptimeCheck() throws Exception {
UptimeSample.main("update", "-n", config.getDisplayName(), "-a", "/updated");
UptimeSample.main("update", "-n", checkName, "-a", "/updated");
assertThat(bout.toString()).contains("/updated");
}

@Test
public void test2_GetUptimeCheck() throws Exception {
UptimeSample.main("get", "-n", config.getDisplayName());
UptimeSample.main("get", "-n", checkName);
assertThat(bout.toString()).contains(config.getDisplayName());
}

Expand All @@ -96,6 +99,6 @@ public void test2_ListUptimeIps() throws Exception {

@Test
public void test3_DeleteUptimeCheck() throws Exception {
UptimeSample.main("delete", "-n", config.getDisplayName());
UptimeSample.main("delete", "-n", checkName);
}
}

0 comments on commit 7143f96

Please sign in to comment.