Skip to content

Commit

Permalink
docs(samples): Update metadata and arguments (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
averikitsch authored and grayside committed Nov 3, 2022
1 parent 4d3a1a7 commit 039454b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
20 changes: 11 additions & 9 deletions cloud-tasks/createHttpTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
// sample-metadata:
// title: Cloud Tasks Create HTTP Target
// description: Create Cloud Tasks with a HTTP Target
// usage: node createHttpTask.js <projectId> <queueName> <location> <url> <payload> <delayInSeconds>

/**
* Create a task with an HTTP target for a given queue with an arbitrary payload.
*/
async function createHttpTask(
project,
location,
queue,
url,
payload,
inSeconds
project = 'my-project-id', // Your GCP Project id
queue = 'my-appengine-queue', // Name of your Queue
location = 'us-central1', // The GCP region of your queue
url = 'https://example.com/taskhandler', // The full url path that the request will be sent to
payload = 'Hello, World!', // The task HTTP request body
inSeconds = 0 // Delay in task execution
) {
// [START cloud_tasks_create_http_task]
// Imports the Google Cloud Tasks library.
Expand All @@ -41,16 +42,16 @@ async function createHttpTask(
// const project = 'my-project-id';
// const queue = 'my-queue';
// const location = 'us-central1';
// const url = 'https://example.com/taskhandler'
// const payload = 'hello';
// const url = 'https://example.com/taskhandler';
// const payload = 'Hello, World!';

// 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.
url,
},
};

Expand All @@ -59,6 +60,7 @@ async function createHttpTask(
}

if (inSeconds) {
// The time when the task is scheduled to be attempted.
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
Expand Down
24 changes: 13 additions & 11 deletions cloud-tasks/createHttpTaskWithToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
// sample-metadata:
// title: Cloud Tasks Create HTTP Target with Token
// description: Create Cloud Tasks with a HTTP Target with Token
// usage: node createHttpTaskWithToken.js <projectId> <queueName> <location> <url> <serviceAccountEmail> <payload> <delayInSeconds>

/**
* 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
project = 'my-project-id', // Your GCP Project id
queue = 'my-queue', // Name of your Queue
location = 'us-central1', // The GCP region of your queue
url = 'https://example.com/taskhandler', // The full url path that the request will be sent to
email = 'client@<project-id>.iam.gserviceaccount.com', // Cloud IAM service account
payload = 'Hello, World!', // The task HTTP request body
inSeconds = 0 // Delay in task execution
) {
// [START cloud_tasks_create_http_task_with_token]
// Imports the Google Cloud Tasks library.
Expand All @@ -42,17 +43,17 @@ async function createHttpTaskWithToken(
// const project = 'my-project-id';
// const queue = 'my-queue';
// const location = 'us-central1';
// const url = 'https://example.com/taskhandler'
// const email = 'client@<project-id>.iam.gserviceaccount.com'
// const payload = 'hello';
// const url = 'https://example.com/taskhandler';
// email = 'client@<project-id>.iam.gserviceaccount.com';
// const payload = 'Hello, World!';

// 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.
url,
oidcToken: {
serviceAccountEmail: email,
},
Expand All @@ -64,6 +65,7 @@ async function createHttpTaskWithToken(
}

if (inSeconds) {
// The time when the task is scheduled to be attempted.
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
Expand Down
17 changes: 14 additions & 3 deletions cloud-tasks/createTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@

'use strict';

// sample-metadata:
// title: Cloud Tasks Create App Engine Target
// description: Create Cloud Tasks with a Google App Engine Target
// usage: node createTask.js <projectId> <queueName> <location> <payload> <delayInSeconds>

/**
* Create a task for a given queue with an arbitrary payload.
*/
async function createTask(project, location, queue, payload, inSeconds) {
async function createTask(
project = 'my-project-id', // Your GCP Project id
queue = 'my-appengine-queue', // Name of your Queue
location = 'us-central1', // The GCP region of your queue
payload = 'Hello, World!', // The task HTTP request body
inSeconds = 0 // Delay in task execution
) {
// [START cloud_tasks_appengine_create_task]
// [START tasks_quickstart]
// Imports the Google Cloud Tasks library.
Expand All @@ -31,8 +42,7 @@ async function createTask(project, location, queue, payload, inSeconds) {
// const project = 'my-project-id';
// const queue = 'my-appengine-queue';
// const location = 'us-central1';
// const payload = 'hello';
// const inSeconds = 30;
// const payload = 'Hello, World!';

// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);
Expand All @@ -49,6 +59,7 @@ async function createTask(project, location, queue, payload, inSeconds) {
}

if (inSeconds) {
// The time when the task is scheduled to be attempted.
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
Expand Down
3 changes: 2 additions & 1 deletion cloud-tasks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
},
"scripts": {
"test": "mocha",
"start": "node server.js"
"start": "node server.js",
"lint": "eslint '**/*.js'"
},
"dependencies": {
"@google-cloud/tasks": "^1.1.0",
Expand Down
8 changes: 4 additions & 4 deletions cloud-tasks/test/test.samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Cloud Task Sample Tests', () => {
before(async () => {
const client = new CloudTasksClient();
projectId = await client.getProjectId();
url = `https://${projectId}.appspot.com/log_payload`;
url = `https://example.com/taskhandler`;
});

it('should create a queue', () => {
Expand All @@ -41,7 +41,7 @@ describe('Cloud Task Sample Tests', () => {

it('should create a task', () => {
const stdout = exec(
`node createTask ${projectId} us-central1 ${queueName}`
`node createTask ${projectId} ${queueName} us-central1`
);
assert.match(stdout, /Created task/);
});
Expand All @@ -55,14 +55,14 @@ describe('Cloud Task Sample Tests', () => {

it('should create a HTTP task', () => {
const stdout = exec(
`node createHttpTask ${projectId} us-central1 my-appengine-queue ${url}`
`node createHttpTask ${projectId} my-queue us-central1 ${url}`
);
assert.match(stdout, /Created task/);
});

it('should create a HTTP task with token', () => {
const stdout = exec(
`node createHttpTaskWithToken ${projectId} us-central1 my-appengine-queue ${url} ${SERVICE_ACCOUNT}`
`node createHttpTaskWithToken ${projectId} my-queue us-central1 ${url} ${SERVICE_ACCOUNT}`
);
assert.match(stdout, /Created task/);
});
Expand Down

0 comments on commit 039454b

Please sign in to comment.