def _build_storage_array(st_array): '@types: StorageArray -> osh' osh = ObjectStateHolder('storagearray') populateOSH(osh, { 'storagearray_serialnumber': st_array.sn, 'data_name': st_array.alias, 'data_description': st_array.type, 'storagearray_model': st_array.model, 'storagearray_vendor': st_array.vendor, 'storagearray_version':'Microcode %s, patch %s' % ( st_array.microcode, st_array.microcode_patch), 'host_key': st_array.sn, 'host_iscomplete': True}) osh.setListAttribute('node_role', ['storage_array']) return osh
def build(self, certificate): ''' Build ssl_certificate OSH from X509Certificate @types: X509Certificate -> ObjectStateHolderVector ''' certOsh = ObjectStateHolder('digital_certificate') certOsh.setDateAttribute("valid_to", certificate.expiresOn) certOsh.setDateAttribute("create_on", certificate.createOn) certOsh.setStringAttribute("issuer", unicode(certificate.issuer.raw)) certOsh.setStringAttribute("subject", unicode(certificate.subject.raw)) certOsh.setStringAttribute("serial_number", certificate.sn) if certificate.version: certOsh.setIntegerAttribute("version", certificate.version) if certificate.signatureAlgorithm: certOsh.setStringAttribute("signature_algorithm", certificate.signatureAlgorithm) if certificate.type: certOsh.setStringAttribute("type", certificate.type) organization = certificate.subject.dn.find_first('O') if organization: certOsh.setStringAttribute("organization", organization.value) organization_unit = certificate.subject.dn.lookup('OU') if organization_unit: ou = map(lambda obj: str(obj.value), organization_unit) certOsh.setListAttribute("organization_unit", ou) cnSubject = certificate.subject.dn.find_first('CN') if cnSubject and cnSubject.value: certOsh.setStringAttribute("common_name", cnSubject.value) cnIssuer = certificate.issuer.dn.find_first('CN') oIssuer = certificate.issuer.dn.find_first('O') issuerName = None if cnIssuer and cnIssuer.value: issuerName = cnIssuer.value else: issuerName = oIssuer and oIssuer.value certOsh.setStringAttribute("issuer_name", issuerName) isSelfSigned = certificate.subject.raw == certificate.issuer.raw certOsh.setBoolAttribute("is_self_signed", isSelfSigned) return certOsh
class OshBuilder(CiBuilder): def __init__(self, targetCiType): self.__type = targetCiType self.__osh = ObjectStateHolder(self.__type) def setCiAttribute(self, name, value): attributeType = self.__getAttributeType(self.__type, name) if value: self.__setValue(name, attributeType, value) else: logger.debug("Meet none value for %s, type:%s" % (name, attributeType)) def build(self): return self.__osh def __setValue(self, name, attributeType, value): if attributeType == 'string': self.__osh.setStringAttribute(name, str(value)) elif attributeType == 'integer': self.__osh.setIntegerAttribute(name, int(value)) elif attributeType.endswith('enum'): if isinstance(value, int): self.__osh.setEnumAttribute(name, value) else: self.__osh.setAttribute(name, value) elif attributeType == 'string_list': self.__osh.setListAttribute(name, value) else: raise ValueError('no setter defined for type %s' % attributeType) def __getAttributeType(self, ciType, attributeName): try: attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition( ciType, attributeName) return attributeDefinition.getType() except: if DEBUG: if attributeName in ['memory_size', 'port_index']: return 'integer' return 'string' raise ValueError("Failed to determine type of %s.%s" % (ciType, attributeName))
class OshBuilder(CiBuilder): def __init__(self, targetCiType): self.__type = targetCiType self.__osh = ObjectStateHolder(self.__type) def setCiAttribute(self, name, value): attributeType = self.__getAttributeType(self.__type, name) if value: self.__setValue(name, attributeType, value) else: logger.debug("Meet none value for %s, type:%s" % (name, attributeType)) def build(self): return self.__osh def __setValue(self, name, attributeType, value): if attributeType == 'string': self.__osh.setStringAttribute(name, str(value)) elif attributeType == 'integer': self.__osh.setIntegerAttribute(name, int(value)) elif attributeType.endswith('enum'): if isinstance(value, int): self.__osh.setEnumAttribute(name, value) else: self.__osh.setAttribute(name, value) elif attributeType == 'string_list': self.__osh.setListAttribute(name, value) else: raise ValueError('no setter defined for type %s' % attributeType) def __getAttributeType(self, ciType, attributeName): try: attributeDefinition = modeling._CMDB_CLASS_MODEL.getAttributeDefinition(ciType, attributeName) return attributeDefinition.getType() except: if DEBUG: if attributeName in ['memory_size', 'port_index']: return 'integer' return 'string' raise ValueError("Failed to determine type of %s.%s" % (ciType, attributeName))
def processObjects(allObjects): vector = ObjectStateHolderVector() iter = allObjects.iterator() #ciList = [[id, type, props]] ciList = [] ciDict = {} createCi = 1 while iter.hasNext(): #attributes = [name, type, key, value] attributes = [] objectElement = iter.next() mamId = objectElement.getAttribute('mamId').getValue() cit = objectElement.getAttribute('name').getValue() if mamId != None and cit != None: # add the attributes... allAttributes = objectElement.getChildren('field') iterAtt = allAttributes.iterator() while iterAtt.hasNext(): attElement = iterAtt.next() attName = attElement.getAttribute('name').getValue() attType = attElement.getAttribute('datatype').getValue() #print 'GOT TYPE: ', attType attKey = attElement.getAttribute('key') attValue = attElement.getText() if attType == None or attType == "": attType = "string" if attKey == None or attKey == "": attKey = "false" else: attKey = attKey.getValue() if attName != "" and attType != "": attributes.append([attName, attType, attKey, attValue]) # create CI or not? Is key empty or none? if attKey == "true": # print 'KEY ATTRIB <', attName, '> with value <', attValue, '> for CIT <', cit, '> with MAMID <', mamId, '>' if attValue != None and attValue.strip() != "": createCi = 1 else: createCi = 0 #info (concatenate("Id: ", mamId, ", Type: ", cit, ", Properties: ", attributes)) if createCi == 1: ciList.append([mamId, cit, attributes]) ciDict[mamId] = [mamId, cit, attributes] for ciVal in ciList: dontCreateCI = 0 #info("\tAdding %s [%s] => [%s]" % (ciVal[1], ciVal[0], ciVal[2]) ) id = ciVal[0] type = ciVal[1] osh = ObjectStateHolder(type) if ciVal[2] != None: props = ciVal[2] createContainer = 0 containerOsh = None for prop in props: if prop[0] == 'root_container': if dontCreateCI or prop[3] not in ciDict.keys(): # print 'MAM ID <', prop[3], '> not found!' dontCreateCI = 1 continue parent = ciDict[prop[3]] # create the container osh if parent != None: #parentId = parent[0] parentType = parent[1] parentProps = parent[2] containerOsh = ObjectStateHolder(parentType) if parentProps != None: for parentProp in parentProps: containerOsh.setAttribute( parentProp[0], parentProp[3]) createContainer = 1 #print 'Props <', prop, '>' try: if prop[1] == 'StrProp': osh.setStringAttribute(prop[0], prop[3]) elif prop[1] == 'StrListProp' and prop[3] != None and prop[ 3] != '' and len(prop[3]) > 0: osh.setListAttribute(prop[0], [prop[3]]) elif prop[1] == 'DoubleProp' and prop[3] != None and prop[ 3] != '' and len(prop[3]) > 0: osh.setDoubleAttribute(prop[0], prop[3]) elif prop[1] == 'FloatProp' and prop[3] != None and prop[ 3] != '' and len(prop[3]) > 0: osh.setFloatAttribute(prop[0], prop[3]) elif prop[1] == 'IntProp' and prop[3] != None and prop[ 3] != '' and len(prop[3]) > 0: #print '[VINAY] Got int <', prop[3], '>' osh.setIntegerAttribute(prop[0], prop[3]) elif prop[1] == 'LongProp' and prop[3] != None and prop[ 3] != '' and len(prop[3]) > 0: #print '[VINAY] Got long <', prop[3], '>' osh.setLongAttribute(prop[0], prop[3]) elif prop[1] == 'BoolProp' and prop[3] != None and prop[ 3] != '' and len(prop[3]) > 0: osh.setBoolAttribute(prop[0], prop[3]) elif prop[3] != None and prop[3] != '' and len( prop[3]) > 0: osh.setAttribute(prop[0], prop[3]) if createContainer == 1: osh.setContainer(containerOsh) except: stacktrace = traceback.format_exception( sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) logger.warn('Exception setting attribute <', prop[0], '> with value <', prop[3], '>:\n', stacktrace) pass if dontCreateCI: continue vector.add(osh) return (vector, ciDict)
def processObjects(allObjects): vector = ObjectStateHolderVector() iter = allObjects.iterator() #ciList = [[id, type, props]] ciList = [] ciDict = {} createCi = 1 while iter.hasNext(): #attributes = [name, type, key, value] attributes = [] objectElement = iter.next() mamId = objectElement.getAttribute('mamId').getValue() cit = objectElement.getAttribute('name').getValue() if mamId != None and cit != None: # add the attributes... allAttributes = objectElement.getChildren('field') iterAtt = allAttributes.iterator() while iterAtt.hasNext(): attElement = iterAtt.next() attName = attElement.getAttribute('name').getValue() attType = attElement.getAttribute('datatype').getValue() #print 'GOT TYPE: ', attType attKey = attElement.getAttribute('key') attValue = attElement.getText() if attType == None or attType == "": attType = "string" if attKey == None or attKey == "": attKey = "false" else: attKey = attKey.getValue() if attName != "" and attType != "": attributes.append([attName, attType, attKey, attValue]) # create CI or not? Is key empty or none? if attKey == "true": # print 'KEY ATTRIB <', attName, '> with value <', attValue, '> for CIT <', cit, '> with MAMID <', mamId, '>' if attValue != None and attValue.strip() != "": createCi = 1 else: createCi = 0 #info (concatenate("Id: ", mamId, ", Type: ", cit, ", Properties: ", attributes)) if createCi == 1: ciList.append([mamId, cit, attributes]) ciDict[mamId] = [mamId, cit, attributes] for ciVal in ciList: dontCreateCI = 0 #info("\tAdding %s [%s] => [%s]" % (ciVal[1], ciVal[0], ciVal[2]) ) id = ciVal[0] type = ciVal[1] osh = ObjectStateHolder(type) if ciVal[2] != None: props = ciVal[2] createContainer = 0 containerOsh = None for prop in props: if prop[0] == 'root_container': if dontCreateCI or prop[3] not in ciDict.keys(): # print 'MAM ID <', prop[3], '> not found!' dontCreateCI = 1 continue parent = ciDict[prop[3]] # create the container osh if parent != None: #parentId = parent[0] parentType = parent[1] parentProps = parent[2] containerOsh = ObjectStateHolder(parentType) if parentProps != None: for parentProp in parentProps: containerOsh.setAttribute(parentProp[0], parentProp[3]) createContainer = 1 #print 'Props <', prop, '>' try: if prop[1] == 'StrProp': osh.setStringAttribute(prop[0], prop[3]) elif prop[1] == 'StrListProp' and prop[3] != None and prop[3] != '' and len(prop[3]) > 0: osh.setListAttribute(prop[0], [prop[3]]) elif prop[1] == 'DoubleProp' and prop[3] != None and prop[3] != '' and len(prop[3]) > 0: osh.setDoubleAttribute(prop[0], prop[3]) elif prop[1] == 'FloatProp' and prop[3] != None and prop[3] != '' and len(prop[3]) > 0: osh.setFloatAttribute(prop[0], prop[3]) elif prop[1] == 'IntProp' and prop[3] != None and prop[3] != '' and len(prop[3]) > 0: #print '[VINAY] Got int <', prop[3], '>' osh.setIntegerAttribute(prop[0], prop[3]) elif prop[1] == 'LongProp' and prop[3] != None and prop[3] != '' and len(prop[3]) > 0: #print '[VINAY] Got long <', prop[3], '>' osh.setLongAttribute(prop[0], prop[3]) elif prop[1] == 'BoolProp' and prop[3] != None and prop[3] != '' and len(prop[3]) > 0: osh.setBoolAttribute(prop[0], prop[3]) elif prop[3] != None and prop[3] != '' and len(prop[3]) > 0: osh.setAttribute(prop[0], prop[3]) if createContainer == 1: osh.setContainer(containerOsh) except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) logger.warn('Exception setting attribute <', prop[0], '> with value <', prop[3], '>:\n', stacktrace) pass if dontCreateCI: continue vector.add(osh) return (vector, ciDict)