blob: 29084f9482c4bb94d2192a25f6cb2fe5edc7405e [file] [log] [blame]
David K. Bainbridgedf9df632016-07-07 18:47:46 -07001// 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. 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 (
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
46var names = []string{"New", "Commissioning", "FailedCommissioning", "Missing", "Ready", "Reserved",
47 "Deployed", "Retired", "Broken", "Deploying", "Allocated", "FailedDeployment",
48 "Releasing", "FailedReleasing", "DiskErasing", "FailedDiskErasing"}
49
50func (v MaasNodeStatus) String() string {
51 return names[v]
52}
53
54// FromString lookup the constant value for a given node state name
55func 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
65type MaasNode struct {
66 maas.MAASObject
67}
68
69// GetString get attribute value as string
70func (n *MaasNode) GetString(key string) (string, error) {
71 return n.GetMap()[key].GetString()
72}
73
74// GetFloat64 get attribute value as float64
75func (n *MaasNode) GetFloat64(key string) (float64, error) {
76 return n.GetMap()[key].GetFloat64()
77}
78
79// ID get the system id of the node
80func (n *MaasNode) ID() string {
81 id, _ := n.GetString("system_id")
82 return id
83}
84
David K. Bainbridge6ea57c12016-06-06 23:29:12 -070085func (n *MaasNode) PowerType() string {
86 ptype, _ := n.GetString("power_type")
87 return ptype
88}
89
David K. Bainbridgeb5415042016-05-13 17:06:10 -070090func (n *MaasNode) PowerState() string {
91 state, _ := n.GetString("power_state")
92 return state
93}
94
David K. Bainbridge6ea57c12016-06-06 23:29:12 -070095func (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. Bainbridgea9c2e0a2016-07-01 18:33:50 -0700103 log.Errorf("error updating power settings : %s", err.Error())
David K. Bainbridge6ea57c12016-06-06 23:29:12 -0700104 }
105}
106
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700107// Hostname get the hostname
108func (n *MaasNode) Hostname() string {
109 hn, _ := n.GetString("hostname")
110 return hn
111}
112
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700113// IPs get the IP Addresses
114func (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. Bainbridge856e6b52016-06-14 19:10:43 -0700127 if ip != "" {
128 result = append(result, ip)
129 }
David K. Bainbridgeefa951d2016-05-26 10:54:25 -0700130 }
131 }
132
133 return result
134}
135
David K. Bainbridgeb5415042016-05-13 17:06:10 -0700136// MACs get the MAC Addresses
137func (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
155func (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
163func (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}