def load_class(class_id): """ This loads the class into the current name space Args: class_id (str): class_id Returns: MangedObject or ExtenalMethod Object or None """ class_id = ucsgenutils.word_u(class_id) if class_id and class_id in MO_CLASS_ID: mod_class_id = ucsgenutils.word_l(class_id) class_id_sub_pkg = re.match("([a-z])+", mod_class_id).group() mo_pkg = mometa.__name__ + ".%s.%s" % (class_id_sub_pkg, class_id) mo_module = __import__(mo_pkg, globals(), locals(), [class_id]) mo_class = getattr(mo_module, class_id) return mo_class elif class_id and class_id in METHOD_CLASS_ID: mo_import = methodmeta.__name__ + ".%sMeta" % (class_id) method_meta = __import__(mo_import, globals(), locals(), [class_id]) return getattr(method_meta, class_id) return None
def query_classids(self, *class_ids): """ Queries multiple obects from the server based of a comma separated list of their class Ids. Args: class_ids (comma separated strings): Class Ids to be queried for Returns: Dictionary {class_id1: [objects], class_id2: [objects]} Example: obj = handle.lookup_by_dns("OrgOrg", "LsServer") """ # ToDo - How to handle unknown class_id from ucsbasetype import ClassIdSet, ClassId from ucsmeta import MO_CLASS_ID if not class_ids: raise ValueError("Provide Comma Separated string of Class Ids") class_id_list = [class_id.strip() for class_id in class_ids] class_id_dict = {} class_id_set = ClassIdSet() for class_id_ in class_id_list: class_id_obj = ClassId() meta_class_id = ucsgenutils.word_u(class_id_) if meta_class_id in MO_CLASS_ID: class_id_dict[meta_class_id] = [] class_id_obj.value = ucsgenutils.word_l(class_id_) else: class_id_dict[class_id_] = [] class_id_obj.value = class_id_ class_id_set.child_add(class_id_obj) elem = config_resolve_classes(cookie=self.cookie, in_ids=class_id_set) response = self.post_elem(elem) if response.error_code != 0: raise UcsException(response.error_code, response.error_descr) for out_mo in response.out_configs.child: class_id_dict[out_mo._class_id].append(out_mo) return class_id_dict
def to_xml(self, xml_doc=None, option=None): """ This method returns the xml element node for the current object with it's hierarchy. Args: xml_doc: document to which the Mo attributes are added. Can be None. option: not required for Generic Mo class object Example: from ucsmsdk.ucsmo import GenericMo\n args = {"a": 1, "b": 2, "c":3}\n obj = GenericMo("testLsA", "org-root", **args)\n obj1 = GenericMo("testLsB", "org-root", **args)\n obj.add_child(obj1)\n elem = obj.write_xml()\n import ucsmsdk.ucsxmlcodec as xc\n xc.to_xml_str(elem)\n Output: '<testLsA a="1" b="2" c="3" dn="org-root/" rn="">\n <testLsB a="1" b="2" c="3" dn="org-root/" rn="" />\n </testLsA>' """ if xml_doc is None: xml_obj = Element(ucsgenutils.word_l(self._class_id)) else: xml_obj = SubElement(xml_doc, ucsgenutils.word_l(self._class_id)) for key in self.__dict__: if not key.startswith('_'): xml_obj.set(key, getattr(self, key)) self.child_to_xml(xml_obj) return xml_obj
def parse_filter_obj(sstr, loc, toks): """ Supporting class to parse filter expression. """ # print toks[0] #logger prop_ = toks[0]["prop"] value_ = toks[0]["value"] type_ = "re" if "type_exp" in toks[0]: type_ = toks[0]["type_exp"]["types"] flag_ = "C" if "flag_exp" in toks[0]: flag_ = toks[0]["flag_exp"]["flags"] # print prop_, value_, type_, flag_ #logger if flag_ == "I": value_ = re.sub(r"[a-zA-Z]", lambda x: "[" + x.group().upper() + x.group().lower() + "]", value_) if ParseFilter.is_meta_classid: class_obj = ucscoreutils.load_class(ParseFilter.class_id) prop_mo_meta = class_obj.prop_meta[prop_] if prop_mo_meta: prop_ = prop_mo_meta.xml_attribute sub_filter = create_basic_filter(types[type_], class_=ucsgenutils.word_l( ParseFilter.class_id), property=prop_, value=value_) return sub_filter
def query_children(self, in_mo=None, in_dn=None, class_id=None, hierarchy=False): """ Finds children of a given managed object or distinguished name. Arguments can be specified to query only a specific type(class_id) of children. Arguments can also be specified to query only direct children or the entire hierarchy of children. Args: in_mo (managed object): query children managed object under this object. in_dn (dn string): query children managed object for a given managed object of the respective dn. class_id(str): by default None, if given find only specific children object for a given class_id. hierarchy(bool): if set to True will return all the child hierarchical objects. Returns: managedobjectlist or None by default\n managedobjectlist or None if hierarchy=True\n Example: mo_list = handle.query_children(in_mo=mo)\n mo_list = handle.query_children(in_mo=mo, class_id="classid")\n mo_list = handle.query_children(in_dn=dn)\n mo_list = handle.query_children(in_dn=dn, class_id="classid")\n """ from ucsmeta import MO_CLASS_ID from ucsmethodfactory import config_resolve_children if not in_mo and not in_dn: raise ValueError('[Error]: GetChild: Provide in_mo or in_dn.') if in_mo: parent_dn = in_mo.dn elif in_dn: parent_dn = in_dn if class_id: if ucsgenutils.word_u(class_id) in MO_CLASS_ID: meta_class_id = ucsgenutils.word_l(class_id) else: meta_class_id = class_id else: meta_class_id = class_id elem = config_resolve_children(cookie=self.cookie, class_id=meta_class_id, in_dn=parent_dn, in_filter=None, in_hierarchical=hierarchy) response = self.post_elem(elem) if response.error_code != 0: raise UcsException(response.error_code, response.error_descr) out_mo_list = ucscoreutils.extract_molist_from_method_response( response, hierarchy) return out_mo_list