Skip to content

Instantly share code, notes, and snippets.

@tleyden
Created July 8, 2017 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tleyden/7b665d3949ae80c2dc11e0677fe28d0c to your computer and use it in GitHub Desktop.
Save tleyden/7b665d3949ae80c2dc11e0677fe28d0c to your computer and use it in GitHub Desktop.
openwhisk debugging
package main
import (
"context"
"fmt"
"encoding/json"
"github.com/flimzy/kivik"
_ "github.com/flimzy/kivik/driver/couchdb" // The CouchDB driver
"os"
"log"
)
// This writes/updates a doc in CouchDB. If the doc doesn't exist, it will write it. If it does exist,
// it will "clobber" it and create a new revision based on the updated content.
// TODO: ------------- Write doc to stdout
// TODO: Currently when I invoke an action in OpenWhisk, it emits
// TODO: "error": "The action did not return a dictionary."
// TODO: This happens becuse this isn't writing the doc to stdout. It should actually GET the doc based on ID
// TODO: so that the doc written to stdout has a _rev parameter.
type Params struct {
Username string
Password string
Host string
DbName string
Doc map[string]interface{}
DocId string
}
func main() {
ctx := context.TODO()
params := discoverParams()
dataSourceName := fmt.Sprintf(
"http://%s:%s@%s",
params.Username,
params.Password,
params.Host,
)
client, err := kivik.New(ctx, "couch", dataSourceName)
if err != nil {
panic(err)
}
db, err := client.DB(ctx, params.DbName)
if err != nil {
panic(err)
}
// Cas loop where we get the latest rev of the doc
for {
fetchedDocRow, err := db.Get(ctx, params.DocId)
if err != nil {
// Assume this will be the first rev of the doc, so do nothing in this case
} else {
// Got an existing doc, update the doc being inserted to be based on this rev
// we had a valid previous rev
fetchedDoc := map[string]interface{}{}
scanDocErr := fetchedDocRow.ScanDoc(&fetchedDoc)
if scanDocErr != nil {
panic(fmt.Sprintf("Could not read doc into map"))
}
revRaw, ok := fetchedDoc["_rev"]
if !ok {
panic(fmt.Sprintf("Doc does not have _rev field"))
}
rev := revRaw.(string)
params.Doc["_rev"] = rev
}
log.Printf("Going to put doc: %+v", params.Doc)
rev, err := db.Put(context.TODO(), params.DocId, params.Doc)
if err != nil {
// TODO: this should write to stderr rather than stdout
log.Printf("Error putting doc, assuming to be 409 cas error. Actual err: %v", err)
continue
}
fmt.Printf("%s inserted with revision %s\n", params.DocId, rev)
break
}
// TODO: do a get on the doc and write it to stdout
// remove all other debugging
}
func discoverParams() (params Params) {
// native actions receive one argument, the JSON object as a string
arg := os.Args[1]
// unmarshal the string to a JSON object
json.Unmarshal([]byte(arg), &params)
return params
}
FROM openwhisk/dockerskeleton
COPY exec /action/exec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment