forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redundancy.go
154 lines (130 loc) · 4.62 KB
/
redundancy.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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"reflect"
"github.com/stmcginnis/gofish/common"
)
// RedundancyMode is the redundancy mode.
type RedundancyMode string
const (
// FailoverRedundancyMode Failure of one unit will automatically cause
// its functions to be taken over by a standby or offline unit in the
// redundancy set.
FailoverRedundancyMode RedundancyMode = "Failover"
// NMRedundancyMode Multiple units are available and active such that
// normal operation will continue if one or more units fail.
NMRedundancyMode RedundancyMode = "N+m"
// SharingRedundancyMode Multiple units contribute or share such that
// operation will continue, but at a reduced capacity, if one or more
// units fail.
SharingRedundancyMode RedundancyMode = "Sharing"
// SparingRedundancyMode One or more spare units are available to take
// over the function of a failed unit, but takeover is not automatic.
SparingRedundancyMode RedundancyMode = "Sparing"
// NotRedundantRedundancyMode The subsystem is not configured in a
// redundancy mode, either due to configuration or the functionality has
// been disabled by the user.
NotRedundantRedundancyMode RedundancyMode = "NotRedundant"
)
// Redundancy represents the Redundancy element property.
// All values for resources described by this schema shall comply to the
// requirements as described in the Redfish specification. The value of
// this string shall be of the format for the reserved word *Redundancy*.
type Redundancy struct {
common.Entity
// MaxNumSupported shall contain the maximum number of members allowed in
// the redundancy group.
MaxNumSupported int
// MemberID value of this string shall uniquely identify the member within
// the collection.
MemberID string `json:"MemberId"`
// MinNumNeeded shall contain the minimum
// number of members allowed in the redundancy group for the current
// redundancy mode to still be fault tolerant.
MinNumNeeded int
// Mode shall contain the information about the redundancy mode of this
// subsystem.
Mode RedundancyMode
// RedundancyEnabled shall be a boolean indicating whether the redundancy is
// enabled.
RedundancyEnabled bool
// RedundancySet shall contain the ids of components that are part of this
// redundancy set. The id values may or may not be dereferenceable.
redundancySet []string
// RedundancySetCount is the number of RedundancySets.
RedundancySetCount int `json:"RedundancySet@odata.count"`
// Status shall contain any status or health properties of the resource.
Status common.Status
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
}
// UnmarshalJSON unmarshals a Redundancy object from the raw JSON.
func (redundancy *Redundancy) UnmarshalJSON(b []byte) error {
type temp Redundancy
var t struct {
temp
RedundancySet common.Links
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
// Extract the links to other entities for later
*redundancy = Redundancy(t.temp)
redundancy.redundancySet = t.RedundancySet.ToStrings()
// This is a read/write object, so we need to save the raw object data for later
redundancy.rawData = b
return nil
}
// Update commits updates to this object's properties to the running system.
func (redundancy *Redundancy) Update() error {
// Get a representation of the object's original state so we can find what
// to update.
original := new(Redundancy)
original.UnmarshalJSON(redundancy.rawData)
readWriteFields := []string{
"Mode",
"RedundancyEnabled",
}
originalElement := reflect.ValueOf(original).Elem()
currentElement := reflect.ValueOf(redundancy).Elem()
return redundancy.Entity.Update(originalElement, currentElement, readWriteFields)
}
// GetRedundancy will get a Redundancy instance from the service.
func GetRedundancy(c common.Client, uri string) (*Redundancy, error) {
resp, err := c.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var redundancy Redundancy
err = json.NewDecoder(resp.Body).Decode(&redundancy)
if err != nil {
return nil, err
}
redundancy.SetClient(c)
return &redundancy, nil
}
// ListReferencedRedundancies gets the collection of Redundancy from
// a provided reference.
func ListReferencedRedundancies(c common.Client, link string) ([]*Redundancy, error) {
var result []*Redundancy
if link == "" {
return result, nil
}
links, err := common.GetCollection(c, link)
if err != nil {
return result, err
}
for _, redundancyLink := range links.ItemLinks {
redundancy, err := GetRedundancy(c, redundancyLink)
if err != nil {
return result, err
}
result = append(result, redundancy)
}
return result, nil
}