Add list recommendations code sample#1789
Add list recommendations code sample#1789kurtisvg merged 9 commits intoGoogleCloudPlatform:masterfrom
Conversation
| ## Quickstart | ||
|
|
||
| ### Setup | ||
| - Install [Maven](http://maven.apache.org/). |
There was a problem hiding this comment.
Preferable we can point to the "Setting up a Java environment" page on GCP which is a little more detailed on what the need for their environment.
There was a problem hiding this comment.
Done, linked to Setting up a Java environment page for the first step.
| public class ListRecommendations { | ||
|
|
||
| // List recommendations for a specified project, location, and recommender | ||
| public static void listRecommendations(String projectId, String location, String recommender) { |
There was a problem hiding this comment.
Please add a no-arg, overloaded function for this - see Function Structure
There was a problem hiding this comment.
If location isn't required for testing (looks like it's set to global atm), please just inline in the sample (and add a comment with a link to where the rules around what region can be specified for recommender's
There was a problem hiding this comment.
Sounds good. Went ahead and just use global and google.iam.policy.Recommender as default location and recommender.
| } | ||
| } | ||
|
|
||
| public static void main(String... args) { |
There was a problem hiding this comment.
For snippets, we strongly prefer not to include CLI parsing as part of the snippet for maintenance reasons. Most users will find the sample from the docs for the snippet, and most often copy/paste the sample and use their IDE to execute - See Exception Handling
recommender/beta/cloud-client/src/main/java/com/example/recommender/ListRecommendations.java
Show resolved
Hide resolved
recommender/beta/cloud-client/src/main/java/com/example/recommender/ListRecommendations.java
Show resolved
Hide resolved
| System.out.println(); | ||
| } | ||
| System.out.println("List recommendations successful"); | ||
| } catch (IOException e) { |
There was a problem hiding this comment.
IOException is usually a low level file/networking error and should be allowed to bubble up - we only want to catch errors if it's a response from the service or if it's something we are going to show the user how to recover from - see Exception Handling
There was a problem hiding this comment.
Done, letting error bubble up.
Co-Authored-By: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com>
Co-Authored-By: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com>
| ListRecommendationsRequest.newBuilder().setParent(parent).build(); | ||
|
|
||
| // Send the request and print out each recommendation | ||
| for (Recommendation responseItem : |
There was a problem hiding this comment.
This looks better, but please separate out the call from the iteration to make more clear. Often users are unsure when the API call actually happens with built requests, so it's helpful to be specific:
// Send the request to the service and receive the response
List<Recommendation> response = recommenderClient.listRecommendations(request);
// Print each recommendation
for (Recommendation recommendation : response.iterateAll()) {
// ...
}
System.out.println("List recommendations successful");There was a problem hiding this comment.
Separated out the request from the print loop.
| // List recommendations for a specified project, location, and recommender | ||
| public static void listRecommendations(String projectId, String location, String recommender) | ||
| throws IOException { | ||
| RecommenderClient recommenderClient = RecommenderClient.create(); |
There was a problem hiding this comment.
Please enclose with a try-with-resources and add the comment from the Sample Format guide regarding using .close():
// 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 (DlpServiceClient dlp = DlpServiceClient.create()) {
// Do something
}|
|
||
| // List IAM recommendations for GOOGLE_CLOUD_PROJECT environment variable | ||
| public static void listRecommendations() throws IOException { | ||
| String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); |
There was a problem hiding this comment.
Assume the user wants to edit inline and run from IDE:
| String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); | |
| // TODO(developer): Replace these variables before running the sample. | |
| String projectId = "my-project-id"; |
| System.out.println("List recommendations successful"); | ||
| } | ||
|
|
||
| public static void main(String... args) throws IOException { |
There was a problem hiding this comment.
Should exclude main altogether - users can run the snippet by replacing the variables in the no-arg function call and triggering themselves.
There was a problem hiding this comment.
Done, also removed option to run from main in the readme.
|
|
||
| @Test | ||
| public void listRecommendations() throws IOException { | ||
| ListRecommendations.listRecommendations(); |
There was a problem hiding this comment.
You'll need to update to listRecommendations(String projectId, String location, String recommender) - When move the env vars to this side, make sure to throw an error if they aren't there. See this example.
There was a problem hiding this comment.
Updated and added env var checks.
* Add list recommendations code sample * Update recommender/beta/cloud-client/pom.xml Co-Authored-By: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> * Update recommender/beta/cloud-client/pom.xml Co-Authored-By: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> * Address review comments * Add additional comments for location and recommender * Address CL comments
No description provided.