-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
a8693a9
commit 28b83b9
Showing
4 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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://<project-id>.appspot.com/log_payload' | ||
// const email = 'client@<project-id>.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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
}, | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"mocha": "^6.0.0", | ||
"mocha": "^6.1.3", | ||
"uuid": "^3.3.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters