-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add list recommendations code sample #1789
Conversation
## Quickstart | ||
|
||
### Setup | ||
- Install [Maven](http://maven.apache.org/). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a no-arg, overloaded function for this - see Function Structure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, letting error bubble up.
Co-Authored-By: Kurtis Van Gent <[email protected]>
Co-Authored-By: Kurtis Van Gent <[email protected]>
ListRecommendationsRequest.newBuilder().setParent(parent).build(); | ||
|
||
// Send the request and print out each recommendation | ||
for (Recommendation responseItem : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
System.out.println("List recommendations successful"); | ||
} | ||
|
||
public static void main(String... args) throws IOException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated and added env var checks.
* Add list recommendations code sample * Update recommender/beta/cloud-client/pom.xml Co-Authored-By: Kurtis Van Gent <[email protected]> * Update recommender/beta/cloud-client/pom.xml Co-Authored-By: Kurtis Van Gent <[email protected]> * Address review comments * Add additional comments for location and recommender * Address CL comments
No description provided.