Package backend :: Package server :: Package importlib :: Module orgImport
[hide private]
[frames] | no frames]

Source Code for Module backend.server.importlib.orgImport

  1  # 
  2  # Copyright (c) 2013--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15  # 
 16  # Org import process 
 17  # 
 18   
 19  from importLib import Import 
 20   
 21  # Thanks for this class goes to Alex Martelli: 
 22  # http://stackoverflow.com/questions/1151658/python-hashable-dicts 
 23   
 24   
25 -class hashabledict(dict):
26
27 - def __key(self):
28 return tuple((k, self[k]) for k in sorted(self))
29
30 - def __hash__(self):
31 return hash(self.__key())
32
33 - def __eq__(self, other):
34 return self.__key() == other.__key()
35 36
37 -class OrgImport(Import):
38
39 - def __init__(self, batch, backend, master_label, create_orgs=False):
40 Import.__init__(self, batch, backend) 41 self.master_label = master_label 42 self.create_orgs = create_orgs 43 self._create_maps()
44
45 - def _create_maps(self):
46 org_map = self.backend.lookupOrgMap(self.master_label) 47 self.mn_to_mi = org_map['master-name-to-master-id'] 48 self.mi_to_li = org_map['master-id-to-local-id']
49
50 - def submit(self):
51 try: 52 # Always happens: if it does not exist, we create a master record 53 if not self.backend.lookupMaster(self.master_label): 54 self.backend.createMaster(self.master_label) 55 56 # Always happens: if each org has not been synced before, 57 # create master org record. 58 missing_master_orgs = [] 59 for org in self.batch: 60 if org['name'] not in list(self.mn_to_mi.keys()): 61 missing_master_orgs.append(org) 62 if len(missing_master_orgs) > 0: 63 self.backend.createMasterOrgs(self.master_label, 64 missing_master_orgs) 65 66 # Iff we are force-creating local orgs, create any orgs that are 67 # not already mapped to local orgs. If a local org exists with 68 # the same name as the master org, use that instead. Link local 69 # orgs with master orgs. 70 if self.create_orgs: 71 orgs_to_create = [] 72 orgs_to_link = [] 73 update_master_orgs = [] 74 for org in self.batch: 75 if (org['id'] not in list(self.mi_to_li.keys()) 76 or not self.mi_to_li[org['id']]): 77 local_id = self.backend.lookupOrg(org['name']) 78 if local_id: 79 orgs_to_link.append({ 80 'master_id': org['id'], 81 'local_id': local_id}) 82 else: 83 orgs_to_create.append(org['name']) 84 if len(orgs_to_create) > 0: 85 new_org_map = self.backend.createOrgs(orgs_to_create) 86 for org in orgs_to_create: 87 update_master_orgs.append({ 88 'master_id': self.mn_to_mi[org], 89 'local_id': new_org_map[org]}) 90 update_master_orgs += orgs_to_link 91 if len(update_master_orgs) > 0: 92 self.backend.updateMasterOrgs(update_master_orgs) 93 94 # refresh maps after we've just changed things 95 self._create_maps() 96 97 # Iff we have a master org mapped to local org, create org 98 # trust records 99 # we need to uniquify in case user has mapped multiple orgs 100 # together 101 trusts_to_create = set([]) 102 for org in self.batch: 103 for trust in org['org_trust_ids']: 104 if (org['id'] in list(self.mi_to_li.keys()) 105 and trust['org_id'] in list(self.mi_to_li.keys())): 106 my_org_id = self.mi_to_li[org['id']] 107 self.backend.clearOrgTrusts(my_org_id) 108 my_trust_id = self.mi_to_li[trust['org_id']] 109 trusts_to_create.add(hashabledict({ 110 'org_id': my_org_id, 111 'trust': my_trust_id})) 112 # org trusts are always bi-directional 113 # (even if we are not syncing the other org) 114 trusts_to_create.add(hashabledict({ 115 'org_id': my_trust_id, 116 'trust': my_org_id})) 117 118 if len(trusts_to_create) > 0: 119 self.backend.createOrgTrusts(trusts_to_create) 120 except: 121 self.backend.rollback() 122 raise 123 self.backend.commit()
124