David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | maas "github.com/juju/gomaasapi" |
| 7 | ) |
| 8 | |
| 9 | // MaasNodeStatus MAAS lifecycle status for nodes |
| 10 | type MaasNodeStatus int |
| 11 | |
| 12 | // MAAS Node Statuses |
| 13 | const ( |
| 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 | |
| 33 | var names = []string{"New", "Commissioning", "FailedCommissioning", "Missing", "Ready", "Reserved", |
| 34 | "Deployed", "Retired", "Broken", "Deploying", "Allocated", "FailedDeployment", |
| 35 | "Releasing", "FailedReleasing", "DiskErasing", "FailedDiskErasing"} |
| 36 | |
| 37 | func (v MaasNodeStatus) String() string { |
| 38 | return names[v] |
| 39 | } |
| 40 | |
| 41 | // FromString lookup the constant value for a given node state name |
| 42 | func 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 |
| 52 | type MaasNode struct { |
| 53 | maas.MAASObject |
| 54 | } |
| 55 | |
| 56 | // GetString get attribute value as string |
| 57 | func (n *MaasNode) GetString(key string) (string, error) { |
| 58 | return n.GetMap()[key].GetString() |
| 59 | } |
| 60 | |
| 61 | // GetFloat64 get attribute value as float64 |
| 62 | func (n *MaasNode) GetFloat64(key string) (float64, error) { |
| 63 | return n.GetMap()[key].GetFloat64() |
| 64 | } |
| 65 | |
| 66 | // ID get the system id of the node |
| 67 | func (n *MaasNode) ID() string { |
| 68 | id, _ := n.GetString("system_id") |
| 69 | return id |
| 70 | } |
| 71 | |
| 72 | func (n *MaasNode) PowerState() string { |
| 73 | state, _ := n.GetString("power_state") |
| 74 | return state |
| 75 | } |
| 76 | |
| 77 | // Hostname get the hostname |
| 78 | func (n *MaasNode) Hostname() string { |
| 79 | hn, _ := n.GetString("hostname") |
| 80 | return hn |
| 81 | } |
| 82 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 83 | // IPs get the IP Addresses |
| 84 | func (n *MaasNode) IPs() []string { |
| 85 | ifaceObj, _ := n.GetMap()["interface_set"] |
| 86 | ifaces, _ := ifaceObj.GetArray() |
| 87 | result := []string{} |
| 88 | |
| 89 | for _, iface := range ifaces { |
| 90 | obj, _ := iface.GetMap() |
| 91 | linksObj, _ := obj["links"] |
| 92 | links, _ := linksObj.GetArray() |
| 93 | for _, link := range links { |
| 94 | linkObj, _ := link.GetMap() |
| 95 | ipObj, _ := linkObj["ip_address"] |
| 96 | ip, _ := ipObj.GetString() |
| 97 | result = append(result, ip) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return result |
| 102 | } |
| 103 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 104 | // MACs get the MAC Addresses |
| 105 | func (n *MaasNode) MACs() []string { |
| 106 | macsObj, _ := n.GetMap()["macaddress_set"] |
| 107 | macs, _ := macsObj.GetArray() |
| 108 | if len(macs) == 0 { |
| 109 | return []string{} |
| 110 | } |
| 111 | result := make([]string, len(macs)) |
| 112 | for i, mac := range macs { |
| 113 | obj, _ := mac.GetMap() |
| 114 | addr, _ := obj["mac_address"] |
| 115 | s, _ := addr.GetString() |
| 116 | result[i] = s |
| 117 | } |
| 118 | |
| 119 | return result |
| 120 | } |
| 121 | |
| 122 | // Zone get the zone |
| 123 | func (n *MaasNode) Zone() string { |
| 124 | zone := n.GetMap()["zone"] |
| 125 | attrs, _ := zone.GetMap() |
| 126 | v, _ := attrs["name"].GetString() |
| 127 | return v |
| 128 | } |
| 129 | |
| 130 | // GetInteger get attribute value as integer |
| 131 | func (n *MaasNode) GetInteger(key string) (int, error) { |
| 132 | v, err := n.GetMap()[key].GetFloat64() |
| 133 | if err != nil { |
| 134 | return 0, err |
| 135 | } |
| 136 | return int(v), nil |
| 137 | } |