|
| 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); |
0 commit comments