summaryrefslogtreecommitdiff
path: root/novaclient/v2/contrib/baremetal.py
diff options
context:
space:
mode:
Diffstat (limited to 'novaclient/v2/contrib/baremetal.py')
-rw-r--r--novaclient/v2/contrib/baremetal.py339
1 files changed, 339 insertions, 0 deletions
diff --git a/novaclient/v2/contrib/baremetal.py b/novaclient/v2/contrib/baremetal.py
new file mode 100644
index 0000000..d23a38e
--- /dev/null
+++ b/novaclient/v2/contrib/baremetal.py
@@ -0,0 +1,339 @@
1# Copyright 2013 Hewlett-Packard Development Company, L.P.
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16"""
17Baremetal interface (v2 extension).
18"""
19
20from novaclient import base
21from novaclient.i18n import _
22from novaclient.openstack.common import cliutils
23from novaclient import utils
24
25
26class BareMetalNode(base.Resource):
27 """
28 A baremetal node (typically a physical server or an empty VM).
29 """
30
31 def __repr__(self):
32 return "<BareMetalNode: %s>" % self.id
33
34
35class BareMetalNodeInterface(base.Resource):
36 """
37 An interface belonging to a baremetal node.
38 """
39
40 def __repr__(self):
41 return "<BareMetalNodeInterface: %s>" % self.id
42
43
44class BareMetalNodeManager(base.ManagerWithFind):
45 """
46 Manage :class:`BareMetalNode` resources.
47 """
48 resource_class = BareMetalNode
49
50 def create(self,
51 service_host,
52 cpus,
53 memory_mb,
54 local_gb,
55 prov_mac_address,
56 pm_address=None,
57 pm_user=None,
58 pm_password=None,
59 terminal_port=None):
60 """
61 Create a baremetal node.
62
63 :param service_host: Name of controlling compute host
64 :param cpus: Number of CPUs in the node
65 :param memory_mb: Megabytes of RAM in the node
66 :param local_gb: Gigabytes of local storage in the node
67 :param pm_address: Power management IP for the node
68 :param pm_user: Username for the node's power management
69 :param pm_password: Password for the node's power management
70 :param prov_mac_address: MAC address to provision the node
71 :param terminal_port: ShellInABox port
72 :rtype: :class:`BareMetalNode`
73 """
74 body = {'node': {'service_host': service_host,
75 'cpus': cpus,
76 'memory_mb': memory_mb,
77 'local_gb': local_gb,
78 'pm_address': pm_address,
79 'pm_user': pm_user,
80 'pm_password': pm_password,
81 'prov_mac_address': prov_mac_address,
82 'terminal_port': terminal_port}}
83
84 return self._create('/os-baremetal-nodes', body, 'node')
85
86 def delete(self, node):
87 """
88 Delete a baremetal node.
89
90 :param node: The :class:`BareMetalNode` to delete.
91 """
92 self._delete('/os-baremetal-nodes/%s' % base.getid(node))
93
94 def get(self, node_id):
95 """
96 Get a baremetal node.
97
98 :param node_id: The ID of the node to delete.
99 :rtype: :class:`BareMetalNode`
100 """
101 return self._get("/os-baremetal-nodes/%s" % node_id, 'node')
102
103 def list(self):
104 """
105 Get a list of all baremetal nodes.
106
107 :rtype: list of :class:`BareMetalNode`
108 """
109 return self._list('/os-baremetal-nodes', 'nodes')
110
111 def add_interface(self, node_id, address, datapath_id=0, port_no=0):
112 """
113 Add an interface to a baremetal node.
114
115 :param node_id: The ID of the node to modify.
116 :param address: The MAC address to add.
117 :param datapath_id: Datapath ID of OpenFlow switch for the interface
118 :param port_no: OpenFlow port number for the interface
119 :rtype: :class:`BareMetalNodeInterface`
120 """
121 body = {'add_interface': {'address': address,
122 'datapath_id': datapath_id,
123 'port_no': port_no}}
124 url = '/os-baremetal-nodes/%s/action' % node_id
125 _resp, body = self.api.client.post(url, body=body)
126 return BareMetalNodeInterface(self, body['interface'])
127
128 def remove_interface(self, node_id, address):
129 """
130 Remove an interface from a baremetal node.
131
132 :param node_id: The ID of the node to modify.
133 :param address: The MAC address to remove.
134 :rtype: bool
135 """
136 req_body = {'remove_interface': {'address': address}}
137 url = '/os-baremetal-nodes/%s/action' % node_id
138 self.api.client.post(url, body=req_body)
139
140 def list_interfaces(self, node_id):
141 """
142 List the interfaces on a baremetal node.
143
144 :param node_id: The ID of the node to list.
145 :rtype: list
146 """
147 interfaces = []
148 node = self._get("/os-baremetal-nodes/%s" % node_id, 'node')
149 for interface in node.interfaces:
150 interface_object = BareMetalNodeInterface(self, interface)
151 interfaces.append(interface_object)
152 return interfaces
153
154
155@cliutils.arg(
156 'service_host',
157 metavar='<service_host>',
158 help=_('Name of nova compute host which will control this baremetal '
159 'node'))
160@cliutils.arg(
161 'cpus',
162 metavar='<cpus>',
163 type=int,
164 help=_('Number of CPUs in the node'))
165@cliutils.arg(
166 'memory_mb',
167 metavar='<memory_mb>',
168 type=int,
169 help=_('Megabytes of RAM in the node'))
170@cliutils.arg(
171 'local_gb',
172 metavar='<local_gb>',
173 type=int,
174 help=_('Gigabytes of local storage in the node'))
175@cliutils.arg(
176 'prov_mac_address',
177 metavar='<prov_mac_address>',
178 help=_('MAC address to provision the node'))
179@cliutils.arg(
180 '--pm_address', default=None,
181 metavar='<pm_address>',
182 help=_('Power management IP for the node'))
183@cliutils.arg(
184 '--pm_user', default=None,
185 metavar='<pm_user>',
186 help=_('Username for the node\'s power management'))
187@cliutils.arg(
188 '--pm_password', default=None,
189 metavar='<pm_password>',
190 help=_('Password for the node\'s power management'))
191@cliutils.arg(
192 '--terminal_port', default=None,
193 metavar='<terminal_port>',
194 type=int,
195 help=_('ShellInABox port?'))
196def do_baremetal_node_create(cs, args):
197 """Create a baremetal node."""
198 node = cs.baremetal.create(args.service_host, args.cpus,
199 args.memory_mb, args.local_gb,
200 args.prov_mac_address,
201 pm_address=args.pm_address,
202 pm_user=args.pm_user,
203 pm_password=args.pm_password,
204 terminal_port=args.terminal_port)
205 _print_baremetal_resource(node)
206
207
208@cliutils.arg(
209 'node',
210 metavar='<node>',
211 help=_('ID of the node to delete.'))
212def do_baremetal_node_delete(cs, args):
213 """Remove a baremetal node and any associated interfaces."""
214 node = _find_baremetal_node(cs, args.node)
215 cs.baremetal.delete(node)
216
217
218def _translate_baremetal_node_keys(collection):
219 convert = [('service_host', 'host'),
220 ('local_gb', 'disk_gb'),
221 ('prov_mac_address', 'mac_address'),
222 ('pm_address', 'pm_address'),
223 ('pm_user', 'pm_username'),
224 ('pm_password', 'pm_password'),
225 ('terminal_port', 'terminal_port'),
226 ]
227 for item in collection:
228 keys = item.__dict__.keys()
229 for from_key, to_key in convert:
230 if from_key in keys and to_key not in keys:
231 setattr(item, to_key, item._info[from_key])
232
233
234def _print_baremetal_nodes_list(nodes):
235 """Print the list of baremetal nodes."""
236
237 def _parse_address(fields):
238 macs = []
239 for interface in fields.interfaces:
240 macs.append(interface['address'])
241 return ', '.join("%s" % i for i in macs)
242
243 formatters = {
244 'MAC Address': _parse_address
245 }
246
247 _translate_baremetal_node_keys(nodes)
248 utils.print_list(nodes, [
249 'ID',
250 'Host',
251 'Task State',
252 'CPUs',
253 'Memory_MB',
254 'Disk_GB',
255 'MAC Address',
256 'PM Address',
257 'PM Username',
258 'PM Password',
259 'Terminal Port',
260 ], formatters=formatters)
261
262
263def do_baremetal_node_list(cs, _args):
264 """Print list of available baremetal nodes."""
265 nodes = cs.baremetal.list()
266 _print_baremetal_nodes_list(nodes)
267
268
269def _find_baremetal_node(cs, node):
270 """Get a node by ID."""
271 return utils.find_resource(cs.baremetal, node)
272
273
274def _print_baremetal_resource(resource):
275 """Print details of a baremetal resource."""
276 info = resource._info.copy()
277 utils.print_dict(info)
278
279
280def _print_baremetal_node_interfaces(interfaces):
281 """Print interfaces of a baremetal node."""
282 utils.print_list(interfaces, [
283 'ID',
284 'Datapath_ID',
285 'Port_No',
286 'Address',
287 ])
288
289
290@cliutils.arg(
291 'node',
292 metavar='<node>',
293 help=_("ID of node"))
294def do_baremetal_node_show(cs, args):
295 """Show information about a baremetal node."""
296 node = _find_baremetal_node(cs, args.node)
297 _print_baremetal_resource(node)
298
299
300@cliutils.arg(
301 'node',
302 metavar='<node>',
303 help=_("ID of node"))
304@cliutils.arg(
305 'address',
306 metavar='<address>',
307 help=_("MAC address of interface"))
308@cliutils.arg(
309 '--datapath_id',
310 default=0,
311 metavar='<datapath_id>',
312 help=_("OpenFlow Datapath ID of interface"))
313@cliutils.arg(
314 '--port_no',
315 default=0,
316 metavar='<port_no>',
317 help=_("OpenFlow port number of interface"))
318def do_baremetal_interface_add(cs, args):
319 """Add a network interface to a baremetal node."""
320 bmif = cs.baremetal.add_interface(args.node, args.address,
321 args.datapath_id, args.port_no)
322 _print_baremetal_resource(bmif)
323
324
325@cliutils.arg('node', metavar='<node>', help=_("ID of node"))
326@cliutils.arg(
327 'address',
328 metavar='<address>',
329 help=_("MAC address of interface"))
330def do_baremetal_interface_remove(cs, args):
331 """Remove a network interface from a baremetal node."""
332 cs.baremetal.remove_interface(args.node, args.address)
333
334
335@cliutils.arg('node', metavar='<node>', help=_("ID of node"))
336def do_baremetal_interface_list(cs, args):
337 """List network interfaces associated with a baremetal node."""
338 interfaces = cs.baremetal.list_interfaces(args.node)
339 _print_baremetal_node_interfaces(interfaces)