blob: 4f57c3bb5df8effe7f228ff32880be29c1bafa14 [file] [log] [blame]
Tony Mackf1c12242013-04-09 16:08:43 -04001from plstackapi.core.models import Slice
Tony Mackf593dba2013-03-24 13:17:43 -04002
3class SliceImporter:
4
Tony Mack743ddf22013-04-02 07:36:25 -04005 def __init__(self, api):
6 self.api = api
7 self.remote_slices = {}
8 self.local_slices = {}
Tony Mackf593dba2013-03-24 13:17:43 -04009
Tony Mack743ddf22013-04-02 07:36:25 -040010 def run(self, remote_sites={}, local_sites={}):
11 if not remote_sites:
Tony Mack172f8882013-04-02 11:36:34 -040012 sites = self.api.GetSites({'peer_id': None})
Tony Mack743ddf22013-04-02 07:36:25 -040013 for site in sites:
14 remote_sites[site['site_id']] = site
Tony Mackdd12c682013-04-02 10:15:27 -040015
Tony Mack743ddf22013-04-02 07:36:25 -040016
17 if not local_sites:
Tony Mackf1c12242013-04-09 16:08:43 -040018 from plstackapi.core.models import Site
Tony Mack743ddf22013-04-02 07:36:25 -040019 sites = Site.objects.all()
20 for site in sites:
21 local_sites[site.login_base] = site
22
Tony Mackf593dba2013-03-24 13:17:43 -040023 db_slices = Slice.objects.all()
Tony Mack743ddf22013-04-02 07:36:25 -040024 for db_slice in db_slices:
25 self.local_slices[db_slice.name] = db_slice
Tony Mackdd12c682013-04-02 10:15:27 -040026 print "%s local slices" % len(db_slices)
Tony Mack743ddf22013-04-02 07:36:25 -040027
Tony Mack1da8b202013-04-02 11:43:30 -040028 slices = self.api.GetSlices({'peer_id': None})
Tony Mack787f46e2013-04-02 11:24:45 -040029 print "%s remote slices" % len(slices)
Tony Mackf1afe392013-04-02 07:40:38 -040030 count = 0
Tony Mackf593dba2013-03-24 13:17:43 -040031 for slice in slices:
Tony Macka3a40cf2013-04-02 11:21:21 -040032 self.remote_slices[slice['slice_id']] = slice
Tony Mack743ddf22013-04-02 07:36:25 -040033 if slice['name'] not in self.local_slices:
34 site = local_sites[remote_sites[slice['site_id']]['login_base']]
Tony Macka3a40cf2013-04-02 11:21:21 -040035 new_slice = Slice(name=slice['name'],
Tony Mackf593dba2013-03-24 13:17:43 -040036 instantiation=slice['instantiation'],
37 omf_friendly = False,
38 description = slice['description'],
Tony Mack743ddf22013-04-02 07:36:25 -040039 slice_url = slice['url'],
40 site = site)
41 new_slice.save()
Tony Mackf1afe392013-04-02 07:40:38 -040042 count += 1
Tony Mack743ddf22013-04-02 07:36:25 -040043 self.local_slices[new_slice.name] = new_slice
Tony Mackf1afe392013-04-02 07:40:38 -040044 print "Imported %s slices" % count
Tony Mackf593dba2013-03-24 13:17:43 -040045
46
47