|
4 | 4 |
|
5 | 5 | namespace Tests; |
6 | 6 |
|
| 7 | +use Closure; |
| 8 | +use GuzzleHttp\Client; |
7 | 9 | use LaunchDarkly\Integrations\Guzzle; |
8 | 10 | use LaunchDarkly\LDClient; |
9 | 11 | use LaunchDarkly\LDContext; |
| 12 | +use LaunchDarkly\Migrations\ExecutionOrder; |
| 13 | +use LaunchDarkly\Migrations\MigratorBuilder; |
| 14 | +use LaunchDarkly\Migrations\Operation; |
| 15 | +use LaunchDarkly\Migrations\Stage; |
| 16 | +use LaunchDarkly\Types\Result; |
10 | 17 | use Monolog\Formatter\LineFormatter; |
11 | 18 | use Monolog\Handler\StreamHandler; |
12 | 19 | use Monolog\Logger; |
@@ -87,10 +94,16 @@ public function doCommand(mixed $reqParams): mixed |
87 | 94 |
|
88 | 95 | case 'contextBuild': |
89 | 96 | return $this->doContextBuild($commandParams); |
90 | | - |
| 97 | + |
91 | 98 | case 'contextConvert': |
92 | 99 | return $this->doContextConvert($commandParams); |
93 | | - |
| 100 | + |
| 101 | + case 'migrationVariation': |
| 102 | + return $this->doMigrationVariation($commandParams); |
| 103 | + |
| 104 | + case 'migrationOperation': |
| 105 | + return $this->doMigrationOperation($commandParams); |
| 106 | + |
94 | 107 | default: |
95 | 108 | return false; // means invalid command |
96 | 109 | } |
@@ -208,6 +221,86 @@ private function doContextConvert(array $params): array |
208 | 221 | } |
209 | 222 | } |
210 | 223 |
|
| 224 | + private function doMigrationVariation(array $params): array |
| 225 | + { |
| 226 | + try { |
| 227 | + $results = $this->_client->migrationVariation( |
| 228 | + $params['key'], |
| 229 | + $this->makeContext($params['context']), |
| 230 | + Stage::from($params['defaultStage']) |
| 231 | + ); |
| 232 | + |
| 233 | + return ['result' => $results['stage']->value]; |
| 234 | + } catch (\Throwable $e) { |
| 235 | + return ['error' => "$e"]; |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + private function doMigrationOperation(array $params): array |
| 240 | + { |
| 241 | + $builder = new MigratorBuilder($this->_client); |
| 242 | + |
| 243 | + // PHP doesn't support concurrent, so we just do the best we can. |
| 244 | + if ($params['readExecutionOrder'] == 'concurrent') { |
| 245 | + $params['readExecutionOrder'] = 'serial'; |
| 246 | + } |
| 247 | + |
| 248 | + $builder->readExecutionOrder(ExecutionOrder::from($params['readExecutionOrder'])); |
| 249 | + $builder->trackLatency($params['trackLatency']); |
| 250 | + $builder->trackErrors($params['trackErrors']); |
| 251 | + |
| 252 | + $callback = function (string $endpoint): Closure { |
| 253 | + return function ($payload) use ($endpoint): Result { |
| 254 | + $client = new Client(); |
| 255 | + $response = $client->request('POST', $endpoint, ['body' => $payload]); |
| 256 | + |
| 257 | + $statusCode = $response->getStatusCode(); |
| 258 | + if ($statusCode == 200) { |
| 259 | + return Result::success($response->getBody()->getContents()); |
| 260 | + }; |
| 261 | + |
| 262 | + return Result::error("Request failed with status code {$statusCode}"); |
| 263 | + }; |
| 264 | + }; |
| 265 | + |
| 266 | + $consistency = null; |
| 267 | + if ($params['trackConsistency'] ?? false) { |
| 268 | + $consistency = fn ($lhs, $rhs) => $lhs == $rhs; |
| 269 | + } |
| 270 | + |
| 271 | + $builder->read(($callback)($params['oldEndpoint']), ($callback)($params['newEndpoint']), $consistency); |
| 272 | + $builder->write(($callback)($params['oldEndpoint']), ($callback)($params['newEndpoint'])); |
| 273 | + |
| 274 | + $results = $builder->build(); |
| 275 | + |
| 276 | + if (!$results->isSuccessful()) { |
| 277 | + return ['result' => $results->error]; |
| 278 | + } |
| 279 | + |
| 280 | + /** @var \LaunchDarkly\Migrations\Migrator */ |
| 281 | + $migrator = $results->value; |
| 282 | + |
| 283 | + if ($params['operation'] == Operation::READ->value) { |
| 284 | + $result = $migrator->read( |
| 285 | + $params['key'], |
| 286 | + $this->makeContext($params['context']), |
| 287 | + Stage::from($params['defaultStage']), |
| 288 | + $params['payload'], |
| 289 | + ); |
| 290 | + |
| 291 | + return ['result' => $result->isSuccessful() ? $result->value : $result->error]; |
| 292 | + } |
| 293 | + |
| 294 | + $result = $migrator->write( |
| 295 | + $params['key'], |
| 296 | + $this->makeContext($params['context']), |
| 297 | + Stage::from($params['defaultStage']), |
| 298 | + $params['payload'], |
| 299 | + ); |
| 300 | + |
| 301 | + return ['result' => $result->authoritative->isSuccessful() ? $result->authoritative->value : $result->authoritative->error]; |
| 302 | + } |
| 303 | + |
211 | 304 | private function makeContext(array $data): LDContext |
212 | 305 | { |
213 | 306 | return LDContext::fromJson($data); |
|
0 commit comments