/
distro.go
142 lines (116 loc) · 4.17 KB
/
distro.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
/*
Copyright 2015 Container Solutions
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cobblerclient
import (
"fmt"
"reflect"
)
// Distro is a created distro.
// Get the fields from cobbler/items/distro.py
type Distro struct {
// These are internal fields and cannot be modified.
Ctime float64 `mapstructure:"ctime" cobbler:"noupdate"` // TODO: convert to time
Depth int `mapstructure:"depth" cobbler:"noupdate"`
Mtime float64 `mapstructure:"mtime" cobbler:"noupdate"` // TODO: convert to time
SourceRepos []string `mapstructure:"source_repos" cobbler:"noupdate"`
TreeBuildTime string `mapstructure:tree_build_time" cobbler:"noupdate"`
UID string `mapstructure:"uid" cobbler:"noupdate"`
Arch string `mapstructure:"arch"`
BootFiles []string `mapstructure:"boot_files"`
BootLoaders []string `mapstructure:"boot_loaders"`
Breed string `mapstructure:"breed"`
Comment string `mapstructure:"comment"`
FetchableFiles []string `mapstructure:"fetchable_files"`
Initrd string `mapstructure:"initrd"`
Kernel string `mapstructure:"kernel"`
KernelOptions []string `mapstructure:"kernel_options"`
KernelOptionsPost []string `mapstructure:"kernel_options_post"`
MGMTClasses []string `mapstructure:"mgmt_classes"`
Name string `mapstructure:"name"`
OSVersion string `mapstructure:"os_version"`
Owners []string `mapstructure:"owners"`
TemplateFiles []string `mapstructure:"template_files"`
}
// GetDistros returns all systems in Cobbler.
func (c *Client) GetDistros() ([]*Distro, error) {
var distros []*Distro
result, err := c.Call("get_distros", "-1", c.Token)
if err != nil {
return nil, err
}
for _, d := range result.([]interface{}) {
var distro Distro
decodedResult, err := decodeCobblerItem(d, &distro)
if err != nil {
return nil, err
}
distros = append(distros, decodedResult.(*Distro))
}
return distros, nil
}
// GetDistro returns a single distro obtained by its name.
func (c *Client) GetDistro(name string) (*Distro, error) {
var distro Distro
result, err := c.Call("get_distro", name, c.Token)
if result == "~" {
return nil, fmt.Errorf("Distro %s not found.", name)
}
if err != nil {
return nil, err
}
decodeResult, err := decodeCobblerItem(result, &distro)
if err != nil {
return nil, err
}
return decodeResult.(*Distro), nil
}
// CreateDistro creates a distro.
func (c *Client) CreateDistro(distro Distro) (*Distro, error) {
// Make sure a distro with the same name does not already exist
if _, err := c.GetDistro(distro.Name); err == nil {
return nil, fmt.Errorf("A Distro with the name %s already exists.", distro.Name)
}
result, err := c.Call("new_distro", c.Token)
if err != nil {
return nil, err
}
newID := result.(string)
item := reflect.ValueOf(&distro).Elem()
if err := c.updateCobblerFields("distro", item, newID); err != nil {
return nil, err
}
if _, err := c.Call("save_distro", newID, c.Token); err != nil {
return nil, err
}
return c.GetDistro(distro.Name)
}
// UpdateDistro updates a single distro.
func (c *Client) UpdateDistro(distro *Distro) error {
item := reflect.ValueOf(distro).Elem()
id, err := c.GetItemHandle("distro", distro.Name)
if err != nil {
return err
}
if err := c.updateCobblerFields("distro", item, id); err != nil {
return err
}
if _, err := c.Call("save_distro", id, c.Token); err != nil {
return err
}
return nil
}
// DeleteDistro deletes a single distro by its name.
func (c *Client) DeleteDistro(name string) error {
_, err := c.Call("remove_distro", name, c.Token)
return err
}