blob: d364fe0babbc39c4decca70319b260c09e1baf2d [file] [log] [blame]
David K. Bainbridgeb5415042016-05-13 17:06:10 -07001package main
2
3import (
4 "fmt"
5
6 maas "github.com/juju/gomaasapi"
7)
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
72func (n *MaasNode) PowerState() string {
73 state, _ := n.GetString("power_state")
74 return state
75}
76
77// Hostname get the hostname
78func (n *MaasNode) Hostname() string {
79 hn, _ := n.GetString("hostname")
80 return hn
81}
82
83// MACs get the MAC Addresses
84func (n *MaasNode) MACs() []string {
85 macsObj, _ := n.GetMap()["macaddress_set"]
86 macs, _ := macsObj.GetArray()
87 if len(macs) == 0 {
88 return []string{}
89 }
90 result := make([]string, len(macs))
91 for i, mac := range macs {
92 obj, _ := mac.GetMap()
93 addr, _ := obj["mac_address"]
94 s, _ := addr.GetString()
95 result[i] = s
96 }
97
98 return result
99}
100
101// Zone get the zone
102func (n *MaasNode) Zone() string {
103 zone := n.GetMap()["zone"]
104 attrs, _ := zone.GetMap()
105 v, _ := attrs["name"].GetString()
106 return v
107}
108
109// GetInteger get attribute value as integer
110func (n *MaasNode) GetInteger(key string) (int, error) {
111 v, err := n.GetMap()[key].GetFloat64()
112 if err != nil {
113 return 0, err
114 }
115 return int(v), nil
116}