Skip to content

Commit

Permalink
feat: eventarc generic snippet – and eventarc pub/sub brushup (#692)
Browse files Browse the repository at this point in the history
* feat: add eventarc generic snippet

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>

* docs: brush up readme

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>

* lint: fix lint issue

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
  • Loading branch information
grant committed Oct 30, 2020
1 parent bae30b7 commit b540c9c
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 10 deletions.
5 changes: 5 additions & 0 deletions eventarc/generic/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Dockerfile
README.md
.ruby-version
.bundle/
vendor/
4 changes: 4 additions & 0 deletions eventarc/generic/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
README.md
.ruby-version
.bundle/
vendor/
33 changes: 33 additions & 0 deletions eventarc/generic/Dockerfile
Original file line number Diff line number Diff line change
@@ -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]
11 changes: 11 additions & 0 deletions eventarc/generic/Gemfile
Original file line number Diff line number Diff line change
@@ -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
65 changes: 65 additions & 0 deletions eventarc/generic/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions eventarc/generic/README.md
Original file line number Diff line number Diff line change
@@ -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
```
53 changes: 53 additions & 0 deletions eventarc/generic/app.rb
Original file line number Diff line number Diff line change
@@ -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}<br>"
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]
38 changes: 38 additions & 0 deletions eventarc/generic/e2e_test_setup.yaml
Original file line number Diff line number Diff line change
@@ -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}'
36 changes: 36 additions & 0 deletions eventarc/generic/spec/handler_spec.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions eventarc/pubsub/README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
```
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions eventarc/pubsub/e2e_test_setup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
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
args:
- 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}'
File renamed without changes.

0 comments on commit b540c9c

Please sign in to comment.