forked from marpaia/chef-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
149 lines (141 loc) · 4.84 KB
/
node.go
File metadata and controls
149 lines (141 loc) · 4.84 KB
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
package chef
import (
"encoding/json"
"fmt"
"strings"
)
// chef.Node represents the relevant parameters of a Chef node
type Node struct {
Name string `json:"name"`
Environment string `json:"chef_environment"`
JSONClass string `json:"json_class"`
RunList []string `json:"run_list"`
ChefType string `json:"chef_type"`
Info struct {
Languages map[string]map[string]string `json:"languages"`
Kernel struct {
Name string `json:"name"`
Release string `json:"release"`
Version string `json:"version"`
Machine string `json:"machine"`
Modules map[string]map[string]string `json:"modules"`
} `json:"kernel"`
OS string `json:"os"`
OSVersion string `json:"os_version"`
Hostname string `json:"hostname"`
FQDN string `json:"fqdn"`
Domain string `json:"domain"`
Network struct {
Interfaces map[string]struct {
Type string `json:"type"`
Number string `json:"number"`
Encapsulation string `json:"encapsulation"`
Addresses map[string]struct {
Family string `json:"family"`
Broadcast string `json:"broadcast"`
Netmast string `json:"netmast"`
} `json:"addresses"`
Flags []string `json:"flags"`
MTU string `json:"mtu"`
Arp map[string]string `json:"arp"`
} `json:"interfaces"`
} `json:"network"`
IPAddress string `json:"ipaddress"`
MACAddress string `json:"macaddress"`
ChefPackages map[string]map[string]string `json:"chef_packages"`
Keys map[string]map[string]string `json:"keys"`
Platform string `json:"platform"`
PlatformVersion string `json:"platform_version"`
CPU map[string]interface{} `json:"cpu"`
Filesystem map[string]struct {
KBSize string `json:"ks_size"`
KBUsed string `json:"ks_used"`
KBavailable string `json:"ks_available"`
PercentUsed string `json:"percent_used"`
Mount string `json:"mount"`
FSType string `json:"fs_type"`
MountOptions []string `json:"mount_options"`
} `json:"filesystem"`
Memory map[string]interface{} `json:"memory"`
UptimeSeconds int `json:"uptime_seconds"`
Uptime string `json:"uptime"`
IdletimeSeconds int `json:"idletime_seconds"`
Idletime string `json:"idletime"`
BlockDevice map[string]interface{} `json:"block_device"`
Datacenter map[string]interface{} `json:"datacenter"`
Ipmi struct {
Address string `json:"address"`
MacAddress string `json:"mac_address"`
} `json:"ipmi"`
Recipes []string `json:"recipes"`
Roles []string `json:"roles"`
} `json:"automatic"`
Normal map[string]interface{} `json:"normal"`
Default map[string]interface{} `json:"default"`
}
// chef.GetNodes returns a map of nodes names to the nodes's RESTful URL as well
// as an error indicating if the request was successful or not.
//
// Usage:
//
// nodes, err := chef.GetNodes()
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// // do what you please with the "node" variable which is a map of
// // node names to node URLs
// for node := range nodes {
// fmt.Println(node)
// }
func (chef *Chef) GetNodes() (map[string]string, error) {
resp, err := chef.Get("nodes")
if err != nil {
return nil, err
}
body, err := responseBody(resp)
if err != nil {
return nil, err
}
nodes := map[string]string{}
json.Unmarshal(body, &nodes)
return nodes, nil
}
// chef.GetNode accepts a string which represents the name of a Chef role and
// returns a chef.Environment type representing that role as well as a bool
// indicating whether or not the role was found and an error indicating if the
// request failed or not.
//
// Note that if the request is successful but no such client existed, the error
// return value will be nil but the bool will be false.
//
// Usage:
//
// node, ok, err := chef.GetNode("neo4j.example.com")
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
// if !ok {
// fmt.Println("Couldn't find that node!")
// } else {
// // do what you please with the "node" variable which is of the
// // *Chef.Node type
// fmt.Printf("%#v\n", node)
// }
func (chef *Chef) GetNode(name string) (*Node, bool, error) {
resp, err := chef.Get(fmt.Sprintf("nodes/%s", name))
if err != nil {
return nil, false, err
}
body, err := responseBody(resp)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil, false, nil
}
return nil, false, err
}
node := new(Node)
json.Unmarshal(body, node)
return node, true, nil
}