Skip to content

Commit 28b83b9

Browse files
averikitschgrayside
authored andcommitted
docs(samples): Add HTTP Task Sample with Authentication Token (#200)
* Task with authentication sample * Update region tags * Linting error * Small updates * Add real service account to tests * Update queue id
1 parent a8693a9 commit 28b83b9

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

cloud-tasks/Dockerfile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use the official Node.js 10 image.
2+
# https://hub.docker.com/_/node
3+
FROM node:10
4+
5+
# Create and change to the app directory.
6+
WORKDIR /usr/src/app
7+
8+
# Copy application dependency manifests to the container image.
9+
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
10+
# Copying this separately prevents re-running npm install on every code change.
11+
COPY package.json package*.json ./
12+
13+
# Install production dependencies.
14+
RUN npm install --only=production
15+
16+
# Copy local code to the container image.
17+
COPY . .
18+
19+
# Run the web service on container startup.
20+
CMD [ "npm", "start" ]
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright 2019, Google LLC
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+
'use strict';
17+
18+
/**
19+
* Create a task with an HTTP target for a given queue with an arbitrary payload.
20+
*/
21+
async function createHttpTaskWithToken(
22+
project,
23+
location,
24+
queue,
25+
url,
26+
email,
27+
payload,
28+
inSeconds
29+
) {
30+
// [START cloud_tasks_create_http_task_with_token]
31+
// Imports the Google Cloud Tasks library.
32+
const {v2beta3} = require('@google-cloud/tasks');
33+
34+
// Instantiates a client.
35+
const client = new v2beta3.CloudTasksClient();
36+
37+
// TODO(developer): Uncomment these lines and replace with your values.
38+
// const project = 'my-project-id';
39+
// const queue = 'my-appengine-queue';
40+
// const location = 'us-central1';
41+
// const url = 'https://<project-id>.appspot.com/log_payload'
42+
// const email = 'client@<project-id>.iam.gserviceaccount.com'
43+
// const options = {payload: 'hello'};
44+
45+
// Construct the fully qualified queue name.
46+
const parent = client.queuePath(project, location, queue);
47+
48+
const task = {
49+
httpRequest: {
50+
httpMethod: 'POST',
51+
url, //The full url path that the request will be sent to.
52+
oidcToken: {
53+
serviceAccountEmail: email,
54+
},
55+
},
56+
};
57+
58+
if (payload) {
59+
task.httpRequest.body = Buffer.from(payload).toString('base64');
60+
}
61+
62+
if (inSeconds) {
63+
task.scheduleTime = {
64+
seconds: inSeconds + Date.now() / 1000,
65+
};
66+
}
67+
68+
const request = {
69+
parent: parent,
70+
task: task,
71+
};
72+
73+
console.log('Sending task:');
74+
console.log(task);
75+
// Send create task request.
76+
const [response] = await client.createTask(request);
77+
const name = response.name;
78+
console.log(`Created task ${name}`);
79+
80+
// [END cloud_tasks_create_http_task_with_token]
81+
}
82+
83+
createHttpTaskWithToken(...process.argv.slice(2)).catch(console.error);

cloud-tasks/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"devDependencies": {
2121
"chai": "^4.2.0",
22-
"mocha": "^6.0.0",
22+
"mocha": "^6.1.3",
2323
"uuid": "^3.3.2"
2424
}
2525
}

cloud-tasks/test/test.samples.js

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2323
const PROJECT_ID = process.env.GCLOUD_PROJECT;
2424
const queueName = `gcloud-${uuid.v4().split('-')[0]}`;
2525
const URL = `https://${PROJECT_ID}.appspot.com/log_payload`;
26+
const SERVICE_ACCOUNT = process.env.CLOUD_RUN_INVOKER_SERVICE_ACCOUNT;
2627

2728
describe('Cloud Task Sample Tests', () => {
2829
it('should create a queue', () => {
@@ -44,6 +45,13 @@ describe('Cloud Task Sample Tests', () => {
4445
assert.match(stdout, /Created task/);
4546
});
4647

48+
it('should create a HTTP task with token', () => {
49+
const stdout = execSync(
50+
`node createHttpTaskWithToken ${PROJECT_ID} us-central1 my-appengine-queue ${URL} ${SERVICE_ACCOUNT}`
51+
);
52+
assert.match(stdout, /Created task/);
53+
});
54+
4755
it('should delete a queue', () => {
4856
const stdout = execSync(`node deleteQueue ${PROJECT_ID} ${queueName}`);
4957
assert.match(stdout, /Deleted queue/);

0 commit comments

Comments
 (0)