当前位置: 首页>>代码示例>>Python>>正文


Python Dao.updateObjects方法代码示例

本文整理汇总了Python中dao.Dao.updateObjects方法的典型用法代码示例。如果您正苦于以下问题:Python Dao.updateObjects方法的具体用法?Python Dao.updateObjects怎么用?Python Dao.updateObjects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dao.Dao的用法示例。


在下文中一共展示了Dao.updateObjects方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: L3ClosMediation

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import updateObjects [as 别名]
class L3ClosMediation():
    def __init__(self, conf = {}):
        if any(conf) == False:
            self.conf = util.loadConfig()
            logger.setLevel(logging.getLevelName(self.conf['logLevel'][moduleName]))
        else:
            self.conf = conf

        self.dao = Dao(self.conf)
        self.templateEnv = Environment(loader=PackageLoader('jnpr.openclos', junosTemplateLocation))
        self.templateEnv.keep_trailing_newline = True
        
    def loadClosDefinition(self, closDefination = os.path.join(util.configLocation, 'closTemplate.yaml')):
        '''
        Loads clos definition from yaml file and creates pod object
        '''
        try:
            stream = open(closDefination, 'r')
            yamlStream = yaml.load(stream)
            
            return yamlStream['pods']
        except (OSError, IOError) as e:
            print "File error:", e
        except (yaml.scanner.ScannerError) as e:
            print "YAML error:", e
            stream.close()
        finally:
            pass
       
    def createPod(self, podName, podDict, inventoryDict = None):
        '''
        Create a new POD
        '''
        pod = Pod(podName, **podDict)
        #pod.validate()
        self.dao.createObjects([pod])
        logger.info("Pod[id='%s', name='%s']: created" % (pod.id, podName)) 
        # update status
        pod.state = 'created'
        self.dao.updateObjects([pod])

        # shortcut for updating in createPod
        # this use case is typical in script invocation but not in ND REST invocation
        self.updatePodData(pod, podDict, inventoryDict)
        
        return pod

    def updatePodData(self, pod, podDict, inventoryDict):
        if podDict is None and inventoryDict is None:
            return 
            
        dirty = False
        # inventoryDict is the typical use case for ND REST invocation
        # podDict['inventory'] is the typical use case for script invocation
        inventoryData = None
        if inventoryDict is not None:
            inventoryData = inventoryDict
        elif 'inventory' in podDict and podDict['inventory'] is not None:
            json_inventory = open(os.path.join(util.configLocation, podDict['inventory']))
            inventoryData = json.load(json_inventory)
            json_inventory.close()

        # get current inventory stored in database
        # Note if user does not provide inventory, don't consider it is dirty
        if inventoryData is not None:
            # user provides an inventory. now check the inventory we already have in database
            if pod.inventoryData is not None:
                inventoryDataInDb = json.loads(zlib.decompress(base64.b64decode(pod.inventoryData)))
                if inventoryData != inventoryDataInDb:
                    dirty = True
                    logger.info("Pod[id='%s', name='%s']: inventory changed" % (pod.id, pod.name))
                else:
                    logger.info("Pod[id='%s', name='%s']: inventory not changed" % (pod.id, pod.name))
            else:
                dirty = True
                logger.info("Pod[id='%s', name='%s']: inventory changed" % (pod.id, pod.name))
                
        if 'spineDeviceType' in podDict and pod.spineDeviceType != podDict['spineDeviceType']:
            dirty = True
            logger.info("Pod[id='%s', name='%s']: spineDeviceType changed" % (pod.id, pod.name))
            
        if 'leafDeviceType' in podDict and pod.leafDeviceType != podDict['leafDeviceType']:
            dirty = True
            logger.info("Pod[id='%s', name='%s']: leafDeviceType changed" % (pod.id, pod.name))
            
        if 'interConnectPrefix' in podDict and pod.interConnectPrefix != podDict['interConnectPrefix']:
            dirty = True
            logger.info("Pod[id='%s', name='%s']: interConnectPrefix changed" % (pod.id, pod.name))
            
        if 'vlanPrefix' in podDict and pod.vlanPrefix != podDict['vlanPrefix']:
            dirty = True
            logger.info("Pod[id='%s', name='%s']: vlanPrefix changed" % (pod.id, pod.name))
            
        if 'loopbackPrefix' in podDict and pod.loopbackPrefix != podDict['loopbackPrefix']:
            dirty = True
            logger.info("Pod[id='%s', name='%s']: loopbackPrefix changed" % (pod.id, pod.name))
            
        if 'spineAS' in podDict and pod.spineAS != podDict['spineAS']:
            dirty = True
            logger.info("Pod[id='%s', name='%s']: spineAS changed" % (pod.id, pod.name))
#.........这里部分代码省略.........
开发者ID:bgp44,项目名称:juniper,代码行数:103,代码来源:l3Clos.py

示例2: L3ClosMediation

# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import updateObjects [as 别名]

#.........这里部分代码省略.........
            password = leaf.get('pass')
            device = Device(leaf['name'], pod.leafDeviceType, user, password, 'leaf', leaf['mac_address'], leaf['mgmt_ip'], pod)
            devices.append(device)

            portNames = util.getPortNamesForDeviceFamily(device.family, self.conf['deviceFamily'])
            for name in portNames['uplinkPorts']:   # all uplink IFDs towards spine
                ifd = InterfaceDefinition(name, device, 'uplink')
                interfaces.append(ifd)

            for name in portNames['downlinkPorts']:   # all downlink IFDs towards Access/Server
                ifd = InterfaceDefinition(name, device, 'downlink')
                interfaces.append(ifd)
        
        self.dao.createObjects(devices)
        self.dao.createObjects(interfaces)

    def createLinkBetweenIFDs(self, pod, links):
        # Caching all interfaces by deviceName...interfaceName for easier lookup
        interfaces = {}
        modifiedObjects = []
        for device in pod.devices:
            for interface in device.interfaces:
                name = device.name + '...' + interface.name
                interfaces[name] = interface

        for link in links:
            spineIntf = interfaces[link['s_name'] + '...' + link['s_port']]
            leafIntf = interfaces[link['l_name'] + '...' + link['l_port']]
            # hack to add relation from both sides as on ORM it is oneway one-to-one relation
            spineIntf.peer = leafIntf
            leafIntf.peer = spineIntf
            modifiedObjects.append(spineIntf)
            modifiedObjects.append(leafIntf)
        self.dao.updateObjects(modifiedObjects)
        
    def getLeafSpineFromPod(self, pod):
        '''
        utility method to get list of spines and leafs of a pod
        returns dict with list for 'spines' and 'leafs'
        '''
        deviceDict = {}
        deviceDict['leafs'] = []
        deviceDict['spines'] = []
        for device in pod.devices:
            if (device.role == 'leaf'):
                deviceDict['leafs'].append(device)
            elif (device.role == 'spine'):
                deviceDict['spines'].append(device)
        return deviceDict
    
    def allocateResource(self, pod):
        self.allocateLoopback(pod, pod.loopbackPrefix, pod.devices)
        leafSpineDict = self.getLeafSpineFromPod(pod)
        self.allocateIrb(pod, pod.vlanPrefix, leafSpineDict['leafs'])
        self.allocateInterconnect(pod.interConnectPrefix, leafSpineDict['spines'], leafSpineDict['leafs'])
        self.allocateAsNumber(pod.spineAS, pod.leafAS, leafSpineDict['spines'], leafSpineDict['leafs'])
        
    def allocateLoopback(self, pod, loopbackPrefix, devices):
        numOfIps = len(devices) + 2 # +2 for network and broadcast
        numOfBits = int(math.ceil(math.log(numOfIps, 2))) 
        cidr = 32 - numOfBits
        lo0Block = IPNetwork(loopbackPrefix + "/" + str(cidr))
        lo0Ips = list(lo0Block.iter_hosts())
        
        interfaces = []
        pod.allocatedLoopbackBlock = str(lo0Block.cidr)
开发者ID:rgiyer,项目名称:OpenClos,代码行数:70,代码来源:l3Clos.py


注:本文中的dao.Dao.updateObjects方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。