def test_ypath_with_attributes(self): proj = self.fixture[0][1] for obj, path in self.fixture: cache(path, obj) results = list(find_ypath(proj, Vm(name="server"))) self.assertEqual(2, len(results)) self.assertTrue(all(len(i) == 2 for i in results), results) self.assertTrue(all(isinstance(i[0], Path) for i in results), results) self.assertTrue(all(isinstance(i[1], Vm) for i in results), results) self.assertIn(self.fixture[-1][1], [i[0] for i in results], results) results = list(find_ypath(proj, Gateway(name="0-123-4-567890-edge"))) self.assertTrue(results) results = list(find_ypath(proj, Network(name="USER_NET"))) self.assertTrue(results)
def test_ypath_with_keywords(self): proj = self.fixture[0][1] for obj, path in self.fixture: cache(path, obj) results = list(find_ypath(proj, Vm(), name="server")) self.assertEqual(2, len(results)) self.assertTrue(all(len(i) == 2 for i in results), results) self.assertTrue(all(isinstance(i[0], Path) for i in results), results) self.assertTrue(all(isinstance(i[1], Vm) for i in results), results) self.assertIn(self.fixture[-1][1], [i[0] for i in results], results)
def test_ypath_by_type(self): proj = self.fixture[0][1] for obj, path in self.fixture: cache(path, obj) results = list(find_ypath(proj, Vm())) self.assertEqual(4, len(results)) self.assertTrue(all(len(i) == 2 for i in results), results) self.assertTrue(all(isinstance(i[0], Path) for i in results), results) self.assertTrue(all(isinstance(i[1], Vm) for i in results), results) self.assertIn(self.fixture[-1][1], [i[0] for i in results], results)
def on_vm(path, session, response, results=None, status=None): log = logging.getLogger("maloja.surveyor.on_vm") ns = "{http://www.vmware.com/vcloud/v1.5}" tree = ET.fromstring(response.text) path = path._replace(file="vm.yaml") obj = Vm() found, obj = next(find_ypath(path, obj), (None, obj)) if found is not None: log.debug("Loaded existing object: {0}".format(vars(obj))) # Update the existing object with attributes from the VM obj.feed_xml(tree, ns=ns) cache(path, obj) if results and status: results.put((status._replace(path=path), None))
def do_search(self, arg): """ 'Search' locates items by their attributes: > search org fullName=Dev > search vdc description=Skyscape > search template name=Windows > search vm ip=192.168.2.100 """ log = logging.getLogger("maloja.console.do_search") lookup = {i.__name__.lower(): i for i in (Catalog, Org, Template, VApp, Vdc, Vm)} try: bits = arg.strip().split() if bits[-1].isdigit(): index = bits.pop(-1) else: index = None except IndexError: self.do_help("search") return try: name, spec = bits except ValueError: name, spec = bits[0], "" finally: if name not in lookup: print("Type {} not recognised.".format(name)) return typ = lookup[name] try: key, value = [i.strip() for i in spec.split("=")] if key in ("name", "description", "fullName"): results = [ (path, obj) for path, obj in find_ypath(self.ref, typ()) if value.lower() in getattr(obj, key, "").lower() ] else: results = list(find_ypath(self.ref, typ(), **{key: value})) except ValueError as e: key, value = "", "" results = list(find_ypath(self.ref, typ())) if len(results) > 1: if index is not None: path, obj = results[int(index)] self.search[obj] = path else: print("Your options:") print( *[ "{0:01}: {1}".format(n, getattr(obj, "name", obj)) for n, (path, obj) in enumerate(results) ], sep="\n" ) sys.stdout.write("\n") elif results: self.search[results[0][1]] = results[0][0] else: print("No matches for pattern {}".format(spec)) print("Search results:\n") print(*[vars(obj) for obj in self.search], sep="\n")