From ff3a1d3780486d98bd3205c8329576b396e8161f Mon Sep 17 00:00:00 2001 From: jichenjc Date: Fri, 19 Feb 2016 22:52:38 +0800 Subject: [PATCH] [compute] Add set host command set host command is missing, add it as SetHost class. Change-Id: I7acb94150718b7150598632cbebc3d85018a0d59 --- doc/source/command-objects/host.rst | 35 +++++++++ openstackclient/compute/v2/host.py | 57 ++++++++++++++ openstackclient/tests/compute/v2/fakes.py | 56 ++++++++++++++ openstackclient/tests/compute/v2/test_host.py | 75 +++++++++++++++++++ .../notes/bug-1556929-edd78cded88ecdc9.yaml | 4 + setup.cfg | 1 + 6 files changed, 228 insertions(+) create mode 100644 openstackclient/tests/compute/v2/test_host.py create mode 100644 releasenotes/notes/bug-1556929-edd78cded88ecdc9.yaml diff --git a/doc/source/command-objects/host.rst b/doc/source/command-objects/host.rst index 680efcc2e8..ede62c562e 100644 --- a/doc/source/command-objects/host.rst +++ b/doc/source/command-objects/host.rst @@ -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-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:: + + The host (name or ID) + host show --------- diff --git a/openstackclient/compute/v2/host.py b/openstackclient/compute/v2/host.py index f2257d1219..5af2531084 100644 --- a/openstackclient/compute/v2/host.py +++ b/openstackclient/compute/v2/host.py @@ -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="", + 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""" diff --git a/openstackclient/tests/compute/v2/fakes.py b/openstackclient/tests/compute/v2/fakes.py index 32d257f13f..4531f3bf40 100644 --- a/openstackclient/tests/compute/v2/fakes.py +++ b/openstackclient/tests/compute/v2/fakes.py @@ -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 diff --git a/openstackclient/tests/compute/v2/test_host.py b/openstackclient/tests/compute/v2/test_host.py new file mode 100644 index 0000000000..de5375771f --- /dev/null +++ b/openstackclient/tests/compute/v2/test_host.py @@ -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) diff --git a/releasenotes/notes/bug-1556929-edd78cded88ecdc9.yaml b/releasenotes/notes/bug-1556929-edd78cded88ecdc9.yaml new file mode 100644 index 0000000000..44f9a4646f --- /dev/null +++ b/releasenotes/notes/bug-1556929-edd78cded88ecdc9.yaml @@ -0,0 +1,4 @@ +--- +features: + - Command ``host set`` is now available for compute + [Bug `1556929 `_] diff --git a/setup.cfg b/setup.cfg index b49eeba9eb..91f09a3a5a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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