mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
events: add API helpers for service events stream topics.
This commit is contained in:
@@ -16,6 +16,7 @@ const (
|
||||
TopicAllocation Topic = "Allocation"
|
||||
TopicJob Topic = "Job"
|
||||
TopicNode Topic = "Node"
|
||||
TopicService Topic = "Service"
|
||||
TopicAll Topic = "*"
|
||||
)
|
||||
|
||||
@@ -91,12 +92,23 @@ func (e *Event) Node() (*Node, error) {
|
||||
return out.Node, nil
|
||||
}
|
||||
|
||||
// Service returns a ServiceRegistration struct from a given event payload. If
|
||||
// the Event Topic is Service this will return a valid ServiceRegistration.
|
||||
func (e *Event) Service() (*ServiceRegistration, error) {
|
||||
out, err := e.decodePayload()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out.Service, nil
|
||||
}
|
||||
|
||||
type eventPayload struct {
|
||||
Allocation *Allocation `mapstructure:"Allocation"`
|
||||
Deployment *Deployment `mapstructure:"Deployment"`
|
||||
Evaluation *Evaluation `mapstructure:"Evaluation"`
|
||||
Job *Job `mapstructure:"Job"`
|
||||
Node *Node `mapstructure:"Node"`
|
||||
Allocation *Allocation `mapstructure:"Allocation"`
|
||||
Deployment *Deployment `mapstructure:"Deployment"`
|
||||
Evaluation *Evaluation `mapstructure:"Evaluation"`
|
||||
Job *Job `mapstructure:"Job"`
|
||||
Node *Node `mapstructure:"Node"`
|
||||
Service *ServiceRegistration `mapstructure:"Service"`
|
||||
}
|
||||
|
||||
func (e *Event) decodePayload() (*eventPayload, error) {
|
||||
|
||||
@@ -258,6 +258,18 @@ func TestEventStream_PayloadValueHelpers(t *testing.T) {
|
||||
}, n)
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "service",
|
||||
input: []byte(`{"Topic": "Service", "Payload": {"Service":{"ID":"some-service-id","Namespace":"some-service-namespace-id","Datacenter":"us-east-1a"}}}`),
|
||||
expectFn: func(t *testing.T, event Event) {
|
||||
require.Equal(t, TopicService, event.Topic)
|
||||
a, err := event.Service()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "us-east-1a", a.Datacenter)
|
||||
require.Equal(t, "some-service-id", a.ID)
|
||||
require.Equal(t, "some-service-namespace-id", a.Namespace)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
|
||||
Reference in New Issue
Block a user