From 9378e2da586449242c578256098bed5b99d3a399 Mon Sep 17 00:00:00 2001 From: liusheng Date: Mon, 7 Aug 2017 16:00:38 +0800 Subject: [PATCH] Add support for node list command Change-Id: Ie21f6277fa920834a52a5a67067c74a5c55eb787 --- moganclient/osc/v1/node.py | 35 ++++++++++++++++++++ moganclient/tests/unit/fakes.py | 2 ++ moganclient/tests/unit/osc/v1/test_node.py | 38 ++++++++++++++++++++++ moganclient/v1/client.py | 2 ++ moganclient/v1/node.py | 28 ++++++++++++++++ setup.cfg | 3 ++ 6 files changed, 108 insertions(+) create mode 100644 moganclient/osc/v1/node.py create mode 100644 moganclient/tests/unit/osc/v1/test_node.py create mode 100644 moganclient/v1/node.py diff --git a/moganclient/osc/v1/node.py b/moganclient/osc/v1/node.py new file mode 100644 index 0000000..ae4e61a --- /dev/null +++ b/moganclient/osc/v1/node.py @@ -0,0 +1,35 @@ +# Copyright 2017 Huawei, Inc. All rights reserved. +# +# 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. +# + + +"""Mogan v1 Baremetal node implementations""" + +import logging + +from osc_lib.command import command + +LOG = logging.getLogger(__name__) + + +class ListNode(command.Lister): + """List all baremetal nodes names""" + + def take_action(self, parsed_args): + bc_client = self.app.client_manager.baremetal_compute + + data = bc_client.node.list() + + return (('Node',), + tuple((d,) for d in data)) diff --git a/moganclient/tests/unit/fakes.py b/moganclient/tests/unit/fakes.py index 6fa0080..b9b3edd 100644 --- a/moganclient/tests/unit/fakes.py +++ b/moganclient/tests/unit/fakes.py @@ -23,6 +23,7 @@ from requests import Response from moganclient.common import base from moganclient.v1 import availability_zone from moganclient.v1 import flavor +from moganclient.v1 import node from moganclient.v1 import server @@ -65,6 +66,7 @@ class FakeBaremetalComputeV1Client(object): self.server = server.ServerManager(self.fake_http_client) self.availability_zone = availability_zone.AvailabilityZoneManager( self.fake_http_client) + self.node = node.NodeManager(self.fake_http_client) class FakeHTTPClient(object): diff --git a/moganclient/tests/unit/osc/v1/test_node.py b/moganclient/tests/unit/osc/v1/test_node.py new file mode 100644 index 0000000..7aa3318 --- /dev/null +++ b/moganclient/tests/unit/osc/v1/test_node.py @@ -0,0 +1,38 @@ +# Copyright 2017 Huawei, Inc. All rights reserved. +# +# 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. +# + +import mock + +from moganclient.osc.v1 import node +from moganclient.tests.unit import base as test_base +from moganclient.v1 import node as node_mgr + + +@mock.patch.object(node_mgr.NodeManager, '_list') +class TestNodeList(test_base.TestBaremetalComputeV1): + def setUp(self): + super(TestNodeList, self).setUp() + self.cmd = node.ListNode(self.app, None) + self.fake_node = ("node-1", "node-2", "node-3") + + def test_list_node(self, mock_list): + arglist = [] + verifylist = [] + mock_list.return_value = [self.fake_node] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + mock_list.assert_called_once_with('/nodes', response_key='nodes') + self.assertEqual(('Node',), columns) + self.assertEqual(((("node-1", "node-2", "node-3"),),), data) diff --git a/moganclient/v1/client.py b/moganclient/v1/client.py index c142bd1..207e80c 100644 --- a/moganclient/v1/client.py +++ b/moganclient/v1/client.py @@ -17,6 +17,7 @@ from moganclient.common import http from moganclient.v1 import availability_zone from moganclient.v1 import flavor from moganclient.v1 import keypair +from moganclient.v1 import node from moganclient.v1 import server @@ -33,3 +34,4 @@ class Client(object): self.availability_zone = availability_zone.AvailabilityZoneManager( self.http_client) self.keypair = keypair.KeyPairManager(self.http_client) + self.node = node.NodeManager(self.http_client) diff --git a/moganclient/v1/node.py b/moganclient/v1/node.py new file mode 100644 index 0000000..3a923ce --- /dev/null +++ b/moganclient/v1/node.py @@ -0,0 +1,28 @@ +# Copyright 2017 Huawei, Inc. All rights reserved. +# +# 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 moganclient.common import base + + +class Node(base.Resource): + pass + + +class NodeManager(base.ManagerWithFind): + resource_class = Node + + def list(self): + url = '/nodes' + return self._list(url, response_key='nodes') diff --git a/setup.cfg b/setup.cfg index f709543..34c71f7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -56,6 +56,9 @@ openstack.baremetal_compute.v1 = baremetal_keypair_show = moganclient.osc.v1.keypair:ShowKeyPair baremetal_keypair_list = moganclient.osc.v1.keypair:ListKeyPair baremetal_keypair_delete = moganclient.osc.v1.keypair:DeleteKeyPair + # TODO(liusheng): may change the "baremetal" to another word to avoid + # conflict with Ironic + baremetal_compute_node_list = moganclient.osc.v1.node:ListNode [build_sphinx]