Merge Master in geni-v3 conflict resolution
[sfa.git] / sfa / client / client_helper.py
1 ###
2 #
3 # Thierry - 2012 sept 21
4 #
5 # it seems terribly wrong that the client should decide to use PG- or PL- related code
6 # esp. in a context where we're trying to have more and more kinds of testbeds involved
7 #
8 # also, the 'users' filed that CreateSliver is expecting (the key point here is to get this right)
9 # is specified to have at least a urn and a list of keys, both of these being supported natively
10 # in the sfa db
11 # So long story short, it seems to me that we should have a common code that fills in 'urn' and 'keys'
12 # and then code that tentatively tries to add as much extra info that we can get on these users
13 #
14 # the fact e.g. that PlanetLab insists on getting a first_name and last_name is not
15 # exactly consistent with the GENI spec. of CreateSliver
16 #
17 def pg_users_arg(records):
18     users = []  
19     for record in records:
20         if record['type'] != 'user': 
21             continue
22         user = {'urn': record['geni_urn'],
23                 'keys': record['keys'],
24                 'email': record['email']}
25         users.append(user)
26     return users    
27
28 def sfa_users_arg (records, slice_record):
29     users = []
30     for record in records:
31         if record['type'] != 'user': 
32             continue
33         user = {'urn': record['reg-urn'],
34                 'keys': record['reg-keys'],
35                 'slice_record': slice_record,
36                 }
37         # fill as much stuff as possible from planetlab or similar
38         # note that reg-email is not yet available
39         pl_fields = ['email', 'person_id', 'first_name', 'last_name', 'key_ids']
40         nitos_fields = [ 'email', 'user_id' ]
41         extra_fields = list ( set(pl_fields).union(set(nitos_fields)))
42         # try to fill all these in
43         for field in extra_fields:
44             if record.has_key(field): user[field]=record[field]
45         users.append(user)
46
47     return users
48
49 def sfa_to_pg_users_arg(users):
50
51     new_users = []
52     fields = ['urn', 'keys']
53     for user in users:
54         new_user = dict([item for item in user.items() \
55           if item[0] in fields])
56         new_users.append(new_user)
57     return new_users