forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventdestination.go
454 lines (395 loc) · 16.8 KB
/
eventdestination.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"fmt"
"net/url"
"reflect"
"strings"
"github.com/stmcginnis/gofish/common"
)
// DeliveryRetryPolicy is the retry policy for delivery failure.
type DeliveryRetryPolicy string
const (
// TerminateAfterRetriesDeliveryRetryPolicy The subscription is
// terminated after the maximum number of retries is reached.
TerminateAfterRetriesDeliveryRetryPolicy DeliveryRetryPolicy = "TerminateAfterRetries"
// SuspendRetriesDeliveryRetryPolicy The subscription is suspended after
// the maximum number of retries is reached.
SuspendRetriesDeliveryRetryPolicy DeliveryRetryPolicy = "SuspendRetries"
// RetryForeverDeliveryRetryPolicy shall continue even after the after
// the maximum number of retries is reached.
RetryForeverDeliveryRetryPolicy DeliveryRetryPolicy = "RetryForever"
)
// EventDestinationProtocol is the communication protocol of the event destination.
type EventDestinationProtocol string
const (
// RedfishEventDestinationProtocol The destination follows the Redfish
// specification for event notifications.
RedfishEventDestinationProtocol EventDestinationProtocol = "Redfish"
// SNMPv1EventDestinationProtocol shall indicate the destination follows
// the RFC1157-defined SNMPv1 protocol.
SNMPv1EventDestinationProtocol EventDestinationProtocol = "SNMPv1"
// SNMPv2cEventDestinationProtocol shall indicate the destination follows
// the SNMPv2c protocol as defined by RFC1441 and RFC1452.
SNMPv2cEventDestinationProtocol EventDestinationProtocol = "SNMPv2c"
// SNMPv3EventDestinationProtocol shall indicate the destination follows
// the SNMPv3 protocol as defined by RFC3411 and RFC3418.
SNMPv3EventDestinationProtocol EventDestinationProtocol = "SNMPv3"
// SMTPEventDestinationProtocol shall indicate the destination follows
// the RFC5321-defined SMTP specification.
SMTPEventDestinationProtocol EventDestinationProtocol = "SMTP"
)
// SNMPAuthenticationProtocols is
type SNMPAuthenticationProtocols string
const (
// NoneSNMPAuthenticationProtocols shall indicate authentication is not
// required.
NoneSNMPAuthenticationProtocols SNMPAuthenticationProtocols = "None"
// CommunityStringSNMPAuthenticationProtocols shall indicate
// authentication using SNMP community strings and the value of
// TrapCommunity.
CommunityStringSNMPAuthenticationProtocols SNMPAuthenticationProtocols = "CommunityString"
// HMACMD5SNMPAuthenticationProtocols shall indicate authentication
// conforms to the RFC3414-defined HMAC-MD5-96 authentication protocol.
HMACMD5SNMPAuthenticationProtocols SNMPAuthenticationProtocols = "HMAC_MD5"
// HMACSHA96SNMPAuthenticationProtocols shall indicate authentication
// conforms to the RFC3414-defined HMAC-SHA-96 authentication protocol.
HMACSHA96SNMPAuthenticationProtocols SNMPAuthenticationProtocols = "HMAC_SHA96"
)
// SNMPEncryptionProtocols is
type SNMPEncryptionProtocols string
const (
// NoneSNMPEncryptionProtocols shall indicate there is no encryption.
NoneSNMPEncryptionProtocols SNMPEncryptionProtocols = "None"
// CBCDESSNMPEncryptionProtocols shall indicate encryption conforms to
// the RFC3414-defined CBC-DES encryption protocol.
CBCDESSNMPEncryptionProtocols SNMPEncryptionProtocols = "CBC_DES"
// CFB128AES128SNMPEncryptionProtocols shall indicate encryption
// conforms to the RFC3826-defined CFB128-AES-128 encryption protocol.
CFB128AES128SNMPEncryptionProtocols SNMPEncryptionProtocols = "CFB128_AES128"
)
// SubscriptionType is the type of subscription used.
type SubscriptionType string
const (
// RedfishEventSubscriptionType The subscription follows the Redfish
// specification for event notifications, which is done by a service
// sending an HTTP POST to the subscriber's destination URI.
RedfishEventSubscriptionType SubscriptionType = "RedfishEvent"
// SSESubscriptionType The subscription follows the HTML5 Server-Sent
// Event definition for event notifications.
SSESubscriptionType SubscriptionType = "SSE"
// SNMPTrapSubscriptionType shall indicate the subscription follows the
// various versions of SNMP Traps for event notifications.
// EventDestinationProtocol shall specify the appropriate version of
// SNMP.
SNMPTrapSubscriptionType SubscriptionType = "SNMPTrap"
// SNMPInformSubscriptionType shall indicate the subscription follows
// versions 2 and 3 of SNMP Inform for event notifications.
// EventDestinationProtocol shall specify the appropriate version of
// SNMP.
SNMPInformSubscriptionType SubscriptionType = "SNMPInform"
)
// EventDestination is used to represent the target of an event
// subscription, including the types of events subscribed and context to
// provide to the target in the Event payload.
type EventDestination struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// Context shall contain a client supplied context that will remain with the
// connection through the connections lifetime.
Context string
// DeliveryRetryPolicy shall indicate the subscription
// delivery retry policy for events where the subscription type is
// RedfishEvent. If this property is not present, the policy shall be
// assumed to be TerminateAfterRetries.
DeliveryRetryPolicy DeliveryRetryPolicy
// Description provides a description of this resource.
Description string
// Destination shall contain a URI to the destination where the events will
// be sent.
Destination string
// EventFormatType shall indicate the the content types of the message that
// this service will send to the EventDestination. If this property is not
// present, the EventFormatType shall be assumed to be Event.
EventFormatType EventFormatType
// EventTypes contains the types of events
// that will be sent to the destination.
EventTypes []EventType
// HTTPHeaders shall contain an object consisting of the names and values of
// of HTTP header to be included with every event POST to the Event
// Destination. This property shall be null or an empty array on a GET. An
// empty array is the preferred return value on GET.
HTTPHeaders []HTTPHeaderProperty `json:"HttpHeaders"`
// IncludeOriginOfCondition shall indicate whether the
// event payload sent to the subscription destination will expand the
// OriginOfCondition property to include the resource or object
// referenced by the OriginOfCondition property.
IncludeOriginOfCondition bool
// MessageIDs shall specify an array of MessageIds that are the only
// allowable values for the MessageId property within an EventRecord sent to
// the subscriber. Events with MessageIds not contained in this array shall
// not be sent to the subscriber. If this property is absent or the array is
// empty, the service shall send Events with any MessageId to the subscriber.
MessageIDs []string `json:"MessageIds"`
// metricReportDefinitions shall specify an array of
// metric report definitions that are the only allowable generators of
// metric reports for this subscription. Metric reports originating from
// metric report definitions not contained in this array shall not be
// sent to the subscriber. If this property is absent or the array is
// empty, the service shall send metric reports originating from any
// metric report definition to the subscriber.
metricReportDefinitions []string
// MetricReportDefinitions@odata.count is
MetricReportDefinitionsCount int `json:"MetricReportDefinitions@odata.count"`
// originResources shall specify an array of Resources, Resource Collections,
// or Referenceable Members that are the only allowable values for the
// OriginOfCondition property within an EventRecord sent to the subscriber.
// Events originating from Resources, Resource Collections, or Referenceable
// Members not contained in this array shall not be sent to the subscriber.
// If this property is absent or the array is empty, the service shall send
// Events originating from any Resource, Resource Collection, or
// Referenceable Member to the subscriber.
originResources []string
// OriginResourcesCount is the number of OriginResources.
OriginResourcesCount int `json:"OriginResources@odata.count"`
// Protocol is used to indicate that the event type shall adhere to that
// defined in the Redfish specification.
Protocol EventDestinationProtocol
// RegistryPrefixes is The value of this property is the array of the
// Prefixes of the Message Registries that contain the MessageIds in the
// Events that shall be sent to the EventDestination. If this property
// is absent or the array is empty, the service shall send Events with
// MessageIds from any Message Registry.
RegistryPrefixes []string
// ResourceTypes shall specify an array of Resource Type values. When an
// event is generated, if the OriginOfCondition's Resource Type matches a
// value in this array, the event shall be sent to the event destination
// (unless it would be filtered by other property conditions such as
// RegistryPrefix). If this property is absent or the array is empty, the
// service shall send Events from any Resource Type to the subscriber. The
// value of this property shall be only the general namespace for the type
// and not the versioned value. For example, it shall not be Task.v1_2_0.Task
// and instead shall just be Task. To specify that a client is subscribing
// for Metric Reports, the EventTypes property should include 'MetricReport'.
ResourceTypes []string
// SNMP shall contain the settings for an SNMP event destination.
SNMP SNMPSettings
// Status shall contain the status of the subscription.
Status common.Status
// SubordinateResources is When set to true and OriginResources is
// specified, indicates the subscription shall be for events from the
// OriginsResources specified and all subordinate resources. When set to
// false and OriginResources is specified, indicates subscription shall
// be for events only from the OriginResources. If OriginResources is
// not specified, it has no relevance.
SubordinateResources bool
// SubscriptionType shall indicate the type of subscription for events. If
// this property is not present, the SubscriptionType shall be assumed to be
// RedfishEvent.
SubscriptionType SubscriptionType
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
}
// UnmarshalJSON unmarshals a EventDestination object from the raw JSON.
func (eventdestination *EventDestination) UnmarshalJSON(b []byte) error {
type temp EventDestination
var t struct {
temp
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
// Extract the links to other entities for later
*eventdestination = EventDestination(t.temp)
// This is a read/write object, so we need to save the raw object data for later
eventdestination.rawData = b
return nil
}
// Update commits updates to this object's properties to the running system.
func (eventdestination *EventDestination) Update() error {
// Get a representation of the object's original state so we can find what
// to update.
original := new(EventDestination)
original.UnmarshalJSON(eventdestination.rawData)
readWriteFields := []string{
"Context",
"DeliveryRetryPolicy",
}
originalElement := reflect.ValueOf(original).Elem()
currentElement := reflect.ValueOf(eventdestination).Elem()
return eventdestination.Entity.Update(originalElement, currentElement, readWriteFields)
}
// GetEventDestination will get a EventDestination instance from the service.
func GetEventDestination(c common.Client, uri string) (*EventDestination, error) {
// validate uri
if len(strings.TrimSpace(uri)) == 0 {
return nil, fmt.Errorf("uri should not be empty")
}
resp, err := c.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var eventdestination EventDestination
err = json.NewDecoder(resp.Body).Decode(&eventdestination)
if err != nil {
return nil, err
}
eventdestination.SetClient(c)
return &eventdestination, nil
}
// subscriptionPayload is the payload to create the event subscription
type subscriptionPayload struct {
Destination string `json:"Destination"`
EventTypes []EventType `json:"EventTypes"`
HTTPHeaders map[string]string `json:"HttpHeaders,omitempty"`
Oem interface{} `json:"Oem,omitempty"`
Protocol EventDestinationProtocol `json:"Protocol,omitempty"`
Context string `json:"Context,omitempty"`
}
// validateCreateEventDestinationParams will validate
// CreateEventDestination parameters
func validateCreateEventDestinationParams(
uri string,
destination string,
eventTypes []EventType,
) error {
// validate uri
if len(strings.TrimSpace(uri)) == 0 {
return fmt.Errorf("uri should not be empty")
}
// validate destination
if len(strings.TrimSpace(destination)) == 0 {
return fmt.Errorf("empty destination is not valid")
}
if !strings.HasPrefix(destination, "http") {
return fmt.Errorf("destination should start with http")
}
// validate event types
if len(eventTypes) == 0 {
return fmt.Errorf("at least one event type for subscription should be defined")
}
for _, et := range eventTypes {
if !et.IsValidEventType() {
return fmt.Errorf("invalid event type")
}
}
return nil
}
// CreateEventDestination will create a EventDestination instance.
// destination should contain the URL of the destination for events to be sent.
// eventTypes is a list of EventType to subscribe to.
// httpHeaders is optional and gives the opportunity to specify any arbitrary
// HTTP headers required for the event POST operation.
// protocol should be the communication protocol of the event endpoint,
// usually RedfishEventDestinationProtocol
// context is a required client-supplied string that is sent with the event notifications
// oem is optional and gives the opportunity to specify any OEM specific properties,
// it should contain the vendor specific struct that goes inside the Oem session.
// It returns the new subscription URI if the event subscription is created
// with success or any error encountered.
func CreateEventDestination(
c common.Client,
uri string,
destination string,
eventTypes []EventType,
httpHeaders map[string]string,
protocol EventDestinationProtocol,
context string,
oem interface{},
) (string, error) {
// validate input parameters
err := validateCreateEventDestinationParams(
uri,
destination,
eventTypes,
)
if err != nil {
return "", err
}
// create subscription payload
s := &subscriptionPayload{
Destination: destination,
EventTypes: eventTypes,
Protocol: protocol,
Context: context,
}
// HTTP headers
if len(httpHeaders) > 0 {
s.HTTPHeaders = httpHeaders
}
// Oem
if oem != nil {
s.Oem = oem
}
resp, err := c.Post(uri, s)
if err != nil {
return "", err
}
defer resp.Body.Close()
// return subscription link from returned location
subscriptionLink := resp.Header.Get("Location")
if urlParser, err := url.ParseRequestURI(subscriptionLink); err == nil {
subscriptionLink = urlParser.RequestURI()
}
return subscriptionLink, nil
}
// DeleteEventDestination will delete a EventDestination.
func DeleteEventDestination(c common.Client, uri string) (err error) {
// validate uri
if len(strings.TrimSpace(uri)) == 0 {
return fmt.Errorf("uri should not be empty")
}
_, err = c.Delete(uri)
//defer resp.Body.Close()
return err
}
// ListReferencedEventDestinations gets the collection of EventDestination from
// a provided reference.
func ListReferencedEventDestinations(c common.Client, link string) ([]*EventDestination, error) {
var result []*EventDestination
if link == "" {
return result, nil
}
links, err := common.GetCollection(c, link)
if err != nil {
return result, err
}
for _, eventdestinationLink := range links.ItemLinks {
eventdestination, err := GetEventDestination(c, eventdestinationLink)
if err != nil {
return result, err
}
result = append(result, eventdestination)
}
return result, nil
}
// HTTPHeaderProperty shall a names and value of an HTTP header to be included
// with every event POST to the Event Destination.
type HTTPHeaderProperty map[string][]string
// SNMPSettings is shall contain the settings for an SNMP event
// destination.
type SNMPSettings struct {
// AuthenticationKey is used for SNMPv3 authentication. The value shall
// be `null` in responses.
AuthenticationKey string
// AuthenticationProtocol is This property shall contain the SNMPv3
// authentication protocol.
AuthenticationProtocol SNMPAuthenticationProtocols
// EncryptionKey is This property shall contain the key for SNMPv3
// encryption. The value shall be `null` in responses.
EncryptionKey string
// EncryptionProtocol is This property shall contain the SNMPv3
// encryption protocol.
EncryptionProtocol SNMPEncryptionProtocols
// TrapCommunity is This property shall contain the SNMP trap community
// string. The value shall be `null` in responses.
TrapCommunity string
}