Skip to content
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

samples: migrate code from googleapis/java-recommender #7410

Closed
wants to merge 96 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
e63bf28
fix!: operation value is now a one-of path_value (#41)
yoshi-automation Dec 4, 2019
4bdd608
samples: Add list recommendations code sample (#1789)
ouitavon Dec 7, 2019
d4249bb
feat!: use RecommendationName, remove deprecated recommendation name …
yoshi-automation Jan 27, 2020
962325b
samples: move generated samples to generated directory (#106)
chingor13 Feb 24, 2020
b8a74d0
samples: scaffold pom.xml files (#120)
yoshi-automation Mar 17, 2020
c705ffd
chore(deps): update dependency com.google.cloud:libraries-bom to v4.3…
renovate-bot Mar 23, 2020
85da8ca
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Mar 25, 2020
694de53
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Mar 30, 2020
abac118
chore(deps): update dependency com.google.cloud:libraries-bom to v4.4…
renovate-bot Apr 1, 2020
a237d35
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 6, 2020
013e01a
chore(deps): update dependency com.google.cloud:libraries-bom to v4.4…
renovate-bot Apr 6, 2020
ea15507
chore(deps): update dependency com.google.cloud:libraries-bom to v5 (…
renovate-bot Apr 14, 2020
b880ad3
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 16, 2020
c76fb64
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 17, 2020
38ac673
chore: fix samples snippet names, name in repo-metadata (#157)
chingor13 Apr 20, 2020
aeaa22e
chore(deps): update dependency com.google.cloud:libraries-bom to v5.2…
renovate-bot Apr 23, 2020
1c59fbe
chore(deps): update dependency com.google.cloud:libraries-bom to v5.3…
renovate-bot Apr 28, 2020
8a7019d
chore(deps): update dependency com.google.cloud:libraries-bom to v5.4…
renovate-bot May 19, 2020
9acbbb2
chore(deps): update dependency com.google.cloud:libraries-bom to v5.6…
renovate-bot Jun 9, 2020
0843008
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Jun 10, 2020
c5f2fe4
chore(deps): update dependency com.google.cloud:libraries-bom to v5.7…
renovate-bot Jun 10, 2020
885409b
chore(deps): update dependency com.google.cloud:libraries-bom to v6 (…
renovate-bot Jun 16, 2020
7d603ef
chore(deps): update dependency com.google.cloud:libraries-bom to v7 (…
renovate-bot Jun 17, 2020
9063dae
chore(deps): update dependency com.google.cloud:libraries-bom to v7.0…
renovate-bot Jun 22, 2020
a879e21
chore(deps): update dependency com.google.cloud:libraries-bom to v8 (…
renovate-bot Jun 26, 2020
d7de9f8
chore(deps): update dependency com.google.cloud:libraries-bom to v8.1…
renovate-bot Jul 16, 2020
6ac9783
Merge remote-tracking branch 'dpe/master' into samples-recommender-be…
chingor13 Aug 3, 2020
edfa233
chore(deps): update dependency com.google.cloud:libraries-bom to v10
renovate-bot Sep 10, 2020
831aa18
chore(deps): update dependency com.google.cloud:libraries-bom to v11 …
renovate-bot Sep 24, 2020
936d243
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Oct 2, 2020
39ea54b
chore(deps): update dependency com.google.cloud:libraries-bom to v12 …
renovate-bot Oct 6, 2020
0720106
chore(deps): update dependency com.google.cloud:libraries-bom to v13 …
renovate-bot Oct 20, 2020
a9bc233
chore(deps): update dependency com.google.cloud:libraries-bom to v13.…
renovate-bot Oct 21, 2020
f05d24a
chore(deps): update dependency com.google.cloud:libraries-bom to v13.…
renovate-bot Oct 23, 2020
675d707
chore(deps): update dependency com.google.cloud:libraries-bom to v13.…
renovate-bot Oct 27, 2020
c283850
test(deps): update dependency junit:junit to v4.13.1 (#294)
renovate-bot Oct 27, 2020
08dee7d
chore(deps): update dependency com.google.cloud:libraries-bom to v13.…
renovate-bot Oct 30, 2020
d264729
chore(deps): update dependency com.google.cloud:libraries-bom to v14 …
renovate-bot Nov 4, 2020
a1464b8
chore(deps): update dependency com.google.cloud:libraries-bom to v15 …
renovate-bot Nov 5, 2020
54c35fa
chore(deps): update dependency com.google.cloud:libraries-bom to v16 …
renovate-bot Nov 18, 2020
8e0bc1e
chore(deps): update dependency com.google.cloud:libraries-bom to v16.…
renovate-bot Nov 19, 2020
4897401
chore(deps): update dependency com.google.cloud:libraries-bom to v16.…
renovate-bot Dec 15, 2020
a2f0a41
chore(deps): update dependency com.google.cloud:libraries-bom to v16.…
renovate-bot Jan 7, 2021
51b0f38
chore(deps): update dependency com.google.cloud:libraries-bom to v16.…
renovate-bot Jan 14, 2021
29aef11
chore(deps): update dependency com.google.cloud:libraries-bom to v16.…
renovate-bot Feb 10, 2021
367f318
test(deps): update dependency junit:junit to v4.13.2 (#412)
renovate-bot Feb 16, 2021
d144100
test(deps): update dependency com.google.truth:truth to v1.1.2 (#397)
renovate-bot Feb 19, 2021
32d5f97
chore(deps): update dependency com.google.cloud:libraries-bom to v17 …
renovate-bot Feb 24, 2021
9ede3f1
chore(deps): update dependency com.google.cloud:libraries-bom to v18 …
renovate-bot Feb 25, 2021
a3fb948
chore(deps): update dependency com.google.cloud:libraries-bom to v18.…
renovate-bot Mar 3, 2021
25abd19
chore(deps): update dependency com.google.cloud:libraries-bom to v19 …
renovate-bot Mar 4, 2021
5e19bb6
chore(deps): update dependency com.google.cloud:libraries-bom to v19.…
renovate-bot Mar 29, 2021
ec5f936
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 9, 2021
526c6e4
chore(deps): update dependency com.google.cloud:libraries-bom to v20 …
renovate-bot Apr 12, 2021
1e54c5c
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Apr 19, 2021
35d8d4f
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Apr 29, 2021
6de9ec4
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot May 13, 2021
e697a10
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot May 17, 2021
51cfc6e
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot May 25, 2021
3c4232b
test(deps): update dependency com.google.truth:truth to v1.1.3 (#515)
renovate-bot May 26, 2021
2920053
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Jun 6, 2021
0ae7694
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Jun 7, 2021
5881532
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Jun 23, 2021
0861343
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Jul 9, 2021
d5b3d01
chore: migrate to owlbot (#545)
Neenu1995 Jul 16, 2021
8d0c0b5
chore: remove auto generated samples (#553)
Neenu1995 Aug 3, 2021
a43a163
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Aug 11, 2021
d31a86e
chore(deps): update dependency com.google.cloud:libraries-bom to v21 …
renovate-bot Aug 19, 2021
eca3f51
chore(deps): update dependency com.google.cloud:libraries-bom to v22 …
renovate-bot Aug 27, 2021
159a793
chore(deps): update dependency com.google.cloud:libraries-bom to v23 …
renovate-bot Sep 13, 2021
7a3605f
chore(deps): update dependency com.google.cloud:libraries-bom to v23.…
renovate-bot Oct 1, 2021
27f391a
chore(deps): update dependency com.google.cloud:libraries-bom to v24 …
renovate-bot Oct 27, 2021
4429642
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Dec 6, 2021
370724d
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Dec 8, 2021
a0e361c
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Dec 28, 2021
a13da0c
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Jan 7, 2022
7f43e29
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Jan 18, 2022
5ee4812
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Feb 8, 2022
8b42149
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Mar 3, 2022
43a2d40
chore(deps): update dependency com.google.cloud:libraries-bom to v25 …
renovate-bot Mar 14, 2022
bfd398a
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot Apr 1, 2022
7e20e4d
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot Apr 27, 2022
5b43adc
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot May 16, 2022
a0d2356
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot Jun 6, 2022
da8ea64
chore(deps): update dependency com.google.cloud:libraries-bom to v26 …
renovate-bot Jul 11, 2022
acf7708
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Aug 16, 2022
4115203
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Aug 31, 2022
aeb9c0a
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Sep 20, 2022
ab6235a
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Oct 7, 2022
b6237f3
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Nov 8, 2022
28c7b71
Merge remote-tracking branch 'migration/main' into java-recommender-m…
Shabirmean Nov 11, 2022
8e07810
chore: post migration updates - groupId, artifact url, repo references
Shabirmean Nov 11, 2022
fb04080
cleanup: update region tag prefix to match product
Shabirmean Nov 11, 2022
8c18a11
cleanup: update region tag prefix to match product
Shabirmean Nov 11, 2022
e305792
cleanup: update region tag prefix to match product
Shabirmean Nov 11, 2022
139b803
cleanup: update region tag prefix to match product
Shabirmean Nov 11, 2022
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
60 changes: 60 additions & 0 deletions recommender/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.recommender</groupId>
<artifactId>google-cloud-recommender-snippets</artifactId>
<packaging>jar</packaging>
<name>Google Recommender Snippets</name>
<url>https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/recommender</url>

<!--
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.2.0</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<!-- [START cloudrecommender_install_with_bom] -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.1.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

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

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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");
}
}