blob: 9e045e6edf6198f337ad4ac21d310daa0c00b167 [file] [log] [blame]
David K. Bainbridgeb5415042016-05-13 17:06:10 -07001package main
2
3import (
4 "fmt"
David K. Bainbridge6ea57c12016-06-06 23:29:12 -07005 "log"
David K. Bainbridgeb5415042016-05-13 17:06:10 -07006
7 maas "github.com/juju/gomaasapi"
David K. Bainbridge6ea57c12016-06-06 23:29:12 -07008 "net/url"
David K. Bainbridgeb5415042016-05-13 17:06:10 -07009)
10
11// MaasNodeStatus MAAS lifecycle status for nodes
12type MaasNodeStatus int
13
14// MAAS Node Statuses
15const (
16 Invalid MaasNodeStatus = -1
17 New MaasNodeStatus = 0
18 Commissioning MaasNodeStatus = 1
19 FailedCommissioning MaasNodeStatus = 2
20 Missing MaasNodeStatus = 3
21 Ready MaasNodeStatus = 4
22 Reserved MaasNodeStatus = 5
23 Deployed MaasNodeStatus = 6
24 Retired MaasNodeStatus = 7
25 Broken MaasNodeStatus = 8
26 Deploying MaasNodeStatus = 9
27 Allocated MaasNodeStatus = 10
28 FailedDeployment MaasNodeStatus = 11
29 Releasing MaasNodeStatus = 12
30 FailedReleasing MaasNodeStatus = 13
31 DiskErasing MaasNodeStatus = 14
32 FailedDiskErasing MaasNodeStatus = 15
33)
34
35var names = []string{"New", "Commissioning", "FailedCommissioning", "Missing", "Ready", "Reserved",
36 "Deployed", "Retired", "Broken", "Deploying", "Allocated", "FailedDeployment",
37 "Releasing", "FailedReleasing", "DiskErasing", "FailedDiskErasing"}
38
39func (v MaasNodeStatus) String() string {
40 return names[v]
41}
42
43// FromString lookup the constant value for a given node state name
44func FromString(name string) (MaasNodeStatus, error) {
45 for i, v := range names {
46 if v == name {
47 return MaasNodeStatus(i), nil
48 }
49 }
50 return -1, fmt.Errorf("Unknown MAAS node state name, '%s'", name)
51}
52
53// MaasNode convenience wrapper for an MAAS node on top of a generic MAAS object
54type MaasNode struct {
55 maas.MAASObject
56}
57
58// GetString get attribute value as string
59func (n *MaasNode) GetString(key string) (string, error) {
60 return n.GetMap()[key].GetString()
61}
62
63// GetFloat64 get attribute value as float64
64func (n *MaasNode) GetFloat64(key string) (float64, error) {
65 return n.GetMap()[key].GetFloat64()
66}
67
68// ID get the system id of the node
69func (n *MaasNode) ID() string {
70 id, _ := n.GetString("system_id")
71 return id
72}
73
David K. Bainbridge6ea57c12016-06-06 23:29:12 -070074func (n *MaasNode) PowerType() string {
75 ptype, _ := n.GetString("power_type")
76 return ptype
77}
78
David K. Bainbridgeb5415042016-05-13 17:06:10 -070079func (n *MaasNode) PowerState() string {
80 state, _ := n.GetString("power_state")
81 return state
82}
83
David K. Bainbridge6ea57c12016-06-06 23:29:12 -070084func (n *MaasNode) UpdatePowerParameters(ptype string, params map[string]string) {
85 values := url.Values{}
86 values.Add("power_type", ptype)
87 for k, v := range params {
88 values.Add("power_parameters_"+k, v)
89 }
90 _, err := n.Update(values)
91 if err != nil {
92 log.Printf("[error] error updating power settings : %s", err.Error())
93 }
94}
95
David K. Bainbridgeb5415042016-05-13 17:06:10 -070096// Hostname get the hostname
97func (n *MaasNode) Hostname() string {
98 hn, _ := n.GetString("hostname")
99 return hn
100}
101
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700102// IPs get the IP Addresses
103func (n *MaasNode) IPs() []string {
104 ifaceObj, _ := n.GetMap()["interface_set"]
105 ifaces, _ := ifaceObj.GetArray()
106 result := []string{}
107
108 for _, iface := range ifaces {
109 obj, _ := iface.GetMap()
110 linksObj, _ := obj["links"]
111 links, _ := linksObj.GetArray()
112 for _, link := range links {
113 linkObj, _ := link.GetMap()
114 ipObj, _ := linkObj["ip_address"]
115 ip, _ := ipObj.GetString()
David K. Bainbridge856e6b52016-06-14 19:10:43 -0700116 if ip != "" {
117 result = append(result, ip)
118 }
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700119 }
120 }
121
122 return result
123}
124
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700125// MACs get the MAC Addresses
126func (n *MaasNode) MACs() []string {
127 macsObj, _ := n.GetMap()["macaddress_set"]
128 macs, _ := macsObj.GetArray()
129 if len(macs) == 0 {
130 return []string{}
131 }
132 result := make([]string, len(macs))
133 for i, mac := range macs {
134 obj, _ := mac.GetMap()
135 addr, _ := obj["mac_address"]
136 s, _ := addr.GetString()
137 result[i] = s
138 }
139
140 return result
141}
142
143// Zone get the zone
144func (n *MaasNode) Zone() string {
145 zone := n.GetMap()["zone"]
146 attrs, _ := zone.GetMap()
147 v, _ := attrs["name"].GetString()
148 return v
149}
150
151// GetInteger get attribute value as integer
152func (n *MaasNode) GetInteger(key string) (int, error) {
153 v, err := n.GetMap()[key].GetFloat64()
154 if err != nil {
155 return 0, err
156 }
157 return int(v), nil
158}