-
Notifications
You must be signed in to change notification settings - Fork 630
Add utility to send events #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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 len(flag.Args()) != 1 { | ||
| fmt.Println("Usage: sendevent [flags] <webhook>\nFor details about valid flags, run sendevent --help") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| webhook := flag.Arg(0) | ||
|
|
||
| var untyped map[string]interface{} | ||
| if err := json.Unmarshal([]byte(data), &untyped); err != nil { | ||
| fmt.Println("Currently 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't you just use := on the next line?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. |
||
| ctx.Source, err = os.Hostname() | ||
| if err != nil { | ||
| ctx.Source = "localhost" | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest naming the flag something that ends with "json", and specifying in the description it should be JSON.
(You could add flags for other formats later.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I still don't really feel like I know how this tool is going to grow. I've also been considering adding a --file version of the data, and don't want to do a cross-product of {literal or by-file data} x {content-type}. I might do a content-type flag or just limit the tool to JSON.