call logging
[sfa.git] / sfa / methods / remove.py
1 ### $Id$
2 ### $URL$
3
4 from sfa.util.faults import *
5 from sfa.util.method import Method
6 from sfa.util.parameter import Parameter, Mixed
7 from sfa.trust.auth import Auth
8 from sfa.util.record import GeniRecord
9 from sfa.util.genitable import GeniTable
10 from sfa.util.debug import log
11 from sfa.trust.credential import Credential
12
13 class remove(Method):
14     """
15     Remove an object from the registry. If the object represents a PLC object,
16     then the PLC records will also be removed.
17     
18     @param cred credential string
19     @param type record type
20     @param hrn human readable name of record to remove
21
22     @return 1 if successful, faults otherwise 
23     """
24
25     interfaces = ['registry']
26     
27     accepts = [
28         Parameter(str, "Credential string"),
29         Parameter(str, "Record type"),
30         Parameter(str, "Human readable name (hrn) of record to be removed")
31         ]
32
33     returns = Parameter(int, "1 if successful")
34     
35     def call(self, cred, type, hrn, caller_cred=None):
36         self.api.auth.check(cred, "remove")
37         if caller_cred==None:
38            caller_cred=cred
39         
40         #log the call
41         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, Credential(string=caller_cred).get_gid_caller().get_hrn(), hrn, self.name))
42         self.api.auth.verify_object_permission(hrn)
43         table = GeniTable()
44         filter = {'hrn': hrn}
45         if type not in ['all', '*']:
46             filter['type'] = type
47         records = table.find(filter)
48         if not records:
49             raise RecordNotFound(hrn)
50         record = records[0]
51         
52         type = record['type']
53         if type == "user":
54             persons = self.api.plshell.GetPersons(self.api.plauth, record['pointer'])
55             # only delete this person if he has site ids. if he doesnt, it probably means 
56             # he was just removed from a site, not actually deleted
57             if persons and persons[0]['site_ids']:
58                 self.api.plshell.DeletePerson(self.api.plauth, record['pointer'])
59         elif type == "slice":
60             if self.api.plshell.GetSlices(self.api.plauth, record['pointer']):
61                 self.api.plshell.DeleteSlice(self.api.plauth, record['pointer'])
62         elif type == "node":
63             if self.api.plshell.GetNodes(self.api.plauth, record['pointer']):
64                 self.api.plshell.DeleteNode(self.api.plauth, record['pointer'])
65         elif type == "authority":
66             if self.api.plshell.GetSites(self.api.plauth, record['pointer']):
67                 self.api.plshell.DeleteSite(self.api.plauth, record['pointer'])
68         else:
69             raise UnknownGeniType(type)
70
71         table.remove(record)
72
73         return 1