From a3e0e4ae1dae5c585792ef17fde6472244f6277a Mon Sep 17 00:00:00 2001 From: Grant Timmerman Date: Mon, 28 Sep 2020 14:52:20 -0500 Subject: [PATCH] Eventarc Quickstart Brushup (#1986) * feat: add event type conversion to run pubsub event sample Signed-off-by: Grant Timmerman * feat: re-add invalid pub/sub error Signed-off-by: Grant Timmerman * fix: invalid pub/sub case Signed-off-by: Grant Timmerman * style: polish eventarc samples Signed-off-by: Grant Timmerman --- eventarc/audit-storage/Dockerfile | 2 +- eventarc/audit-storage/index.js | 2 +- eventarc/pubsub/Dockerfile | 16 +++++++++++++--- eventarc/pubsub/app.js | 26 ++++++++++++++++++-------- eventarc/pubsub/index.js | 16 +++++++++++++--- eventarc/pubsub/package.json | 1 + 6 files changed, 47 insertions(+), 16 deletions(-) diff --git a/eventarc/audit-storage/Dockerfile b/eventarc/audit-storage/Dockerfile index 98a03b0eca..4db5f92001 100644 --- a/eventarc/audit-storage/Dockerfile +++ b/eventarc/audit-storage/Dockerfile @@ -4,7 +4,7 @@ # 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 +# 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, diff --git a/eventarc/audit-storage/index.js b/eventarc/audit-storage/index.js index 7e207fe0d8..05de6b695e 100644 --- a/eventarc/audit-storage/index.js +++ b/eventarc/audit-storage/index.js @@ -17,6 +17,6 @@ const app = require('./app.js'); const PORT = process.env.PORT || 8080; app.listen(PORT, () => -console.log(`nodejs-events-storage listening on port ${PORT}`) + console.log(`nodejs-events-storage listening on port ${PORT}`) ); // [END eventarc_gcs_server] diff --git a/eventarc/pubsub/Dockerfile b/eventarc/pubsub/Dockerfile index aa72234944..99318ab2d6 100644 --- a/eventarc/pubsub/Dockerfile +++ b/eventarc/pubsub/Dockerfile @@ -1,6 +1,16 @@ -# Copyright 2020 Google LLC. All rights reserved. -# Use of this source code is governed by the Apache 2.0 -# license that can be found in the LICENSE file. +# Copyright 2020 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. # [START eventarc_pubsub_dockerfile] diff --git a/eventarc/pubsub/app.js b/eventarc/pubsub/app.js index 70511b4491..665c63ebf9 100644 --- a/eventarc/pubsub/app.js +++ b/eventarc/pubsub/app.js @@ -1,10 +1,20 @@ -// Copyright 2020 Google LLC. All rights reserved. -// Use of this source code is governed by the Apache 2.0 -// license that can be found in the LICENSE file. +// Copyright 2020 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. // [START eventarc_pubsub_handler] -// [START run_events_pubsub_handler] const express = require('express'); +const {toMessagePublishedEvent} = require('@google/events/cloud/pubsub/v1/MessagePublishedData'); const app = express(); app.use(express.json()); @@ -21,9 +31,10 @@ app.post('/', (req, res) => { console.log(`Bad Request: ${errorMessage}`); return; } - const pubSubMessage = req.body.message; - const name = pubSubMessage.data - ? Buffer.from(pubSubMessage.data, 'base64').toString().trim() + // Cast to MessagePublishedEvent for IDE autocompletion + const pubSubMessage = toMessagePublishedEvent(req.body); + const name = pubSubMessage.message && pubSubMessage.message.data + ? Buffer.from(pubSubMessage.message.data, 'base64').toString().trim() : 'World'; const result = `Hello, ${name}! ID: ${req.get('ce-id') || ''}`; @@ -32,5 +43,4 @@ app.post('/', (req, res) => { }); module.exports = app; -// [END run_events_pubsub_handler] // [END eventarc_pubsub_handler] \ No newline at end of file diff --git a/eventarc/pubsub/index.js b/eventarc/pubsub/index.js index 4095b00cc5..fa30f762f6 100644 --- a/eventarc/pubsub/index.js +++ b/eventarc/pubsub/index.js @@ -1,6 +1,16 @@ -// Copyright 2020 Google LLC. All rights reserved. -// Use of this source code is governed by the Apache 2.0 -// license that can be found in the LICENSE file. +// Copyright 2020 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. // [START eventarc_pubsub_server] const app = require('./app.js'); diff --git a/eventarc/pubsub/package.json b/eventarc/pubsub/package.json index cdc9d88af8..710c98e983 100644 --- a/eventarc/pubsub/package.json +++ b/eventarc/pubsub/package.json @@ -19,6 +19,7 @@ "system-test": "test/runner.sh mocha test/system.test.js --timeout=10000" }, "dependencies": { + "@google/events": "^1.4.2", "express": "^4.16.4" }, "devDependencies": {