-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
item.go
285 lines (258 loc) · 10.3 KB
/
item.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package cobblerclient
import (
"errors"
"strings"
)
const inherit string = "<<inherit>>"
const none string = "none"
// Value is a helper struct that wraps the multi-typed values being returned from the Cobbler API.
type Value[T any] struct {
// Data contains the unresolved or resolved data of the attribute. This is not set in case the value is flattened.
Data T
// FlattenedValue contains the unresolved or resolved flattened data of the Attribute. This is not set in case the
// value is not flattened.
FlattenedValue string
// IsInherited is a flag that signals if the attribute is inherited or not. If this flag is true then both Data and
// FlattenedValue are not set.
IsInherited bool
// RawData contains the data as received by the API. This field is not evaluated when updating an Item via the
// API.
RawData interface{}
}
type ItemMeta struct {
IsFlattened bool
IsResolved bool
// This flag signals if the item was modified by a called method server-side.
IsDirty bool
}
// Item general fields
type Item struct {
// Meta information about an item
Meta ItemMeta `cobbler:"noupdate"`
// Item fields
Parent string `mapstructure:"parent"`
Depth int `mapstructure:"depth" cobbler:"noupdate"`
Children []string `mapstructure:"children" cobbler:"noupdate"`
CTime float64 `mapstructure:"ctime" cobbler:"noupdate"`
MTime float64 `mapstructure:"mtime" cobbler:"noupdate"`
Uid string `mapstructure:"uid" cobbler:"noupdate"`
Name string `mapstructure:"name"`
Comment string `mapstructure:"comment"`
KernelOptions Value[map[string]interface{}] `mapstructure:"kernel_options"`
KernelOptionsPost Value[map[string]interface{}] `mapstructure:"kernel_options_post"`
AutoinstallMeta Value[map[string]interface{}] `mapstructure:"autoinstall_meta"`
FetchableFiles Value[map[string]interface{}] `mapstructure:"fetchable_files"`
BootFiles Value[map[string]interface{}] `mapstructure:"boot_files"`
TemplateFiles Value[map[string]interface{}] `mapstructure:"template_files"`
Owners Value[[]string] `mapstructure:"owners"`
MgmtClasses Value[[]string] `mapstructure:"mgmt_classes"`
MgmtParameters Value[map[string]interface{}] `mapstructure:"mgmt_parameters"`
}
// NewItem is a method to initialize the struct with the values that the server-side would internally use. Using this is
// important since the client overwrites all fields with those chosen locally inside the item.
func NewItem() Item {
return Item{
AutoinstallMeta: Value[map[string]interface{}]{
Data: make(map[string]interface{}),
},
BootFiles: Value[map[string]interface{}]{
Data: make(map[string]interface{}),
},
Children: make([]string, 0),
FetchableFiles: Value[map[string]interface{}]{
Data: make(map[string]interface{}),
},
KernelOptions: Value[map[string]interface{}]{
Data: make(map[string]interface{}),
},
KernelOptionsPost: Value[map[string]interface{}]{
Data: make(map[string]interface{}),
},
Owners: Value[[]string]{
Data: make([]string, 0),
IsInherited: true,
},
MgmtClasses: Value[[]string]{
Data: make([]string, 0),
},
MgmtParameters: Value[map[string]interface{}]{
IsInherited: true,
},
TemplateFiles: Value[map[string]interface{}]{
Data: make(map[string]interface{}),
},
}
}
// ModifyItem is a generic method to modify items. Changes made with this method are not persisted until a call to
// SaveItem or one of its other concrete methods.
func (c *Client) ModifyItem(what, objectId, attribute string, arg interface{}) error {
_, err := c.Call("modify_item", what, objectId, attribute, arg, c.Token)
return err
}
// ModifyItemInPlace attempts to recreate the functionality of the "in_place" parameter for the "xapi_object_edit"
// XML-RPC method.
func (c *Client) ModifyItemInPlace(what, name, attribute string, value map[string]interface{}) error {
itemKey := []string{
"autoinstall_meta",
"kernel_options",
"kernel_options_post",
"template_files",
"boot_files",
"fetchable_files",
"params",
}
if !stringInSlice(attribute, itemKey) {
return errors.New("invalid attribute for in-place modification")
}
rawItem, err := c.GetItem(what, name, false, false)
if err != nil {
return err
}
newMapInterface, keyExists := rawItem[attribute]
if !keyExists {
return errors.New("attribute not found in ")
}
newMap, castSuccessful := newMapInterface.(map[string]interface{})
if !castSuccessful {
return errors.New("failed to cast to map[string]interface{}")
}
for key, mapValue := range value {
if strings.HasPrefix(key, "~") && len(key) > 1 {
delete(newMap, key[1:])
} else {
newMap[key] = mapValue
}
}
itemHandle, err := c.GetItemHandle(what, name)
if err != nil {
return err
}
err = c.ModifyItem(what, itemHandle, attribute, newMap)
if err != nil {
return err
}
return c.SaveItem(what, itemHandle, c.Token, "bypass")
}
// GetItemNames returns the list of names for a specified object type present inside Cobbler.
func (c *Client) GetItemNames(what string) ([]string, error) {
resultUnmarshalled, err := c.Call("get_item_names", what)
return returnStringSlice(resultUnmarshalled, err)
}
// GetItemResolvedValue retrieves the value of a single attribute of a single item which was passed through the
// inheritance chain of Cobbler.
func (c *Client) GetItemResolvedValue(itemUuid string, attribute string) error {
_, err := c.Call("get_item_resolved_value", itemUuid, attribute)
return err
}
// GetItem retrieves a single item from the database. An empty map means that the item could not be found.
func (c *Client) GetItem(what string, name string, flatten, resolved bool) (map[string]interface{}, error) {
unmarshalledResult, err := c.Call("get_item", what, name, flatten, resolved)
if err != nil {
return nil, err
}
marshalledResult, marshallSuccessful := unmarshalledResult.(map[string]interface{})
if !marshallSuccessful {
notFoundMarker, marshallSuccessful := unmarshalledResult.(string)
if !marshallSuccessful {
return nil, errors.New("marshall to map unsuccessful and not-found marker not detected")
}
if notFoundMarker == "~" {
return make(map[string]interface{}), nil
}
}
return marshalledResult, nil
}
func (c *Client) getConcreteItem(method, name string, flattened, resolved bool) (interface{}, error) {
// Verify CachedVersion is set
err := c.setCachedVersion()
if err != nil {
return nil, err
}
// resolved was added with 3.3.3
var result interface{}
if c.CachedVersion.GreaterThan(&CobblerVersion{3, 3, 3}) {
// name, flatten, resolved, token
result, err = c.Call(method, name, flattened, resolved, c.Token)
} else {
// name, flatten, token
result, err = c.Call(method, name, flattened, c.Token)
}
return result, err
}
// FindItems searches for one or more items by any of its attributes.
func (c *Client) FindItems(what string, criteria map[string]interface{}, sortField string, expand bool) ([]interface{}, error) {
unmarshalledResult, err := c.Call("find_items", what, criteria, sortField, expand)
return unmarshalledResult.([]interface{}), err
}
func (c *Client) FindItemNames(what string, criteria map[string]interface{}, sortField string) ([]string, error) {
unmarshalledResult, err := c.Call("find_items", what, criteria, sortField, false)
return returnStringSlice(unmarshalledResult, err)
}
type PageInfo struct {
Page int `mapstructure:"page"`
PrevPage int `mapstructure:"prev_page"`
NextPage int `mapstructure:"next_page"`
Pages []int `mapstructure:"pages"`
NumPages int `mapstructure:"num_pages"`
NumItems int `mapstructure:"num_items"`
StartItem int `mapstructure:"start_item"`
EndItem int `mapstructure:"end_item"`
ItemsPerPage int `mapstructure:"items_per_page"`
ItemsPerPageList []int `mapstructure:"items_per_page_list"`
}
type PagedSearchResult struct {
FoundItems []interface{} `mapstructure:"items"`
PageInfo PageInfo `mapstructure:"pageinfo"`
}
// FindItemsPaged searches for items with the given criteria and returning
func (c *Client) FindItemsPaged(what string, criteria map[string]interface{}, sortField string, page, itemsPerPage int32) (*PagedSearchResult, error) {
var pagedSearchResult PagedSearchResult
unmarshalledResult, err := c.Call("find_items_paged", what, criteria, sortField, page, itemsPerPage, c.Token)
if err != nil {
return nil, err
}
parsedResult, err := decodeCobblerItem(unmarshalledResult, &pagedSearchResult)
if err != nil {
return nil, err
}
return parsedResult.(*PagedSearchResult), err
}
// HasItem checks if an item with the given name exists.
func (c *Client) HasItem(what string, name string) (bool, error) {
result, err := c.Call("has_item", what, name, c.Token)
return result.(bool), err
}
// GetItemHandle gets the internal ID of a Cobbler item.
func (c *Client) GetItemHandle(what, name string) (string, error) {
result, err := c.Call("get_item_handle", what, name, c.Token)
if err != nil {
return "", err
}
return result.(string), err
}
// RenameItem renames an item.
func (c *Client) RenameItem(what, objectId, newName string) error {
_, err := c.Call("rename_item", what, objectId, newName, c.Token)
return err
}
// NewItem creates a new empty item that has to be filled with data. The item does not exist in the database
// before [Client.SaveItem] was called.
func (c *Client) NewItem(what string, isSubobject bool) error {
_, err := c.Call("new_item", what, c.Token, isSubobject)
return err
}
// SaveItem saves the changes done via XML-RPC.
func (c *Client) SaveItem(what, objectId, token, editmode string) error {
_, err := c.Call("save_item", what, objectId, token, editmode)
return err
}
// RemoveItem deletes an item from the Cobbler database.
func (c *Client) RemoveItem(what, name string, recursive bool) error {
_, err := c.Call("remove_item", what, name, c.Token, recursive)
return err
}
// CopyItem duplicates an item on the server with a new name.
func (c *Client) CopyItem(what, objectId, newName string) error {
_, err := c.Call("copy_item", what, objectId, newName, c.Token)
return err
}