forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventservice.go
214 lines (192 loc) · 7.27 KB
/
eventservice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"time"
"github.com/stmcginnis/gofish/common"
)
// EventFormatType is
type EventFormatType string
const (
// EventEventFormatType The subscription destination will receive JSON
// Bodies of the Resource Type Event.
EventEventFormatType EventFormatType = "Event"
// MetricReportEventFormatType The subscription destination will receive
// JSON Bodies of the Resource Type MetricReport.
MetricReportEventFormatType EventFormatType = "MetricReport"
)
// EventService is used to represent an event service for a Redfish
// implementation.
type EventService struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataEtag is the odata etag.
ODataEtag string `json:"@odata.etag"`
// ODataID is the odata identifier.
ODataID string `json:"@odata.id"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// DeliveryRetryAttempts shall be the
// number of retrys attempted for any given event to the subscription
// destination before the subscription is terminated. This retry is at
// the service level, meaning the HTTP POST to the Event Destination was
// returned by the HTTP operation as unsuccessful (4xx or 5xx return
// code) or an HTTP timeout occurred this many times before the Event
// Destination subscription is terminated.
DeliveryRetryAttempts int
// DeliveryRetryIntervalSeconds shall be the interval in seconds between the
// retry attempts for any given event
// to the subscription destination.
DeliveryRetryIntervalSeconds int
// Description provides a description of this resource.
Description string
// EventFormatTypes shall indicate the the
// content types of the message that this service can send to the event
// destination. If this property is not present, the EventFormatType
// shall be assumed to be Event.
EventFormatTypes []EventFormatType
// RegistryPrefixes is the array of the Prefixes of the Message Registries
// that shall be allowed for an Event Subscription.
RegistryPrefixes []string
// ResourceTypes is used for an Event Subscription.
ResourceTypes []string
// SSEFilterPropertiesSupported shall contain a set of properties that
// indicate which properties are supported in the $filter query parameter
// for the URI indicated by the ServerSentEventUri property.
SSEFilterPropertiesSupported SSEFilterPropertiesSupported
// ServerSentEventURI shall be a URI that specifies an HTML5 Server-Sent
// Event conformant endpoint.
ServerSentEventURI string `json:"ServerSentEventUri"`
// ServiceEnabled shall be a boolean indicating whether this service is enabled.
ServiceEnabled bool
// Status is This property shall contain any status or health properties of
// the resource.
Status common.Status
// SubordinateResourcesSupported is When set to true, the service is
// indicating that it supports the SubordinateResource property on Event
// Subscriptions and on generated Events.
SubordinateResourcesSupported bool
// Subscriptions shall contain the link to a collection of type
// EventDestinationCollection.
subscriptions string
// submitTestEventTarget is the URL to send SubmitTestEvent actions.
submitTestEventTarget string
}
// UnmarshalJSON unmarshals a EventService object from the raw JSON.
func (eventservice *EventService) UnmarshalJSON(b []byte) error {
type temp EventService
type Actions struct {
SubmitTestEvent struct {
Target string
} `json:"#EventService.SubmitTestEvent"`
}
var t struct {
temp
Subscriptions common.Link
Actions Actions
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
// Extract the links to other entities for later
*eventservice = EventService(t.temp)
eventservice.subscriptions = string(t.Subscriptions)
eventservice.submitTestEventTarget = t.Actions.SubmitTestEvent.Target
return nil
}
// GetEventService will get a EventService instance from the service.
func GetEventService(c common.Client, uri string) (*EventService, error) {
resp, err := c.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var eventservice EventService
err = json.NewDecoder(resp.Body).Decode(&eventservice)
if err != nil {
return nil, err
}
eventservice.SetClient(c)
return &eventservice, nil
}
// ListReferencedEventServices gets the collection of EventService from
// a provided reference.
func ListReferencedEventServices(c common.Client, link string) ([]*EventService, error) {
var result []*EventService
if link == "" {
return result, nil
}
links, err := common.GetCollection(c, link)
if err != nil {
return result, err
}
for _, eventserviceLink := range links.ItemLinks {
eventservice, err := GetEventService(c, eventserviceLink)
if err != nil {
return result, err
}
result = append(result, eventservice)
}
return result, nil
}
// SubmitTestEvent shall add a test event to the event service with the event
// data specified in the action parameters. This message should then be sent to
// any appropriate ListenerDestination targets.
func (eventservice *EventService) SubmitTestEvent(message string) error {
type temp struct {
EventGroupID string `json:"EventGroupId"`
EventID string `json:"EventId"`
EventTimestamp string
EventType string
Message string
MessageArgs []string
MessageID string `json:"MessageId"`
OriginOfCondition string
Severity string
}
t := temp{
EventGroupID: "TESTING123",
EventID: "TEST123",
EventTimestamp: time.Now().String(),
EventType: "Alert",
Message: message,
MessageID: "test123",
OriginOfCondition: eventservice.ODataID,
Severity: "Informational",
}
_, err := eventservice.Client.Post(eventservice.submitTestEventTarget, t)
return err
}
// SSEFilterPropertiesSupported shall contain a set of properties that indicate
// which properties are supported in the $filter query parameter for the URI
// indicated by the ServerSentEventUri property.
type SSEFilterPropertiesSupported struct {
// EventFormatType shall be a boolean indicating if this service supports
// the use of the EventFormatType property in the $filter query parameter as
// described by the specification.
EventFormatType bool
// MessageID shall be a boolean indicating if this service supports the use
// of the MessageId property in the $filter query parameter as described by
// the specification.
MessageID bool `json:"MessageId"`
// MetricReportDefinition shall be a boolean indicating if this service
// supports the use of the MetricReportDefinition property in the $filter
// query parameter as described by the specification.
MetricReportDefinition bool
// OriginResource shall be a boolean indicating if this service supports the
// use of the OriginResource property in the $filter query parameter as
// described by the specification.
OriginResource bool
// RegistryPrefix shall be a boolean indicating if this service supports the
// use of the RegistryPrefix property in the $filter query parameter as
// described by the specification.
RegistryPrefix bool
// ResourceType shall be a boolean indicating if this service supports the
// use of the ResourceType property in the $filter query parameter as
// described by the specification.
ResourceType bool
}