blob: 86c2d03a8bd8ae807d15209e4dcac6c0f0021f3e [file] [log] [blame]
Brian O'Connor6a37ea92017-08-03 22:45:59 -07001// Copyright 2016 Open Networking Foundation
David K. Bainbridgedf9df632016-07-07 18:47:46 -07002//
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. Bainbridgeb5415042016-05-13 17:06:10 -070014package main
15
16import (
17 "fmt"
David K. Bainbridgeb5415042016-05-13 17:06:10 -070018 maas "github.com/juju/gomaasapi"
David K. Bainbridge6ea57c12016-06-06 23:29:12 -070019 "net/url"
David K. Bainbridgeb5415042016-05-13 17:06:10 -070020)
21
22// MaasNodeStatus MAAS lifecycle status for nodes
23type MaasNodeStatus int
24
25// MAAS Node Statuses
26const (
Cem Turkerf203f932018-02-21 23:42:32 +000027 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. Bainbridgeb5415042016-05-13 17:06:10 -070051)
52
53var names = []string{"New", "Commissioning", "FailedCommissioning", "Missing", "Ready", "Reserved",
54 "Deployed", "Retired", "Broken", "Deploying", "Allocated", "FailedDeployment",
Cem Turkerf203f932018-02-21 23:42:32 +000055 "Releasing", "FailedReleasing", "DiskErasing", "FailedDiskErasing","RescueMode",
56 "EnteringRescueMode", "FailedEnteringRescueMode", "ExitingRescueMode", "FailedExitingRescueMode",
57 "Testing", "FailedTesting"}
David K. Bainbridgeb5415042016-05-13 17:06:10 -070058
59func (v MaasNodeStatus) String() string {
60 return names[v]
61}
62
63// FromString lookup the constant value for a given node state name
64func 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
74type MaasNode struct {
75 maas.MAASObject
76}
77
78// GetString get attribute value as string
79func (n *MaasNode) GetString(key string) (string, error) {
80 return n.GetMap()[key].GetString()
81}
82
83// GetFloat64 get attribute value as float64
84func (n *MaasNode) GetFloat64(key string) (float64, error) {
85 return n.GetMap()[key].GetFloat64()
86}
87
88// ID get the system id of the node
89func (n *MaasNode) ID() string {
90 id, _ := n.GetString("system_id")
91 return id
92}
93
David K. Bainbridge6ea57c12016-06-06 23:29:12 -070094func (n *MaasNode) PowerType() string {
95 ptype, _ := n.GetString("power_type")
96 return ptype
97}
98
David K. Bainbridgeb5415042016-05-13 17:06:10 -070099func (n *MaasNode) PowerState() string {
100 state, _ := n.GetString("power_state")
101 return state
102}
103
David K. Bainbridge6ea57c12016-06-06 23:29:12 -0700104func (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. Bainbridgea9c2e0a2016-07-01 18:33:50 -0700112 log.Errorf("error updating power settings : %s", err.Error())
David K. Bainbridge6ea57c12016-06-06 23:29:12 -0700113 }
114}
115
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700116// Hostname get the hostname
117func (n *MaasNode) Hostname() string {
118 hn, _ := n.GetString("hostname")
119 return hn
120}
121
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700122// IPs get the IP Addresses
123func (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. Bainbridge856e6b52016-06-14 19:10:43 -0700136 if ip != "" {
137 result = append(result, ip)
138 }
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700139 }
140 }
141
142 return result
143}
144
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700145// MACs get the MAC Addresses
146func (n *MaasNode) MACs() []string {
Cem Turkerf203f932018-02-21 23:42:32 +0000147 ifaceObj, _ := n.GetMap()["interface_set"]
148 ifaces, _ := ifaceObj.GetArray()
149 if len(ifaces) == 0 {
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700150 return []string{}
151 }
Cem Turkerf203f932018-02-21 23:42:32 +0000152 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. Bainbridgeb5415042016-05-13 17:06:10 -0700158 }
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700159 return result
160}
161
162// Zone get the zone
163func (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
171func (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}