本文整理汇总了Python中dao.Dao.createObjects方法的典型用法代码示例。如果您正苦于以下问题:Python Dao.createObjects方法的具体用法?Python Dao.createObjects怎么用?Python Dao.createObjects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dao.Dao
的用法示例。
在下文中一共展示了Dao.createObjects方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: L3ClosMediation
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import createObjects [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))
#.........这里部分代码省略.........
示例2: L3ClosMediation
# 需要导入模块: from dao import Dao [as 别名]
# 或者: from dao.Dao import createObjects [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 isRecreateFabric(self, podInDb, podDict):
'''
If any device type/family, ASN range or IP block changed, that would require
re-generation of the fabric, causing new set of IP and ASN assignment per device
'''
if (podInDb.spineDeviceType != podDict['spineDeviceType'] or \
podInDb.leafDeviceType != podDict['leafDeviceType'] or \
podInDb.interConnectPrefix != podDict['interConnectPrefix'] or \
podInDb.vlanPrefix != podDict['vlanPrefix'] or \
podInDb.loopbackPrefix != podDict['loopbackPrefix'] or \
podInDb.spineAS != podDict['spineAS'] or \
podInDb.leafAS != podDict['leafAS']):
return True
return False
def processFabric(self, podName, pod, reCreateFabric = False):
# REVISIT use of reCreateFabric
try:
podInDb = self.dao.getUniqueObjectByName(Pod, podName)
except (exc.NoResultFound) as e:
logger.debug("No Pod found with pod name: '%s', exc.NoResultFound: %s" % (podName, e.message))
else:
logger.debug("Deleted existing pod name: '%s'" % (podName))
self.dao.deleteObject(podInDb)
podInDb = Pod(podName, **pod)
podInDb.validate()
self.dao.createObjects([podInDb])
logger.info("Created pod name: '%s'" % (podName))
self.processTopology(podName, reCreateFabric)
# backup current database
util.backupDatabase(self.conf)
return podInDb
def processTopology(self, podName, reCreateFabric = False):
'''
Finds Pod object by name and process topology
It also creates the output folders for pod
'''
try:
pod = self.dao.getUniqueObjectByName(Pod, podName)
except (exc.NoResultFound) as e:
raise ValueError("No Pod found with pod name: '%s', exc.NoResultFound: %s" % (podName, e.message))
except (exc.MultipleResultsFound) as e:
raise ValueError("Multiple Pods found with pod name: '%s', exc.MultipleResultsFound: %s" % (podName, e.message))
if pod.inventory is not None:
# topology handling is divided into 3 steps:
# 1. load inventory
# 2. create cabling plan
# 3. create configuration files
# 1. load inventory
json_inventory = open(os.path.join(util.configLocation, pod.inventory))
inventory = json.load(json_inventory)
json_inventory.close()
self.createSpineIFDs(pod, inventory['spines'])
self.createLeafIFDs(pod, inventory['leafs'])
# 2. create cabling plan in JSON format
cablingPlanWriter = CablingPlanWriter(self.conf, pod, self.dao)
cablingPlanJSON = cablingPlanWriter.writeJSON()
self.createLinkBetweenIFDs(pod, cablingPlanJSON['links'])
# create cabling plan in DOT format
cablingPlanWriter.writeDOT()
# 3. allocate resource and create configuration files
self.allocateResource(pod)
self.generateConfig(pod);
#.........这里部分代码省略.........