|
| 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] |
0 commit comments