def to_mo(self): """ Converts GenericMo to ManagedObject """ import ucsmeta class_id = ucsgenutils.word_u(self._class_id) if class_id not in ucsmeta.MO_CLASS_ID: return None mo = self.__get_mo_obj(class_id) if not mo: # or not isinstance(mo, ManagedObject): return None for prop in self.__properties: if prop in mo.prop_map: mo.__dict__[mo.prop_map[prop]] = self.__properties[prop] else: UcsWarning("Property %s Not Exist in MO %s" % ( ucsgenutils.word_u(prop), class_id)) if len(self.child): for ch_ in self.child: mo_ch = ch_.to_mo() mo.child_add(mo_ch) return mo
def load_module(module_name): """ This loads the module into the current name space Args: module_name (str): module_name Returns: None """ module_name = ucsgenutils.word_u(module_name) if module_name and module_name in MO_CLASS_ID: fq_module_name = mometa.__name__ + ".%s" % module_name module_import = __import__(fq_module_name, globals(), locals(), [module_name]) return module_import elif module_name and module_name in METHOD_CLASS_ID: fq_module_name = methodmeta.__name__ + ".%sMeta" % module_name module_import = __import__(fq_module_name, globals(), locals(), [module_name]) return module_import elif module_name and module_name in OTHER_TYPE_CLASS_ID: fq_module_name = OTHER_TYPE_CLASS_ID[module_name] module_import = __import__(fq_module_name, globals(), locals(), [module_name]) return module_import
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 from_xml_str(xml_str): """ Generates response object from the given xml string. Args: xml_str (str): xml string Returns: object (external method or managed object or generic managed object) Example: xml_str='''\n <lsServer dn="org-root/ls-testsp" dynamicConPolicyName="test"\n extIPPoolName="ext-mgmt" name="testsp" />\n '''\n root_element = extract_root_elem(xml_str)\n """ root_elem = ET.fromstring(xml_str) if root_elem.tag == "error": error_code = root_elem.attrib["errorCode"] error_descr = root_elem.attrib["errorDescr"] raise ex.UcsException(error_code, error_descr) class_id = ucsgenutils.word_u(root_elem.tag) response = ucscoreutils.get_ucs_obj(class_id, root_elem) response.from_xml(root_elem) return response
def from_xml(self, elem): """ This method is form objects out of xml element. This is called internally from ucsxmlcode.from_xml_str method. Example: xml = '<testLsA a="1" b="2" c="3" dn="org-root/" rn=""> <testLsB a="1" b="2" c="3" dn="org-root/" rn="" /></testLsA>'\n obj = xc.from_xml_str(xml)\n print type(obj)\n Outputs: <class 'ucsmsdk.ucsmo.GenericMo'> """ if elem is None: return None self._class_id = elem.tag if elem.attrib: for name, value in elem.attrib.iteritems(): self.__dict__[name] = value self.__properties[name] = str(value) if self.rn and self.dn: pass elif self.rn and not self.dn: if self.__parent_dn is not None and self.__parent_dn != "": self.dn = self.__parent_dn + '/' + self.rn self.__properties['dn'] = self.dn else: self.dn = self.rn self.__properties['dn'] = self.dn elif not self.rn and self.dn: self.rn = os.path.basename(self.dn) self.__properties['rn'] = self.rn # else: # raise ValueError("Both rn and dn does not present.") children = elem.getchildren() if children: for child in children: if not ET.iselement(child): continue class_id = ucsgenutils.word_u(child.tag) # child_obj = ucscoreutils.get_ucs_obj(class_id, child, self) pdn = None if 'dn' in dir(self): pdn = self.dn child_obj = GenericMo(class_id, parent_mo_or_dn=pdn) self.child_add(child_obj) child_obj.from_xml(child)
def print_mo_hierarchy(class_id, level=0, break_level=None, show_level=[]): """ print hierarchy of class_id Args: class_id (str): class id level (int): by default zero break_level (int or None): last level to process show_level (int list): levels to display Returns: dictionary Example: response=handle.query_dn("org-root", need_response=True)\n tree_dict = write_mo_tree(response, break_level=3, show_level=[1, 3])\n """ indent = " " level_indent = "%s%s)" % (indent * level, level) class_id = ucsgenutils.word_u(class_id) if level == 0: parents = [ucsgenutils.word_u(parent) for parent in MO_CLASS_META[class_id].parents] print "[%s]" % (", ".join(sorted(parents))) if level == 0 or not show_level or level in show_level: print "%s%s" % (level_indent, ucsgenutils.word_u(class_id)) children = sorted(MO_CLASS_META[class_id].children) level += 1 if break_level is None or level <= break_level: for ch_ in children: child = ucsgenutils.word_u(ch_) if child == class_id: continue print_mo_hierarchy(child, level, break_level, show_level) level -= 1
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 from_xml(self, elem): """This method creates the object from the xml representation of the Method object.""" if elem.attrib: for attr_name, attr_value in elem.attrib.iteritems(): self.attr_set(ucsgenutils.convert_to_python_var_name(attr_name) , str(attr_value)) child_elems = elem.getchildren() if child_elems: for child_elem in child_elems: if not ET.iselement(child_elem): continue cln = ucsgenutils.word_u(child_elem.tag) child = ucscoreutils.get_ucs_obj(cln, child_elem) self._child.append(child) child.from_xml(child_elem)
def __init__(self, class_id, parent_mo_or_dn=None, **kwargs): self.__parent_mo = None self.__status = None self.__parent_dn = None self.__xtra_props = {} self.__xtra_props_dirty_mask = 0x1L self._rn_set() self._dn_set(parent_mo_or_dn) xml_attribute = self.mo_meta.xml_attribute UcsBase.__init__(self, ucsgenutils.word_u(xml_attribute)) # self.mark_dirty() if self.__parent_mo: self.__parent_mo.child_add(self) if kwargs: for prop_name, prop_value in kwargs.iteritems(): if prop_name not in self.prop_meta: log.debug("Unknown property %s" % prop_name) self.__set_prop(prop_name, prop_value)
def from_xml(self, elem): """ Method updates the object from the xml representation of the managed object. """ if elem.attrib: if self.__class__.__name__ != "ManagedObject": for attr_name, attr_value in elem.attrib.iteritems(): if attr_name in self.prop_map: attr_name = self.prop_map[attr_name] else: self.__xtra_props[attr_name] = _GenericProp( attr_name, attr_value, False) object.__setattr__(self, attr_name, attr_value) else: for attr_name, attr_value in elem.attrib.iteritems(): object.__setattr__(self, attr_name, attr_value) self.mark_clean() child_elems = elem.getchildren() if child_elems: for child_elem in child_elems: if not ET.iselement(child_elem): continue if self.__class__.__name__ != "ManagedObject" and ( child_elem.tag in self.mo_meta.field_names): pass class_id = ucsgenutils.word_u(child_elem.tag) child_obj = ucscoreutils.get_ucs_obj(class_id, child_elem, self) self.child_add(child_obj) child_obj.from_xml(child_elem)
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