-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: eventarc generic snippet – and eventarc pub/sub brushup (#692)
* 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
Showing
12 changed files
with
292 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Dockerfile | ||
README.md | ||
.ruby-version | ||
.bundle/ | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
README.md | ||
.ruby-version | ||
.bundle/ | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.