-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.go
93 lines (88 loc) · 2.2 KB
/
inventory.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
package inventory
import (
"encoding/csv"
"errors"
"fmt"
"gopkg.in/yaml.v3"
"io"
"os"
)
type Inventory struct {
DefaultUsername string `yaml:"default_username" json:"default_username"`
DefaultPassword string `yaml:"default_password" json:"default_password"`
Hosts []Host `yaml:"hosts" json:"hosts"`
}
type Host struct {
Hostname string `yaml:"hostname" json:"hostname"`
Username string `yaml:"username" json:"username"`
Password string `yaml:"password" json:"password"`
Data map[string]interface{} `yaml:"data" json:"data"`
}
func LoadFromYAML(filename string) (*Inventory, error) {
inv := Inventory{}
yFile, err := os.Open(filename)
if err != nil {
return nil, err
}
yContent, err := io.ReadAll(yFile)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(yContent, &inv); err != nil {
return nil, err
}
return &inv, nil
}
func LoadFromCSV(path string) (*Inventory, error) {
inv := Inventory{}
file, err := os.Open(path)
if err != nil {
return nil, err
}
records := make([]map[string]interface{}, 1)
// here we read the header line
records[0] = make(map[string]interface{})
reader := csv.NewReader(file)
hrec, err := reader.Read()
if err != nil {
return nil, err
}
headers := make([]string, len(hrec))
for i, field := range hrec {
headers[i] = field
}
for {
host := Host{Data: make(map[string]interface{})}
rec, err := reader.Read()
if err != nil {
if err == io.EOF {
break
} else {
return nil, err
}
}
for i, field := range rec {
switch headers[i] {
case "hostname":
host.Hostname = field
case "username":
host.Username = field
case "password":
host.Password = field
default:
host.Data[headers[i]] = field
}
}
if len(host.Hostname) == 0 {
return nil, errors.New(fmt.Sprintf("inventory file %s does not contain hostname field", path))
}
if len(host.Username) == 0 {
return nil, errors.New(fmt.Sprintf("inventory file %s does not contain username field", path))
}
if len(host.Password) == 0 {
return nil, errors.New(fmt.Sprintf("inventory file %s does not contain password field", path))
}
inv.Hosts = append(inv.Hosts, host)
}
return &inv, nil
}