[compute] Add set host command

set host command is missing, add it as SetHost class.

Change-Id: I7acb94150718b7150598632cbebc3d85018a0d59
This commit is contained in:
jichenjc 2016-02-19 22:52:38 +08:00
parent 8787ad2b49
commit ff3a1d3780
6 changed files with 228 additions and 0 deletions

View File

@ -21,6 +21,41 @@ List all hosts
Only return hosts in the availability zone
host set
--------
Set host command
.. program:: host set
.. code:: bash
os host set
[--enable | --disable]
[--enable-maintenance | --disable-maintenance]
<host>
.. _host-set:
.. option:: --enable
Enable the host
.. option:: --disable
Disable the host
.. _maintenance-set:
.. option:: --enable-maintenance
Enable maintenance mode for the host
.. option:: --disable-maintenance
Disable maintenance mode for the host
.. describe:: <host>
The host (name or ID)
host show
---------

View File

@ -44,6 +44,63 @@ class ListHost(command.Lister):
) for s in data))
class SetHost(command.Command):
"""Set host properties"""
def get_parser(self, prog_name):
parser = super(SetHost, self).get_parser(prog_name)
parser.add_argument(
"host",
metavar="<host>",
help="The host to modify (name or ID)"
)
status = parser.add_mutually_exclusive_group()
status.add_argument(
'--enable',
action='store_true',
help='Enable the host'
)
status.add_argument(
'--disable',
action='store_true',
help='Disable the host'
)
maintenance = parser.add_mutually_exclusive_group()
maintenance.add_argument(
'--enable-maintenance',
action='store_true',
help='Enable maintenance mode for the host'
)
maintenance.add_argument(
'--disable-maintenance',
action='store_true',
help='Disable maintenance mode for the host',
)
return parser
def take_action(self, parsed_args):
kwargs = {}
if parsed_args.enable:
kwargs['status'] = True
if parsed_args.disable:
kwargs['status'] = False
if parsed_args.enable_maintenance:
kwargs['maintenance_mode'] = True
if parsed_args.disable_maintenance:
kwargs['maintenance_mode'] = False
compute_client = self.app.client_manager.compute
foundhost = utils.find_resource(
compute_client.hosts,
parsed_args.host
)
compute_client.hosts.update(
foundhost.id,
kwargs
)
class ShowHost(command.Lister):
"""Show host command"""

View File

@ -140,6 +140,9 @@ class FakeComputev2Client(object):
self.keypairs = mock.Mock()
self.keypairs.resource_class = fakes.FakeResource(None, {})
self.hosts = mock.Mock()
self.hosts.resource_class = fakes.FakeResource(None, {})
self.auth_token = kwargs['token']
self.management_url = kwargs['endpoint']
@ -878,3 +881,56 @@ class FakeNetwork(object):
networks.append(FakeNetwork.create_one_network(attrs, methods))
return networks
class FakeHost(object):
"""Fake one host."""
@staticmethod
def create_one_host(attrs=None):
"""Create a fake host.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object, with id and other attributes
"""
if attrs is None:
attrs = {}
# Set default attributes.
host_info = {
"id": 1,
"service_id": 1,
"host": "host1",
"uuid": 'host-id-' + uuid.uuid4().hex,
"vcpus": 10,
"memory_mb": 100,
"local_gb": 100,
"vcpus_used": 5,
"memory_mb_used": 50,
"local_gb_used": 10,
"hypervisor_type": "xen",
"hypervisor_version": 1,
"hypervisor_hostname": "devstack1",
"free_ram_mb": 50,
"free_disk_gb": 50,
"current_workload": 10,
"running_vms": 1,
"cpu_info": "",
"disk_available_least": 1,
"host_ip": "10.10.10.10",
"supported_instances": "",
"metrics": "",
"pci_stats": "",
"extra_resources": "",
"stats": "",
"numa_topology": "",
"ram_allocation_ratio": 1.0,
"cpu_allocation_ratio": 1.0
}
host_info.update(attrs)
host = fakes.FakeResource(
info=copy.deepcopy(host_info),
loaded=True)
return host

View File

@ -0,0 +1,75 @@
# Copyright 2016 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
from openstackclient.compute.v2 import host
from openstackclient.tests.compute.v2 import fakes as compute_fakes
class TestHost(compute_fakes.TestComputev2):
def setUp(self):
super(TestHost, self).setUp()
# Get a shortcut to the FlavorManager Mock
self.host_mock = self.app.client_manager.compute.hosts
self.host_mock.reset_mock()
class TestHostSet(TestHost):
def setUp(self):
super(TestHostSet, self).setUp()
self.host = compute_fakes.FakeHost.create_one_host()
self.host_mock.get.return_value = self.host
self.host_mock.update.return_value = None
self.cmd = host.SetHost(self.app, None)
def test_host_set_no_option(self):
arglist = [
str(self.host.id)
]
verifylist = [
('host', str(self.host.id))
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
self.assertIsNone(result)
body = {}
self.host_mock.update.assert_called_with(self.host.id, body)
def test_host_set(self):
arglist = [
'--enable',
'--disable-maintenance',
str(self.host.id)
]
verifylist = [
('enable', True),
('enable_maintenance', False),
('host', str(self.host.id))
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
result = self.cmd.take_action(parsed_args)
self.assertIsNone(result)
body = {'status': True, 'maintenance_mode': False}
self.host_mock.update.assert_called_with(self.host.id, body)

View File

@ -0,0 +1,4 @@
---
features:
- Command ``host set`` is now available for compute
[Bug `1556929 <https://bugs.launchpad.net/python-openstackclient/+bug/1556929>`_]

View File

@ -79,6 +79,7 @@ openstack.compute.v2 =
flavor_unset = openstackclient.compute.v2.flavor:UnsetFlavor
host_list = openstackclient.compute.v2.host:ListHost
host_set = openstackclient.compute.v2.host:SetHost
host_show = openstackclient.compute.v2.host:ShowHost
hypervisor_list = openstackclient.compute.v2.hypervisor:ListHypervisor