Skip to content

Commit b1ae06d

Browse files
committed
Merge branch 'master' of https://github.com/GoogleCloudPlatform/java-docs-samples into speech-ga
2 parents 0400287 + 633b9d6 commit b1ae06d

File tree

17 files changed

+643
-445
lines changed

17 files changed

+643
-445
lines changed

appengine-java8/tasks/README.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ App Engine task attempts.
2121
[Cloud Tasks API](https://console.cloud.google.com/launcher/details/google/cloudtasks.googleapis.com).
2222
* Download and install the [Cloud SDK](https://cloud.google.com/sdk).
2323
* Download and install [Maven](http://maven.apache.org/install.html).
24+
* Set up [Google Application Credentials](https://cloud.google.com/docs/authentication/getting-started).
2425

2526
## Creating a queue
2627

@@ -37,25 +38,18 @@ version unless configured to do otherwise.
3738
[Using Maven and the App Engine Plugin](https://cloud.google.com/appengine/docs/flexible/java/using-maven)
3839
& [Maven Plugin Goals and Parameters](https://cloud.google.com/appengine/docs/flexible/java/maven-reference)
3940

40-
### Running locally
41-
42-
```
43-
mvn appengine:run
44-
```
45-
### Deploying
46-
4741
```
4842
mvn appengine:deploy
4943
```
5044

51-
## Running the Sample
45+
## Run the Sample Using the Command Line
5246

5347
Set environment variables:
5448

5549
First, your project ID:
5650

5751
```
58-
export GOOGLE_CLOUD_PROJECT=<YOUR_PROJECT_ID>
52+
export GOOGLE_CLOUD_PROJECT=<YOUR_GOOGLE_CLOUD_PROJECT>
5953
```
6054

6155
Then the queue ID, as specified at queue creation time. Queue IDs already
@@ -85,11 +79,7 @@ mvn exec:java -Dexec.mainClass="com.example.task.CreateTask" \
8579

8680
The App Engine app serves as a target for the push requests. It has an
8781
endpoint `/tasks/create` that reads the payload (i.e., the request body) of the
88-
HTTP POST request and logs it. The log output can be viewed with:
89-
90-
```
91-
gcloud app logs read
92-
```
82+
HTTP POST request and logs it. The log output can be viewed with [Stackdriver Logging](https://console.cloud.google.com/logs/viewer?minLogLevel=0).
9383

9484
Create a task that will be scheduled for a time in the future using the
9585
`--in-seconds` flag:

appengine-java8/tasks/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Copyright 2018 Google LLC
3737
<properties>
3838
<maven.compiler.target>1.8</maven.compiler.target>
3939
<maven.compiler.source>1.8</maven.compiler.source>
40+
<failOnMissingWebXml>false</failOnMissingWebXml>
4041
</properties>
4142

4243
<dependencies>
@@ -51,7 +52,7 @@ Copyright 2018 Google LLC
5152
<dependency>
5253
<groupId>com.google.cloud</groupId>
5354
<artifactId>google-cloud-tasks</artifactId>
54-
<version>0.54.0-beta</version>
55+
<version>0.61.0-beta</version>
5556
</dependency>
5657
<dependency>
5758
<groupId>commons-cli</groupId>
@@ -69,7 +70,7 @@ Copyright 2018 Google LLC
6970
<plugin>
7071
<groupId>com.google.cloud.tools</groupId>
7172
<artifactId>appengine-maven-plugin</artifactId>
72-
<version>1.3.1</version>
73+
<version>1.3.2</version>
7374
<configuration>
7475
<deploy.promote>true</deploy.promote>
7576
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>

appengine-java8/tasks/src/main/java/com/example/task/CreateTask.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package com.example.task;
1818

19-
import com.google.cloud.tasks.v2beta2.AppEngineHttpRequest;
20-
import com.google.cloud.tasks.v2beta2.CloudTasksClient;
21-
import com.google.cloud.tasks.v2beta2.HttpMethod;
22-
import com.google.cloud.tasks.v2beta2.QueueName;
23-
import com.google.cloud.tasks.v2beta2.Task;
19+
import com.google.cloud.tasks.v2beta3.AppEngineHttpRequest;
20+
import com.google.cloud.tasks.v2beta3.CloudTasksClient;
21+
import com.google.cloud.tasks.v2beta3.HttpMethod;
22+
import com.google.cloud.tasks.v2beta3.QueueName;
23+
import com.google.cloud.tasks.v2beta3.Task;
2424
import com.google.common.base.Strings;
2525
import com.google.protobuf.ByteString;
2626
import com.google.protobuf.Timestamp;
@@ -38,7 +38,7 @@
3838
import org.apache.commons.cli.ParseException;
3939

4040
public class CreateTask {
41-
private static String GGOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT";
41+
private static String GOOGLE_CLOUD_PROJECT_KEY = "GOOGLE_CLOUD_PROJECT";
4242

4343
private static Option PROJECT_ID_OPTION = Option.builder("pid")
4444
.longOpt("project-id")
@@ -109,7 +109,7 @@ public static void main(String... args) throws Exception {
109109
if (params.hasOption("project-id")) {
110110
projectId = params.getOptionValue("project-id");
111111
} else {
112-
projectId = System.getenv(GGOGLE_CLOUD_PROJECT_KEY);
112+
projectId = System.getenv(GOOGLE_CLOUD_PROJECT_KEY);
113113
}
114114
if (Strings.isNullOrEmpty(projectId)) {
115115
printUsage(options);
@@ -121,22 +121,37 @@ public static void main(String... args) throws Exception {
121121
String payload = params.getOptionValue(PAYLOAD_OPTION.getOpt(), "default payload");
122122

123123
// [START cloud_tasks_appengine_create_task]
124+
// Instantiates a client.
124125
try (CloudTasksClient client = CloudTasksClient.create()) {
126+
127+
// Variables provided by the CLI.
128+
// projectId = "my-project-id";
129+
// queueName = "my-appengine-queue";
130+
// location = "us-central1";
131+
// payload = "hello";
132+
133+
// Construct the fully qualified queue name.
134+
String queuePath = QueueName.of(projectId, location, queueName).toString();
135+
136+
// Construct the task body.
125137
Task.Builder taskBuilder = Task
126138
.newBuilder()
127139
.setAppEngineHttpRequest(AppEngineHttpRequest.newBuilder()
128-
.setPayload(ByteString.copyFrom(payload, Charset.defaultCharset()))
129-
.setRelativeUrl("/tasks/create")
140+
.setBody(ByteString.copyFrom(payload, Charset.defaultCharset()))
141+
.setRelativeUri("/tasks/create")
130142
.setHttpMethod(HttpMethod.POST)
131143
.build());
144+
132145
if (params.hasOption(IN_SECONDS_OPTION.getOpt())) {
146+
// Add the scheduled time to the request.
133147
int seconds = Integer.parseInt(params.getOptionValue(IN_SECONDS_OPTION.getOpt()));
134148
taskBuilder.setScheduleTime(Timestamp
135149
.newBuilder()
136150
.setSeconds(Instant.now(Clock.systemUTC()).plusSeconds(seconds).getEpochSecond()));
137151
}
138-
Task task = client.createTask(
139-
QueueName.of(projectId, location, queueName).toString(), taskBuilder.build());
152+
153+
// Send create task request.
154+
Task task = client.createTask(queuePath, taskBuilder.build());
140155
System.out.println("Task created: " + task.getName());
141156
}
142157
// [END cloud_tasks_appengine_create_task]

appengine-java8/tasks/src/main/java/com/example/task/TaskServlet.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ public class TaskServlet extends HttpServlet {
3535
@Override
3636
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
3737
log.info("Received task request: " + req.getServletPath());
38-
if (req.getParameter("payload") != null) {
39-
String payload = req.getParameter("payload");
40-
log.info("Request payload: " + payload);
41-
String output = String.format("Received task with payload %s", payload);
38+
String body = req.getReader()
39+
.lines()
40+
.reduce("", (accumulator, actual) -> accumulator + actual);
41+
42+
if (!body.isEmpty()) {
43+
log.info("Request payload: " + body);
44+
String output = String.format("Received task with payload %s", body);
4245
resp.getOutputStream().write(output.getBytes());
4346
log.info("Sending response: " + output);
4447
resp.setStatus(HttpServletResponse.SC_OK);

iam/api-client/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Cloud Identity & Access Management Samples
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=iam/api-client/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
[Google Cloud Identity & Access Management](https://cloud.google.com/iam/) (IAM)
7+
lets administrators authorize who can take action on specific resources.
8+
These sample applications demonstrate how to interact with Cloud IAM using
9+
the Google API Client Library for Java.
10+
11+
## Quickstart
12+
13+
Install [Maven](http://maven.apache.org/).
14+
15+
Build the project with:
16+
17+
```xml
18+
mvn clean package
19+
```
20+
21+
Run the Quickstart, which lists roles in a project:
22+
23+
```xml
24+
mvn exec:java
25+
```

iam/api-client/pom.xml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<!--
2+
Copyright 2018 Google Inc.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
14+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
15+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
16+
<modelVersion>4.0.0</modelVersion>
17+
<groupId>com.google.iam.snippets</groupId>
18+
<artifactId>iam-snippets</artifactId>
19+
<packaging>jar</packaging>
20+
<version>1.0</version>
21+
<name>iam-snippets</name>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.10</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>1.8</maven.compiler.target>
35+
<maven.compiler.source>1.8</maven.compiler.source>
36+
</properties>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>com.google.apis</groupId>
41+
<artifactId>google-api-services-iam</artifactId>
42+
<version>v1-rev247-1.23.0</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>commons-cli</groupId>
46+
<artifactId>commons-cli</artifactId>
47+
<version>1.4</version>
48+
</dependency>
49+
50+
<!-- Test dependencies -->
51+
<dependency>
52+
<groupId>junit</groupId>
53+
<artifactId>junit</artifactId>
54+
<version>4.12</version>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.google.truth</groupId>
59+
<artifactId>truth</artifactId>
60+
<version>0.40</version>
61+
<scope>test</scope>
62+
</dependency>
63+
</dependencies>
64+
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.codehaus.mojo</groupId>
69+
<artifactId>exec-maven-plugin</artifactId>
70+
<version>1.4.0</version>
71+
<configuration>
72+
<mainClass>com.google.iam.snippets.GrantableRoles</mainClass>
73+
</configuration>
74+
</plugin>
75+
</plugins>
76+
</build>
77+
78+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Copyright 2018 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.google.iam.snippets;
17+
18+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
19+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
20+
import com.google.api.client.json.jackson2.JacksonFactory;
21+
import com.google.api.services.iam.v1.Iam;
22+
import com.google.api.services.iam.v1.IamScopes;
23+
import com.google.api.services.iam.v1.model.QueryGrantableRolesRequest;
24+
import com.google.api.services.iam.v1.model.QueryGrantableRolesResponse;
25+
import com.google.api.services.iam.v1.model.Role;
26+
import java.util.Collections;
27+
28+
public class GrantableRoles {
29+
30+
public static void main(String[] args) throws Exception {
31+
32+
GoogleCredential credential =
33+
GoogleCredential.getApplicationDefault()
34+
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
35+
36+
Iam service =
37+
new Iam.Builder(
38+
GoogleNetHttpTransport.newTrustedTransport(),
39+
JacksonFactory.getDefaultInstance(),
40+
credential)
41+
.setApplicationName("grantable-roles")
42+
.build();
43+
44+
String fullResourceName = args[0];
45+
46+
// [START iam_view_grantable_roles]
47+
QueryGrantableRolesRequest request = new QueryGrantableRolesRequest();
48+
request.setFullResourceName(fullResourceName);
49+
50+
QueryGrantableRolesResponse response = service.roles().queryGrantableRoles(request).execute();
51+
52+
for (Role role : response.getRoles()) {
53+
System.out.println("Title: " + role.getTitle());
54+
System.out.println("Name: " + role.getName());
55+
System.out.println("Description: " + role.getDescription());
56+
System.out.println();
57+
}
58+
// [START iam_view_grantable_roles]
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Copyright 2018 Google LLC
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
// [START iam_quickstart]
17+
18+
package com.google.iam.snippets;
19+
20+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
21+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
22+
import com.google.api.client.json.jackson2.JacksonFactory;
23+
import com.google.api.services.iam.v1.Iam;
24+
import com.google.api.services.iam.v1.IamScopes;
25+
import com.google.api.services.iam.v1.model.ListRolesResponse;
26+
import com.google.api.services.iam.v1.model.Role;
27+
import java.util.Collections;
28+
import java.util.List;
29+
30+
public class Quickstart {
31+
32+
public static void main(String[] args) throws Exception {
33+
// Get credentials
34+
GoogleCredential credential =
35+
GoogleCredential.getApplicationDefault()
36+
.createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM));
37+
38+
// Create the Cloud IAM service object
39+
Iam service =
40+
new Iam.Builder(
41+
GoogleNetHttpTransport.newTrustedTransport(),
42+
JacksonFactory.getDefaultInstance(),
43+
credential)
44+
.setApplicationName("quickstart")
45+
.build();
46+
47+
// Call the Cloud IAM Roles API
48+
ListRolesResponse respose = service.roles().list().execute();
49+
List<Role> roles = respose.getRoles();
50+
51+
// Process the response
52+
for (Role role : roles) {
53+
System.out.println("Title: " + role.getTitle());
54+
System.out.println("Name: " + role.getName());
55+
System.out.println("Description: " + role.getDescription());
56+
System.out.println();
57+
}
58+
}
59+
}
60+
// [END iam_quickstart]

0 commit comments

Comments
 (0)