+ return
+
+def GetNodesByIds(ids):
+ ret = []
+ for node_id in ids:
+ node = PlcNode.get_by(node_id=node_id)
+ ret.append(node.plc_node_stats)
+ return ret
+
+def GetNodesBySite(loginbase):
+ site = PlcSite.get_by(loginbase=loginbase)
+ return GetNodesByIds(site.plc_site_stats['node_ids'])
+
+def GetNodeByName(hostname):
+ node = PlcNode.get_by(hostname=hostname)
+ return node.plc_node_stats
+
+def GetSitesByName(sitelist):
+ ret = []
+ for site in sitelist:
+ site = PlcSite.get_by(loginbase=site)
+ ret.append(site.plc_site_stats)
+ return ret
+
+def GetSitesById(idlist):
+ ret = []
+ for site_id in idlist:
+ site = PlcSite.get_by(site_id=site_id)
+ ret.append(site.plc_site_stats)
+ return ret
+
+def deleteExtra(l_plc, objectClass=PlcSite, dbKey='loginbase', plcKey='login_base'):
+ dbobjs = objectClass.query.all()
+ dbobj_key = [ getattr(s, dbKey) for s in dbobjs ]
+ plcobj_key = [ s[plcKey] for s in l_plc ]
+ extra_key = set(dbobj_key) - set(plcobj_key)
+ for obj in extra_key:
+ print "deleting %s" % obj
+ dbobj = objectClass.get_by(**{dbKey : obj})
+ dbobj.delete()
+
+def sync():
+ l_sites = plc.api.GetSites({'peer_id':None},
+ ['login_base', 'site_id', 'abbreviated_name', 'latitude',
+ 'longitude', 'max_slices', 'slice_ids', 'node_ids',
+ 'enabled', 'date_created' ])
+ l_nodes = plc.api.GetNodes({'peer_id':None},
+ ['hostname', 'node_id', 'ports', 'site_id', 'boot_state',
+ 'version', 'last_updated', 'date_created', 'key',
+ 'last_contact', 'pcu_ids', 'interface_ids'])
+ l_pcus = plc.api.GetPCUs()
+
+ print "sync sites"
+ for site in l_sites:
+ dbsite = PlcSite.findby_or_create(site_id=site['site_id'])
+ dbsite.loginbase = site['login_base']
+ dbsite.date_checked = datetime.now()
+ dbsite.plc_site_stats = site
+ deleteExtra(l_sites, PlcSite, 'loginbase', 'login_base')
+ deleteExtra(l_sites, HistorySiteRecord, 'loginbase', 'login_base')
+ session.flush()
+
+ print "sync pcus"
+ for pcu in l_pcus:
+ dbpcu = PlcPCU.findby_or_create(pcu_id=pcu['pcu_id'])
+ dbpcu.date_checked = datetime.now()
+ dbpcu.plc_pcu_stats = pcu
+ deleteExtra(l_pcus, PlcPCU, 'pcu_id', 'pcu_id')
+ deleteExtra(l_pcus, HistoryPCURecord, 'plc_pcuid', 'pcu_id')
+ deleteExtra(l_pcus, FindbadPCURecord, 'plc_pcuid', 'pcu_id')
+ session.flush()
+
+ print "sync nodes"
+ for node in l_nodes:
+ dbnode = PlcNode.findby_or_create(node_id=node['node_id'])
+ dbnode.hostname = node['hostname']
+ dbnode.date_checked = datetime.now()
+ dbnode.plc_node_stats = node
+ deleteExtra(l_nodes, PlcNode, 'hostname', 'hostname')
+ deleteExtra(l_nodes, HistoryNodeRecord, 'hostname', 'hostname')
+ deleteExtra(l_nodes, FindbadNodeRecord, 'hostname', 'hostname')
+ session.flush()