def decorate(*args, **kwargs): try: response = func(*args, **kwargs) return response except CdbException as ex: raise except Exception as ex: cls.getLogger().exception('%s' % ex) raise CdbException(exception=ex)
def query(*args, **kwargs): try: dbManager = DbManager.getInstance() session = dbManager.openSession() kwargs['session'] = session try: return func(*args, **kwargs) except CdbException, ex: raise except Exception, ex: cls.getLogger().exception('%s' % ex) raise CdbException(exception=ex)
def transaction(*args, **kwargs): try: dbManager = DbManager.getInstance() session = dbManager.openSession() kwargs['session'] = session try: result = func(*args, **kwargs) session.commit() return result except CdbException, ex: session.rollback() raise except Exception, ex: session.rollback() cls.getLogger().exception('%s' % ex) raise CdbException(exception=ex)
class FileSystemRestApi(CdbRestApi): def __init__(self, username=None, password=None, host=None, port=None, protocol=None): CdbRestApi.__init__(self, username, password, host, port, protocol) def getDirectoryList(self, path): try: directoryName = os.path.basename(path) parentDirectory = os.path.dirname(path) url = '%s/directories/%s?parentDirectory=%s' % (self.getContextRoot(), directoryName, parentDirectory) responseDict = self.sendRequest(url=url, method='GET') return responseDict except CdbException, ex: raise except Exception, ex: self.getLogger().exception('%s' % ex) raise CdbException(exception=ex)
def checkStatus(cls, httpHeaders): """ Map cdb status code into appropriate exception. """ code = httpHeaders.get('Cdb-Status-Code', None) msg = httpHeaders.get('Cdb-Status-Message', 'Internal Error') if code is None or code == str(cdbStatus.CDB_OK): return elif cdbExceptionMap.CDB_EXCEPTION_MAP.has_key(int(code)): # Exception string is value of the form 'x.y.z' # where 'x.y' is cdb module, and 'z' class in that module exStr = cdbExceptionMap.CDB_EXCEPTION_MAP.get(int(code)) exClass = exStr.split('.')[-1] # 'z' in 'x.y.z' exModule = '.'.join(exStr.split('.')[:-1]) # 'x.y' in 'x.y.z' exec 'from cdb.common.exceptions.%s import %s' % (exModule, exClass) exec 'ex = %s(msg)' % (exClass) raise ex else: raise CdbException(msg)
def __init__(self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_INVALID_ARGUMENT, **kwargs)
def __init__(self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_CONFIGURATION_ERROR, **kwargs)
def __init__ (self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_MULTIPLE_OBJECTS_FOUND, **kwargs)
def __init__(self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_INVALID_OBJECT_STATE, **kwargs)
def __init__(self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_AUTHORIZATION_ERROR, **kwargs)
def __init__(self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_COMMAND_FAILED, **kwargs)
def __init__(self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_DB_ERROR, **kwargs)
def __init__(self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_OBJECT_NOT_FOUND, **kwargs)
def __init__ (self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_OBJECT_ALREADY_EXISTS, **kwargs)
except Exception, ex: self.getLogger().exception('%s' % ex) raise CdbException(exception=ex) def writeFile(self, path, content): try: fileName = os.path.basename(path) parentDirectory = os.path.dirname(path) encodedFileContent = Encoder.encode(content) url = '%s/files/%s?parentDirectory=%s&encodedFileContent=%s' % ( self.getContextRoot(), fileName, parentDirectory, encodedFileContent) responseDict = self.sendSessionRequest(url=url, method='POST') return responseDict except CdbException, ex: raise except Exception, ex: self.getLogger().exception('%s' % ex) raise CdbException(exception=ex) ####################################################################### # Testing. if __name__ == '__main__': #api = FileSystemRestApi('sveseli', 'sveseli', 'zagreb.svdev.net', 10232, 'https') api = FileSystemRestApi('sveseli', 'sveseli', 'zagreb.svdev.net', 10232, 'http') print api.getDirectoryList('/home/sveseli') print api.writeFile('/tmp/xyz', 'Hi there, qweqweqsad \dsdd')
def __init__(self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_INVALID_SESSION, **kwargs)
def __init__(self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_EXTERNAL_SERVICE_ERROR, **kwargs)
def checkSpares(self): self.logger.debug('Checking status of spare parts.') catalogItemsWithSparePartsConfiguration = self.itemDbApi.getItemsWithPropertyType( self.SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME, itemDomainName=self.CATALOG_DOMAIN_NAME) for catalogItem in catalogItemsWithSparePartsConfiguration: catalogItemId = catalogItem.data['id'] catalogSelfElementId = self.getSelfElementIdForItemId( catalogItemId) sparePartsConfigurationPropertyValue = self.propertyDbApi.getPropertyValueListForItemElementId( catalogSelfElementId, propertyTypeName=self. SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME) if sparePartsConfigurationPropertyValue: metadataDict = self.getPropertyMetadataDict( sparePartsConfigurationPropertyValue[0]) else: raise ObjectNotFound( "Could not find required property: %s" % self.SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME) if self.SPARE_PARTS_CONFIGURATION_MIN_KEY in metadataDict: minSpares = int( metadataDict[self.SPARE_PARTS_CONFIGURATION_MIN_KEY]) else: raise CdbException( "required metadata %s not specified for spare part configuration" % self.SPARE_PARTS_CONFIGURATION_MIN_KEY) email = '' if metadataDict.has_key(self.SPARE_PARTS_CONFIGURATION_EMAIL_KEY): emailValue = metadataDict[ self.SPARE_PARTS_CONFIGURATION_EMAIL_KEY] if emailValue == self.SPARE_PARTS_CONFIGURATION_NOEMAIL_VALUE: email = None else: email = emailValue else: # Use owner user email. email = self.getOwnerUserEmail(catalogItemId) sparePartsList = self.itemDbApi.getItemsWithPropertyType( self.ITEM_DOMAIN_INVENTORY_STATUS_PROPERTY_TYPE_NAME, itemDerivedFromItemId=catalogItemId, propertyValueMatch=self. ITEM_DOMAIN_INVENTORY_STATUS_SPARE_VALUE) spares = sparePartsList.__len__() if minSpares > spares: validNotification = True sparesMessage = self.generateSparesMessage(minSpares, spares) spareLogEntries = self.logDbApi.getLogEntriesForItemElementId( catalogSelfElementId, self.SPARE_PARTS_WARNING_LOG_LEVEL) if spareLogEntries: lastSpareLogEntry = spareLogEntries[-1] lastSparesLogMessage = lastSpareLogEntry.data['text'] if lastSparesLogMessage == sparesMessage: validNotification = False if validNotification: self.addSparePartsWarningLogEntryToItem( catalogSelfElementId, sparesMessage) if email is not None: itemUrl = self.cdbPortalUrlUtility.getItemUrlAddress( catalogItemId) catalogItemName = catalogItem.data['name'] catalogItemModel = catalogItem.data['item_identifier1'] catalogItemAlternateName = catalogItem.data[ 'item_identifier2'] itemDictValue = '<a href=%s>%s</a>' % (itemUrl, catalogItemName) # Create an ordered dict for the table emailed to user. informationDict = OrderedDict() informationDict['Name'] = itemDictValue informationDict['Model'] = catalogItemModel informationDict[ 'Alternate Name'] = catalogItemAlternateName informationDict['Spares On Hand'] = spares informationDict['Minimum Spares Required '] = minSpares emailTable = CdbEmailUtility.generateSimpleHtmlTableMessage( informationDict) emailMessage = '%s <br/><br/> %s' % (sparesMessage, emailTable) self.emailUtility.sendEmailNotification( email, self.SPARE_PARTS_EMAIL_NOTIFICATION_NAME, emailMessage)
def __init__(self, error='', **kwargs): CdbException.__init__(self, error, cdbStatus.CDB_IMAGE_PROCESSING_FAILED, **kwargs)