python3 - 2to3 + miscell obvious tweaks
[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' field 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
18
19 def pg_users_arg(records):
20     users = []
21     for record in records:
22         if record['type'] != 'user':
23             continue
24         user = {'urn': record['reg-urn'],
25                 'keys': record['reg-keys'],
26                 'email': record['email']}
27         users.append(user)
28     return users
29
30
31 def sfa_users_arg(records, slice_record):
32     users = []
33     for record in records:
34         if record['type'] != 'user':
35             continue
36         user = {'urn': record['reg-urn'],
37                 'keys': record['reg-keys'],
38                 'slice_record': slice_record,
39                 }
40         # fill as much stuff as possible from planetlab or similar
41         # note that reg-email is not yet available
42         pl_fields = ['email', 'person_id',
43                      'first_name', 'last_name', 'key_ids']
44         # try to fill all these in
45         for field in pl_fields:
46             if field in record:
47                 user[field] = record[field]
48         users.append(user)
49
50     return users
51
52
53 def sfa_to_pg_users_arg(users):
54
55     new_users = []
56     fields = ['urn', 'keys']
57     for user in users:
58         new_user = dict([item for item in list(user.items())
59                          if item[0] in fields])
60         new_users.append(new_user)
61     return new_users