-
Notifications
You must be signed in to change notification settings - Fork 286
/
cloudstackmachineconfig.go
74 lines (66 loc) · 2.01 KB
/
cloudstackmachineconfig.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
package v1alpha1
import (
"fmt"
"io/ioutil"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)
const CloudStackMachineConfigKind = "CloudStackMachineConfig"
// Used for generating yaml for generate clusterconfig command.
func NewCloudStackMachineConfigGenerate(name string) *CloudStackMachineConfigGenerate {
return &CloudStackMachineConfigGenerate{
TypeMeta: metav1.TypeMeta{
Kind: CloudStackMachineConfigKind,
APIVersion: SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: ObjectMeta{
Name: name,
},
Spec: CloudStackMachineConfigSpec{
ComputeOffering: CloudStackResourceIdentifier{
Id: "",
},
Template: CloudStackResourceIdentifier{
Id: "",
},
Users: []UserConfiguration{{
Name: "capc",
SshAuthorizedKeys: []string{"ssh-rsa AAAA..."},
}},
},
}
}
func (c *CloudStackMachineConfigGenerate) APIVersion() string {
return c.TypeMeta.APIVersion
}
func (c *CloudStackMachineConfigGenerate) Kind() string {
return c.TypeMeta.Kind
}
func (c *CloudStackMachineConfigGenerate) Name() string {
return c.ObjectMeta.Name
}
func GetCloudStackMachineConfigs(fileName string) (map[string]*CloudStackMachineConfig, error) {
configs := make(map[string]*CloudStackMachineConfig)
content, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("unable to read file due to: %v", err)
}
for _, c := range strings.Split(string(content), YamlSeparator) {
var config CloudStackMachineConfig
if err = yaml.UnmarshalStrict([]byte(c), &config); err == nil {
if config.Kind == CloudStackMachineConfigKind {
configs[config.Name] = &config
continue
}
}
_ = yaml.Unmarshal([]byte(c), &config) // this is to check if there is a bad spec in the file
if config.Kind == CloudStackMachineConfigKind {
return nil, fmt.Errorf("unable to unmarshall content from file due to: %v", err)
}
}
if len(configs) == 0 {
return nil, fmt.Errorf("unable to find kind %v in file", CloudStackMachineConfigKind)
}
return configs, nil
}