Skip to content

Instantly share code, notes, and snippets.

View grant's full-sized avatar

Grant Timmerman grant

View GitHub Profile
@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 / Dockerfile
Created August 3, 2019 01:32
Run Node 12 Dockerfile
# Use the official Node.js 12 image.
# https://hub.docker.com/_/node
FROM node:12
# Create and change to the app directory.
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
# Copying this separately prevents re-running npm install on every code change.
COPY package*.json ./
# Install production dependencies.
import { serve } from "https://deno.land/std@0.89.0/http/server.ts";
import "https://deno.land/x/dotenv/mod.ts";
const PORT = Deno.env.get('PORT') || 8080;
const s = serve(`0.0.0.0:${PORT}`);
const body = new TextEncoder().encode("Hello, Deno\n");
console.log(`Server started on port ${PORT}`);
for await (const req of s) {
req.respond({ body });
@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 / package.json
Last active January 28, 2021 06:32
run-workflow-function package.json
{
"name": "run-workflow-function",
"main": "index.js",
"scripts": {
"start": "functions-framework --target=runWorkflow"
},
"dependencies": {
"@google-cloud/functions-framework": "^1.7.1",
"@google-cloud/workflows": "^1.1.0"
}
@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();
@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
Created November 12, 2020 04:29
Critterwatch Vision
const vision = require('@google-cloud/vision');
const visionClient = new vision.ImageAnnotatorClient();
const {Firestore} = require('@google-cloud/firestore');
const firestore = new Firestore();
const trackedAnimals = ['raccoon', 'alpaca', 'peacock', 'dog', 'gecko', 'squirrel', 'red panda'];
/**
* Triggered from a change to a Cloud Storage bucket.
@grant
grant / package.json
Last active August 5, 2020 18:03
Sheets as a Service – Basic Package
{
"name": "sheets-on-run",
"main": "index.js",
"scripts": {
"start": "functions-framework --target=addRow"
},
"dependencies": {
"@google-cloud/functions-framework": "^1.6.0",
"googleapis": "^58.0.0"
}
@grant
grant / index.js
Last active August 1, 2020 02:45
Sheets as a Service – Basic Script
const path = require('path');
const {google} = require('googleapis');
const sheets = google.sheets('v4');
// TODO: Add your Sheet ID
// https://docs.google.com/spreadsheets/d/2L2LpYPyxJQTf14OLM6mVuiou88MsEi4Q-IUUxswNlfe/edit
const SPREADSHEET_ID = '2L2LpYPyxJQTf14OLM6mVuiou88MsEi4Q-IUUxswNlfe';
/**
* Appends row data to the Google Sheet