Brian O'Connor | 6a37ea9 | 2017-08-03 22:45:59 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Foundation |
David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 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 ( |
Cem Turker | f203f93 | 2018-02-21 23:42:32 +0000 | [diff] [blame] | 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 | RescueMode MaasNodeStatus = 16 |
| 45 | EnteringRescueMode MaasNodeStatus = 17 |
| 46 | FailedEnteringRescueMode MaasNodeStatus = 18 |
| 47 | ExitingRescueMode MaasNodeStatus = 19 |
| 48 | FailedExitingRescueMode MaasNodeStatus = 20 |
| 49 | Testing MaasNodeStatus = 21 |
| 50 | FailedTesting MaasNodeStatus = 22 |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 51 | ) |
| 52 | |
| 53 | var names = []string{"New", "Commissioning", "FailedCommissioning", "Missing", "Ready", "Reserved", |
| 54 | "Deployed", "Retired", "Broken", "Deploying", "Allocated", "FailedDeployment", |
Cem Turker | f203f93 | 2018-02-21 23:42:32 +0000 | [diff] [blame] | 55 | "Releasing", "FailedReleasing", "DiskErasing", "FailedDiskErasing","RescueMode", |
| 56 | "EnteringRescueMode", "FailedEnteringRescueMode", "ExitingRescueMode", "FailedExitingRescueMode", |
| 57 | "Testing", "FailedTesting"} |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 58 | |
| 59 | func (v MaasNodeStatus) String() string { |
| 60 | return names[v] |
| 61 | } |
| 62 | |
| 63 | // FromString lookup the constant value for a given node state name |
| 64 | func FromString(name string) (MaasNodeStatus, error) { |
| 65 | for i, v := range names { |
| 66 | if v == name { |
| 67 | return MaasNodeStatus(i), nil |
| 68 | } |
| 69 | } |
| 70 | return -1, fmt.Errorf("Unknown MAAS node state name, '%s'", name) |
| 71 | } |
| 72 | |
| 73 | // MaasNode convenience wrapper for an MAAS node on top of a generic MAAS object |
| 74 | type MaasNode struct { |
| 75 | maas.MAASObject |
| 76 | } |
| 77 | |
| 78 | // GetString get attribute value as string |
| 79 | func (n *MaasNode) GetString(key string) (string, error) { |
| 80 | return n.GetMap()[key].GetString() |
| 81 | } |
| 82 | |
| 83 | // GetFloat64 get attribute value as float64 |
| 84 | func (n *MaasNode) GetFloat64(key string) (float64, error) { |
| 85 | return n.GetMap()[key].GetFloat64() |
| 86 | } |
| 87 | |
| 88 | // ID get the system id of the node |
| 89 | func (n *MaasNode) ID() string { |
| 90 | id, _ := n.GetString("system_id") |
| 91 | return id |
| 92 | } |
| 93 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 94 | func (n *MaasNode) PowerType() string { |
| 95 | ptype, _ := n.GetString("power_type") |
| 96 | return ptype |
| 97 | } |
| 98 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 99 | func (n *MaasNode) PowerState() string { |
| 100 | state, _ := n.GetString("power_state") |
| 101 | return state |
| 102 | } |
| 103 | |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 104 | func (n *MaasNode) UpdatePowerParameters(ptype string, params map[string]string) { |
| 105 | values := url.Values{} |
| 106 | values.Add("power_type", ptype) |
| 107 | for k, v := range params { |
| 108 | values.Add("power_parameters_"+k, v) |
| 109 | } |
| 110 | _, err := n.Update(values) |
| 111 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 112 | log.Errorf("error updating power settings : %s", err.Error()) |
David K. Bainbridge | 6ea57c1 | 2016-06-06 23:29:12 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 116 | // Hostname get the hostname |
| 117 | func (n *MaasNode) Hostname() string { |
| 118 | hn, _ := n.GetString("hostname") |
| 119 | return hn |
| 120 | } |
| 121 | |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 122 | // IPs get the IP Addresses |
| 123 | func (n *MaasNode) IPs() []string { |
| 124 | ifaceObj, _ := n.GetMap()["interface_set"] |
| 125 | ifaces, _ := ifaceObj.GetArray() |
| 126 | result := []string{} |
| 127 | |
| 128 | for _, iface := range ifaces { |
| 129 | obj, _ := iface.GetMap() |
| 130 | linksObj, _ := obj["links"] |
| 131 | links, _ := linksObj.GetArray() |
| 132 | for _, link := range links { |
| 133 | linkObj, _ := link.GetMap() |
| 134 | ipObj, _ := linkObj["ip_address"] |
| 135 | ip, _ := ipObj.GetString() |
David K. Bainbridge | 856e6b5 | 2016-06-14 19:10:43 -0700 | [diff] [blame] | 136 | if ip != "" { |
| 137 | result = append(result, ip) |
| 138 | } |
David K. Bainbridge | efa951d | 2016-05-26 10:54:25 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | return result |
| 143 | } |
| 144 | |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 145 | // MACs get the MAC Addresses |
| 146 | func (n *MaasNode) MACs() []string { |
Cem Turker | f203f93 | 2018-02-21 23:42:32 +0000 | [diff] [blame] | 147 | ifaceObj, _ := n.GetMap()["interface_set"] |
| 148 | ifaces, _ := ifaceObj.GetArray() |
| 149 | if len(ifaces) == 0 { |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 150 | return []string{} |
| 151 | } |
Cem Turker | f203f93 | 2018-02-21 23:42:32 +0000 | [diff] [blame] | 152 | result := make([]string, len(ifaces)) |
| 153 | for i, iface := range ifaces { |
| 154 | obj, _ := iface.GetMap() |
| 155 | macAddressObj, _ := obj["mac_address"] |
| 156 | macAddress,_ := macAddressObj.GetString() |
| 157 | result[i] = macAddress |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 158 | } |
David K. Bainbridge | b541504 | 2016-05-13 17:06:10 -0700 | [diff] [blame] | 159 | return result |
| 160 | } |
| 161 | |
| 162 | // Zone get the zone |
| 163 | func (n *MaasNode) Zone() string { |
| 164 | zone := n.GetMap()["zone"] |
| 165 | attrs, _ := zone.GetMap() |
| 166 | v, _ := attrs["name"].GetString() |
| 167 | return v |
| 168 | } |
| 169 | |
| 170 | // GetInteger get attribute value as integer |
| 171 | func (n *MaasNode) GetInteger(key string) (int, error) { |
| 172 | v, err := n.GetMap()[key].GetFloat64() |
| 173 | if err != nil { |
| 174 | return 0, err |
| 175 | } |
| 176 | return int(v), nil |
| 177 | } |