-
Notifications
You must be signed in to change notification settings - Fork 1k
Tasks create http task with token #1112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bshaffer
merged 8 commits into
GoogleCloudPlatform:master
from
MartinBarriga:tasks-create-http-task-with-token
Jul 8, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f0dff6f
add php snippet for creating a http task with token
MartinBarriga 78d3721
add php test for creating a Http task with token
MartinBarriga bf76f20
update licenses and apply suggested format changes
MartinBarriga 411d11f
Add setUpBeforeClass and removed url, email and payload variables
MartinBarriga 42e6922
delete newline between braces and add newline at the end of last brace
MartinBarriga 4930942
make service account a placeholder
MartinBarriga efd9031
Address PR comments
MartinBarriga 34995ce
change region tag and move payload comment to the end
MartinBarriga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,72 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2020 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. | ||
| */ | ||
|
|
||
| // Include Google Cloud dependendencies using Composer | ||
| require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
|
||
| if ($argc < 6 || $argc > 7) { | ||
| return printf("Usage: php %s PROJECT_ID LOCATION_ID QUEUE_ID URL SERVICE_ACCOUNT_EMAIL [PAYLOAD]\n", __FILE__); | ||
| } | ||
| list($_, $projectId, $locationId, $queueId, $url, $serviceAccountEmail) = $argv; | ||
| $payload = isset($payload[6]) ? $payload[6] : null; | ||
|
|
||
| # [START cloud_tasks_create_http_task_with_token] | ||
| use Google\Cloud\Tasks\V2\CloudTasksClient; | ||
| use Google\Cloud\Tasks\V2\HttpMethod; | ||
| use Google\Cloud\Tasks\V2\HttpRequest; | ||
| use Google\Cloud\Tasks\V2\Task; | ||
| use Google\Cloud\Tasks\V2\OidcToken; | ||
|
|
||
| /** Uncomment and populate these variables in your code */ | ||
| // $projectId = 'The Google project ID'; | ||
| // $locationId = 'The Location ID'; | ||
| // $queueId = 'The Cloud Tasks Queue ID'; | ||
| // $url = 'The full url path that the task request will be sent to.'; | ||
| // $serviceAccountEmail = 'The Cloud IAM service account to be used at the construction of the token'; | ||
| // $payload = 'The payload your task should carry to the task handler. Optional'; | ||
|
|
||
| // Instantiate the client and queue name. | ||
| $client = new CloudTasksClient(); | ||
| $queueName = $client->queueName($projectId, $locationId, $queueId); | ||
|
|
||
| // Add your service account email to construct the OIDC token | ||
| // in order to add an authentication header to the request. | ||
| $oidcToken = new OidcToken(); | ||
| $oidcToken->setServiceAccountEmail($serviceAccountEmail); | ||
|
|
||
| // Create an Http Request Object. | ||
| $httpRequest = new HttpRequest(); | ||
| // The full url path that the task request will be sent to. | ||
| $httpRequest->setUrl($url); | ||
| // POST is the default HTTP method, but any HTTP method can be used. | ||
| $httpRequest->setHttpMethod(HttpMethod::POST); | ||
| //The oidcToken used to assert identity. | ||
| $httpRequest->setOidcToken($oidcToken); | ||
| // Setting a body value is only compatible with HTTP POST and PUT requests. | ||
| if (isset($payload)) { | ||
| $httpRequest->setBody($payload); | ||
| } | ||
|
|
||
| // Create a Cloud Task object. | ||
| $task = new Task(); | ||
| $task->setHttpRequest($httpRequest); | ||
|
|
||
| // Send request and print the task name. | ||
| $response = $client->createTask($queueName, $task); | ||
| printf('Created task %s' . PHP_EOL, $response->getName()); | ||
|
|
||
| # [END cloud_tasks_create_http_task_with_token] | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2019 Google LLC. | ||
| * Copyright 2020 Google LLC. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
|
|
@@ -27,27 +27,54 @@ class TasksTest extends TestCase | |
| { | ||
| use TestTrait; | ||
|
|
||
| public function testCreateHttpTask() | ||
| private static $queue; | ||
| private static $location; | ||
|
|
||
| public static function setUpBeforeClass() | ||
| { | ||
| $queue = $this->requireEnv('CLOUD_TASKS_APPENGINE_QUEUE'); | ||
| $location = $this->requireEnv('CLOUD_TASKS_LOCATION'); | ||
| self::$queue = self::requireEnv('CLOUD_TASKS_APPENGINE_QUEUE'); | ||
| self::$location = self::requireEnv('CLOUD_TASKS_LOCATION'); | ||
| } | ||
|
|
||
| public function testCreateHttpTask() | ||
| { | ||
| $output = $this->runSnippet('create_http_task', [ | ||
| $location, | ||
| $queue, | ||
| 'http://example.com/taskhandler', | ||
| self::$location, | ||
| self::$queue, | ||
| 'https://example.com/taskhandler', | ||
| 'Task Details', | ||
| ]); | ||
|
|
||
| $taskNamePrefix = $this->getTaskNamePrefix(); | ||
| $expectedOutput = sprintf('Created task %s', $taskNamePrefix); | ||
| $this->assertContains($expectedOutput, $output); | ||
| } | ||
|
|
||
| public function testCreateHttpTaskWithToken() | ||
| { | ||
| $output = $this->runSnippet('create_http_task_with_token', [ | ||
| self::$location, | ||
| self::$queue, | ||
| 'https://example.com/taskhandler', | ||
| '[email protected]', | ||
| 'Task Details', | ||
| ]); | ||
| $taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/', | ||
| self::$projectId, | ||
| $location, | ||
| $queue | ||
| ); | ||
|
|
||
| $taskNamePrefix = $this->getTaskNamePrefix(); | ||
| $expectedOutput = sprintf('Created task %s', $taskNamePrefix); | ||
| $this->assertContains($expectedOutput, $output); | ||
| } | ||
|
|
||
| private function getTaskNamePrefix() | ||
| { | ||
| $taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/', | ||
| self::$projectId, | ||
| self::$location, | ||
| self::$queue | ||
| ); | ||
| return $taskNamePrefix; | ||
| } | ||
|
|
||
| private function runSnippet($sampleName, $params = []) | ||
| { | ||
| $argv = array_merge([0, self::$projectId], array_values($params)); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.