Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 11 additions & 12 deletions tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,31 @@ export LOCATION_ID=<YOUR_ZONE>

### Creating Tasks with HTTP Targets

Set an environment variable for the endpoint to your task handler. This is an
example url:
```
export URL=https://example.com/taskshandler
```
Set an endpoint to your task handler by replacing the variable `url` with your
HTTP target in `CreateHttpTask.java`.

Running the sample will create a task and add it to your queue. As the queue
processes each task, it will send the task to the specific URL endpoint:
Running the sample will create a task and add it to your queue.
As the queue processes each task, it will send the task to the specific URL
endpoint:

```
mvn exec:java@HttpTask"
mvn exec:java@HttpTask
```

### Using HTTP Targets with Authentication Tokens

Set an endpoint to your task handler by replacing the variable `url` with your
HTTP target in `CreateHttpTaskWithToken.java`.

Your Cloud Tasks [service account][sa],
(service-<project-number>@gcp-sa-cloudtasks.iam.gserviceaccount.com), must
have the role of: `Service Account Token Creator` to generate a tokens.

Create or use an existing [service account][sa] to replace `<SERVICE_ACCOUNT_EMAIL>`
in `CreateHttpTaskWithToken.java`. This service account will be used to
authenticate the OIDC token.
Create or use an existing [service account][sa] to authenticate the OIDC token.

Running the sample with command:
```
mvn exec:java@WithToken"
mvn exec:java@WithToken -Dexec.args="<SERVICE_ACCOUNT_EMAIL>"
```


Expand Down
13 changes: 13 additions & 0 deletions tasks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,20 @@ Copyright 2018 Google LLC
<artifactId>google-cloud-tasks</artifactId>
<version>0.88.0-beta</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13-beta-2</version>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.44</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
5 changes: 2 additions & 3 deletions tasks/src/main/java/com/example/task/CreateHttpTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@

public class CreateHttpTask {

public static void main(String[] args) throws Exception {
public static void main(String... args) throws Exception {
String projectId = System.getenv("PROJECT_ID");
String queueName = System.getenv("QUEUE_ID");
String location = System.getenv("LOCATION_ID");
String url = System.getenv("URL");

// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {
// Variables provided by the system variables.
// projectId = "my-project-id";
// queueName = "my-queue";
// location = "us-central1";
// url = "https://example.com/taskhandler";
String url = "https://example.com/taskhandler";
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you call this out in the README? (CHANGEME)

String payload = "hello";

// Construct the fully qualified queue name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ public static void main(String[] args) throws Exception {
String projectId = System.getenv("PROJECT_ID");
String queueName = System.getenv("QUEUE_ID");
String location = System.getenv("LOCATION_ID");
String url = System.getenv("URL");

// Instantiates a client.
try (CloudTasksClient client = CloudTasksClient.create()) {
// Variables provided by the system variables.
// projectId = "my-project-id";
// queueName = "my-queue";
// location = "us-central1";
// url = "https://example.com/taskhandler";
String payload = "hello";
String email = args[0]; // Cloud IAM service account
String url =
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we suggest changing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since this is a snippet. I have updated with the sample without a main and highlighted the parameters.

"https://example.com/taskhandler"; // The full url path that the request will be sent to
String payload = "Hello, World!"; // The task HTTP request body

// Construct the fully qualified queue name.
String queuePath = QueueName.of(projectId, location, queueName).toString();

// Add your service account email to construct the OIDC token.
// in order to add an authentication header to the request.
OidcToken.Builder oidcTokenBuilder =
OidcToken.newBuilder().setServiceAccountEmail("<SERVICE_ACCOUNT_EMAIL>");
OidcToken.Builder oidcTokenBuilder = OidcToken.newBuilder().setServiceAccountEmail(email);

// Construct the task body.
Task.Builder taskBuilder =
Expand Down
65 changes: 65 additions & 0 deletions tasks/src/test/java/com/example/task/CreateHttpTaskIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.task;

import static com.google.common.truth.Truth.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for creating Tasks with HTTP targets. */
@RunWith(JUnit4.class)
public class CreateHttpTaskIT {

private ByteArrayOutputStream bout;
private PrintStream out;
// @Rule public Timeout globalTimeout = Timeout.seconds(300); // 5 minute timeout

Choose a reason for hiding this comment

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

Kept intentionally?


@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void testCreateHttpTask() throws Exception {
CreateHttpTask.main();
String got = bout.toString();
assertThat(got).contains("Task created:");
}

@Test
public void testCreateHttpTaskWithToken() throws Exception {
String[] args = {" tasks-test@java-docs-samples-tests.iam.gserviceaccount.com"};
CreateHttpTaskWithToken.main(args);
String got = bout.toString();
assertThat(got).contains("Task created:");
}
}