From: Sage Weil Date: Thu, 6 Feb 2020 03:33:34 +0000 (-0600) Subject: mgr/orch: include addr (and labels) in 'host ls' X-Git-Tag: v15.1.1~506^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=5276871e15514e25eed58db9a39e0bcfad4d5efd;p=ceph.git mgr/orch: include addr (and labels) in 'host ls' Signed-off-by: Sage Weil --- diff --git a/src/pybind/mgr/cephadm/module.py b/src/pybind/mgr/cephadm/module.py index a882d026f365..67414a2b0381 100644 --- a/src/pybind/mgr/cephadm/module.py +++ b/src/pybind/mgr/cephadm/module.py @@ -1184,7 +1184,15 @@ class CephadmOrchestrator(MgrModule, orchestrator.OrchestratorClientMixin): TODO: - InventoryNode probably needs to be able to report labels """ - return [orchestrator.InventoryNode(host_name) for host_name in self.inventory_cache] + r = [] + for hostname, info in self.inventory.items(): + self.log.debug('host %s info %s' % (hostname, info)) + r.append(orchestrator.InventoryNode( + hostname, + addr=info.get('addr', hostname), + labels=info.get('labels', []), + )) + return r @async_completion def add_host_label(self, host, label): diff --git a/src/pybind/mgr/dashboard/tests/test_orchestrator.py b/src/pybind/mgr/dashboard/tests/test_orchestrator.py index 2817a6916809..1a5ff9d37393 100644 --- a/src/pybind/mgr/dashboard/tests/test_orchestrator.py +++ b/src/pybind/mgr/dashboard/tests/test_orchestrator.py @@ -71,6 +71,7 @@ class OrchestratorControllerTest(ControllerTestCase): inventory = [ { 'name': 'host-0', + 'addr': '1.2.3.4', 'devices': [ {'path': 'nvme0n1'}, {'path': '/dev/sdb'}, @@ -79,6 +80,7 @@ class OrchestratorControllerTest(ControllerTestCase): }, { 'name': 'host-1', + 'addr': '1.2.3.5', 'devices': [ {'path': '/dev/sda'}, {'path': 'sdb'}, @@ -97,11 +99,13 @@ class OrchestratorControllerTest(ControllerTestCase): self.assertEqual(len(resp), 2) host0 = resp[0] self.assertEqual(host0['name'], 'host-0') + self.assertEqual(host0['addr'], '1.2.3.4') self.assertEqual(host0['devices'][0]['osd_ids'], [1, 2]) self.assertEqual(host0['devices'][1]['osd_ids'], [1]) self.assertEqual(host0['devices'][2]['osd_ids'], [2]) host1 = resp[1] self.assertEqual(host1['name'], 'host-1') + self.assertEqual(host1['addr'], '1.2.3.5') self.assertEqual(host1['devices'][0]['osd_ids'], []) self.assertEqual(host1['devices'][1]['osd_ids'], [3]) diff --git a/src/pybind/mgr/orchestrator.py b/src/pybind/mgr/orchestrator.py index f926fee1868e..78214b6296c4 100644 --- a/src/pybind/mgr/orchestrator.py +++ b/src/pybind/mgr/orchestrator.py @@ -1360,8 +1360,8 @@ class InventoryNode(object): When fetching inventory, all Devices are groups inside of an InventoryNode. """ - def __init__(self, name, devices=None, labels=None): - # type: (str, Optional[inventory.Devices], Optional[List[str]]) -> None + def __init__(self, name, devices=None, labels=None, addr=None): + # type: (str, Optional[inventory.Devices], Optional[List[str]], Optional[str]) -> None if devices is None: devices = inventory.Devices([]) if labels is None: @@ -1369,12 +1369,14 @@ class InventoryNode(object): assert isinstance(devices, inventory.Devices) self.name = name # unique within cluster. For example a hostname. + self.addr = addr or name self.devices = devices self.labels = labels def to_json(self): return { 'name': self.name, + 'addr': self.addr, 'devices': self.devices.to_json(), 'labels': self.labels, } @@ -1384,12 +1386,13 @@ class InventoryNode(object): try: _data = copy.deepcopy(data) name = _data.pop('name') + addr = _data.pop('addr') or name devices = inventory.Devices.from_json(_data.pop('devices')) if _data: error_msg = 'Unknown key(s) in Inventory: {}'.format(','.join(_data.keys())) raise OrchestratorValidationError(error_msg) labels = _data.get('labels', list()) - return cls(name, devices, labels) + return cls(name, devices, labels, addr) except KeyError as e: error_msg = '{} is required for {}'.format(e, cls.__name__) raise OrchestratorValidationError(error_msg) diff --git a/src/pybind/mgr/orchestrator_cli/module.py b/src/pybind/mgr/orchestrator_cli/module.py index 94c2dd298493..5c5946a6c96d 100644 --- a/src/pybind/mgr/orchestrator_cli/module.py +++ b/src/pybind/mgr/orchestrator_cli/module.py @@ -192,13 +192,13 @@ class OrchestratorCli(orchestrator.OrchestratorClientMixin, MgrModule): output = json.dumps(hosts, sort_keys=True) else: table = PrettyTable( - ['HOST', 'LABELS'], + ['HOST', 'ADDR', 'LABELS'], border=False) table.align = 'l' table.left_padding_width = 0 table.right_padding_width = 1 for node in completion.result: - table.add_row((node.name, ' '.join(node.labels))) + table.add_row((node.name, node.addr, ' '.join(node.labels))) output = table.get_string() return HandleCommandResult(stdout=output) diff --git a/src/pybind/mgr/tests/test_orchestrator.py b/src/pybind/mgr/tests/test_orchestrator.py index b86e8cb5fe75..a1b6dae3b8f5 100644 --- a/src/pybind/mgr/tests/test_orchestrator.py +++ b/src/pybind/mgr/tests/test_orchestrator.py @@ -57,6 +57,7 @@ def _test_resource(data, resource_class, extra=None): def test_inventory(): json_data = { 'name': 'host0', + 'addr': '1.2.3.4', 'devices': [ { 'sys_api': { @@ -74,7 +75,7 @@ def test_inventory(): for devices in json_data['devices']: _test_resource(devices, inventory.Device) - json_data = [{}, {'name': 'host0'}, {'devices': []}] + json_data = [{}, {'name': 'host0', 'addr': '1.2.3.4'}, {'devices': []}] for data in json_data: with pytest.raises(OrchestratorValidationError): InventoryNode.from_json(data)