def applyOtherProps(self, device, device_specs): """ Apply non-zProperty settings (if any) to the device. @parameter device: device to modify @type device: DMD device object @parameter device_specs: device creation dictionary @type device_specs: dictionary """ self.log.debug("Applying other properties...") internalVars = [ 'deviceName', 'devicePath', 'loader', 'loader_arg_keys', 'manageIp', ] @transactional def setNamedProp(self, org, name, description): setattr(org, name, description) for functor, value in device_specs.items(): if iszprop(functor) or \ iscustprop(functor) or functor in internalVars: continue # Special case for organizers which can take a description if functor in ('description', 'address', 'comments', 'rackSlot'): if hasattr(device, functor): setNamedProp(self, device, functor, value) continue try: self.log.debug("For %s, calling device.%s(%s)", device.id, functor, value) func = getattr(device, functor, None) if func is None or not callable(func): self.log.warn( "The function '%s' for device %s is not found.", functor, device.id) elif isinstance(value, (list, tuple)): # The function either expects a list or arguments # So, try as an arguments try: func(*value) # Try as a list except TypeError: func(value) else: func(value) except ConflictError: raise except Exception: msg = "Device %s device.%s(%s) failed" % (device.id, functor, value) self.reportException(msg, device.id)
def applyOtherProps(self, device, device_specs): """ Apply non-zProperty settings (if any) to the device. @parameter device: device to modify @type device: DMD device object @parameter device_specs: device creation dictionary @type device_specs: dictionary """ self.log.debug("Applying other properties...") internalVars = [ 'deviceName', 'devicePath', 'loader', 'loader_arg_keys', 'manageIp', ] @transactional def setNamedProp(self, org, name, description): setattr(org, name, description) for functor, value in device_specs.items(): if iszprop(functor) or \ iscustprop(functor) or functor in internalVars: continue # Special case for organizers which can take a description if functor in ('description', 'address', 'comments', 'rackSlot'): if hasattr(device, functor): setNamedProp(self, device, functor, value) continue try: self.log.debug( "For %s, calling device.%s(%s)", device.id, functor, value ) func = getattr(device, functor, None) if func is None or not callable(func): self.log.warn( "The function '%s' for device %s is not found.", functor, device.id ) elif isinstance(value, (list, tuple)): # The function either expects a list or arguments # So, try as an arguments try: func(*value) # Try as a list except TypeError: func(value) else: func(value) except ConflictError: raise except Exception: msg = "Device %s device.%s(%s) failed" % ( device.id, functor, value ) self.reportException(msg, device.id)
def device_info_fact(device): """Given a device or component, generates its device info fact. """ f = Fact() f.set_context_uuid_from_object(device) f.set_meta_type_from_object(device) f.metadata[DimensionKeys.PLUGIN_KEY] = DEVICE_INFO_FACT_PLUGIN f.data[MetadataKeys.NAME_KEY] = device.titleOrId() f.data[MetadataKeys.PROD_STATE_KEY] = device.getProductionStateString() valid_types = ( str, int, long, float, bool, list, tuple, set, ) for propdict in device._propertyMap(): propId = propdict.get("id") if (not device.isLocal(propId) or iszprop(propId) or iscustprop(propId) or device.zenPropIsPassword(propId)): continue value = None # Some of the device properties can be methods, # so we have to call them and get values. if callable(device.getProperty(propId)): try: value = device.getProperty(propId).__call__() except TypeError as e: log.exception( "Unable to call property: %s. Exception %s", device.getProperty(propId), e, ) else: value = device.getProperty(propId) if value is None: value = "" if isinstance(value, valid_types): f.data[propId] = value f.data[MetadataKeys.ID_KEY] = device.id title = device.title if callable(title): title = title() f.data[MetadataKeys.TITLE_KEY] = title try: dev_rel = device.device() f.data.update({ MetadataKeys.DEVICE_UUID_KEY: get_context_uuid(dev_rel), MetadataKeys.DEVICE_KEY: dev_rel.id, }) except Exception: pass return f
def applyZProps(self, device, device_specs): """ Apply zProperty settings (if any) to the device. @parameter device: device to modify @type device: DMD device object @parameter device_specs: device creation dictionary @type device_specs: dictionary """ self.log.debug("Applying zProperties...") # Returns a list of (key, value) pairs. # Convert it to a dictionary. dev_zprops = dict(device.zenPropertyItems()) for zprop, value in device_specs.items(): self.log.debug( "Evaluating zProperty <%s -> %s> on %s", zprop, value, device.id ) if not iszprop(zprop): self.log.debug( "Evaluating zProperty <%s -> %s> on %s: not iszprop()", zprop, value, device.id ) continue if zprop in dev_zprops: try: self.log.debug( "Setting zProperty <%s -> %s> on %s " "(currently set to %s)", zprop, value, device.id, getattr(device, zprop, 'notset') ) device.setZenProperty(zprop, value) except BadRequest: self.log.warn( "Object %s zproperty %s is invalid or duplicate", device.titleOrId(), zprop ) except Exception as ex: self.log.warn( "Object %s zproperty %s not set (%s)", device.titleOrId(), zprop, ex ) self.log.debug( "Set zProperty <%s -> %s> on %s (now set to %s)", zprop, value, device.id, getattr(device, zprop, 'notset') ) else: self.log.warn( "The zproperty %s doesn't exist in %s", zprop, device_specs.get('deviceName', device.id) )
def applyZProps(self, device, device_specs): """ Apply zProperty settings (if any) to the device. @parameter device: device to modify @type device: DMD device object @parameter device_specs: device creation dictionary @type device_specs: dictionary """ self.log.debug("Applying zProperties...") # Returns a list of (key, value) pairs. # Convert it to a dictionary. dev_zprops = dict(device.zenPropertyItems()) for zprop, value in device_specs.items(): self.log.debug("Evaluating zProperty <%s -> %s> on %s" % (zprop, value, device.id)) if not iszprop(zprop): self.log.debug( "Evaluating zProperty <%s -> %s> on %s: not iszprop()" % (zprop, value, device.id)) continue if zprop in dev_zprops: try: self.log.debug( "Setting zProperty <%s -> %s> on %s (currently set to %s)" % (zprop, value, device.id, getattr(device, zprop, 'notset'))) device.setZenProperty(zprop, value) except BadRequest: self.log.warn( "Object %s zproperty %s is invalid or duplicate" % (device.titleOrId(), zprop)) except Exception, ex: self.log.warn("Object %s zproperty %s not set (%s)" % (device.titleOrId(), zprop, ex)) self.log.debug( "Set zProperty <%s -> %s> on %s (now set to %s)" % (zprop, value, device.id, getattr(device, zprop, 'notset'))) else: self.log.warn( "The zproperty %s doesn't exist in %s" % (zprop, device_specs.get('deviceName', device.id)))
def __init__(self, jobrecord): self.zProperties = {} for prop in jobrecord.__dict__: if iszprop(prop): self.zProperties[prop] = getattr(jobrecord, prop)