/
keyservice.go
130 lines (109 loc) · 3.52 KB
/
keyservice.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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"github.com/stmcginnis/gofish/common"
)
// KeyService shall represent the key service properties for a Redfish implementation.
type KeyService struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataEtag is the odata etag.
ODataEtag string `json:"@odata.etag"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// Description provides a description of this resource.
Description string
// NVMeoFKeyPolicies shall contain a link to a resource collection of type KeyPolicyCollection that contains the
// NVMe-oF key policies maintained by this service. The KeyPolicyType property for all members of this collection
// shall contain the value 'NVMeoF'.
nvmeoFKeyPolicies string
// NVMeoFSecrets shall contain a link to a resource collection of type KeyCollection that contains the NVMe-oF keys
// maintained by this service. The KeyType property for all members of this collection shall contain the value
// 'NVMeoF'.
nvmeoFSecrets string
// Oem shall contain the OEM extensions. All values for properties that this object contains shall conform to the
// Redfish Specification-described requirements.
OEM json.RawMessage `json:"Oem"`
}
// UnmarshalJSON unmarshals a KeyService object from the raw JSON.
func (keyservice *KeyService) UnmarshalJSON(b []byte) error {
type temp KeyService
var t struct {
temp
NVMeoFKeyPolicies common.Link
NVMeoFSecrets common.Link
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*keyservice = KeyService(t.temp)
// Extract the links to other entities for later
keyservice.nvmeoFKeyPolicies = t.NVMeoFKeyPolicies.String()
keyservice.nvmeoFSecrets = t.NVMeoFSecrets.String()
return nil
}
// NVMeoFKeyPolicies gets the NVMe-oF key policies maintained by this service.
func (keyservice *KeyService) NVMeoFKeyPolicies() ([]*KeyPolicy, error) {
return ListReferencedKeyPolicys(keyservice.GetClient(), keyservice.nvmeoFKeyPolicies)
}
// NVMeofSecrets gets the NVMe-oF keys maintained by this service.
func (keyservice *KeyService) NVMeoFSecrets() ([]*Key, error) {
return ListReferencedKeys(keyservice.GetClient(), keyservice.nvmeoFSecrets)
}
// GetKeyService will get a KeyService instance from the service.
func GetKeyService(c common.Client, uri string) (*KeyService, error) {
resp, err := c.Get(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var keyservice KeyService
err = json.NewDecoder(resp.Body).Decode(&keyservice)
if err != nil {
return nil, err
}
keyservice.SetClient(c)
return &keyservice, nil
}
// ListReferencedKeyServices gets the collection of KeyService from
// a provided reference.
func ListReferencedKeyServices(c common.Client, link string) ([]*KeyService, error) {
var result []*KeyService
if link == "" {
return result, nil
}
type GetResult struct {
Item *KeyService
Link string
Error error
}
ch := make(chan GetResult)
collectionError := common.NewCollectionError()
get := func(link string) {
keyservice, err := GetKeyService(c, link)
ch <- GetResult{Item: keyservice, 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
}