diff --git a/Spanner/src/SpannerClient.php b/Spanner/src/SpannerClient.php index 4f6b6694213..74845af7723 100644 --- a/Spanner/src/SpannerClient.php +++ b/Spanner/src/SpannerClient.php @@ -28,6 +28,7 @@ use Google\Cloud\Core\Exception\GoogleException; use Google\Cloud\Core\Int64; use Google\Cloud\Core\Iterator\ItemIterator; +use Google\Cloud\Core\LongRunning\LongRunningGapicConnection; use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient; use Google\Cloud\Spanner\Admin\Instance\V1\Client\InstanceAdminClient; use Google\Cloud\Spanner\Admin\Instance\V1\ListInstanceConfigOperationsRequest; @@ -514,6 +515,70 @@ function (Operation $operation) { ); } + /** + * Resume a Long Running Operation + * + * Example: + * ``` + * $operation = $spanner->resumeOperation($operationName); + * ``` + * + * @param string $operationName The Long Running Operation name. + * @return LongRunningOperation + */ + public function resumeOperation($operationName, array $info = []) + { + return new LongRunningOperation( + new LongRunningGapicConnection($this->databaseAdminClient, $this->serializer), + $operationName, + [ + [ + 'typeUrl' => 'type.googleapis.com/google.spanner.admin.instance.v1.UpdateInstanceMetadata', + 'callable' => function ($instance) { + $name = InstanceAdminClient::parseName($instance['name'])['instance']; + return $this->instance($name, $instance); + } + ], [ + 'typeUrl' => 'type.googleapis.com/google.spanner.admin.database.v1.CreateDatabaseMetadata', + 'callable' => function ($database) { + $databaseNameComponents = DatabaseAdminClient::parseName($database['name']); + $instanceName = $databaseNameComponents['instance']; + $databaseName = $databaseNameComponents['database']; + + $instance = $this->instance($instanceName); + return $instance->database($databaseName); + } + ], [ + 'typeUrl' => 'type.googleapis.com/google.spanner.admin.database.v1.RestoreDatabaseMetadata', + 'callable' => function ($database) { + $databaseNameComponents = DatabaseAdminClient::parseName($database['name']); + $instanceName = $databaseNameComponents['instance']; + $databaseName = $databaseNameComponents['database']; + + $instance = $this->instance($instanceName); + return $instance->database($databaseName); + } + ],[ + 'typeUrl' => 'type.googleapis.com/google.spanner.admin.instance.v1.CreateInstanceMetadata', + 'callable' => function ($instance) { + $name = InstanceAdminClient::parseName($instance['name'])['instance']; + return $this->instance($name, $instance); + } + ], [ + 'typeUrl' => 'type.googleapis.com/google.spanner.admin.database.v1.CreateBackupMetadata', + 'callable' => function ($backup) { + $backupNameComponents = DatabaseAdminClient::parseName($backup['name']); + $instanceName = $backupNameComponents['instance']; + + $instance = $this->instance($instanceName); + return $instance->backup($backup['name'], $backup); + } + ] + ], + $info + ); + } + /** * Create a new instance. * diff --git a/Spanner/tests/Snippet/SpannerClientTest.php b/Spanner/tests/Snippet/SpannerClientTest.php index fa3cf903a00..c3cae42be78 100644 --- a/Spanner/tests/Snippet/SpannerClientTest.php +++ b/Spanner/tests/Snippet/SpannerClientTest.php @@ -296,6 +296,19 @@ public function factoriesProvider() ]; } + public function testResumeOperation() + { + $opName = 'operations/foo'; + $snippet = $this->snippetFromMagicMethod(SpannerClient::class, 'resumeOperation'); + $snippet->addLocal('spanner', $this->client); + $snippet->addLocal('operationName', $opName); + + $res = $snippet->invoke('operation'); + $op = $res->returnVal(); + $this->assertInstanceOf(LongRunningOperation::class, $op); + $this->assertEquals($op->name(), $opName); + } + public function testEmulator() { $snippet = $this->snippetFromClass(SpannerClient::class, 1); diff --git a/Spanner/tests/Unit/SpannerClientTest.php b/Spanner/tests/Unit/SpannerClientTest.php index 62c41e68fab..87a776743be 100644 --- a/Spanner/tests/Unit/SpannerClientTest.php +++ b/Spanner/tests/Unit/SpannerClientTest.php @@ -418,6 +418,18 @@ public function testInstances() $this->assertEquals('bar', InstanceAdminClient::parseName($instances[1]->name())['instance']); } + /** + * @group spanner-admin + */ + public function testResumeOperation() + { + $opName = 'operations/foo'; + + $op = $this->spannerClient->resumeOperation($opName); + $this->assertInstanceOf(LongRunningOperation::class, $op); + $this->assertEquals($op->name(), $opName); + } + public function testConnect() { $database = $this->spannerClient->connect(self::INSTANCE, self::DATABASE);