forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbios.go
145 lines (124 loc) · 3.48 KB
/
bios.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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"fmt"
"github.com/stmcginnis/gofish/common"
)
// Bios is used to represent BIOS attributes.
// TODO: Sort out how to handle Attributes.
type Bios struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataEtag is the odata etag.
ODataEtag string `json:"@odata.etag"`
// ODataID is the odata identifier.
ODataID string `json:"@odata.id"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// AttributeRegistry is the Resource ID of the Attribute Registry that has
// the system-specific information about a BIOS resource.
AttributeRegistry string
// Attributes are additional properties in this object, and can be looked up
// in the Attribute Registry by their AttributeName.
// Attributes string
// Description provides a description of this resource.
Description string
// changePasswordTarget is the URL to send ChangePassword requests.
changePasswordTarget string
// resetBiosTarget is the URL to send ResetBios requests.
resetBiosTarget string
}
// UnmarshalJSON unmarshals an Bios object from the raw JSON.
func (bios *Bios) UnmarshalJSON(b []byte) error {
type temp Bios
type Actions struct {
ChangePassword struct {
Target string
} `json:"#Bios.ChangePassword"`
ResetBios struct {
Target string
} `json:"#Bios.ResetBios"`
}
var t struct {
temp
Actions Actions
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*bios = Bios(t.temp)
// Extract the links to other entities for later
bios.changePasswordTarget = t.Actions.ChangePassword.Target
bios.resetBiosTarget = t.Actions.ResetBios.Target
return nil
}
// GetBios will get a Bios instance from the service.
func GetBios(c common.Client, uri string) (*Bios, error) {
resp, err := c.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var bios Bios
err = json.NewDecoder(resp.Body).Decode(&bios)
if err != nil {
return nil, err
}
bios.SetClient(c)
return &bios, nil
}
// ListReferencedBioss gets the collection of Bios from a provided reference.
func ListReferencedBioss(c common.Client, link string) ([]*Bios, error) {
var result []*Bios
if link == "" {
return result, nil
}
links, err := common.GetCollection(c, link)
if err != nil {
return result, err
}
for _, biosLink := range links.ItemLinks {
bios, err := GetBios(c, biosLink)
if err != nil {
return result, err
}
result = append(result, bios)
}
return result, nil
}
// ChangePassword shall change the selected BIOS password.
func (bios *Bios) ChangePassword(passwordName string, oldPassword string, newPassword string) error {
if passwordName == "" {
return fmt.Errorf("Password name must be supplied")
}
if oldPassword == "" {
return fmt.Errorf("Existing password must be supplied")
}
if newPassword == "" {
return fmt.Errorf("New password must be supplied")
}
type temp struct {
PasswordName string
OldPassword string
NewPassword string
}
t := temp{
PasswordName: passwordName,
OldPassword: oldPassword,
NewPassword: newPassword,
}
_, err := bios.Client.Post(bios.changePasswordTarget, t)
return err
}
// ResetBios shall perform a reset of the BIOS attributes to their default values.
// A system reset may be required for the default values to be applied. This
// action may impact other resources.
func (bios *Bios) ResetBios() error {
_, err := bios.Client.Post(bios.resetBiosTarget, nil)
return err
}