David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 5 | maas "github.com/juju/gomaasapi" |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 6 | "net/url" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 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 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 72 | func (n *MaasNode) PowerType() string { |
| 73 | ptype, _ := n.GetString("power_type") |
| 74 | return ptype |
| 75 | } |
| 76 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 77 | func (n *MaasNode) PowerState() string { |
| 78 | state, _ := n.GetString("power_state") |
| 79 | return state |
| 80 | } |
| 81 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 82 | func (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. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 90 | log.Errorf("error updating power settings : %s", err.Error()) |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 94 | // Hostname get the hostname |
| 95 | func (n *MaasNode) Hostname() string { |
| 96 | hn, _ := n.GetString("hostname") |
| 97 | return hn |
| 98 | } |
| 99 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 100 | // IPs get the IP Addresses |
| 101 | func (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. Bainbridge | 856e6b5 | 2016-06-14 19:10:43 -0700 | [diff] [blame] | 114 | if ip != "" { |
| 115 | result = append(result, ip) |
| 116 | } |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | return result |
| 121 | } |
| 122 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 123 | // MACs get the MAC Addresses |
| 124 | func (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 |
| 142 | func (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 |
| 150 | func (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 | } |