From 1f7600b4697e4fd54b77ee305582d24c41a7040d Mon Sep 17 00:00:00 2001 From: feywind <57276408+feywind@users.noreply.github.com> Date: Wed, 10 Jan 2024 15:33:42 -0500 Subject: [PATCH] samples: add listSchemaRevisions sample --- samples/listSchemaRevisions.js | 68 +++++++++++++++++++++++ samples/system-test/schema.test.ts | 17 ++++++ samples/typescript/listSchemaRevisions.ts | 64 +++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 samples/listSchemaRevisions.js create mode 100644 samples/typescript/listSchemaRevisions.ts diff --git a/samples/listSchemaRevisions.js b/samples/listSchemaRevisions.js new file mode 100644 index 000000000..b6a621b87 --- /dev/null +++ b/samples/listSchemaRevisions.js @@ -0,0 +1,68 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This is a generated sample, using the typeless sample bot. Please +// look for the source TypeScript sample (.ts) for modifications. +'use strict'; + +/** + * This application demonstrates how to perform basic operations on + * schemas with the Google Cloud Pub/Sub API. + * + * For more information, see the README.md under /pubsub and the documentation + * at https://cloud.google.com/pubsub/docs. + */ + +// sample-metadata: +// title: List Revisions on a Schema +// description: Gets a list of revisions on a schema which was previously created in the project. +// usage: node listSchemaRevisions.js + +// [START pubsub_list_schema_revisions] +/** + * TODO(developer): Uncomment this variable before running the sample. + */ +// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID'; + +// Imports the Google Cloud client library +const {PubSub} = require('@google-cloud/pubsub'); + +// Creates a client; cache this for further use +const pubSubClient = new PubSub(); + +async function listSchemaRevisions(schemaNameOrId) { + // Get the fully qualified schema name. + const schema = pubSubClient.schema(schemaNameOrId); + const name = await schema.getName(); + + // Use the gapic client to list the schema revisions. + const schemaClient = await pubSubClient.getSchemaClient(); + const [results] = await schemaClient.listSchemaRevisions({ + name, + }); + for (const rev of results) { + console.log(rev.revisionId, rev.revisionCreateTime); + } + console.log(`Listed revisions of schema ${name}.`); +} +// [END pubsub_list_schema_revisions] + +function main(schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID') { + listSchemaRevisions(schemaNameOrId).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); +} + +main(...process.argv.slice(2)); diff --git a/samples/system-test/schema.test.ts b/samples/system-test/schema.test.ts index 47616f1b2..1e190c34a 100644 --- a/samples/system-test/schema.test.ts +++ b/samples/system-test/schema.test.ts @@ -315,6 +315,23 @@ describe('schema', () => { assert.include(output, 'rolled back.'); }); + it("should list a schema's revisions", async () => { + const id = 'list_rev'; + const schema = await createSchema(id, 'proto'); + const committed = await commitSchema(await schema.getName(), 'proto'); + const revId = committed.revisionId!; + const committed2 = await commitSchema(await schema.getName(), 'proto'); + const revId2 = committed2.revisionId!; + + const output = execSync( + `${commandFor('listSchemaRevisions')} ${schema.id}` + ); + assert.include(output, schema.id); + assert.include(output, revId); + assert.include(output, revId2); + assert.include(output, 'Listed revisions'); + }); + it('should get a schema', async () => { const schema = await createSchema('get', 'proto'); diff --git a/samples/typescript/listSchemaRevisions.ts b/samples/typescript/listSchemaRevisions.ts new file mode 100644 index 000000000..e7b49bdce --- /dev/null +++ b/samples/typescript/listSchemaRevisions.ts @@ -0,0 +1,64 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * This application demonstrates how to perform basic operations on + * schemas with the Google Cloud Pub/Sub API. + * + * For more information, see the README.md under /pubsub and the documentation + * at https://cloud.google.com/pubsub/docs. + */ + +// sample-metadata: +// title: List Revisions on a Schema +// description: Gets a list of revisions on a schema which was previously created in the project. +// usage: node listSchemaRevisions.js + +// [START pubsub_list_schema_revisions] +/** + * TODO(developer): Uncomment this variable before running the sample. + */ +// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID'; + +// Imports the Google Cloud client library +import {PubSub} from '@google-cloud/pubsub'; + +// Creates a client; cache this for further use +const pubSubClient = new PubSub(); + +async function listSchemaRevisions(schemaNameOrId: string) { + // Get the fully qualified schema name. + const schema = pubSubClient.schema(schemaNameOrId); + const name = await schema.getName(); + + // Use the gapic client to list the schema revisions. + const schemaClient = await pubSubClient.getSchemaClient(); + const [results] = await schemaClient.listSchemaRevisions({ + name, + }); + for (const rev of results) { + console.log(rev.revisionId, rev.revisionCreateTime); + } + console.log(`Listed revisions of schema ${name}.`); +} +// [END pubsub_list_schema_revisions] + +function main(schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID') { + listSchemaRevisions(schemaNameOrId).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); +} + +main(...process.argv.slice(2));