|
| 1 | +/** |
| 2 | + * Copyright 2017, 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 | + * |
| 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 | +const google = require('googleapis'); |
| 19 | +const cloudtasks = google.cloudtasks('v2beta2'); |
| 20 | + |
| 21 | +function authorize (callback) { |
| 22 | + google.auth.getApplicationDefault(function (err, authClient) { |
| 23 | + if (err) { |
| 24 | + console.error('authentication failed: ', err); |
| 25 | + return; |
| 26 | + } |
| 27 | + if (authClient.createScopedRequired && authClient.createScopedRequired()) { |
| 28 | + var scopes = ['https://www.googleapis.com/auth/cloud-platform']; |
| 29 | + authClient = authClient.createScoped(scopes); |
| 30 | + } |
| 31 | + callback(authClient); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Create a task for a given queue with an arbitrary payload. |
| 37 | + */ |
| 38 | +function createTask (project, location, queue, options) { |
| 39 | + authorize((authClient) => { |
| 40 | + const task = { |
| 41 | + app_engine_http_request: { |
| 42 | + http_method: 'POST', |
| 43 | + relative_url: '/log_payload' |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + if (options.payload !== undefined) { |
| 48 | + task.app_engine_http_request.payload = Buffer.from(options.payload).toString('base64'); |
| 49 | + } |
| 50 | + |
| 51 | + if (options.inSeconds !== undefined) { |
| 52 | + task.schedule_time = (new Date(options.inSeconds * 1000 + Date.now())).toISOString(); |
| 53 | + } |
| 54 | + |
| 55 | + const request = { |
| 56 | + parent: `projects/${project}/locations/${location}/queues/${queue}`, // TODO: Update placeholder value. |
| 57 | + resource: { |
| 58 | + task: task |
| 59 | + }, |
| 60 | + auth: authClient |
| 61 | + }; |
| 62 | + |
| 63 | + console.log('Sending task %j', task); |
| 64 | + |
| 65 | + cloudtasks.projects.locations.queues.tasks.create(request, (err, response) => { |
| 66 | + if (err) { |
| 67 | + console.error(err); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + console.log('Created task.', response.name); |
| 72 | + console.log(JSON.stringify(response, null, 2)); |
| 73 | + }); |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +const cli = require(`yargs`) |
| 78 | + .options({ |
| 79 | + location: { |
| 80 | + alias: 'l', |
| 81 | + description: 'Location of the queue to add the task to.', |
| 82 | + type: 'string', |
| 83 | + requiresArg: true, |
| 84 | + required: true |
| 85 | + }, |
| 86 | + queue: { |
| 87 | + alias: 'q', |
| 88 | + description: 'ID (short name) of the queue to add the task to.', |
| 89 | + type: 'string', |
| 90 | + requiresArg: true, |
| 91 | + required: true |
| 92 | + }, |
| 93 | + project: { |
| 94 | + alias: 'p', |
| 95 | + description: 'Project of the queue to add the task to.', |
| 96 | + default: process.env.GCLOUD_PROJECT, |
| 97 | + type: 'string', |
| 98 | + requiresArg: true, |
| 99 | + required: true |
| 100 | + }, |
| 101 | + payload: { |
| 102 | + alias: 'd', |
| 103 | + description: '(Optional) Payload to attach to the push queue.', |
| 104 | + type: 'string', |
| 105 | + requiresArg: true |
| 106 | + }, |
| 107 | + inSeconds: { |
| 108 | + alias: 's', |
| 109 | + description: '(Optional) The number of seconds from now to schedule task attempt.', |
| 110 | + type: 'number', |
| 111 | + requiresArg: true |
| 112 | + } |
| 113 | + }) |
| 114 | + .example(`node $0 --project my-project-id`) |
| 115 | + .wrap(120) |
| 116 | + .recommendCommands() |
| 117 | + .epilogue(`For more information, see https://cloud.google.com/cloud-tasks`) |
| 118 | + .strict(); |
| 119 | + |
| 120 | +if (module === require.main) { |
| 121 | + const opts = cli.help().parse(process.argv.slice(2)); |
| 122 | + |
| 123 | + process.env.GCLOUD_PROJECT = opts.project; |
| 124 | + |
| 125 | + createTask(opts.project, opts.location, opts.queue, opts); |
| 126 | +} |
| 127 | + |
| 128 | +exports.authorize = authorize; |
| 129 | +exports.createTask = createTask; |
0 commit comments