From 7eae54fa02a628f6984ccbf5347381ec248c088e Mon Sep 17 00:00:00 2001 From: Jerjou Cheng Date: Wed, 15 Jul 2015 10:41:05 -0700 Subject: [PATCH] Move java cloud monitoring sample into github. Also - add tests. --- monitoring/pom.xml | 76 +++++++++++++ .../main/java/CloudMonitoringAuthSample.java | 106 ++++++++++++++++++ .../java/CloudMonitoringAuthSampleTest.java | 63 +++++++++++ pom.xml | 1 + 4 files changed, 246 insertions(+) create mode 100644 monitoring/pom.xml create mode 100644 monitoring/src/main/java/CloudMonitoringAuthSample.java create mode 100644 monitoring/src/test/java/CloudMonitoringAuthSampleTest.java diff --git a/monitoring/pom.xml b/monitoring/pom.xml new file mode 100644 index 00000000000..9fa09084ec9 --- /dev/null +++ b/monitoring/pom.xml @@ -0,0 +1,76 @@ + + 4.0.0 + com.google.cloud.monitoring.samples + cloud-monitoring-samples + jar + + + doc-samples + com.google.cloud + 1.0.0 + .. + + + + + + googleapis + https://google-api-client-libraries.appspot.com/mavenrepo + + + + + + com.google.apis + google-api-services-cloudmonitoring + v2beta2-rev20-1.20.0 + + + com.google.oauth-client + google-oauth-client + ${project.oauth.version} + + + com.google.http-client + google-http-client-jackson2 + ${project.http.version} + + + com.google.oauth-client + google-oauth-client-jetty + ${project.oauth.version} + + + junit + junit + + + com.jcabi + jcabi-matchers + 1.3 + + + + + 1.19.0 + 1.19.0 + UTF-8 + + + + src/main/java + + + org.apache.maven.plugins + maven-compiler-plugin + 3.2 + + 5 + 5 + + + + + + diff --git a/monitoring/src/main/java/CloudMonitoringAuthSample.java b/monitoring/src/main/java/CloudMonitoringAuthSample.java new file mode 100644 index 00000000000..76f40b66cd1 --- /dev/null +++ b/monitoring/src/main/java/CloudMonitoringAuthSample.java @@ -0,0 +1,106 @@ +/** + * Copyright (c) 2015 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +[START all] +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.cloudmonitoring.CloudMonitoring; +import com.google.api.services.cloudmonitoring.CloudMonitoringScopes; + +import java.io.IOException; +import java.security.GeneralSecurityException; + +/** + * Simple command-line program to demonstrate connecting to and retrieving data + * from the Google Cloud Monitoring API using application default credentials. + */ +public final class CloudMonitoringAuthSample { + + /** + * The metric that we want to fetch. + */ + private static final String METRIC = + "compute.googleapis.com/instance/disk/read_ops_count"; + + /** + * The end of the time interval to fetch. + */ + private static final String YOUNGEST = "2015-01-01T00:00:00Z"; + + /** + * Utility class doesn't need to be instantiated. + */ + private CloudMonitoringAuthSample() { } + + + /** + * Builds and returns a CloudMonitoring service object authorized with the + * application default credentials. + * + * @return CloudMonitoring service object that is ready to make requests. + * @throws GeneralSecurityException if authentication fails. + * @throws IOException if authentication fails. + */ + private static CloudMonitoring authenticate() + throws GeneralSecurityException, IOException { + // Grab the Application Default Credentials from the environment. + GoogleCredential credential = GoogleCredential.getApplicationDefault() + .createScoped(CloudMonitoringScopes.all()); + + // Create and return the CloudMonitoring service object + HttpTransport httpTransport = new NetHttpTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + CloudMonitoring service = new CloudMonitoring.Builder(httpTransport, + jsonFactory, credential) + .setApplicationName("Demo") + .build(); + return service; + } + + /** + * Query the Google Cloud Monitoring API using a service account and print the + * result to the console. + * + * @param args The first arg should be the project name you'd like to inspect. + * @throws Exception if something goes wrong. + */ + public static void main(final String[] args) throws Exception { + if (args.length != 2) { + System.err.println(String.format("Usage: %s ", args[0])); + return; + } + + String project = args[1]; + + // Create an authorized API client + CloudMonitoring cloudmonitoring = authenticate(); + + CloudMonitoring.Timeseries.List timeseriesListRequest = + cloudmonitoring.timeseries().list(project, METRIC, YOUNGEST); + + System.out.println("Timeseries.list raw response:"); + System.out.println(timeseriesListRequest.execute().toPrettyString()); + + // This example only demonstrates completing the OAuth flow and displaying + // the raw response from a simple request. See the API client library docs + // for applicable methods for working with the returned data, including + // getting results and paging through results. + + } +} +[END all] diff --git a/monitoring/src/test/java/CloudMonitoringAuthSampleTest.java b/monitoring/src/test/java/CloudMonitoringAuthSampleTest.java new file mode 100644 index 00000000000..daeab53bb2a --- /dev/null +++ b/monitoring/src/test/java/CloudMonitoringAuthSampleTest.java @@ -0,0 +1,63 @@ +/** + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import static com.jcabi.matchers.RegexMatchers.*; +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.regex.Pattern; + +/** + * Tests the Cloud Monitoring auth sample. + */ +public class CloudMonitoringAuthSampleTest { + private final ByteArrayOutputStream stdout = + new ByteArrayOutputStream(); + private final ByteArrayOutputStream stderr = + new ByteArrayOutputStream(); + + @Before + public void setUp() { + System.setOut(new PrintStream(stdout)); + System.setErr(new PrintStream(stderr)); + } + + @After + public void tearDown() { + System.setOut(null); + System.setErr(null); + } + + @Test + public void testUsage() throws Exception { + CloudMonitoringAuthSample.main(new String[] { "command-name" }); + assertEquals("Usage: command-name \n", stderr.toString()); + } + + @Test + public void testListTimeSeries() throws Exception { + CloudMonitoringAuthSample.main(new String[] { "", "cloud-samples-tests" }); + String out = stdout.toString(); + assertThat(out, containsPattern("Timeseries.list raw response:")); + assertThat(out, containsPattern("\\{\\s*\"kind\" *: *\"cloudmonitoring#listTimeseriesResponse\",")); + assertThat(out, containsPattern(".*oldest.*")); + } +} diff --git a/pom.xml b/pom.xml index 03967ed9bc8..09ba94138aa 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,7 @@ bigquery cloud-storage/xml-api/cmdline-sample cloud-storage/xml-api/serviceaccount-appengine-sample + monitoring