Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions BigQuery/src/Dataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,22 @@ function (array $routine) {
* ]);
* ```
*
* ```
* // Create a routine with remote function options.
* $routine = $dataset->createRoutine('my_remote_udf', [
* 'routineType' => 'SCALAR_FUNCTION',
* 'language' => 'SQL',
* 'remoteFunctionOptions' => [
* 'endpoint' => 'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add',
* 'connection' => 'projects/project-id/locations/us/connections/connection-id',
* 'maxBatchingRows' => '10',
* 'userDefinedContext' => [
* 'key' => 'value'
* ]
* ]
* ]);
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/routines/insert Insert Routines API documentation.
*
* @param string $id The routine ID.
Expand Down
14 changes: 14 additions & 0 deletions BigQuery/src/Routine.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ public function exists(array $options = [])
* ]);
* ```
*
* ```
* // Update the routine with remote function options.
* $routine->update([
* 'remoteFunctionOptions' => [
* 'endpoint' => 'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add',
* 'connection' => 'projects/project-id/locations/us/connections/connection-id',
* 'maxBatchingRows' => '10',
* 'userDefinedContext' => [
* 'key' => 'value'
* ]
* ]
* ]);
* ```
*
* @see https://cloud.google.com/bigquery/docs/reference/rest/v2/routines/update Update Routines API documentation.
* @param array $metadata The full routine resource with desired
* modifications.
Expand Down
86 changes: 86 additions & 0 deletions BigQuery/tests/System/ManageRoutinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
namespace Google\Cloud\BigQuery\Tests\System;

use Google\Cloud\BigQuery\Routine;
use Google\Cloud\Core\RequestWrapper;
use GuzzleHttp\Psr7\Request;

/**
* @group bigquery
Expand Down Expand Up @@ -133,4 +135,88 @@ public function testCreateAndDeleteRoutine()

$this->assertFalse($routine->exists());
}

public function testCreateRemoteUdf()
{
$routineId = uniqid(self::TESTING_PREFIX);
$connectionId = uniqid('udf_conn');

$connectionName = $this->createConnection($connectionId);

try {
$routine = self::$dataset->createRoutine($routineId, [
'routineType' => 'SCALAR_FUNCTION',
'language' => 'SQL',
'returnType' => [
'typeKind' => 'STRING'
],
'remoteFunctionOptions' => [
'endpoint' => 'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add',
'connection' => $connectionName,
'maxBatchingRows' => '10',
'userDefinedContext' => [
'key' => 'value'
]
]
]);

$this->assertTrue($routine->exists());

$info = $routine->info();
$this->assertEquals('SCALAR_FUNCTION', $info['routineType']);
$this->assertArrayHasKey('remoteFunctionOptions', $info);
$this->assertEquals(
'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add',
$info['remoteFunctionOptions']['endpoint']
);

$routine->delete();
} finally {
$this->deleteConnection($connectionName);
}
}

private function createConnection($connectionId)
{
// There is a BigQueryConnection client available in the PHP cloud
// but decided to create it here manually instead of adding it as a dependency just for testing.
$projectId = self::$dataset->identity()['projectId'];
$location = 'us';
$parent = "projects/$projectId/locations/$location";
$uri = "https://bigqueryconnection.googleapis.com/v1/$parent/connections?connectionId=$connectionId";

$body = json_encode([
'friendlyName' => $connectionId,
'cloudResource' => new \stdClass()
]);

$request = new Request(
'POST',
$uri,
['Content-Type' => 'application/json'],
$body
);

$requestWrapper = new RequestWrapper([
'keyFilePath' => getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH'),
'scopes' => ['https://www.googleapis.com/auth/cloud-platform']
]);

$response = $requestWrapper->send($request);
$data = json_decode($response->getBody(), true);
return $data['name'];
}

private function deleteConnection($name)
{
$uri = "https://bigqueryconnection.googleapis.com/v1/$name";
$request = new Request('DELETE', $uri);

$requestWrapper = new RequestWrapper([
'keyFilePath' => getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH'),
'scopes' => ['https://www.googleapis.com/auth/cloud-platform']
]);

$requestWrapper->send($request);
}
}
35 changes: 35 additions & 0 deletions BigQuery/tests/Unit/RoutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,41 @@ public function testUpdateWithMask()
$this->assertEquals($expected, $res);
}

public function testUpdateRemoteFunctionOptions()
{
$old = [
'routineType' => 'SCALAR_FUNCTION',
'definitionBody' => '',
'language' => 'SQL'
];

$new = [
'remoteFunctionOptions' => [
'endpoint' => 'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add',
'connection' => 'projects/project-id/locations/us/connections/connection-id',
'maxBatchingRows' => '10',
'userDefinedContext' => [
'key' => 'value'
]
]
];

$expected = $old + $new;

$this->connection->getRoutine($this->identity)
->willReturn($old)
->shouldBeCalledTimes(1);

$this->connection->updateRoutine($this->identity + $expected + ['retries' => 0])
->shouldBeCalledTimes(1)
->willReturn($expected);

$this->routine->___setProperty('connection', $this->connection->reveal());
$res = $this->routine->update($new);

$this->assertEquals($expected, $res);
}

public function testDelete()
{
$this->connection->deleteRoutine($this->identity + ['retries' => 0])
Expand Down
Loading