Skip to content

Commit

Permalink
docs(samples): Add HTTP Task Sample with Authentication Token (#200)
Browse files Browse the repository at this point in the history
* Task with authentication sample

* Update region tags

* Linting error

* Small updates

* Add real service account to tests

* Update queue id
  • Loading branch information
averikitsch authored and grayside committed Nov 3, 2022
1 parent a8693a9 commit 28b83b9
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
20 changes: 20 additions & 0 deletions cloud-tasks/Dockerfile
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" ]
83 changes: 83 additions & 0 deletions cloud-tasks/createHttpTaskWithToken.js
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);
2 changes: 1 addition & 1 deletion cloud-tasks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.0.0",
"mocha": "^6.1.3",
"uuid": "^3.3.2"
}
}
8 changes: 8 additions & 0 deletions cloud-tasks/test/test.samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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/);
Expand Down

0 comments on commit 28b83b9

Please sign in to comment.