From 28b83b9b13e193eda1e452668e150fcc25699304 Mon Sep 17 00:00:00 2001 From: Averi Kitsch Date: Tue, 16 Apr 2019 09:29:05 -0700 Subject: [PATCH] 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 --- cloud-tasks/Dockerfile | 20 +++++++ cloud-tasks/createHttpTaskWithToken.js | 83 ++++++++++++++++++++++++++ cloud-tasks/package.json | 2 +- cloud-tasks/test/test.samples.js | 8 +++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 cloud-tasks/Dockerfile create mode 100644 cloud-tasks/createHttpTaskWithToken.js diff --git a/cloud-tasks/Dockerfile b/cloud-tasks/Dockerfile new file mode 100644 index 0000000000..9ff85bcafd --- /dev/null +++ b/cloud-tasks/Dockerfile @@ -0,0 +1,20 @@ +# Use the official Node.js 10 image. +# https://hub.docker.com/_/node +FROM node:10 + +# Create and change to the app directory. +WORKDIR /usr/src/app + +# Copy application dependency manifests to the container image. +# A wildcard is used to ensure both package.json AND package-lock.json are copied. +# Copying this separately prevents re-running npm install on every code change. +COPY package.json package*.json ./ + +# Install production dependencies. +RUN npm install --only=production + +# Copy local code to the container image. +COPY . . + +# Run the web service on container startup. +CMD [ "npm", "start" ] diff --git a/cloud-tasks/createHttpTaskWithToken.js b/cloud-tasks/createHttpTaskWithToken.js new file mode 100644 index 0000000000..f86ec24fd1 --- /dev/null +++ b/cloud-tasks/createHttpTaskWithToken.js @@ -0,0 +1,83 @@ +/** + * 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. + */ + +'use strict'; + +/** + * Create a task with an HTTP target for a given queue with an arbitrary payload. + */ +async function createHttpTaskWithToken( + project, + location, + queue, + url, + email, + payload, + inSeconds +) { + // [START cloud_tasks_create_http_task_with_token] + // Imports the Google Cloud Tasks library. + const {v2beta3} = require('@google-cloud/tasks'); + + // Instantiates a client. + const client = new v2beta3.CloudTasksClient(); + + // TODO(developer): Uncomment these lines and replace with your values. + // const project = 'my-project-id'; + // const queue = 'my-appengine-queue'; + // const location = 'us-central1'; + // const url = 'https://.appspot.com/log_payload' + // const email = 'client@.iam.gserviceaccount.com' + // const options = {payload: 'hello'}; + + // Construct the fully qualified queue name. + const parent = client.queuePath(project, location, queue); + + const task = { + httpRequest: { + httpMethod: 'POST', + url, //The full url path that the request will be sent to. + oidcToken: { + serviceAccountEmail: email, + }, + }, + }; + + if (payload) { + task.httpRequest.body = Buffer.from(payload).toString('base64'); + } + + if (inSeconds) { + task.scheduleTime = { + seconds: inSeconds + Date.now() / 1000, + }; + } + + const request = { + parent: parent, + task: task, + }; + + console.log('Sending task:'); + console.log(task); + // Send create task request. + const [response] = await client.createTask(request); + const name = response.name; + console.log(`Created task ${name}`); + + // [END cloud_tasks_create_http_task_with_token] +} + +createHttpTaskWithToken(...process.argv.slice(2)).catch(console.error); diff --git a/cloud-tasks/package.json b/cloud-tasks/package.json index 6882eb404d..b1ca7cc666 100644 --- a/cloud-tasks/package.json +++ b/cloud-tasks/package.json @@ -19,7 +19,7 @@ }, "devDependencies": { "chai": "^4.2.0", - "mocha": "^6.0.0", + "mocha": "^6.1.3", "uuid": "^3.3.2" } } diff --git a/cloud-tasks/test/test.samples.js b/cloud-tasks/test/test.samples.js index 14904947da..fa6a732e3d 100644 --- a/cloud-tasks/test/test.samples.js +++ b/cloud-tasks/test/test.samples.js @@ -23,6 +23,7 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const PROJECT_ID = process.env.GCLOUD_PROJECT; const queueName = `gcloud-${uuid.v4().split('-')[0]}`; const URL = `https://${PROJECT_ID}.appspot.com/log_payload`; +const SERVICE_ACCOUNT = process.env.CLOUD_RUN_INVOKER_SERVICE_ACCOUNT; describe('Cloud Task Sample Tests', () => { it('should create a queue', () => { @@ -44,6 +45,13 @@ describe('Cloud Task Sample Tests', () => { assert.match(stdout, /Created task/); }); + it('should create a HTTP task with token', () => { + const stdout = execSync( + `node createHttpTaskWithToken ${PROJECT_ID} us-central1 my-appengine-queue ${URL} ${SERVICE_ACCOUNT}` + ); + assert.match(stdout, /Created task/); + }); + it('should delete a queue', () => { const stdout = execSync(`node deleteQueue ${PROJECT_ID} ${queueName}`); assert.match(stdout, /Deleted queue/);