diff --git a/recommender/pom.xml b/recommender/pom.xml new file mode 100644 index 00000000000..e59392c320e --- /dev/null +++ b/recommender/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + com.example.recommender + google-cloud-recommender-snippets + jar + Google Recommender Snippets + https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/recommender + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + 1.8 + 1.8 + UTF-8 + + + + + + + + com.google.cloud + libraries-bom + 26.1.4 + pom + import + + + + + + + com.google.cloud + google-cloud-recommender + + + + + junit + junit + 4.13.2 + test + + + com.google.truth + truth + 1.1.3 + test + + + diff --git a/recommender/src/main/java/com/example/recommender/ListRecommendations.java b/recommender/src/main/java/com/example/recommender/ListRecommendations.java new file mode 100644 index 00000000000..229f59f6b65 --- /dev/null +++ b/recommender/src/main/java/com/example/recommender/ListRecommendations.java @@ -0,0 +1,94 @@ +/* + * 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 cloudrecommender_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 = + 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 cloudrecommender_list_recommendations] diff --git a/recommender/src/test/java/com/example/recommender/ListRecommendationsTest.java b/recommender/src/test/java/com/example/recommender/ListRecommendationsTest.java new file mode 100644 index 00000000000..5d7428d07fb --- /dev/null +++ b/recommender/src/test/java/com/example/recommender/ListRecommendationsTest.java @@ -0,0 +1,69 @@ +/* + * 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"); + } +}