forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageregistry.go
246 lines (217 loc) · 7.7 KB
/
messageregistry.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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"fmt"
"strings"
"github.com/stmcginnis/gofish/common"
)
const MessageIDSectionLength = 4
// MessageRegistryMessage is a message contained in the message registry.
type MessageRegistryMessage struct {
// Description is a short description of how and when to use this message.
Description string
// Message is the actual message.
// This property shall contain the message to display. If a %integer is
// included in part of the string, it shall represent a string substitution
// for any MessageArgs that accompany the message, in order.
Message string
// Severity property shall contain the severity of the condition resulting in
// the message, as defined in the Status clause of the Redfish Specification.
// This property has been deprecated in favor of MessageSeverity, which ties
// the values to the enumerations defined for the Health property within Status.
Severity string
// MessageSeverity is the severity of the message. This property shall contain
// the severity of the message.
MessageSeverity string
// NumberOfArgs is the number of arguments in the message.
// This property shall contain the number of arguments that are substituted
// for the locations marked with %<integer> in the message.
NumberOfArgs int
// ParamTypes are the MessageArg types, in order, for the message.
ParamTypes []string
// Resolution is used to provide suggestions on how to resolve the situation
// that caused the error.
Resolution string
// Oem shall contain the OEM extensions. All values for properties that
// this object contains shall conform to the Redfish Specification
// described requirements.
Oem interface{}
}
// MessageRegistry schema describes all message registries.
// It represents the properties for the message registries themselves.
type MessageRegistry struct {
common.Entity
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// Description provides a description of this resource.
Description string
// RegistryPrefix is the single-word prefix that is used in forming and decoding MessageIds.
RegistryPrefix string
// RegistryVersion is the message registry version in the middle portion of a MessageId.
RegistryVersion string
// Language is the RFC5646-conformant language code for the message registry.
Language string
// OwningEntity ins the organization or company that publishes this message registry.
OwningEntity string
// Messages are the messages for the message registry.
Messages map[string]MessageRegistryMessage
}
// GetMessageRegistry will get a MessageRegistry instance from the Redfish service.
func GetMessageRegistry(
c common.Client,
uri string,
) (*MessageRegistry, error) {
var messageRegistry MessageRegistry
return &messageRegistry, messageRegistry.Get(c, uri, &messageRegistry)
}
// ListReferencedMessageRegistries gets the collection of MessageRegistry.
func ListReferencedMessageRegistries(c common.Client, link string) ([]*MessageRegistry, error) {
var result []*MessageRegistry
links, err := common.GetCollection(c, link)
if err != nil {
return nil, err
}
// TODO: Look at what to do to make parallel
for _, sLink := range links.ItemLinks {
mrf, err := GetMessageRegistryFile(c, sLink)
if err != nil {
return nil, err
}
// get message registry from all location
for _, location := range mrf.Location {
mr, err := GetMessageRegistry(c, location.URI)
if err != nil {
return nil, err
}
result = append(result, mr)
}
}
return result, nil
}
// ListReferencedMessageRegistriesByLanguage gets the collection of MessageRegistry.
// language is the RFC5646-conformant language code for the message registry.
func ListReferencedMessageRegistriesByLanguage(c common.Client, link, language string) ([]*MessageRegistry, error) {
language = strings.TrimSpace(language)
if language == "" {
return nil, fmt.Errorf("received empty language")
}
// TODO: Looks at what to do to make parallel.
var result []*MessageRegistry
links, err := common.GetCollection(c, link)
if err != nil {
return nil, err
}
for _, sLink := range links.ItemLinks {
mrf, err := GetMessageRegistryFile(c, sLink)
if err != nil {
return nil, err
}
// get message registry by language
for _, location := range mrf.Location {
if location.Language == language {
mr, err := GetMessageRegistry(c, location.URI)
if err != nil {
return nil, err
}
result = append(result, mr)
}
}
}
return result, nil
}
// GetMessageRegistryByLanguage gets the message registry by language.
// registry is used to identify the correct Message Registry
// file (MessageRegistryFile.Registry) and it shall contain the
// Message Registry name and it major and minor versions, as defined
// by the Redfish Specification.
// language is the RFC5646-conformant language code for the message registry.
func GetMessageRegistryByLanguage(
c common.Client,
link string,
registry string,
language string,
) (*MessageRegistry, error) {
registry = strings.TrimSpace(registry)
if registry == "" {
return nil, fmt.Errorf("received empty registry")
}
language = strings.TrimSpace(language)
if language == "" {
return nil, fmt.Errorf("received empty language")
}
// TODO: Look at what to do to make parallel
links, err := common.GetCollection(c, link)
if err != nil {
return nil, err
}
for _, sLink := range links.ItemLinks {
s, err := GetMessageRegistryFile(c, sLink)
if err != nil {
return nil, err
}
// search for the correct registry
if s.Registry == registry {
// search for the correct location
for _, location := range s.Location {
if location.Language == language {
return GetMessageRegistry(c, location.URI)
}
}
}
}
return nil, fmt.Errorf("message registry not found")
}
// GetMessageFromMessageRegistryByLanguage tries to find and get the message
// from the informed messageID.
// messageID is the key used to find the registry, version and message:
// Example of messageID: Alert.1.0.LanDisconnect
//
// - The segment before the 1st period is the Registry Name (Registry Prefix): Alert
// - 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
//
// language is the RFC5646-conformant language code for the message registry.
// Example of language: en
func GetMessageFromMessageRegistryByLanguage(
c common.Client,
link string,
messageID string,
language string,
) (*MessageRegistryMessage, error) {
messageID = strings.TrimSpace(messageID)
if messageID == "" {
return nil, fmt.Errorf("received empty messageID")
}
language = strings.TrimSpace(language)
if language == "" {
return nil, fmt.Errorf("received empty language")
}
// split messageID
messageIDSplitted := strings.Split(messageID, ".")
// validate messageID
if len(messageIDSplitted) != MessageIDSectionLength {
return nil, fmt.Errorf("received invalid messageID %s", messageID)
}
// get information from the messageID
registryPrefix := messageIDSplitted[0]
registryMajorVersion := messageIDSplitted[1]
registryMinorVersion := messageIDSplitted[2]
registryMajorMinorVersion := registryMajorVersion + "." + registryMinorVersion
registryMessageKey := messageIDSplitted[3]
allMessageRegistryByLanguage, err := ListReferencedMessageRegistriesByLanguage(c, link, language)
if err != nil {
return nil, err
}
for _, mr := range allMessageRegistryByLanguage {
if mr.RegistryPrefix == registryPrefix &&
strings.HasPrefix(mr.RegistryVersion, registryMajorMinorVersion) {
if m, ok := mr.Messages[registryMessageKey]; ok {
return &m, nil
}
}
}
return nil, fmt.Errorf("message not found")
}