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() |
David K. Bainbridge | 856e6b5 | 2016-06-14 19:10:43 -0700 | [diff] [blame^] | 116 | if ip != "" { |
| 117 | result = append(result, ip) |
| 118 | } |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | return result |
| 123 | } |
| 124 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 125 | // MACs get the MAC Addresses |
| 126 | func (n *MaasNode) MACs() []string { |
| 127 | macsObj, _ := n.GetMap()["macaddress_set"] |
| 128 | macs, _ := macsObj.GetArray() |
| 129 | if len(macs) == 0 { |
| 130 | return []string{} |
| 131 | } |
| 132 | result := make([]string, len(macs)) |
| 133 | for i, mac := range macs { |
| 134 | obj, _ := mac.GetMap() |
| 135 | addr, _ := obj["mac_address"] |
| 136 | s, _ := addr.GetString() |
| 137 | result[i] = s |
| 138 | } |
| 139 | |
| 140 | return result |
| 141 | } |
| 142 | |
| 143 | // Zone get the zone |
| 144 | func (n *MaasNode) Zone() string { |
| 145 | zone := n.GetMap()["zone"] |
| 146 | attrs, _ := zone.GetMap() |
| 147 | v, _ := attrs["name"].GetString() |
| 148 | return v |
| 149 | } |
| 150 | |
| 151 | // GetInteger get attribute value as integer |
| 152 | func (n *MaasNode) GetInteger(key string) (int, error) { |
| 153 | v, err := n.GetMap()[key].GetFloat64() |
| 154 | if err != nil { |
| 155 | return 0, err |
| 156 | } |
| 157 | return int(v), nil |
| 158 | } |