From 43708f54aa001797b3e290cc677d03e3129b10a1 Mon Sep 17 00:00:00 2001 From: ouitavon Date: Thu, 5 Dec 2019 17:16:41 -0800 Subject: [PATCH 1/6] Add list recommendations code sample --- recommender/beta/cloud-client/README.md | 49 +++++++++++ recommender/beta/cloud-client/pom.xml | 86 +++++++++++++++++++ .../recommender/ListRecommendations.java | 59 +++++++++++++ .../recommender/ListRecommendationsTest.java | 54 ++++++++++++ 4 files changed, 248 insertions(+) create mode 100644 recommender/beta/cloud-client/README.md create mode 100644 recommender/beta/cloud-client/pom.xml create mode 100644 recommender/beta/cloud-client/src/main/java/com/example/recommender/ListRecommendations.java create mode 100644 recommender/beta/cloud-client/src/test/java/com/example/recommender/ListRecommendationsTest.java diff --git a/recommender/beta/cloud-client/README.md b/recommender/beta/cloud-client/README.md new file mode 100644 index 00000000000..cb8182638c8 --- /dev/null +++ b/recommender/beta/cloud-client/README.md @@ -0,0 +1,49 @@ +# Getting Started with Recommender and the Google Java API Client library + + +Open in Cloud Shell + +[Cloud Recommender](https://cloud.google.com/recommender/) is a service on Google Cloud that provides +usage recommendations for Cloud products and services. This sample Java +application demonstrates how to access the Recommender API using the +[Google Cloud Client Library for Java](https://github.com/GoogleCloudPlatform/google-cloud-java). + +## Quickstart + +### Setup +- Install [Maven](http://maven.apache.org/). +- [Enable Recommender API](https://cloud.google.com/recommender/docs/enabling) for your project. +- [Authenticate using a service account](https://cloud.google.com/docs/authentication/getting-started). +Create a service account, download a JSON key file, and set the +`GOOGLE_APPLICATION_CREDENTIALS` environment variable. +- Set the `GOOGLE_CLOUD_PROJECT` environment variable to your project ID. + +### Build +Build your project with: +``` +mvn clean package -DskipTests +``` + +### List Recommendations +To list recommendations for your project: +``` +mvn exec:java -Dexec.mainClass=com.example.recommender.ListRecommendations \ + -Dexec.args="location-id recommender-id" +``` + +`location-id` is the [Google Cloud location](https://cloud.google.com/compute/docs/regions-zones/) +where resources associated with the recommendations are located and +`recommender-id` is the fully-qualified [recommender ID](https://cloud.google.com/recommender/docs/recommenders#recommenders). + +Press `Ctrl+C` to exit the application. + +To list IAM recommendations for your project: +``` +mvn exec:java -Dexec.mainClass=com.example.recommender.ListRecommendations \ + -Dexec.args="global google.iam.policy.Recommender" +``` +## Testing +To run the unit tests: +``` +mvn clean verify +``` diff --git a/recommender/beta/cloud-client/pom.xml b/recommender/beta/cloud-client/pom.xml new file mode 100644 index 00000000000..63860466da3 --- /dev/null +++ b/recommender/beta/cloud-client/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + com.example.recommender + recommender-samples + jar + + + + com.google.cloud.samples + shared-configuration + 1.0.11 + + + + 1.8 + 1.8 + + + + + + + + com.google.cloud + libraries-bom + 3.0.0 + pom + import + + + + + + + com.google.cloud + google-cloud-recommender + 0.1.2 + + + + + + junit + junit + 4.13-beta-3 + test + + + com.google.truth + truth + 1.0 + test + + + + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + false + + + + + diff --git a/recommender/beta/cloud-client/src/main/java/com/example/recommender/ListRecommendations.java b/recommender/beta/cloud-client/src/main/java/com/example/recommender/ListRecommendations.java new file mode 100644 index 00000000000..7d1e7d28524 --- /dev/null +++ b/recommender/beta/cloud-client/src/main/java/com/example/recommender/ListRecommendations.java @@ -0,0 +1,59 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.recommender; + +// [START recommender_list_recommendations] +import com.google.cloud.recommender.v1beta1.ListRecommendationsRequest; +import com.google.cloud.recommender.v1beta1.Recommendation; +import com.google.cloud.recommender.v1beta1.RecommenderClient; +import java.io.IOException; + +public class ListRecommendations { + + // List recommendations for a specified project, location, and recommender + public static void listRecommendations(String projectId, String location, String recommender) { + try (RecommenderClient recommenderClient = RecommenderClient.create()) { + String parent = + String.format( + "projects/%s/locations/%s/recommenders/%s", projectId, location, recommender); + ListRecommendationsRequest request = + ListRecommendationsRequest.newBuilder().setParent(parent).build(); + for (Recommendation responseItem : + recommenderClient.listRecommendations(request).iterateAll()) { + Recommendation recommendation = responseItem; + System.out.println("Recommendation name: " + recommendation.getName()); + System.out.println("- description: " + recommendation.getDescription()); + System.out.println( + "- primary_impact.category: " + recommendation.getPrimaryImpact().getCategory()); + System.out.println("- state_info.state: " + recommendation.getStateInfo().getState()); + System.out.println(); + } + System.out.println("List recommendations successful"); + } catch (IOException e) { + System.out.println("Unable to initialize recommender client: \n" + e.toString()); + } + } + + public static void main(String... args) { + String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); + String location = args[0]; + String recommender = args[1]; + + listRecommendations(projectId, location, recommender); + } +} +// [END recommender_list_recommendations] diff --git a/recommender/beta/cloud-client/src/test/java/com/example/recommender/ListRecommendationsTest.java b/recommender/beta/cloud-client/src/test/java/com/example/recommender/ListRecommendationsTest.java new file mode 100644 index 00000000000..69504ecc3c8 --- /dev/null +++ b/recommender/beta/cloud-client/src/test/java/com/example/recommender/ListRecommendationsTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2019 Google LLC + * + * 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. + */ + +package com.example.recommender; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ListRecommendationsTest { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String LOCATION = "global"; + private static final String RECOMMENDER = "google.iam.policy.Recommender"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() throws Exception { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void listRecommendations() throws IOException { + ListRecommendations.listRecommendations(PROJECT_ID, LOCATION, RECOMMENDER); + + assertThat(bout.toString()).contains("List recommendations successful"); + } +} From 5763d98dd25f03e269b7d3dd644c3704f3325d82 Mon Sep 17 00:00:00 2001 From: ouitavon <58575490+ouitavon@users.noreply.github.com> Date: Fri, 6 Dec 2019 14:12:36 -0800 Subject: [PATCH 2/6] Update recommender/beta/cloud-client/pom.xml Co-Authored-By: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> --- recommender/beta/cloud-client/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recommender/beta/cloud-client/pom.xml b/recommender/beta/cloud-client/pom.xml index 63860466da3..fb576d54095 100644 --- a/recommender/beta/cloud-client/pom.xml +++ b/recommender/beta/cloud-client/pom.xml @@ -1,5 +1,5 @@