Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 48b12ee

Browse files
committed
"send-event" command added
1 parent b02e77d commit 48b12ee

5 files changed

Lines changed: 109 additions & 6 deletions

File tree

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,32 @@ Watch incoming events:
6969
tmcli watch
7070
```
7171

72+
Create transformation:
73+
```
74+
tmcli create transformation --source foo-awssqssource
75+
```
76+
7277
Create target and trigger:
7378

7479
```
7580
tmcli create target cloudevents --endpoint https://sockeye-tzununbekov.dev.triggermesh.io
76-
tmcli create trigger --source foo-awssqssource --target foo-cloudeventstarget
81+
tmcli create trigger --source foo-transformation --target foo-cloudeventstarget
7782
```
7883

7984
Or, in one command:
8085

8186
```
82-
tmcli create target cloudevents --endpoint https://sockeye-tzununbekov.dev.triggermesh.io --source foo-awssqssource
87+
tmcli create target cloudevents --endpoint https://sockeye-tzununbekov.dev.triggermesh.io --source foo-transformation
8388
```
8489

8590
Open sockeye [web-interface](https://sockeye-tzununbekov.dev.triggermesh.io), send the message to SQS queue specified in the source creation step and observe the received CloudEvent in the sockeye tab.
8691

92+
Or send test event manually:
93+
94+
```
95+
tmcli send-event --eventType com.amazon.sqs.message '{"hello":"world"}'
96+
```
97+
8798
Stop event flow:
8899

89100
```

cmd/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/triggermesh/tmcli/cmd/describe"
3333
"github.com/triggermesh/tmcli/cmd/dump"
3434
"github.com/triggermesh/tmcli/cmd/list"
35+
"github.com/triggermesh/tmcli/cmd/sendevent"
3536
"github.com/triggermesh/tmcli/cmd/start"
3637
"github.com/triggermesh/tmcli/cmd/stop"
3738
"github.com/triggermesh/tmcli/cmd/watch"
@@ -84,6 +85,7 @@ func NewRootCommand() *cobra.Command {
8485
rootCmd.AddCommand(dump.NewCmd())
8586
rootCmd.AddCommand(describe.NewCmd())
8687
rootCmd.AddCommand(list.NewCmd())
88+
rootCmd.AddCommand(sendevent.NewCmd())
8789
rootCmd.AddCommand(start.NewCmd())
8890
rootCmd.AddCommand(stop.NewCmd())
8991
rootCmd.AddCommand(watch.NewCmd())

cmd/dump/dump.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ import (
2323

2424
"github.com/spf13/cobra"
2525
"github.com/spf13/viper"
26-
"github.com/triggermesh/tmcli/pkg/manifest"
2726
"gopkg.in/yaml.v3"
27+
28+
"github.com/triggermesh/tmcli/pkg/manifest"
2829
)
2930

3031
const manifestFile = "manifest.yaml"

cmd/sendevent/sendevent.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Copyright 2022 TriggerMesh Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package sendevent
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"strings"
23+
24+
cloudevents "github.com/cloudevents/sdk-go/v2"
25+
"github.com/spf13/cobra"
26+
"github.com/spf13/viper"
27+
28+
tmbroker "github.com/triggermesh/tmcli/pkg/triggermesh/broker"
29+
)
30+
31+
const (
32+
defaultEventType = "triggermesh-local-event"
33+
defaultEventSource = "triggermesh-cli"
34+
)
35+
36+
type SendOptions struct {
37+
Context string
38+
ConfigDir string
39+
EventType string
40+
}
41+
42+
func NewCmd() *cobra.Command {
43+
o := &SendOptions{}
44+
sendCmd := &cobra.Command{
45+
Use: "send-event <data> [--eventType <type>]",
46+
Short: "Send CloudEvent to the broker",
47+
RunE: func(cmd *cobra.Command, args []string) error {
48+
o.Context = viper.GetString("context")
49+
configDir, err := cmd.Flags().GetString("config")
50+
if err != nil {
51+
return err
52+
}
53+
o.ConfigDir = configDir
54+
return o.send(strings.Join(args, " "))
55+
},
56+
}
57+
sendCmd.Flags().StringVar(&o.EventType, "eventType", defaultEventType, "CloudEvent Type attribute")
58+
return sendCmd
59+
}
60+
61+
func (o *SendOptions) send(data string) error {
62+
ctx := context.Background()
63+
broker, err := tmbroker.New(o.Context, o.ConfigDir)
64+
if err != nil {
65+
return fmt.Errorf("broker object: %v", err)
66+
}
67+
port, err := broker.GetPort(ctx)
68+
if err != nil {
69+
return fmt.Errorf("broker socket: %v", err)
70+
}
71+
72+
c, err := cloudevents.NewClientHTTP()
73+
if err != nil {
74+
return fmt.Errorf("cloudevents client, %w", err)
75+
}
76+
77+
event := cloudevents.NewEvent()
78+
event.SetSource(defaultEventSource)
79+
event.SetType(o.EventType)
80+
event.SetData(cloudevents.ApplicationJSON, data)
81+
82+
brokerEndpoint := fmt.Sprintf("http://localhost:%s", port)
83+
fmt.Printf("%s -> %s\n", data, brokerEndpoint)
84+
result := c.Send(cloudevents.ContextWithTarget(ctx, brokerEndpoint), event)
85+
if cloudevents.IsUndelivered(result) {
86+
return fmt.Errorf("send event: %w", result)
87+
}
88+
fmt.Println("OK")
89+
return nil
90+
}

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/triggermesh/tmcli
33
go 1.19
44

55
require (
6+
github.com/cloudevents/sdk-go/v2 v2.10.1
67
github.com/docker/docker v20.10.12+incompatible
78
github.com/docker/go-connections v0.4.0
89
github.com/spf13/cobra v1.5.0
@@ -12,7 +13,6 @@ require (
1213
k8s.io/api v0.23.5
1314
k8s.io/apimachinery v0.23.5
1415
k8s.io/kube-openapi v0.0.0-20220124234850-424119656bbf
15-
knative.dev/eventing v0.31.1-0.20220523181303-c3e13967001f
1616

1717
)
1818

@@ -52,8 +52,6 @@ require (
5252
github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect
5353
github.com/cespare/xxhash/v2 v2.1.2 // indirect
5454
github.com/cloudevents/sdk-go/observability/opencensus/v2 v2.6.1 // indirect
55-
github.com/cloudevents/sdk-go/sql/v2 v2.8.0 // indirect
56-
github.com/cloudevents/sdk-go/v2 v2.10.1 // indirect
5755
github.com/containerd/containerd v1.6.0 // indirect
5856
github.com/davecgh/go-spew v1.1.1 // indirect
5957
github.com/dimchansky/utfbom v1.1.1 // indirect
@@ -146,6 +144,7 @@ require (
146144
k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible // indirect
147145
k8s.io/klog/v2 v2.60.1-0.20220317184644-43cc75f9ae89 // indirect
148146
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
147+
knative.dev/eventing v0.31.1-0.20220523181303-c3e13967001f // indirect
149148
knative.dev/networking v0.0.0-20220412163509-1145ec58c8be // indirect
150149
knative.dev/pkg v0.0.0-20220525153005-18f69958870f // indirect
151150
knative.dev/serving v0.31.0 // indirect

0 commit comments

Comments
 (0)