/
schedule.go
200 lines (172 loc) · 5.5 KB
/
schedule.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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"reflect"
"github.com/stmcginnis/gofish/common"
)
// DayOfWeek is the days of the week.
type DayOfWeek string
const (
// MondayDayOfWeek Monday.
MondayDayOfWeek DayOfWeek = "Monday"
// TuesdayDayOfWeek Tuesday.
TuesdayDayOfWeek DayOfWeek = "Tuesday"
// WednesdayDayOfWeek Wednesday.
WednesdayDayOfWeek DayOfWeek = "Wednesday"
// ThursdayDayOfWeek Thursday.
ThursdayDayOfWeek DayOfWeek = "Thursday"
// FridayDayOfWeek Friday.
FridayDayOfWeek DayOfWeek = "Friday"
// SaturdayDayOfWeek Saturday.
SaturdayDayOfWeek DayOfWeek = "Saturday"
// SundayDayOfWeek Sunday.
SundayDayOfWeek DayOfWeek = "Sunday"
// EveryDayOfWeek shall be the only member in the array.
EveryDayOfWeek DayOfWeek = "Every"
)
// MonthOfYear is the months of the year.
type MonthOfYear string
const (
// JanuaryMonthOfYear January.
JanuaryMonthOfYear MonthOfYear = "January"
// FebruaryMonthOfYear February.
FebruaryMonthOfYear MonthOfYear = "February"
// MarchMonthOfYear March.
MarchMonthOfYear MonthOfYear = "March"
// AprilMonthOfYear April.
AprilMonthOfYear MonthOfYear = "April"
// MayMonthOfYear May.
MayMonthOfYear MonthOfYear = "May"
// JuneMonthOfYear June.
JuneMonthOfYear MonthOfYear = "June"
// JulyMonthOfYear July.
JulyMonthOfYear MonthOfYear = "July"
// AugustMonthOfYear August.
AugustMonthOfYear MonthOfYear = "August"
// SeptemberMonthOfYear September.
SeptemberMonthOfYear MonthOfYear = "September"
// OctoberMonthOfYear October.
OctoberMonthOfYear MonthOfYear = "October"
// NovemberMonthOfYear November.
NovemberMonthOfYear MonthOfYear = "November"
// DecemberMonthOfYear December.
DecemberMonthOfYear MonthOfYear = "December"
// EveryMonthOfYear shall be the only member in the array.
EveryMonthOfYear MonthOfYear = "Every"
)
// Schedule shall schedule a series of occurrences.
type Schedule struct {
common.Entity
// EnabledDaysOfMonth shall contain the days of the month when scheduled occurrences are enabled, for enabled days
// of week and months of year. If the array contains a single value of '0', or if the property is not present, all
// days of the month shall be enabled.
EnabledDaysOfMonth []int
// EnabledDaysOfWeek shall be enabled.
EnabledDaysOfWeek []DayOfWeek
// EnabledIntervals shall be an ISO 8601 conformant interval specifying when occurrences are enabled.
EnabledIntervals []string
// EnabledMonthsOfYear shall contain the months of the year when scheduled occurrences are enabled, for enabled
// days of week and days of month. If not present, all months of the year shall be enabled.
EnabledMonthsOfYear []MonthOfYear
// InitialStartTime shall contain the date and time when the initial occurrence is scheduled to occur.
InitialStartTime string
// Lifetime shall contain a Redfish Duration that describes the time after provisioning when the schedule expires.
Lifetime string
// MaxOccurrences shall contain the maximum number of scheduled occurrences.
MaxOccurrences int
// RecurrenceInterval shall contain the duration between consecutive occurrences.
RecurrenceInterval string
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
}
// UnmarshalJSON unmarshals a Schedule object from the raw JSON.
func (schedule *Schedule) UnmarshalJSON(b []byte) error {
type temp Schedule
var t struct {
temp
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*schedule = Schedule(t.temp)
// Extract the links to other entities for later
// This is a read/write object, so we need to save the raw object data for later
schedule.rawData = b
return nil
}
// Update commits updates to this object's properties to the running system.
func (schedule *Schedule) Update() error {
// Get a representation of the object's original state so we can find what
// to update.
original := new(Schedule)
original.UnmarshalJSON(schedule.rawData)
readWriteFields := []string{
"EnabledDaysOfMonth",
"EnabledDaysOfWeek",
"EnabledIntervals",
"EnabledMonthsOfYear",
"InitialStartTime",
"Lifetime",
"MaxOccurrences",
"RecurrenceInterval",
}
originalElement := reflect.ValueOf(original).Elem()
currentElement := reflect.ValueOf(schedule).Elem()
return schedule.Entity.Update(originalElement, currentElement, readWriteFields)
}
// GetSchedule will get a Schedule instance from the service.
func GetSchedule(c common.Client, uri string) (*Schedule, error) {
resp, err := c.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var schedule Schedule
err = json.NewDecoder(resp.Body).Decode(&schedule)
if err != nil {
return nil, err
}
schedule.SetClient(c)
return &schedule, nil
}
// ListReferencedSchedules gets the collection of Schedule from
// a provided reference.
func ListReferencedSchedules(c common.Client, link string) ([]*Schedule, error) {
var result []*Schedule
if link == "" {
return result, nil
}
type GetResult struct {
Item *Schedule
Link string
Error error
}
ch := make(chan GetResult)
collectionError := common.NewCollectionError()
get := func(link string) {
schedule, err := GetSchedule(c, link)
ch <- GetResult{Item: schedule, Link: link, Error: err}
}
go func() {
err := common.CollectList(get, c, link)
if err != nil {
collectionError.Failures[link] = err
}
close(ch)
}()
for r := range ch {
if r.Error != nil {
collectionError.Failures[r.Link] = r.Error
} else {
result = append(result, r.Item)
}
}
if collectionError.Empty() {
return result, nil
}
return result, collectionError
}