-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add list recommendations code sample #1789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
43708f5
Add list recommendations code sample
ouitavon ab2da8a
Merge branch 'master' into master
ouitavon 5763d98
Update recommender/beta/cloud-client/pom.xml
ouitavon 6b1a68d
Update recommender/beta/cloud-client/pom.xml
ouitavon a249d73
Address review comments
ouitavon 71fe99b
Merge branch 'master' of https://github.com/ouitavon/java-docs-samples
ouitavon 5dceaef
Add additional comments for location and recommender
ouitavon 9ab658e
Address CL comments
ouitavon 0159c01
Merge branch 'master' into master
kurtisvg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Getting Started with Recommender and the Google Java API Client library | ||
|
|
||
| <a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=README.md&cloudshell_working_dir=recommender/beta/cloud-client/"> | ||
| <img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a> | ||
|
|
||
| [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 | ||
| - [Set up a Java Development Environment for Maven](https://cloud.google.com/java/docs/setup). | ||
| - [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 | ||
| ``` | ||
|
|
||
| ## Testing | ||
| To run the unit tests: | ||
| ``` | ||
| mvn clean verify | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <!-- | ||
| 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. | ||
| --> | ||
| <project> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.example.recommender</groupId> | ||
| <artifactId>recommender-samples</artifactId> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <!-- | ||
| The parent pom defines common style checks and testing strategies for our samples. | ||
| Removing or replacing it should not affect the execution of the samples in anyway. | ||
| --> | ||
| <parent> | ||
| <groupId>com.google.cloud.samples</groupId> | ||
| <artifactId>shared-configuration</artifactId> | ||
| <version>1.0.11</version> | ||
| </parent> | ||
|
|
||
| <properties> | ||
| <maven.compiler.target>1.8</maven.compiler.target> | ||
| <maven.compiler.source>1.8</maven.compiler.source> | ||
| </properties> | ||
|
|
||
| <!-- [START recommender_java_dependencies] --> | ||
| <!-- Using libraries-bom to manage versions. | ||
| See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM --> | ||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>libraries-bom</artifactId> | ||
| <version>3.0.0</version> | ||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-recommender</artifactId> | ||
| <version>0.1.2</version> | ||
| </dependency> | ||
| <!-- [END recommender_java_dependencies] --> | ||
|
|
||
| <!-- Test dependencies --> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.truth</groupId> | ||
| <artifactId>truth</artifactId> | ||
| <version>1.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <!-- [START recommender_java_dependencies] --> | ||
| </dependencies> | ||
| <!-- [END recommender_java_dependencies] --> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.codehaus.mojo</groupId> | ||
| <artifactId>exec-maven-plugin</artifactId> | ||
| <version>1.6.0</version> | ||
| <configuration> | ||
| <cleanupDaemonThreads>false</cleanupDaemonThreads> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
92 changes: 92 additions & 0 deletions
92
recommender/beta/cloud-client/src/main/java/com/example/recommender/ListRecommendations.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * 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.api.gax.rpc.InvalidArgumentException; | ||
| import com.google.api.gax.rpc.PermissionDeniedException; | ||
| import com.google.cloud.recommender.v1beta1.ListRecommendationsRequest; | ||
| import com.google.cloud.recommender.v1beta1.Recommendation; | ||
| import com.google.cloud.recommender.v1beta1.RecommenderClient; | ||
| import com.google.cloud.recommender.v1beta1.RecommenderClient.ListRecommendationsPagedResponse; | ||
| import java.io.IOException; | ||
|
|
||
| public class ListRecommendations { | ||
|
|
||
| // List IAM recommendations for GOOGLE_CLOUD_PROJECT environment variable | ||
| public static void listRecommendations() throws IOException { | ||
| // TODO(developer): Replace the projectId variable before running the sample. | ||
| String projectId = "my-project-id"; | ||
|
|
||
| // Google Cloud location where resources associated with the recommendations are located (for | ||
| // example, "global" or "us-central1-a"). For a full list of supported regions, visit | ||
| // https://cloud.google.com/compute/docs/regions-zones/ | ||
| String location = "global"; | ||
|
|
||
| // Fully-qualified recommender ID (for example, "google.iam.policy.Recommender" or | ||
| // "google.compute.instance.MachineTypeRecommender"). For a full list of supported recommenders | ||
| // visit https://cloud.google.com/recommender/docs/recommenders#recommenders | ||
| String recommender = "google.iam.policy.Recommender"; | ||
|
|
||
| listRecommendations(projectId, location, recommender); | ||
| } | ||
|
|
||
| // List recommendations for a specified project, location, and recommender | ||
| public static void listRecommendations(String projectId, String location, String recommender) | ||
| throws IOException { | ||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (RecommenderClient recommenderClient = RecommenderClient.create()) { | ||
| /// Build the request | ||
| String parent = | ||
| String.format( | ||
| "projects/%s/locations/%s/recommenders/%s", projectId, location, recommender); | ||
| ListRecommendationsRequest request = | ||
kurtisvg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ListRecommendationsRequest.newBuilder().setParent(parent).build(); | ||
|
|
||
| try { | ||
| // Send the request | ||
| ListRecommendationsPagedResponse response = recommenderClient.listRecommendations(request); | ||
|
|
||
| // Print out each recommendation | ||
| for (Recommendation responseItem : response.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(); | ||
| } | ||
|
|
||
| // Indicate the request was successful | ||
| System.out.println("List recommendations successful"); | ||
| } catch (PermissionDeniedException e) { | ||
| System.out.println("Permission denied for project '" + projectId | ||
| + "'. Ensure you have the appropriate permissions to list recommendations: \n" + e | ||
| .toString()); | ||
| } catch (InvalidArgumentException e) { | ||
| System.out.println( | ||
| ("Invalid argument for projectId. Ensure you have 'GOOGLE_CLOUD_PROJECT' set: \n" | ||
| + e.toString())); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // [END recommender_list_recommendations] | ||
70 changes: 70 additions & 0 deletions
70
...nder/beta/cloud-client/src/test/java/com/example/recommender/ListRecommendationsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * 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 static junit.framework.TestCase.assertNotNull; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.PrintStream; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| 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; | ||
|
|
||
| private static void requireEnvVar(String varName) { | ||
| assertNotNull( | ||
| System.getenv(varName), | ||
| "Environment variable '%s' is required to perform these tests.".format(varName) | ||
| ); | ||
| } | ||
|
|
||
| @BeforeClass | ||
| public static void checkRequirements() { | ||
| requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); | ||
| requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
| } | ||
|
|
||
| @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"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.