David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 5 | "log" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 6 | |
| 7 | maas "github.com/juju/gomaasapi" |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 8 | "net/url" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // MaasNodeStatus MAAS lifecycle status for nodes |
| 12 | type MaasNodeStatus int |
| 13 | |
| 14 | // MAAS Node Statuses |
| 15 | const ( |
| 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 | |
| 35 | var names = []string{"New", "Commissioning", "FailedCommissioning", "Missing", "Ready", "Reserved", |
| 36 | "Deployed", "Retired", "Broken", "Deploying", "Allocated", "FailedDeployment", |
| 37 | "Releasing", "FailedReleasing", "DiskErasing", "FailedDiskErasing"} |
| 38 | |
| 39 | func (v MaasNodeStatus) String() string { |
| 40 | return names[v] |
| 41 | } |
| 42 | |
| 43 | // FromString lookup the constant value for a given node state name |
| 44 | func 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 |
| 54 | type MaasNode struct { |
| 55 | maas.MAASObject |
| 56 | } |
| 57 | |
| 58 | // GetString get attribute value as string |
| 59 | func (n *MaasNode) GetString(key string) (string, error) { |
| 60 | return n.GetMap()[key].GetString() |
| 61 | } |
| 62 | |
| 63 | // GetFloat64 get attribute value as float64 |
| 64 | func (n *MaasNode) GetFloat64(key string) (float64, error) { |
| 65 | return n.GetMap()[key].GetFloat64() |
| 66 | } |
| 67 | |
| 68 | // ID get the system id of the node |
| 69 | func (n *MaasNode) ID() string { |
| 70 | id, _ := n.GetString("system_id") |
| 71 | return id |
| 72 | } |
| 73 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 74 | func (n *MaasNode) PowerType() string { |
| 75 | ptype, _ := n.GetString("power_type") |
| 76 | return ptype |
| 77 | } |
| 78 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 79 | func (n *MaasNode) PowerState() string { |
| 80 | state, _ := n.GetString("power_state") |
| 81 | return state |
| 82 | } |
| 83 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 84 | func (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. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 96 | // Hostname get the hostname |
| 97 | func (n *MaasNode) Hostname() string { |
| 98 | hn, _ := n.GetString("hostname") |
| 99 | return hn |
| 100 | } |
| 101 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 102 | // IPs get the IP Addresses |
| 103 | func (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() |
| 116 | result = append(result, ip) |
| 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 | } |