Skip to content

Instantly share code, notes, and snippets.

View grant's full-sized avatar

Grant Timmerman grant

View GitHub Profile
@grant
grant / deploy-cloud-workflow.yaml
Last active May 18, 2021 16:27
A GitHub Workflow that deploys a Cloud Workflow
name: Deploy Cloud Workflow
on:
push:
branches:
- main
jobs:
deploy:
name: Deploy Cloud Workflow
runs-on: ubuntu-18.04
steps:
@grant
grant / myFirstWorkflow.workflows.yaml
Created May 15, 2021 19:02
Cloud Workflow – myFirstWorkflow
- getCurrentTime:
call: http.get
args:
url: https://us-central1-workflowsample.cloudfunctions.net/datetime
result: currentTime
- readWikipedia:
call: http.get
args:
url: https://en.wikipedia.org/w/api.php
query:
@grant
grant / deploy.sh
Created May 4, 2021 17:19
Deploy gRPC to Cloud Run
# Get Cloud Run URL
ENDPOINT=$(\
gcloud run services list \
--project=${PROJECT} \
--region=${REGION} \
--platform=managed \
--format="value(status.address.url)" \
--filter="metadata.name=grpc-calculator")
ENDPOINT=${ENDPOINT#https://} && echo ${ENDPOINT}
@grant
grant / server.js
Created May 4, 2021 17:17
gRPC Server
function calculate(call, callback) {
const request = call.request;
let result;
if (request.operation === 'ADD') {
result = request.first_operand + request.second_operand;
} else {
result = request.first_operand - request.second_operand;
}
callback(null, {result});
}
@grant
grant / index.js
Created May 4, 2021 15:34
Listen to gRPC requests
function main() {
const server = new grpc.Server();
server.addService(calculatorProto.Calculator.service, {calculate});
server.bindAsync(`0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure(), (error, port) => {
if (error) {
throw error;
}
server.start();
});
}
@grant
grant / calculator.proto
Created May 4, 2021 15:28
calculator.proto
enum Operation {
ADD = 0;
SUBTRACT = 1;
}
message BinaryOperation {
float first_operand = 1;
float second_operand = 2;
Operation operation = 3;
};
message CalculationResult {
@grant
grant / cal-gcloud.sh
Created April 18, 2021 20:34
gcloud eventarc triggers create CAL
export TRIGGER_NAME=events-cal-trigger
export RUN_SERVICE=helloworld-events
export PROJECT_NUMBER="$(gcloud projects describe $(gcloud config get-value project) --format='value(projectNumber)')"
gcloud eventarc triggers create $TRIGGER_NAME \
--destination-run-service=$RUN_SERVICE \
--service-account=${PROJECT_NUMBER}-compute@developer.gserviceaccount.com \
--event-filters="type=google.cloud.audit.log.v1.written" \
--event-filters="serviceName=cloudfunctions.googleapis.com" \
--event-filters="methodName=google.cloud.functions.v1.CloudFunctionsService.CreateFunction"
@grant
grant / hello_world_http.cc
Created February 13, 2021 21:29
Hello World, C++ Functions Framework
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <nlohmann/json.hpp>
namespace gcf = ::google::cloud::functions;
gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
auto greeting = [r = std::move(request)] {
auto request_json = nlohmann::json::parse(r.payload(), /*cb=*/nullptr,
/*allow_exceptions=*/false);
@grant
grant / deploy.sh
Created January 27, 2021 21:15
run-workflow-function deploy.sh
# Configuration
export PROJECT_ID=$(gcloud config get-value core/project)
# Deploy Workflow
gcloud workflows deploy myFirstWorkflow \
--source=myFirstWorkflow.yaml
# Deploy (private) Function
gcloud functions deploy runWorkflowFunction \
--runtime nodejs12 \
@grant
grant / index.js
Last active January 27, 2021 21:19
run-workflow-function index.js
const projectId = process.env.PROJECT_ID;
/**
* Run Workflow Cloud Function
*/
exports.runWorkflow = async (req, res) => {
if (req.method !== 'POST') {
return res.status(405).send('Only POST method is allowed');
}
const workflowsAPI = await callWorkflowsAPI();