diff --git a/tasks/src/create_http_task_with_token.php b/tasks/src/create_http_task_with_token.php new file mode 100644 index 0000000000..a021e77646 --- /dev/null +++ b/tasks/src/create_http_task_with_token.php @@ -0,0 +1,72 @@ + 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] diff --git a/tasks/test/tasksTest.php b/tasks/test/tasksTest.php index 8c9d20c69a..2e8136a9b8 100644 --- a/tasks/test/tasksTest.php +++ b/tasks/test/tasksTest.php @@ -1,6 +1,6 @@ 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', + 'example@your-project-id.iam.gserviceaccount.com', '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));