-
Notifications
You must be signed in to change notification settings - Fork 2
/
event.go
122 lines (98 loc) · 3.89 KB
/
event.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
/*
* Copyright 2020, 2021, 2022 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package event
import (
"fmt"
"regexp"
sf "github.com/NearNodeFlash/nnf-ec/pkg/rfsf/pkg/models"
)
const (
MaxNumEvents = 256
)
type Events = []Event
type Event struct {
// The identifier that uniquely identifies the resource within the collection of similar resources.
Id string `json:"Id"`
// The time the event occurred.
Timestamp string `json:"Timestamp,omitempty"`
// The identifier that correlates events with the same root cause. If 0 , no other event is related to this event.
GroupId string `json:"GroupId,omitempty"`
// The identifier for the member within the collection.
MemberId string `json:"MemberId,omitempty"`
// The human-readable event message.
Message string `json:"Message,omitempty"`
// An array of message arguments that are substituted for the arguments in the message when looked up in the message registry.
MessageArgs []string `json:"MessageArgs,omitempty"`
// The identifier for the message.
MessageId MessageId `json:"MessageId,omitempty"`
// The severity of the message in this event.
MessageSeverity sf.ResourceHealth `json:"MessageSeverity,omitempty"`
// A link to the resource or object that originated the condition that caused the event to be generated.
OriginOfCondition string `json:"OriginOfCondition,omitempty"`
}
func (e Event) OdataId() string {
return fmt.Sprintf("redfish/v1/EventService/Events/%s", e.Id)
}
func (e Event) Is(event Event) bool {
return e.MessageId == event.MessageId
}
func (e Event) CopyInto(model *sf.EventV161Event) {
model.EventId = e.Id
model.EventTimestamp = e.Timestamp
model.EventGroupId = e.GroupId
model.MemberId = e.MemberId
model.Message = e.Message
model.MessageArgs = e.MessageArgs
model.MessageSeverity = e.MessageSeverity
if len(e.OriginOfCondition) != 0 {
model.OriginOfCondition = sf.OdataV4IdRef{OdataId: e.OriginOfCondition}
}
}
func (e Event) Args(args ...*string) error {
if len(args) > len(e.MessageArgs) {
return fmt.Errorf("Requested arguments exceeds supplied arguments")
}
for idx, arg := range args {
*arg = e.MessageArgs[idx]
}
return nil
}
type Resource interface {
OdataId() string
}
// MessageId is an encoding of the Message Registry (Registry Prefix), Message Version, and Message Identifer
// The format is [Registry Prefix].[Major Version].[Minor Version].[Message Identifier]
//
// Example:
// With a MessageId of "Alert.1.0.LanDisconnect"
// The segment before the 1st period is the Message Registry Prefix: "Alert"
// The segment between the 1st and 3rd period is the Registry Version: "1.0"
// The segment between the 1st and 2nd period is the Major Version: "1"
// The segment between the 2nd and 3rd period is the Minor Version: "0"
// The segment after the 3rd period is the Message Identifier in the Registry: "LanDisconnect"
//
type MessageId string
func (m MessageId) RegistryPrefix() string { return m.segment(0) }
func (m MessageId) RegistryVersion() string { return m.segment(1) }
func (m MessageId) Identifier() string { return m.segment(2) }
func (m MessageId) String() string { return string(m) }
func (m MessageId) segment(idx int) string {
re := regexp.MustCompile(`^(\w+).(\d+.\d+).(\w+)`)
return re.FindStringSubmatch(m.String())[idx]
}