From f921d44251948bbdeba68fb0c13c0b300b914a6b Mon Sep 17 00:00:00 2001 From: Reda BIYA Date: Tue, 19 Sep 2023 20:13:35 +0200 Subject: [PATCH] feat: [PubSub] add Google Pub/Sub Schema Revision support --- src/Connection/ConnectionInterface.php | 15 ++++++ src/Connection/Grpc.php | 44 +++++++++++++++ src/Connection/Rest.php | 24 +++++++++ src/Schema.php | 75 ++++++++++++++++++++++++++ tests/Unit/Connection/RestTest.php | 3 ++ tests/Unit/SchemaTest.php | 44 +++++++++++++++ 6 files changed, 205 insertions(+) diff --git a/src/Connection/ConnectionInterface.php b/src/Connection/ConnectionInterface.php index fde96ee..af47ced 100644 --- a/src/Connection/ConnectionInterface.php +++ b/src/Connection/ConnectionInterface.php @@ -185,6 +185,21 @@ public function deleteSchema(array $args); */ public function validateSchema(array $args); + /** + * @param array $args + */ + public function listRevisions(array $args); + + /** + * @param array $args + */ + public function commitSchema(array $args); + + /** + * @param array $args + */ + public function deleteRevision(array $args); + /** * @param array $args */ diff --git a/src/Connection/Grpc.php b/src/Connection/Grpc.php index 4f3d432..24e2727 100644 --- a/src/Connection/Grpc.php +++ b/src/Connection/Grpc.php @@ -761,4 +761,48 @@ protected function getSchemaClient() $this->schemaClient = $this->constructGapic(SchemaServiceClient::class, $this->clientConfig); return $this->schemaClient; } + + /** + * Retrieve schema revisions + * + * @param array $args + * @return array + */ + public function listRevisions(array $args) + { + return $this->send([$this->getSchemaClient(), 'listSchemaRevisions'], [ + $this->pluck('name', $args), + $args, + ]); + } + + /** + * Create schema revisions + * + * @param array $args + * @return array + */ + public function commitSchema(array $args) + { + return $this->send([$this->getSchemaClient(), 'commitSchema'], [ + $this->pluck('name', $args), + new Schema($this->pluck('schema', $args)), + $args, + ]); + } + + /** + * Delete schema revision + * + * @param array $args + * @return array + */ + public function deleteRevision(array $args) + { + return $this->send([$this->getSchemaClient(), 'deleteSchemaRevision'], [ + $this->pluck('name', $args), + null, + $args, + ]); + } } diff --git a/src/Connection/Rest.php b/src/Connection/Rest.php index f4e7c47..859d73c 100644 --- a/src/Connection/Rest.php +++ b/src/Connection/Rest.php @@ -341,6 +341,30 @@ public function validateSchema(array $args) return $this->send('schemas', 'validate', $args); } + /** + * @param array $args + */ + public function listRevisions(array $args) + { + return $this->send('schemas', 'listRevisions', $args); + } + + /** + * @param array $args + */ + public function commitSchema(array $args) + { + return $this->send('schemas', 'commit', $args); + } + + /** + * @param array $args + */ + public function deleteRevision(array $args) + { + return $this->send('schemas', 'deleteRevision', $args); + } + /** * @param array $args */ diff --git a/src/Schema.php b/src/Schema.php index 1351a21..a34931b 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -19,6 +19,7 @@ use Google\Cloud\Core\Exception\NotFoundException; use Google\Cloud\PubSub\Connection\ConnectionInterface; +use Google\Cloud\PubSub\V1\Schema\Type; /** * Represents a Pub/Sub Schema resource. @@ -104,6 +105,80 @@ public function delete(array $options = []) ] + $options); } + /** + * Get list schema revisions. + * + * Example: + * ``` + * $revisions = $schema->listRevisions(); + * foreach ($revisions['schemas'] as $revision) { + * echo $revisions['definition']; + * } + * ``` + * @see https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas/listRevisions List revisions + * @param array $options [optional] Configuration Options + * @return array + */ + public function listRevisions(array $options = []) + { + return $this->connection->listRevisions([ + 'name' => $this->name, + ] + $options); + } + + /** + * Commit schema revision. + * + * Example: + * ``` + * $definition = file_get_contents('my-schema.txt'); + * $revision = $schema->commit($definition, 'AVRO); + * + * @see https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas/commit Commit Schema revision. + * ``` + * + * @param string $definition The definition of the schema. This should + * contain a string representing the full definition of the schema that + * is a valid schema definition of the type specified in `type`. See + * [Schema](https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas#Schema) + * for details. + * @param string $type The schema type. Allowed values are `AVRO` and `PROTOCOL_BUFFER`. + * @param array $options [optional] Configuration options + * @return array revision created + */ + public function commit($definition, $type, array $options = []) + { + return $this->connection->commitSchema([ + 'schema' => [ + 'definition' => $definition, + 'type' => $type, + ], + 'name' => $this->name + ] + $options); + } + + + /** + * Commit schema revision. + * + * Example: + * ``` + * $definition = file_get_contents('my-schema.txt'); + * $revision = $schema->commit($definition, 'AVRO); + * + * @see https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas/deleteRevision Delete Schema revision. + * ``` + * + * @param string $revisionId The revisionId + * @return array deleted revision + */ + public function deleteRevision($revisionId) + { + return $this->connection->deleteRevision([ + 'name' => $this->name .'@' . $revisionId + ]); + } + /** * Get schema information. * diff --git a/tests/Unit/Connection/RestTest.php b/tests/Unit/Connection/RestTest.php index 03dc52b..4e58ad1 100644 --- a/tests/Unit/Connection/RestTest.php +++ b/tests/Unit/Connection/RestTest.php @@ -128,6 +128,9 @@ public function methodProvider() ['deleteSchema'], ['validateSchema'], ['validateMessage'], + ['listRevisions'], + ['commitSchema'], + ['deleteRevision'], ]; } } diff --git a/tests/Unit/SchemaTest.php b/tests/Unit/SchemaTest.php index 462936f..c235ae7 100644 --- a/tests/Unit/SchemaTest.php +++ b/tests/Unit/SchemaTest.php @@ -116,4 +116,48 @@ public function testExistsReturnsFalse() $this->assertFalse($this->schema->exists()); } + + public function testlistRevisions() + { + $this->connection + ->listRevisions(['name' => self::NAME,]) + ->shouldBeCalledOnce() + ->willReturn(['foo' => 'bar']); + + $this->schema->___setProperty('connection', $this->connection->reveal()); + + $this->assertEquals('bar', $this->schema->listRevisions()['foo']); + } + + public function testCommit() + { + $this->connection + ->commitSchema( + [ + 'name' => self::NAME, + 'schema' => [ + 'definition' => 'test', + 'type' => 'AVRO', + ], + ] + ) + ->shouldBeCalledOnce() + ->willReturn(['foo' => 'bar']); + + $this->schema->___setProperty('connection', $this->connection->reveal()); + + $this->assertEquals('bar', $this->schema->commit('test', 'AVRO')['foo']); + } + + public function testDeleteRevision() + { + $this->connection + ->deleteRevision(['name' => self::NAME . '@1234567']) + ->shouldBeCalledOnce() + ->willReturn(['foo' => 'bar']); + + $this->schema->___setProperty('connection', $this->connection->reveal()); + + $this->assertEquals('bar', $this->schema->deleteRevision('1234567')['foo']); + } }