diff --git a/eventarc/generic/.dockerignore b/eventarc/generic/.dockerignore new file mode 100644 index 000000000..9314ae21e --- /dev/null +++ b/eventarc/generic/.dockerignore @@ -0,0 +1,5 @@ +Dockerfile +README.md +.ruby-version +.bundle/ +vendor/ diff --git a/eventarc/generic/.gcloudignore b/eventarc/generic/.gcloudignore new file mode 100644 index 000000000..d2ffb8bb2 --- /dev/null +++ b/eventarc/generic/.gcloudignore @@ -0,0 +1,4 @@ +README.md +.ruby-version +.bundle/ +vendor/ diff --git a/eventarc/generic/Dockerfile b/eventarc/generic/Dockerfile new file mode 100644 index 000000000..8f45d0ef4 --- /dev/null +++ b/eventarc/generic/Dockerfile @@ -0,0 +1,33 @@ +# 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] + +# Use the official lightweight Ruby image. +# https://hub.docker.com/_/ruby +FROM ruby:2.7-slim + +# Install production dependencies. +WORKDIR /usr/src/app +COPY Gemfile Gemfile.lock ./ +ENV BUNDLE_FROZEN=true +RUN gem install bundler && bundle install --without test + +# Copy local code to the container image. +COPY . ./ + +# Run the web service on container startup. +CMD ["ruby", "./app.rb"] + +# [END eventarc_pubsub_dockerfile] diff --git a/eventarc/generic/Gemfile b/eventarc/generic/Gemfile new file mode 100644 index 000000000..cdcb8a3de --- /dev/null +++ b/eventarc/generic/Gemfile @@ -0,0 +1,11 @@ +source "https://rubygems.org" + +gem "sinatra", "~>2.0" + +group :test do + gem "rack-test" + gem "rest-client" + gem "rspec" + gem "rspec_junit_formatter" + gem "rubysl-securerandom" +end diff --git a/eventarc/generic/Gemfile.lock b/eventarc/generic/Gemfile.lock new file mode 100644 index 000000000..0899952b3 --- /dev/null +++ b/eventarc/generic/Gemfile.lock @@ -0,0 +1,65 @@ +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.4.4) + domain_name (0.5.20190701) + unf (>= 0.0.5, < 1.0.0) + http-accept (1.7.0) + http-cookie (1.0.3) + domain_name (~> 0.5) + mime-types (3.3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2020.0512) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + netrc (0.11.0) + rack (2.2.3) + rack-protection (2.0.8.1) + rack + rack-test (1.1.0) + rack (>= 1.0, < 3) + rest-client (2.1.0) + http-accept (>= 1.7.0, < 2.0) + http-cookie (>= 1.0.2, < 2.0) + mime-types (>= 1.16, < 4.0) + netrc (~> 0.8) + rspec (3.9.0) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-core (3.9.2) + rspec-support (~> 3.9.3) + rspec-expectations (3.9.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-mocks (3.9.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-support (3.9.3) + rspec_junit_formatter (0.4.1) + rspec-core (>= 2, < 4, != 2.12.0) + ruby2_keywords (0.0.2) + rubysl-securerandom (2.0.0) + sinatra (2.0.8.1) + mustermann (~> 1.0) + rack (~> 2.0) + rack-protection (= 2.0.8.1) + tilt (~> 2.0) + tilt (2.0.10) + unf (0.1.4) + unf_ext + unf_ext (0.0.7.7) + +PLATFORMS + ruby + +DEPENDENCIES + rack-test + rest-client + rspec + rspec_junit_formatter + rubysl-securerandom + sinatra (~> 2.0) + +BUNDLED WITH + 2.1.4 diff --git a/eventarc/generic/README.md b/eventarc/generic/README.md new file mode 100644 index 000000000..c863c81a6 --- /dev/null +++ b/eventarc/generic/README.md @@ -0,0 +1,37 @@ +# Eventarc - Generic + +This sample shows how to create a service that receives and prints generic events. + +[![Run in Google Cloud][run_img]][run_link] + +[run_img]: https://storage.googleapis.com/cloudrun/button.svg +[run_link]: https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/GoogleCloudPlatform/ruby-docs-samples&cloudshell_working_dir=run/events-pubsub + +## Quickstart + +Deploy your Cloud Run service: + +``` +gcloud builds submit \ + --tag gcr.io/$(gcloud config get-value project)/eventarc-generic +gcloud run deploy eventarc-generic \ + --image gcr.io/$(gcloud config get-value project)/eventarc-generic +``` + +## Test + +Test your Cloud Run service with cURL: + +```sh +URL=$(gcloud run services describe eventarc-generic --format='value(status.address.url)') +curl -XPOST $URL -H "CE-ID: 1234" --data '{"foo":"bar"}' +``` + +Observe the Run service replying with the headers and body of the sent HTTP request. + +## Unit Test + +``` +bundle install +bundle exec rspec +``` diff --git a/eventarc/generic/app.rb b/eventarc/generic/app.rb new file mode 100644 index 000000000..50926e4f9 --- /dev/null +++ b/eventarc/generic/app.rb @@ -0,0 +1,53 @@ +# 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] +require "sinatra" +require "json" +require "base64" + +set :bind, "0.0.0.0" +port = ENV["PORT"] || "8080" +set :port, port +# [END eventarc_pubsub_server] + +# [START eventarc_pubsub_handler] +post "/" do + request.body.rewind # in case someone already read it + + puts "Event received!" + + puts "\nHEADERS:" + headers = request.env.select { |k, _v| k.start_with? "HTTP_" } + .collect { |key, val| [key.sub(/^HTTP_/, ""), val] } + .collect { |key, val| "#{key}: #{val}" } + .sort + headers.each do |key, value| + if key != "Authorization" + puts "#{key}: #{value}
" + end + end + headers_json = headers + + puts "\nBODY:" + body_json = JSON.parse request.body.read + puts body_json + + result = { + "headers" => headers_json, + "body" => body_json + } + result.to_json +end +# [END eventarc_pubsub_handler] diff --git a/eventarc/generic/e2e_test_setup.yaml b/eventarc/generic/e2e_test_setup.yaml new file mode 100644 index 000000000..cc6dc3f1a --- /dev/null +++ b/eventarc/generic/e2e_test_setup.yaml @@ -0,0 +1,38 @@ +# 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. + +steps: +- # Build the renderer image + name: gcr.io/cloud-builders/docker:latest + args: ['build', '--tag=gcr.io/$PROJECT_ID/eventarc-${_SUFFIX}', '.'] + +- # Push the container image to Container Registry + name: gcr.io/cloud-builders/docker + args: ['push', 'gcr.io/$PROJECT_ID/eventarc-${_SUFFIX}'] + +- # Deploy to Cloud Run + name: gcr.io/google.com/cloudsdktool/cloud-sdk + args: + - gcloud + - run + - deploy + - eventarc-${_SUFFIX} + - --image=gcr.io/$PROJECT_ID/eventarc-${_SUFFIX} + - --region=us-central1 + - --platform=managed + - --no-allow-unauthenticated + - --set-env-vars=NAME=Test + +images: + - 'gcr.io/$PROJECT_ID/eventarc-${_SUFFIX}' diff --git a/eventarc/generic/spec/handler_spec.rb b/eventarc/generic/spec/handler_spec.rb new file mode 100644 index 000000000..98a304134 --- /dev/null +++ b/eventarc/generic/spec/handler_spec.rb @@ -0,0 +1,36 @@ +# 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. + +require_relative "../app.rb" +require "rspec" +require "rack/test" + +describe "eventarc_generic_handler" do + include Rack::Test::Methods + + def app + Sinatra::Application + end + + it "Service can accept POST requests and prints headers and body" do + message_hash = { data: "some data" } + message_json = JSON.generate message_hash + post "/", message_json, { + 'HTTP_CE_ID' => '1234' + } + expect(last_response.status).to eq 200 + expect(last_response.body).to include 'some data' + expect(last_response.body).to include 'CE_ID: 1234' + end +end diff --git a/eventarc/pubsub/README.md b/eventarc/pubsub/README.md index ddcf4942b..adcac949e 100644 --- a/eventarc/pubsub/README.md +++ b/eventarc/pubsub/README.md @@ -1,4 +1,4 @@ -# Events for Cloud Run – Pub/Sub tutorial +# Eventarc – Pub/Sub tutorial This sample shows how to create a service that processes Pub/Sub messages. @@ -19,7 +19,7 @@ Create a Cloud Pub/Sub topic: ``` gcloud alpha events triggers create pubsub-trigger-ruby \ ---target-service run-events-pubsub \ +--target-service eventarc-pubsub \ --type com.google.cloud.pubsub.topic.publish \ --parameters topic=my-topic ``` @@ -28,9 +28,9 @@ Deploy your Cloud Run service: ``` gcloud builds submit \ - --tag gcr.io/$(gcloud config get-value project)/run-events-pubsub -gcloud run deploy run-events-pubsub \ - --image gcr.io/$(gcloud config get-value project)/run-events-pubsub + --tag gcr.io/$(gcloud config get-value project)/eventarc-pubsub +gcloud run deploy eventarc-pubsub \ + --image gcr.io/$(gcloud config get-value project)/eventarc-pubsub ``` ## Test diff --git a/eventarc/pubsub/e2e_test_setup.yaml b/eventarc/pubsub/e2e_test_setup.yaml index 5cb5a54ad..cc6dc3f1a 100644 --- a/eventarc/pubsub/e2e_test_setup.yaml +++ b/eventarc/pubsub/e2e_test_setup.yaml @@ -15,11 +15,11 @@ steps: - # Build the renderer image name: gcr.io/cloud-builders/docker:latest - args: ['build', '--tag=gcr.io/$PROJECT_ID/run-events-${_SUFFIX}', '.'] + args: ['build', '--tag=gcr.io/$PROJECT_ID/eventarc-${_SUFFIX}', '.'] - # Push the container image to Container Registry name: gcr.io/cloud-builders/docker - args: ['push', 'gcr.io/$PROJECT_ID/run-events-${_SUFFIX}'] + args: ['push', 'gcr.io/$PROJECT_ID/eventarc-${_SUFFIX}'] - # Deploy to Cloud Run name: gcr.io/google.com/cloudsdktool/cloud-sdk @@ -27,12 +27,12 @@ steps: - gcloud - run - deploy - - run-events-${_SUFFIX} - - --image=gcr.io/$PROJECT_ID/run-events-${_SUFFIX} + - eventarc-${_SUFFIX} + - --image=gcr.io/$PROJECT_ID/eventarc-${_SUFFIX} - --region=us-central1 - --platform=managed - --no-allow-unauthenticated - --set-env-vars=NAME=Test images: - - 'gcr.io/$PROJECT_ID/run-events-${_SUFFIX}' + - 'gcr.io/$PROJECT_ID/eventarc-${_SUFFIX}' diff --git a/eventarc/pubsub/spec/run_events_pubsub_handler_spec.rb b/eventarc/pubsub/spec/handler_spec.rb similarity index 100% rename from eventarc/pubsub/spec/run_events_pubsub_handler_spec.rb rename to eventarc/pubsub/spec/handler_spec.rb