def next(self): name = SaNameT() attributes = pointer(pointer(SaImmAttrValuesT_2())) try: immom.saImmOmSearchNext_2(self.search_handle, name, attributes) except SafException as err: if err.value == eSaAisErrorT.SA_AIS_ERR_NOT_EXIST: raise StopIteration else: raise err attribs = {} attr_list = unmarshalNullArray(attributes) for attr in attr_list: attr_range = range(attr.attrValuesNumber) attribs[attr.attrName] = [ attr.attrValueType, [ unmarshalSaImmValue(attr.attrValues[val], attr.attrValueType) for val in attr_range ] ] return ImmObject(name.value, attribs)
def update_rt_object(dn, attributes): ''' Updates the given object with the given attribute modifications ''' # Get the class name for the object class_name = get_class_name_for_dn(dn) # Create and marshall attribute modifications attr_mods = [] for name, values in attributes.iteritems(): if values is None: print "WARNING: Received no values for %s in %s" % (name, dn) continue if not isinstance(values, list): values = [values] attr_type = get_attribute_type(name, class_name) c_attr_mod = SaImmAttrModificationT_2() c_attr_mod.modType = eSaImmAttrModificationTypeT.SA_IMM_ATTR_VALUES_REPLACE c_attr_mod.modAttr = SaImmAttrValuesT_2() c_attr_mod.modAttr.attrName = SaImmAttrNameT(name) c_attr_mod.modAttr.attrValueType = attr_type c_attr_mod.modAttr.attrValuesNumber = len(values) c_attr_mod.modAttr.attrValues = marshal_c_array(attr_type, values) attr_mods.append(c_attr_mod) # Call the function saImmOiRtObjectUpdate_2(HANDLE, SaNameT(dn), attr_mods)
def get(self, object_name, attr_name_list=None, class_name=None): """ Obtain values of some attributes of the given object Args: object_name (str): Object name attr_name_list (list): List of attributes class_name (str): Class name Returns: SaAisErrorT: Return code of the corresponding IMM API call(s) ImmObject: Imm object """ imm_obj = None # Always request the SaImmAttrClassName attribute if needed if attr_name_list and not class_name \ and 'SaImmAttrClassName' not in attr_name_list \ and not attr_name_list == \ ['SA_IMM_SEARCH_GET_CONFIG_ATTR']: attr_name_list.append('SaImmAttrClassName') attr_names = [SaImmAttrNameT(attr) for attr in attr_name_list] \ if attr_name_list else None attributes = pointer(pointer(SaImmAttrValuesT_2())) rc = agent.saImmOmAccessorGet_2(self.accessor_handle, SaNameT(object_name), attr_names, attributes) if rc == eSaAisErrorT.SA_AIS_OK: attrs = {} attr_list = unmarshalNullArray(attributes) for attr in attr_list: attr_range = list(range(attr.attrValuesNumber)) attrs[attr.attrName] = [ attr.attrValueType, [ unmarshalSaImmValue(attr.attrValues[val], attr.attrValueType) for val in attr_range ] ] if 'SaImmAttrClassName' not in attrs and class_name: attrs['SaImmAttrClassName'] = class_name imm_obj = ImmObject(dn=object_name, attributes=attrs) if rc == eSaAisErrorT.SA_AIS_ERR_BAD_HANDLE: init_rc = self.init() # If the re-initialization of agent handle succeeds, we still need # to return BAD_HANDLE to the users, so that they would re-try the # failed operation. Otherwise, the true error code is returned # to the user to decide further actions. if init_rc != eSaAisErrorT.SA_AIS_OK: log_err("saImmOmAccessorGet_2 FAILED - %s" % eSaAisErrorT.whatis(rc)) rc = init_rc return rc, imm_obj
def create_runtime_object(self, class_name, parent_name, runtime_obj): """ Create a runtime object Args: class_name (str): Class name parent_name (str): Parent name runtime_obj (ImmObject): Runtime object to create Returns: SaAisErrorT: Return code of OI create runtime object """ # Marshall parameters c_class_name = SaImmClassNameT(class_name) if parent_name: c_parent_name = SaNameT(parent_name) else: c_parent_name = None c_attr_values = [] for name, (c_attr_type, values) in runtime_obj.attrs.items(): if values is None: values = [] elif values == [None]: values = [] # Make sure all values are in lists if not isinstance(values, list): values = [values] # Create the values struct c_attr = SaImmAttrValuesT_2() c_attr.attrName = SaImmAttrNameT(name) c_attr.attrValueType = c_attr_type c_attr.attrValuesNumber = len(values) if not values: c_attr.attrValues = None else: c_attr.attrValues = marshal_c_array(c_attr_type, values) c_attr_values.append(c_attr) rc = saImmOiRtObjectCreate_2(self.handle, c_class_name, c_parent_name, c_attr_values) if rc != eSaAisErrorT.SA_AIS_OK: log_err("saImmOiRtObjectCreate_2 FAILED - %s" % eSaAisErrorT.whatis(rc)) return rc
def modify_value_add(self, object_name, attr_name, values): ''' add to the CCB an ADD modification of an existing object ''' assert object_name # Make sure the values field is a list if not isinstance(values, list): values = [values] # first get class name to read class description to get value type... try: obj = immom.get(object_name) except SafException as err: print "failed: %s" % err return object_names = [SaNameT(object_name)] class_name = obj.SaImmAttrClassName value_type = None attr_def_list = immom.class_description_get(class_name) for attr_def in attr_def_list: if attr_def.attrName == attr_name: value_type = attr_def.attrValueType break if value_type is None: # means attribute name is invalid raise SafException(eSaAisErrorT.SA_AIS_ERR_NOT_EXIST, "attribute '%s' does not exist" % attr_name) err = immom.saImmOmAdminOwnerSet(self.owner_handle, object_names, eSaImmScopeT.SA_IMM_ONE) attr_mods = [] attr_mod = saImmOm.SaImmAttrModificationT_2() attr_mod.modType = \ saImm.eSaImmAttrModificationTypeT.SA_IMM_ATTR_VALUES_ADD attr_mod.modAttr = SaImmAttrValuesT_2() attr_mod.modAttr.attrName = attr_name attr_mod.modAttr.attrValueType = value_type attr_mod.modAttr.attrValuesNumber = len(values) attr_mod.modAttr.attrValues = marshal_c_array(value_type, values) attr_mods.append(attr_mod) err = immom.saImmOmCcbObjectModify_2(self.ccb_handle, object_names[0], attr_mods)
def create(self, obj, parent_name=None): """ Create the CCB object Args: obj (ImmObject): Imm object parent_name (str): Parent name Return: SaAisErrorT: Return code of the corresponding IMM API calls """ rc = eSaAisErrorT.SA_AIS_OK if parent_name is not None: rc = self.admin_owner.set_owner(parent_name) parent_name = SaNameT(parent_name) if rc == eSaAisErrorT.SA_AIS_OK: attr_values = [] for attr_name, type_values in obj.attrs.items(): values = type_values[1] attr = SaImmAttrValuesT_2() attr.attrName = attr_name attr.attrValueType = type_values[0] attr.attrValuesNumber = len(values) attr.attrValues = marshal_c_array(attr.attrValueType, values) attr_values.append(attr) rc = agent.saImmOmCcbObjectCreate_2(self.ccb_handle, obj.class_name, parent_name, attr_values) if rc != eSaAisErrorT.SA_AIS_OK: log_err("saImmOmCcbObjectCreate_2 FAILED - %s" % eSaAisErrorT.whatis(rc)) if rc == eSaAisErrorT.SA_AIS_ERR_BAD_HANDLE: init_rc = self.init() # If the re-initialization of agent handle succeeds, we still need # to return BAD_HANDLE to the users, so that they would re-try the # failed operation. Otherwise, the true error code is returned # to the user to decide further actions. if init_rc != eSaAisErrorT.SA_AIS_OK: rc = init_rc return rc
def __next__(self): obj_name = SaNameT() attributes = pointer(pointer(SaImmAttrValuesT_2())) rc = agent.saImmOmSearchNext_2(self.search_handle, obj_name, attributes) if rc != eSaAisErrorT.SA_AIS_OK: if rc != eSaAisErrorT.SA_AIS_ERR_NOT_EXIST: log_err("saImmOmSearchNext_2 FAILED - %s" % eSaAisErrorT.whatis(rc)) raise StopIteration attrs = {} attr_list = unmarshalNullArray(attributes) for attr in attr_list: attr_range = list(range(attr.attrValuesNumber)) attrs[attr.attrName] = [attr.attrValueType, [unmarshalSaImmValue(attr.attrValues[val], attr.attrValueType) for val in attr_range]] return ImmObject(str(obj_name), attrs)
def update_runtime_object(self, dn, attributes): """ Update the specified object with the requested attribute modifications Args: dn (str): Object dn attributes (dict): Dictionary of attribute modifications Returns: SaAisErrorT: Return code of OI update runtime object """ # Get the class name for the object class_name = self.get_class_name_for_dn(dn) # Create and marshall attribute modifications attr_mods = [] for name, values in attributes.items(): if values is None: print("WARNING: Received no values for %s in %s" % (name, dn)) continue if not isinstance(values, list): values = [values] attr_type = self.get_attribute_type(name, class_name) c_attr_mod = SaImmAttrModificationT_2() c_attr_mod.modType = \ eSaImmAttrModificationTypeT.SA_IMM_ATTR_VALUES_REPLACE c_attr_mod.modAttr = SaImmAttrValuesT_2() c_attr_mod.modAttr.attrName = SaImmAttrNameT(name) c_attr_mod.modAttr.attrValueType = attr_type c_attr_mod.modAttr.attrValuesNumber = len(values) c_attr_mod.modAttr.attrValues = marshal_c_array(attr_type, values) attr_mods.append(c_attr_mod) rc = saImmOiRtObjectUpdate_2(self.handle, SaNameT(dn), attr_mods) if rc != eSaAisErrorT.SA_AIS_OK: log_err("saImmOiRtObjectUpdate_2 FAILED - %s" % eSaAisErrorT.whatis(rc)) return rc
def create_rt_object(class_name, parent_name, obj): ''' Creates a runtime object ''' # Marshall parameters c_class_name = SaImmClassNameT(class_name) if parent_name: c_parent_name = SaNameT(parent_name) else: c_parent_name = None c_attr_values = [] for name, (c_attr_type, values) in obj.attrs.iteritems(): if values == None: values = [] elif values == [None]: values = [] # Make sure all values are in lists if not isinstance(values, list): values = [values] # Create the values struct c_attr = SaImmAttrValuesT_2() c_attr.attrName = SaImmAttrNameT(name) c_attr.attrValueType = c_attr_type c_attr.attrValuesNumber = len(values) if len(values) == 0: c_attr.attrValues = None else: c_attr.attrValues = marshal_c_array(c_attr_type, values) c_attr_values.append(c_attr) # Call the function saImmOiRtObjectCreate_2(HANDLE, c_class_name, c_parent_name, c_attr_values)
def get(object_name, attr_name_list=None, class_name=None): ''' obtain values of some attributes of the specified object ''' # Always request the SaImmAttrClassName attribute if needed if attr_name_list and \ not class_name and \ not 'SaImmAttrClassName' in attr_name_list and \ not attr_name_list == ['SA_IMM_SEARCH_GET_CONFIG_ATTR']: attr_name_list.append('SaImmAttrClassName') attrib_names = [SaImmAttrNameT(a) for a in attr_name_list]\ if attr_name_list else None attributes = pointer(pointer(SaImmAttrValuesT_2())) try: err = saImmOmAccessorGet_2(ACCESSOR_HANDLE, SaNameT(object_name), attrib_names, attributes) except SafException as err: if err.value == eSaAisErrorT.SA_AIS_ERR_NOT_EXIST: return None else: raise err attribs = {} attr_list = unmarshalNullArray(attributes) for attr in attr_list: attr_range = range(attr.attrValuesNumber) attribs[attr.attrName] = [ attr.attrValueType, [ unmarshalSaImmValue(attr.attrValues[val], attr.attrValueType) for val in attr_range ] ] if not 'SaImmAttrClassName' in attribs and class_name: attribs['SaImmAttrClassName'] = class_name return ImmObject(object_name, attribs)
def create(self, obj, _parent_name=None): ''' add to the CCB the object 'obj' ''' if _parent_name is not None: parent_name = SaNameT(_parent_name) object_names = [parent_name] immom.saImmOmAdminOwnerSet(self.owner_handle, object_names, eSaImmScopeT.SA_IMM_SUBTREE) else: parent_name = None attr_values = [] for attr_name, type_values in obj.attrs.iteritems(): values = type_values[1] attr = SaImmAttrValuesT_2() attr.attrName = attr_name attr.attrValueType = type_values[0] attr.attrValuesNumber = len(values) attr.attrValues = marshal_c_array(attr.attrValueType, values) attr_values.append(attr) immom.saImmOmCcbObjectCreate_2(self.ccb_handle, obj.class_name, parent_name, attr_values)
def _modify(self, object_name, attr_name, values, mod_type): """ Modify an existing object Args: object_name (str): Object name attr_name (str): Attribute name values (list): List of attribute values mod_type (eSaImmAttrModificationTypeT): Modification type Return: SaAisErrorT: Return code of the corresponding IMM API call(s) """ if object_name is None: rc = eSaAisErrorT.SA_AIS_ERR_INVALID_PARAM else: if not self.accessor: self.accessor = ImmOmAccessor(self.init_version) self.accessor.init() # Get the attribute value type by reading the object's class # description rc, obj = self.accessor.get(object_name) if rc == eSaAisErrorT.SA_AIS_OK: class_name = obj.SaImmAttrClassName _, attr_def_list = self.get_class_description(class_name) value_type = None for attr_def in attr_def_list: if str(attr_def.attrName) == attr_name: value_type = attr_def.attrValueType break if value_type: rc = self.admin_owner.set_owner(object_name, eSaImmScopeT.SA_IMM_ONE) if rc == eSaAisErrorT.SA_AIS_OK: # Make sure the values field is a list if not isinstance(values, list): values = [values] attr_mods = [] attr_mod = saImmOm.SaImmAttrModificationT_2() attr_mod.modType = mod_type attr_mod.modAttr = SaImmAttrValuesT_2() attr_mod.modAttr.attrName = SaImmAttrNameT(attr_name) attr_mod.modAttr.attrValueType = value_type attr_mod.modAttr.attrValuesNumber = len(values) attr_mod.modAttr.attrValues = marshal_c_array( value_type, values) attr_mods.append(attr_mod) object_name = SaNameT(object_name) rc = agent.saImmOmCcbObjectModify_2(self.ccb_handle, object_name, attr_mods) if rc != eSaAisErrorT.SA_AIS_OK: log_err("saImmOmCcbObjectModify_2 FAILED - %s" % eSaAisErrorT.whatis(rc)) if rc == eSaAisErrorT.SA_AIS_ERR_BAD_HANDLE: init_rc = self.init() # If the re-initialization of agent handle succeeds, we still # need to return BAD_HANDLE to the users, so that they would # re-try the failed operation. Otherwise, the true error code # is returned to the user to decide further actions. if init_rc != eSaAisErrorT.SA_AIS_OK: rc = init_rc return rc