-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmgmtclass.go
242 lines (205 loc) · 6.85 KB
/
mgmtclass.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
package cobblerclient
import (
"fmt"
"reflect"
"time"
)
type MgmtClass struct {
Item `mapstructure:",squash"`
// Mgmtclass specific fields
IsDefiniton bool `mapstructure:"is_definition"`
Params map[string]string `mapstructure:"params"`
ClassName string `mapstructure:"class_name"`
Files []string `mapstructure:"files"`
Packages []string `mapstructure:"packages"`
}
func NewMgmtClass() MgmtClass {
return MgmtClass{
Item: NewItem(),
Params: make(map[string]string),
Files: make([]string, 0),
Packages: make([]string, 0),
}
}
func convertRawMgmtClass(name string, xmlrpcResult interface{}) (*MgmtClass, error) {
var mgmtclass MgmtClass
if xmlrpcResult == "~" {
return nil, fmt.Errorf("mgmtclass %s not found", name)
}
decodeResult, err := decodeCobblerItem(xmlrpcResult, &mgmtclass)
if err != nil {
return nil, err
}
// Now clean the Value structs
decodedMgmtClass := decodeResult.(*MgmtClass)
err = sanitizeValueMapStruct(&decodedMgmtClass.KernelOptions)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedMgmtClass.KernelOptionsPost)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedMgmtClass.AutoinstallMeta)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedMgmtClass.FetchableFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedMgmtClass.BootFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedMgmtClass.TemplateFiles)
if err != nil {
return nil, err
}
err = sanitizeValueMapStruct(&decodedMgmtClass.MgmtParameters)
if err != nil {
return nil, err
}
err = sanitizeValueSliceStruct(&decodedMgmtClass.Owners)
if err != nil {
return nil, err
}
err = sanitizeValueSliceStruct(&decodedMgmtClass.MgmtClasses)
return decodedMgmtClass, nil
}
func convertRawMgmtClassList(xmlrpcResult interface{}) ([]*MgmtClass, error) {
var mgmtclasses []*MgmtClass
for _, d := range xmlrpcResult.([]interface{}) {
mgmtclass, err := convertRawMgmtClass("unknown", d)
if err != nil {
return nil, err
}
mgmtclass.Meta = ItemMeta{
IsFlattened: false,
IsResolved: false,
}
mgmtclasses = append(mgmtclasses, mgmtclass)
}
return mgmtclasses, nil
}
// GetMgmtClasses returns all mgmtclasses in Cobbler.
func (c *Client) GetMgmtClasses() ([]*MgmtClass, error) {
result, err := c.Call("get_mgmtclasses", "-1", c.Token)
if err != nil {
return nil, err
}
return convertRawMgmtClassList(result)
}
// GetMgmtClass returns a single mgmtclass obtained by its name.
func (c *Client) GetMgmtClass(name string, flattened, resolved bool) (*MgmtClass, error) {
result, err := c.getConcreteItem("get_mgmtclass", name, flattened, resolved)
if err != nil {
return nil, err
}
mgmtclass, err := convertRawMgmtClass(name, result)
if err != nil {
return nil, err
}
mgmtclass.Meta = ItemMeta{
IsFlattened: flattened,
IsResolved: resolved,
}
return mgmtclass, nil
}
// CreateMgmtClass creates a mgmtclass.
func (c *Client) CreateMgmtClass(mgmtclass MgmtClass) (*MgmtClass, error) {
// Make sure a mgmtclass with the same name does not already exist
if _, err := c.GetMgmtClass(mgmtclass.Name, false, false); err == nil {
return nil, fmt.Errorf("a MgmtClass with the name %s already exists", mgmtclass.Name)
}
result, err := c.Call("new_mgmtclass", c.Token)
if err != nil {
return nil, err
}
newID := result.(string)
item := reflect.ValueOf(&mgmtclass).Elem()
if err := c.updateCobblerFields("mgmtclass", item, newID); err != nil {
return nil, err
}
if err := c.SaveMgmtClass(newID, "new"); err != nil {
return nil, err
}
return c.GetMgmtClass(mgmtclass.Name, false, false)
}
// UpdateMgmtClass updates a single MgmtClass.
func (c *Client) UpdateMgmtClass(mgmtclass *MgmtClass) error {
item := reflect.ValueOf(mgmtclass).Elem()
id, err := c.GetItemHandle("mgmtclass", mgmtclass.Name)
if err != nil {
return err
}
if err := c.updateCobblerFields("mgmtclass", item, id); err != nil {
return err
}
if err := c.SaveMgmtClass(id, "bypass"); err != nil {
return err
}
return nil
}
// DeleteMgmtClass deletes a single MgmtClass by its name.
func (c *Client) DeleteMgmtClass(name string) error {
return c.DeleteMgmtClassRecursive(name, false)
}
// DeleteMgmtClassRecursive deletes a single MgmtClass by its name with the option to do so recursively.
func (c *Client) DeleteMgmtClassRecursive(name string, recursive bool) error {
_, err := c.Call("remove_mgmtclass", name, c.Token, recursive)
return err
}
// ListMgmtClassNames returns a list of all managementclass names currently available in Cobbler.
func (c *Client) ListMgmtClassNames() ([]string, error) {
return c.GetItemNames("mgmtclass")
}
// FindMgmtClass searches for one or more managementclasses by any of its attributes.
func (c *Client) FindMgmtClass(criteria map[string]interface{}) ([]*MgmtClass, error) {
result, err := c.Call("find_mgmtclass", criteria, true, c.Token)
if err != nil {
return nil, err
}
return convertRawMgmtClassList(result)
}
// FindMgmtClassNames searches for one or more managementclasses by any of its attributes.
func (c *Client) FindMgmtClassNames(criteria map[string]interface{}) ([]string, error) {
resultUnmarshalled, err := c.Call("find_mgmtclass", criteria, false, c.Token)
return returnStringSlice(resultUnmarshalled, err)
}
// GetMgmtClassHandle gets the internal ID of a Cobbler item.
func (c *Client) GetMgmtClassHandle(name string) (string, error) {
res, err := c.Call("get_mgmtclass_handle", name, c.Token)
return returnString(res, err)
}
// CopyMgmtClass copies a given managementclass server side with a new name.
func (c *Client) CopyMgmtClass(objectId, newName string) error {
_, err := c.Call("copy_mgmtclass", objectId, newName, c.Token)
return err
}
// RenameMgmtClass renames a managementclass with a given object id.
func (c *Client) RenameMgmtClass(objectId, newName string) error {
_, err := c.Call("rename_mgmtclass", objectId, newName, c.Token)
return err
}
// GetMgmtClassesSince returns all managementclasses which were created after the specified date.
func (c *Client) GetMgmtClassesSince(mtime time.Time) ([]*MgmtClass, error) {
result, err := c.Call("get_mgmtclasses_since", float64(mtime.Unix()))
if err != nil {
return nil, err
}
return convertRawMgmtClassList(result)
}
// GetMgmtClassAsRendered returns the datastructure after it has passed through Cobblers inheritance structure.
func (c *Client) GetMgmtClassAsRendered(name string) (map[string]interface{}, error) {
result, err := c.Call("get_mgmtclass_as_rendered", name, c.Token)
if err != nil {
return nil, err
}
return result.(map[string]interface{}), err
}
// SaveMgmtClass saves all changes performed via XML-RPC to disk on the server side.
func (c *Client) SaveMgmtClass(objectId, editmode string) error {
_, err := c.Call("save_mgmtclass", objectId, c.Token, editmode)
return err
}