Skip to content

Commit 6ac9783

Browse files
committed
Merge remote-tracking branch 'dpe/master' into samples-recommender-beta-cloud-client
2 parents d7de9f8 + 4bdd608 commit 6ac9783

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.recommender;
18+
19+
// [START recommender_list_recommendations]
20+
21+
import com.google.api.gax.rpc.InvalidArgumentException;
22+
import com.google.api.gax.rpc.PermissionDeniedException;
23+
import com.google.cloud.recommender.v1beta1.ListRecommendationsRequest;
24+
import com.google.cloud.recommender.v1beta1.Recommendation;
25+
import com.google.cloud.recommender.v1beta1.RecommenderClient;
26+
import com.google.cloud.recommender.v1beta1.RecommenderClient.ListRecommendationsPagedResponse;
27+
import java.io.IOException;
28+
29+
public class ListRecommendations {
30+
31+
// List IAM recommendations for GOOGLE_CLOUD_PROJECT environment variable
32+
public static void listRecommendations() throws IOException {
33+
// TODO(developer): Replace the projectId variable before running the sample.
34+
String projectId = "my-project-id";
35+
36+
// Google Cloud location where resources associated with the recommendations are located (for
37+
// example, "global" or "us-central1-a"). For a full list of supported regions, visit
38+
// https://cloud.google.com/compute/docs/regions-zones/
39+
String location = "global";
40+
41+
// Fully-qualified recommender ID (for example, "google.iam.policy.Recommender" or
42+
// "google.compute.instance.MachineTypeRecommender"). For a full list of supported recommenders
43+
// visit https://cloud.google.com/recommender/docs/recommenders#recommenders
44+
String recommender = "google.iam.policy.Recommender";
45+
46+
listRecommendations(projectId, location, recommender);
47+
}
48+
49+
// List recommendations for a specified project, location, and recommender
50+
public static void listRecommendations(String projectId, String location, String recommender)
51+
throws IOException {
52+
// Initialize client that will be used to send requests. This client only needs to be created
53+
// once, and can be reused for multiple requests. After completing all of your requests, call
54+
// the "close" method on the client to safely clean up any remaining background resources.
55+
try (RecommenderClient recommenderClient = RecommenderClient.create()) {
56+
/// Build the request
57+
String parent =
58+
String.format(
59+
"projects/%s/locations/%s/recommenders/%s", projectId, location, recommender);
60+
ListRecommendationsRequest request =
61+
ListRecommendationsRequest.newBuilder().setParent(parent).build();
62+
63+
try {
64+
// Send the request
65+
ListRecommendationsPagedResponse response = recommenderClient.listRecommendations(request);
66+
67+
// Print out each recommendation
68+
for (Recommendation responseItem : response.iterateAll()) {
69+
Recommendation recommendation = responseItem;
70+
System.out.println("Recommendation name: " + recommendation.getName());
71+
System.out.println("- description: " + recommendation.getDescription());
72+
System.out.println(
73+
"- primary_impact.category: " + recommendation.getPrimaryImpact().getCategory());
74+
System.out.println("- state_info.state: " + recommendation.getStateInfo().getState());
75+
System.out.println();
76+
}
77+
78+
// Indicate the request was successful
79+
System.out.println("List recommendations successful");
80+
} catch (PermissionDeniedException e) {
81+
System.out.println("Permission denied for project '" + projectId
82+
+ "'. Ensure you have the appropriate permissions to list recommendations: \n" + e
83+
.toString());
84+
} catch (InvalidArgumentException e) {
85+
System.out.println(
86+
("Invalid argument for projectId. Ensure you have 'GOOGLE_CLOUD_PROJECT' set: \n"
87+
+ e.toString()));
88+
}
89+
}
90+
}
91+
}
92+
// [END recommender_list_recommendations]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.recommender;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class ListRecommendationsTest {
31+
32+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
33+
private static final String LOCATION = "global";
34+
private static final String RECOMMENDER = "google.iam.policy.Recommender";
35+
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
private static void requireEnvVar(String varName) {
40+
assertNotNull(
41+
System.getenv(varName),
42+
"Environment variable '%s' is required to perform these tests.".format(varName)
43+
);
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
49+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
50+
}
51+
52+
@Before
53+
public void setUp() throws Exception {
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
}
58+
59+
@After
60+
public void tearDown() {
61+
System.setOut(null);
62+
}
63+
64+
@Test
65+
public void listRecommendations() throws IOException {
66+
ListRecommendations.listRecommendations(PROJECT_ID, LOCATION, RECOMMENDER);
67+
68+
assertThat(bout.toString()).contains("List recommendations successful");
69+
}
70+
}

0 commit comments

Comments
 (0)