|
| 1 | +/** |
| 2 | + * Copyright (c) 2015 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +// [START all] |
| 17 | +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; |
| 18 | +import com.google.api.client.http.HttpTransport; |
| 19 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 20 | +import com.google.api.client.json.JsonFactory; |
| 21 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 22 | +import com.google.api.services.cloudmonitoring.CloudMonitoring; |
| 23 | +import com.google.api.services.cloudmonitoring.CloudMonitoringScopes; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.security.GeneralSecurityException; |
| 27 | + |
| 28 | +/** |
| 29 | + * Simple command-line program to demonstrate connecting to and retrieving data |
| 30 | + * from the Google Cloud Monitoring API using application default credentials. |
| 31 | + */ |
| 32 | +public final class CloudMonitoringAuthSample { |
| 33 | + |
| 34 | + /** |
| 35 | + * The metric that we want to fetch. |
| 36 | + */ |
| 37 | + private static final String METRIC = |
| 38 | + "compute.googleapis.com/instance/disk/read_ops_count"; |
| 39 | + |
| 40 | + /** |
| 41 | + * The end of the time interval to fetch. |
| 42 | + */ |
| 43 | + private static final String YOUNGEST = "2015-01-01T00:00:00Z"; |
| 44 | + |
| 45 | + /** |
| 46 | + * Utility class doesn't need to be instantiated. |
| 47 | + */ |
| 48 | + private CloudMonitoringAuthSample() { } |
| 49 | + |
| 50 | + |
| 51 | + /** |
| 52 | + * Builds and returns a CloudMonitoring service object authorized with the |
| 53 | + * application default credentials. |
| 54 | + * |
| 55 | + * @return CloudMonitoring service object that is ready to make requests. |
| 56 | + * @throws GeneralSecurityException if authentication fails. |
| 57 | + * @throws IOException if authentication fails. |
| 58 | + */ |
| 59 | + private static CloudMonitoring authenticate() |
| 60 | + throws GeneralSecurityException, IOException { |
| 61 | + // Grab the Application Default Credentials from the environment. |
| 62 | + GoogleCredential credential = GoogleCredential.getApplicationDefault() |
| 63 | + .createScoped(CloudMonitoringScopes.all()); |
| 64 | + |
| 65 | + // Create and return the CloudMonitoring service object |
| 66 | + HttpTransport httpTransport = new NetHttpTransport(); |
| 67 | + JsonFactory jsonFactory = new JacksonFactory(); |
| 68 | + CloudMonitoring service = new CloudMonitoring.Builder(httpTransport, |
| 69 | + jsonFactory, credential) |
| 70 | + .setApplicationName("Demo") |
| 71 | + .build(); |
| 72 | + return service; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Query the Google Cloud Monitoring API using a service account and print the |
| 77 | + * result to the console. |
| 78 | + * |
| 79 | + * @param args The first arg should be the project name you'd like to inspect. |
| 80 | + * @throws Exception if something goes wrong. |
| 81 | + */ |
| 82 | + public static void main(final String[] args) throws Exception { |
| 83 | + if (args.length != 2) { |
| 84 | + System.err.println(String.format("Usage: %s <project-name>", args[0])); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + String project = args[1]; |
| 89 | + |
| 90 | + // Create an authorized API client |
| 91 | + CloudMonitoring cloudmonitoring = authenticate(); |
| 92 | + |
| 93 | + CloudMonitoring.Timeseries.List timeseriesListRequest = |
| 94 | + cloudmonitoring.timeseries().list(project, METRIC, YOUNGEST); |
| 95 | + |
| 96 | + System.out.println("Timeseries.list raw response:"); |
| 97 | + System.out.println(timeseriesListRequest.execute().toPrettyString()); |
| 98 | + |
| 99 | + // This example only demonstrates completing the OAuth flow and displaying |
| 100 | + // the raw response from a simple request. See the API client library docs |
| 101 | + // for applicable methods for working with the returned data, including |
| 102 | + // getting results and paging through results. |
| 103 | + |
| 104 | + } |
| 105 | +} |
| 106 | +// [END all] |
0 commit comments