2 # Implements support for geni records
4 # TODO: Use existing PLC database methods? or keep this separate?
10 from types import StringTypes
12 from sfa.trust.gid import *
14 import sfa.util.report
15 from sfa.util.rspec import *
16 from sfa.util.parameter import *
17 from sfa.util.misc import *
20 class GeniRecord(dict):
22 The GeniRecord class implements a Geni Record. A GeniRecord is a tuple
23 (Hrn, GID, Type, Info).
25 Hrn specifies the Human Readable Name of the object
26 GID is the GID of the object
27 Type is user | authority | slice | component
29 Info is comprised of the following sub-fields
30 pointer = a pointer to the record in the PL database
32 The pointer is interpreted depending on the type of the record. For example,
33 if the type=="user", then pointer is assumed to be a person_id that indexes
34 into the persons table.
36 A given HRN may have more than one record, provided that the records are
40 ### the wsdl generator assumes this is named 'fields'
42 'record_id': Parameter(int, 'An id that uniquely identifies this record'),
43 'pointer': Parameter(int, 'An id that uniquely identifies this record in an external database ')
47 'authority': Parameter(str, "The authority for this record"),
48 'peer_authority': Parameter(str, "The peer authority for this record"),
49 'hrn': Parameter(str, "Human readable name of object"),
50 'gid': Parameter(str, "GID of the object"),
51 'type': Parameter(str, "Record type"),
52 'last_updated': Parameter(int, 'Date and time of last update'),
53 'date_created': Parameter(int, 'Date and time this record was created'),
55 all_fields = dict(fields.items() + internal_fields.items())
57 # Create a Geni Record
59 # @param name if !=None, assign the name of the record
60 # @param gid if !=None, assign the gid of the record
61 # @param type one of user | authority | slice | component
62 # @param pointer is a pointer to a PLC record
63 # @param dict if !=None, then fill in this record from the dictionary
65 def __init__(self, hrn=None, gid=None, type=None, pointer=None, peer_authority=None, dict=None, string=None):
78 self.set_pointer(pointer)
80 self.set_peer_auth(peer_authority)
82 self.load_from_dict(dict)
84 self.load_from_string(string)
86 def update(self, new_dict):
87 if isinstance(new_dict, list):
88 new_dict = new_dict[0]
90 # Convert any boolean strings to real bools
92 if isinstance(new_dict[key], StringTypes):
93 if new_dict[key].lower() in ["true"]:
95 elif new_dict[key].lower() in ["false"]:
97 dict.update(self, new_dict)
100 # Set the name of the record
102 # @param hrn is a string containing the HRN
104 def set_name(self, hrn):
106 Set the name of the record
113 # Set the GID of the record
115 # @param gid is a GID object or the string representation of a GID object
117 def set_gid(self, gid):
119 Set the GID of the record
122 if isinstance(gid, StringTypes):
126 self.gid = gid.save_to_string(save_parents=True)
127 self['gid'] = gid.save_to_string(save_parents=True)
131 # Set the type of the record
133 # @param type is a string: user | authority | slice | component
135 def set_type(self, type):
137 Set the type of the record
144 # Set the pointer of the record
146 # @param pointer is an integer containing the ID of a PLC record
148 def set_pointer(self, pointer):
150 Set the pointer of the record
152 self.pointer = pointer
153 self['pointer'] = pointer
157 def set_peer_auth(self, peer_authority):
158 self.peer_authority = peer_authority
159 self['peer_authority'] = peer_authority
163 # Return the name (HRN) of the record
167 Return the name (HRN) of the record
172 # Return the type of the record
176 Return the type of the record
181 # Return the pointer of the record. The pointer is an integer that may be
182 # used to look up the record in the PLC database. The evaluation of pointer
183 # depends on the type of the record
185 def get_pointer(self):
187 Return the pointer of the record. The pointer is an integer that may be
188 used to look up the record in the PLC database. The evaluation of pointer
189 depends on the type of the record
194 # Return the GID of the record, in the form of a GID object
195 # TODO: not the best name for the function, because we have things called
196 # gidObjects in the Cred
198 def get_gid_object(self):
200 Return the GID of the record, in the form of a GID object
202 return GID(string=self.gid)
205 # Returns a list of field names in this record.
207 def get_field_names(self):
209 Returns a list of field names in this record.
211 return self.fields.keys()
214 # Given a field name ("hrn", "gid", ...) return the value of that field.
216 # @param fieldname is the name of field to be returned
218 def get_field_value_string(self, fieldname):
220 Given a field name ("hrn", "gid", ...) return the value of that field.
222 if fieldname == "authority":
223 val = get_authority(self['hrn'])
226 val = getattr(self, fieldname)
228 val = self[fieldname]
229 if isinstance(val, str):
230 return "'" + str(val) + "'"
235 # Given a list of field names, return a list of values for those public.
237 # @param fieldnames is a list of field names
239 def get_field_value_strings(self, fieldnames):
241 Given a list of field names, return a list of values for those public.
243 return [ self.get_field_value_string (fieldname) for fieldname in fieldnames ]
246 # Return the record in the form of a dictionary
250 Return the record in the form of a dictionary
255 # Load the record from a dictionary
257 # @param dict dictionary to load record public from
259 def load_from_dict(self, dict):
261 Load the record from a dictionary
263 self.set_name(dict['hrn'])
264 gidstr = dict.get("gid", None)
266 self.set_gid(dict['gid'])
268 if "pointer" in dict:
269 self.set_pointer(dict['pointer'])
271 self.set_type(dict['type'])
275 # Save the record to a string. The string contains an XML representation of
278 def save_to_string(self):
280 Save the record to a string. The string contains an XML representation of
283 recorddict = self.as_dict()
284 filteredDict = dict([(key, val) for (key, val) in recorddict.iteritems() if key in self.fields.keys()])
285 record = RecordSpec()
286 record.parseDict(filteredDict)
288 #str = xmlrpclib.dumps((dict,), allow_none=True)
292 # Load the record from a string. The string is assumed to contain an XML
293 # representation of the record.
295 def load_from_string(self, str):
297 Load the record from a string. The string is assumed to contain an XML
298 representation of the record.
300 #dict = xmlrpclib.loads(str)[0][0]
302 record = RecordSpec()
303 record.parseString(str)
304 record_dict = record.toDict()
305 geni_dict = record_dict['record']
306 self.load_from_dict(geni_dict)
309 # Dump the record to stdout
311 # @param dump_parents if true, then the parents of the GID will be dumped
313 def dump(self, dump_parents=False):
315 Walk tree and dump records.
317 #print "RECORD", self.name
318 #print " hrn:", self.name
319 #print " type:", self.type
324 # self.get_gid_object().dump(8, dump_parents)
325 #print " pointer:", self.pointer
327 order = GeniRecord.fields.keys()
328 for key in self.keys():
332 if key in self and key in self.fields:
333 if key in 'gid' and self[key]:
334 gid = GID(string=self[key])
336 gid.dump(8, dump_parents)
338 print " %s: %s" % (key, self[key])
344 class UserRecord(GeniRecord):
347 'email': Parameter(str, 'email'),
348 'first_name': Parameter(str, 'First name'),
349 'last_name': Parameter(str, 'Last name'),
350 'phone': Parameter(str, 'Phone Number'),
351 'key': Parameter(str, 'Public key'),
352 'slices': Parameter([str], 'List of slices this user belongs to'),
354 fields.update(GeniRecord.fields)
356 class SliceRecord(GeniRecord):
358 'name': Parameter(str, 'Slice name'),
359 'url': Parameter(str, 'Slice url'),
360 'expires': Parameter(int, 'Date and time this slice exipres'),
361 'researcher': Parameter([str], 'List of users for this slice'),
362 'description': Parameter([str], 'Description of this slice'),
364 fields.update(GeniRecord.fields)
367 class NodeRecord(GeniRecord):
369 'hostname': Parameter(str, 'This nodes dns name'),
370 'node_type': Parameter(str, 'Type of node this is'),
371 'node_type': Parameter(str, 'Type of node this is'),
372 'latitude': Parameter(str, 'latitude'),
373 'longitude': Parameter(str, 'longitude'),
375 fields.update(GeniRecord.fields)
378 class AuthorityRecord(GeniRecord):
380 'name': Parameter(str, 'Name'),
381 'login_base': Parameter(str, 'login base'),
382 'enabled': Parameter(bool, 'Is this site enabled'),
383 'url': Parameter(str, 'URL'),
384 'nodes': Parameter([str], 'List of nodes at this site'),
385 'operator': Parameter([str], 'List of operators'),
386 'researcher': Parameter([str], 'List of researchers'),
387 'PI': Parameter([str], 'List of Principal Investigators'),
389 fields.update(GeniRecord.fields)