changed the sfa db schema. All records are now stored in 1 table instead of createing...
[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
12 class remove(Method):
13     """
14     Remove an object from the registry. If the object represents a PLC object,
15     then the PLC records will also be removed.
16     
17     @param cred credential string
18     @param type record type
19     @param hrn human readable name of record to remove
20
21     @return 1 if successful, faults otherwise 
22     """
23
24     interfaces = ['registry']
25     
26     accepts = [
27         Parameter(str, "Credential string"),
28         Parameter(str, "Record type"),
29         Parameter(str, "Human readable name (hrn) of record to be removed")
30         ]
31
32     returns = Parameter(int, "1 if successful")
33     
34     def call(self, cred, type, hrn):
35         self.api.auth.check(cred, "remove")
36         self.api.auth.verify_object_permission(hrn)
37         table = GeniTable()
38         filter = {'hrn': hrn}
39         if type not in ['all', '*']:
40             filter['type'] = type
41         records = table.find(filter)
42         if not records:
43             raise RecordNotFound(hrn)
44         record = records[0]
45         
46         type = record['type']
47         if type == "user":
48             persons = self.api.plshell.GetPersons(self.api.plauth, record['pointer'])
49             # only delete this person if he has site ids. if he doesnt, it probably means 
50             # he was just removed from a site, not actually deleted
51             if persons and persons[0]['site_ids']:
52                 self.api.plshell.DeletePerson(self.api.plauth, record['pointer'])
53         elif type == "slice":
54             if self.api.plshell.GetSlices(self.api.plauth, record['pointer']):
55                 self.api.plshell.DeleteSlice(self.api.plauth, record['pointer'])
56         elif type == "node":
57             if self.api.plshell.GetNodes(self.api.plauth, record['pointer']):
58                 self.api.plshell.DeleteNode(self.api.plauth, record['pointer'])
59         elif type == "authority":
60             if self.api.plshell.GetSites(self.api.plauth, record['pointer']):
61                 self.api.plshell.DeleteSite(self.api.plauth, record['pointer'])
62         else:
63             raise UnknownGeniType(type)
64
65         table.remove(record)
66
67         return 1