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