David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Laboratory |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "fmt" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 18 | maas "github.com/juju/gomaasapi" |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 19 | "net/url" |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | // MaasNodeStatus MAAS lifecycle status for nodes |
| 23 | type MaasNodeStatus int |
| 24 | |
| 25 | // MAAS Node Statuses |
| 26 | const ( |
| 27 | Invalid MaasNodeStatus = -1 |
| 28 | New MaasNodeStatus = 0 |
| 29 | Commissioning MaasNodeStatus = 1 |
| 30 | FailedCommissioning MaasNodeStatus = 2 |
| 31 | Missing MaasNodeStatus = 3 |
| 32 | Ready MaasNodeStatus = 4 |
| 33 | Reserved MaasNodeStatus = 5 |
| 34 | Deployed MaasNodeStatus = 6 |
| 35 | Retired MaasNodeStatus = 7 |
| 36 | Broken MaasNodeStatus = 8 |
| 37 | Deploying MaasNodeStatus = 9 |
| 38 | Allocated MaasNodeStatus = 10 |
| 39 | FailedDeployment MaasNodeStatus = 11 |
| 40 | Releasing MaasNodeStatus = 12 |
| 41 | FailedReleasing MaasNodeStatus = 13 |
| 42 | DiskErasing MaasNodeStatus = 14 |
| 43 | FailedDiskErasing MaasNodeStatus = 15 |
| 44 | ) |
| 45 | |
| 46 | var names = []string{"New", "Commissioning", "FailedCommissioning", "Missing", "Ready", "Reserved", |
| 47 | "Deployed", "Retired", "Broken", "Deploying", "Allocated", "FailedDeployment", |
| 48 | "Releasing", "FailedReleasing", "DiskErasing", "FailedDiskErasing"} |
| 49 | |
| 50 | func (v MaasNodeStatus) String() string { |
| 51 | return names[v] |
| 52 | } |
| 53 | |
| 54 | // FromString lookup the constant value for a given node state name |
| 55 | func FromString(name string) (MaasNodeStatus, error) { |
| 56 | for i, v := range names { |
| 57 | if v == name { |
| 58 | return MaasNodeStatus(i), nil |
| 59 | } |
| 60 | } |
| 61 | return -1, fmt.Errorf("Unknown MAAS node state name, '%s'", name) |
| 62 | } |
| 63 | |
| 64 | // MaasNode convenience wrapper for an MAAS node on top of a generic MAAS object |
| 65 | type MaasNode struct { |
| 66 | maas.MAASObject |
| 67 | } |
| 68 | |
| 69 | // GetString get attribute value as string |
| 70 | func (n *MaasNode) GetString(key string) (string, error) { |
| 71 | return n.GetMap()[key].GetString() |
| 72 | } |
| 73 | |
| 74 | // GetFloat64 get attribute value as float64 |
| 75 | func (n *MaasNode) GetFloat64(key string) (float64, error) { |
| 76 | return n.GetMap()[key].GetFloat64() |
| 77 | } |
| 78 | |
| 79 | // ID get the system id of the node |
| 80 | func (n *MaasNode) ID() string { |
| 81 | id, _ := n.GetString("system_id") |
| 82 | return id |
| 83 | } |
| 84 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 85 | func (n *MaasNode) PowerType() string { |
| 86 | ptype, _ := n.GetString("power_type") |
| 87 | return ptype |
| 88 | } |
| 89 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 90 | func (n *MaasNode) PowerState() string { |
| 91 | state, _ := n.GetString("power_state") |
| 92 | return state |
| 93 | } |
| 94 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 95 | func (n *MaasNode) UpdatePowerParameters(ptype string, params map[string]string) { |
| 96 | values := url.Values{} |
| 97 | values.Add("power_type", ptype) |
| 98 | for k, v := range params { |
| 99 | values.Add("power_parameters_"+k, v) |
| 100 | } |
| 101 | _, err := n.Update(values) |
| 102 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 103 | log.Errorf("error updating power settings : %s", err.Error()) |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 107 | // Hostname get the hostname |
| 108 | func (n *MaasNode) Hostname() string { |
| 109 | hn, _ := n.GetString("hostname") |
| 110 | return hn |
| 111 | } |
| 112 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 113 | // IPs get the IP Addresses |
| 114 | func (n *MaasNode) IPs() []string { |
| 115 | ifaceObj, _ := n.GetMap()["interface_set"] |
| 116 | ifaces, _ := ifaceObj.GetArray() |
| 117 | result := []string{} |
| 118 | |
| 119 | for _, iface := range ifaces { |
| 120 | obj, _ := iface.GetMap() |
| 121 | linksObj, _ := obj["links"] |
| 122 | links, _ := linksObj.GetArray() |
| 123 | for _, link := range links { |
| 124 | linkObj, _ := link.GetMap() |
| 125 | ipObj, _ := linkObj["ip_address"] |
| 126 | ip, _ := ipObj.GetString() |
David K. Bainbridge | 856e6b5 | 2016-06-14 19:10:43 -0700 | [diff] [blame] | 127 | if ip != "" { |
| 128 | result = append(result, ip) |
| 129 | } |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | return result |
| 134 | } |
| 135 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 136 | // MACs get the MAC Addresses |
| 137 | func (n *MaasNode) MACs() []string { |
| 138 | macsObj, _ := n.GetMap()["macaddress_set"] |
| 139 | macs, _ := macsObj.GetArray() |
| 140 | if len(macs) == 0 { |
| 141 | return []string{} |
| 142 | } |
| 143 | result := make([]string, len(macs)) |
| 144 | for i, mac := range macs { |
| 145 | obj, _ := mac.GetMap() |
| 146 | addr, _ := obj["mac_address"] |
| 147 | s, _ := addr.GetString() |
| 148 | result[i] = s |
| 149 | } |
| 150 | |
| 151 | return result |
| 152 | } |
| 153 | |
| 154 | // Zone get the zone |
| 155 | func (n *MaasNode) Zone() string { |
| 156 | zone := n.GetMap()["zone"] |
| 157 | attrs, _ := zone.GetMap() |
| 158 | v, _ := attrs["name"].GetString() |
| 159 | return v |
| 160 | } |
| 161 | |
| 162 | // GetInteger get attribute value as integer |
| 163 | func (n *MaasNode) GetInteger(key string) (int, error) { |
| 164 | v, err := n.GetMap()[key].GetFloat64() |
| 165 | if err != nil { |
| 166 | return 0, err |
| 167 | } |
| 168 | return int(v), nil |
| 169 | } |