Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions recommender/beta/cloud-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Getting Started with Recommender and the Google Java API Client library

<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=README.md&cloudshell_working_dir=recommender/beta/cloud-client/">
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>

[Cloud Recommender](https://cloud.google.com/recommender/) is a service on Google Cloud that provides
usage recommendations for Cloud products and services. This sample Java
application demonstrates how to access the Recommender API using the
[Google Cloud Client Library for Java](https://github.com/GoogleCloudPlatform/google-cloud-java).

## Quickstart

### Setup
- Install [Maven](http://maven.apache.org/).
Copy link
Contributor

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.

Copy link
Contributor Author

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.

- [Enable Recommender API](https://cloud.google.com/recommender/docs/enabling) for your project.
- [Authenticate using a service account](https://cloud.google.com/docs/authentication/getting-started).
Create a service account, download a JSON key file, and set the
`GOOGLE_APPLICATION_CREDENTIALS` environment variable.
- Set the `GOOGLE_CLOUD_PROJECT` environment variable to your project ID.

### Build
Build your project with:
```
mvn clean package -DskipTests
```

### List Recommendations
To list recommendations for your project:
```
mvn exec:java -Dexec.mainClass=com.example.recommender.ListRecommendations \
-Dexec.args="location-id recommender-id"
```

`location-id` is the [Google Cloud location](https://cloud.google.com/compute/docs/regions-zones/)
where resources associated with the recommendations are located and
`recommender-id` is the fully-qualified [recommender ID](https://cloud.google.com/recommender/docs/recommenders#recommenders).

Press `Ctrl+C` to exit the application.

To list IAM recommendations for your project:
```
mvn exec:java -Dexec.mainClass=com.example.recommender.ListRecommendations \
-Dexec.args="global google.iam.policy.Recommender"
```
## Testing
To run the unit tests:
```
mvn clean verify
```
86 changes: 86 additions & 0 deletions recommender/beta/cloud-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<!--
Copyright 2018 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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.recommender</groupId>
<artifactId>recommender-samples</artifactId>
<packaging>jar</packaging>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.11</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<!-- [START recommender_java_dependencies] -->
<!-- Using libraries-bom to manage versions.
See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>3.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-recommender</artifactId>
<version>0.1.2</version>
</dependency>
<!-- [END recommender_java_dependencies] -->

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13-beta-3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<!-- [START recommender_java_dependencies] -->
</dependencies>
<!-- [END recommender_java_dependencies] -->

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 recommender_list_recommendations]
import com.google.cloud.recommender.v1beta1.ListRecommendationsRequest;
import com.google.cloud.recommender.v1beta1.Recommendation;
import com.google.cloud.recommender.v1beta1.RecommenderClient;
import java.io.IOException;

public class ListRecommendations {

// List recommendations for a specified project, location, and recommender
public static void listRecommendations(String projectId, String location, String recommender) {
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor Author

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.

try (RecommenderClient recommenderClient = RecommenderClient.create()) {
String parent =
String.format(
"projects/%s/locations/%s/recommenders/%s", projectId, location, recommender);
ListRecommendationsRequest request =
ListRecommendationsRequest.newBuilder().setParent(parent).build();
for (Recommendation responseItem :
recommenderClient.listRecommendations(request).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();
}
System.out.println("List recommendations successful");
} catch (IOException e) {
Copy link
Contributor

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

Copy link
Contributor Author

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.

System.out.println("Unable to initialize recommender client: \n" + e.toString());
}
}

public static void main(String... args) {
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
String location = args[0];
String recommender = args[1];

listRecommendations(projectId, location, recommender);
}
}
// [END recommender_list_recommendations]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
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;

@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");
}
}