-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef5b999
commit b451b09
Showing
205 changed files
with
41,408 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,43 @@ | ||
package event | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
cenats "github.com/cloudevents/sdk-go/protocol/nats/v2" | ||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
) | ||
|
||
type EventBus struct { | ||
client cloudevents.Client | ||
NatsServer string | ||
Subject string | ||
} | ||
|
||
func NewEventBus(natsServer string, subject string) (*EventBus, error) { | ||
p, err := cenats.NewSender(natsServer, subject, cenats.NatsOptions()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer p.Close(context.Background()) | ||
|
||
c, err := cloudevents.NewClient(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &EventBus{client: c, NatsServer: natsServer, Subject: subject}, nil | ||
} | ||
|
||
func (bus *EventBus) Publish(ctx context.Context, event cloudevents.Event) error { | ||
result := bus.client.Send(ctx, event) | ||
if cloudevents.IsUndelivered(result) { | ||
return errors.New("failed to publish") | ||
} | ||
return nil | ||
} | ||
|
||
func (bus *EventBus) Subscribe(ctx context.Context, handler func(ctx context.Context, event cloudevents.Event)) error { | ||
for { | ||
bus.client.StartReceiver(ctx, handler) | ||
} | ||
} |
This file contains 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,51 @@ | ||
package event | ||
|
||
import ( | ||
"fmt" | ||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type EventPipelineRun struct { | ||
Ref string `json:"ref"` | ||
Desc string `json:"desc"` | ||
Variables string `json:"variables"` | ||
} | ||
|
||
type EventTaskRun struct { | ||
Ref string `json:"ref"` | ||
Desc string `json:"desc"` | ||
Variables string `json:"variables"` | ||
} | ||
|
||
type EventInspection struct { | ||
TypeRef string `json:"typeRef"` | ||
NameRef string `json:"nameRef"` | ||
NodeName string `json:"nodeName"` | ||
Variables string `json:"variables"` | ||
ThresholdValue string `json:"thresholdValue"` | ||
Comparator string `json:"comparator"` | ||
CurrentValue string `json:"currentValue"` | ||
Status string `json:"status"` | ||
Priority string `json:"priority"` | ||
} | ||
|
||
func BuilderEvent(data interface{}) (cloudevents.Event, error) { | ||
e := cloudevents.NewEvent() | ||
e.SetID(uuid.New().String()) | ||
e.SetSource("https://www.chenshaowen.com/ops/") | ||
|
||
var eventType string | ||
switch v := data.(type) { | ||
case EventInspection: | ||
eventType = "ops.inspection" | ||
case *EventInspection: | ||
eventType = "ops.inspection" | ||
default: | ||
eventType = "ops.unknown" | ||
return e, fmt.Errorf("unsupported data type: %T", v) | ||
} | ||
e.SetType(eventType) | ||
err := e.SetData("application/json", data) | ||
return e, err | ||
} |
201 changes: 201 additions & 0 deletions
201
vendor/github.com/cloudevents/sdk-go/protocol/nats/v2/LICENSE
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.