From d6722007f9232dc489c91284eed18c005f3de241 Mon Sep 17 00:00:00 2001 From: Thomas Bouldin Date: Wed, 25 Apr 2018 12:25:46 -0700 Subject: [PATCH 1/2] Add utility to send events --- cmd/sendevent/main.go | 104 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 cmd/sendevent/main.go diff --git a/cmd/sendevent/main.go b/cmd/sendevent/main.go new file mode 100644 index 00000000000..0ca29cbb8de --- /dev/null +++ b/cmd/sendevent/main.go @@ -0,0 +1,104 @@ +/* +Copyright 2018 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 + + https://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. +*/ + +// Implements a simple utility for sending a JSON-encoded sample event. +// Not wired to bazel because this utility is meant to be run directly rather +// than deployed to K8S +package main + +import ( + "crypto/rand" + "encoding/json" + "flag" + "fmt" + "io/ioutil" + "net/http" + "os" + "time" + + "github.com/elafros/eventing/pkg/event" +) + +var ( + context event.Context + webhook string + data string +) + +func init() { + flag.StringVar(&context.EventID, "event-id", "", "Event ID to use. Defaults to a UUID") + flag.StringVar(&context.EventType, "event-type", "google.events.action.demo", "The Event Type to use.") + flag.StringVar(&context.Source, "source", "", "Source URI to use. Defaults to the current machine's hostname") + flag.StringVar(&data, "data", `{"hello": "world!"}`, "Event data") +} + +func main() { + flag.Parse() + + if flag.NArg() != 1 { + fmt.Println("Missing webhook address") + os.Exit(1) + } + + webhook := flag.Args()[0] + + var untyped map[string]interface{} + if err := json.Unmarshal([]byte(data), &untyped); err != nil { + fmt.Println("Currenlty sendevent only supports JSON event data") + os.Exit(1) + } + + fillEventContext(&context) + req, err := event.NewRequest(webhook, untyped, context) + if err != nil { + fmt.Printf("Failed to create request: %s", err) + os.Exit(1) + } + + res, err := http.DefaultClient.Do(req) + if err != nil { + fmt.Printf("Failed to send event to %s: %s\n", webhook, err) + os.Exit(1) + } + fmt.Printf("Got response from %s\n%s\n", webhook, res.Status) + if res.Header.Get("Content-Length") != "" { + bytes, _ := ioutil.ReadAll(res.Body) + fmt.Println(string(bytes)) + } +} + +func fillEventContext(ctx *event.Context) { + ctx.CloudEventsVersion = "0.1" + ctx.EventTime = time.Now().UTC() + + if ctx.EventID == "" { + // A "UUID". Not technically spec complaint + b := make([]byte, 16) + if n, err := rand.Read(b); n != 16 || err != nil { + fmt.Println("Could not create event-id UUID") + os.Exit(1) + } + ctx.EventID = fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]) + } + + if ctx.Source == "" { + var err error + ctx.Source, err = os.Hostname() + if err != nil { + ctx.Source = "localhost" + } + } +} From 2df964497de50472c12667034e5738e972bcca9a Mon Sep 17 00:00:00 2001 From: Thomas Bouldin Date: Wed, 25 Apr 2018 13:51:57 -0700 Subject: [PATCH 2/2] PR feedback --- cmd/sendevent/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/sendevent/main.go b/cmd/sendevent/main.go index 0ca29cbb8de..57ab6cbca90 100644 --- a/cmd/sendevent/main.go +++ b/cmd/sendevent/main.go @@ -48,16 +48,16 @@ func init() { func main() { flag.Parse() - if flag.NArg() != 1 { - fmt.Println("Missing webhook address") + if len(flag.Args()) != 1 { + fmt.Println("Usage: sendevent [flags] \nFor details about valid flags, run sendevent --help") os.Exit(1) } - webhook := flag.Args()[0] + webhook := flag.Arg(0) var untyped map[string]interface{} if err := json.Unmarshal([]byte(data), &untyped); err != nil { - fmt.Println("Currenlty sendevent only supports JSON event data") + fmt.Println("Currently sendevent only supports JSON event data") os.Exit(1) }