diff --git a/qubes/mgmt.py b/qubes/mgmt.py index 0e6ff785..5231e6ec 100644 --- a/qubes/mgmt.py +++ b/qubes/mgmt.py @@ -464,6 +464,20 @@ class QubesMgmt(AbstractQubesMgmt): return label.color + @api('mgmt.label.Index', no_payload=True) + @asyncio.coroutine + def label_index(self): + assert self.dest.name == 'dom0' + + try: + label = self.app.get_label(self.arg) + except KeyError: + raise qubes.exc.QubesValueError + + self.fire_event_for_permission(label=label) + + return str(label.index) + @api('mgmt.label.Create') @asyncio.coroutine def label_create(self, untrusted_payload): diff --git a/qubes/tests/mgmt.py b/qubes/tests/mgmt.py index 16431fd0..809e5cab 100644 --- a/qubes/tests/mgmt.py +++ b/qubes/tests/mgmt.py @@ -681,6 +681,15 @@ class TC_00_VMs(MgmtTestCase): [unittest.mock.call('red')]) self.assertFalse(self.app.save.called) + def test_195_label_index(self): + self.app.get_label = unittest.mock.Mock() + self.app.get_label.configure_mock(**{'return_value.index': 1}) + value = self.call_mgmt_func(b'mgmt.label.Index', b'dom0', b'red') + self.assertEqual(value, '1') + self.assertEqual(self.app.get_label.mock_calls, + [unittest.mock.call('red')]) + self.assertFalse(self.app.save.called) + def test_200_label_create(self): self.app.get_label = unittest.mock.Mock() self.app.get_label.side_effect=KeyError