|
17 | 17 |
|
18 | 18 | namespace Google\Cloud\BigQuery\Tests\System; |
19 | 19 |
|
| 20 | +use Google\Cloud\BigQuery\Connection\V1\Client\ConnectionServiceClient; |
| 21 | +use Google\Cloud\BigQuery\Connection\V1\CloudResourceProperties; |
| 22 | +use Google\Cloud\BigQuery\Connection\V1\Connection; |
| 23 | +use Google\Cloud\BigQuery\Connection\V1\CreateConnectionRequest; |
| 24 | +use Google\Cloud\BigQuery\Connection\V1\DeleteConnectionRequest; |
20 | 25 | use Google\Cloud\BigQuery\Routine; |
21 | 26 |
|
22 | 27 | /** |
@@ -133,4 +138,75 @@ public function testCreateAndDeleteRoutine() |
133 | 138 |
|
134 | 139 | $this->assertFalse($routine->exists()); |
135 | 140 | } |
| 141 | + |
| 142 | + public function testCreateRemoteUdf() |
| 143 | + { |
| 144 | + $routineId = uniqid(self::TESTING_PREFIX); |
| 145 | + $connectionId = uniqid('udf_conn'); |
| 146 | + |
| 147 | + $connectionName = $this->createConnection($connectionId); |
| 148 | + |
| 149 | + try { |
| 150 | + $routine = self::$dataset->createRoutine($routineId, [ |
| 151 | + 'routineType' => 'SCALAR_FUNCTION', |
| 152 | + 'language' => 'SQL', |
| 153 | + 'returnType' => [ |
| 154 | + 'typeKind' => 'STRING' |
| 155 | + ], |
| 156 | + 'remoteFunctionOptions' => [ |
| 157 | + 'endpoint' => 'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add', |
| 158 | + 'connection' => $connectionName, |
| 159 | + 'maxBatchingRows' => '10', |
| 160 | + 'userDefinedContext' => [ |
| 161 | + 'key' => 'value' |
| 162 | + ] |
| 163 | + ] |
| 164 | + ]); |
| 165 | + |
| 166 | + $this->assertTrue($routine->exists()); |
| 167 | + |
| 168 | + $info = $routine->info(); |
| 169 | + $this->assertEquals('SCALAR_FUNCTION', $info['routineType']); |
| 170 | + $this->assertArrayHasKey('remoteFunctionOptions', $info); |
| 171 | + $this->assertEquals( |
| 172 | + 'https://us-east1-my_gcf_project.cloudfunctions.net/remote_add', |
| 173 | + $info['remoteFunctionOptions']['endpoint'] |
| 174 | + ); |
| 175 | + |
| 176 | + $routine->delete(); |
| 177 | + } finally { |
| 178 | + $this->deleteConnection($connectionName); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private function createConnection($connectionId) |
| 183 | + { |
| 184 | + $projectId = self::$dataset->identity()['projectId']; |
| 185 | + $location = 'us'; |
| 186 | + $parent = "projects/$projectId/locations/$location"; |
| 187 | + |
| 188 | + $connectionClient = new ConnectionServiceClient(); |
| 189 | + |
| 190 | + $connection = new Connection(); |
| 191 | + $connection->setFriendlyName($connectionId); |
| 192 | + $connection->setCloudResource(new CloudResourceProperties()); |
| 193 | + |
| 194 | + $request = new CreateConnectionRequest(); |
| 195 | + $request->setParent($parent); |
| 196 | + $request->setConnectionId($connectionId); |
| 197 | + $request->setConnection($connection); |
| 198 | + |
| 199 | + $response = $connectionClient->createConnection($request); |
| 200 | + return $response->getName(); |
| 201 | + } |
| 202 | + |
| 203 | + private function deleteConnection($name) |
| 204 | + { |
| 205 | + $connectionClient = new ConnectionServiceClient(); |
| 206 | + |
| 207 | + $request = new DeleteConnectionRequest(); |
| 208 | + $request->setName($name); |
| 209 | + |
| 210 | + $connectionClient->deleteConnection($request); |
| 211 | + } |
136 | 212 | } |
0 commit comments