check if record exists before removing from plc
[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.debug import log
10
11 class remove(Method):
12     """
13     Remove an object from the registry. If the object represents a PLC object,
14     then the PLC records will also be removed.
15     
16     @param cred credential string
17     @param type record type
18     @param hrn human readable name of record to remove
19
20     @return 1 if successful, faults otherwise 
21     """
22
23     interfaces = ['registry']
24     
25     accepts = [
26         Parameter(str, "Credential string"),
27         Parameter(str, "Record type"),
28         Parameter(str, "Human readable name (hrn) of record to be removed")
29         ]
30
31     returns = Parameter(int, "1 if successful")
32     
33     def call(self, cred, type, hrn):
34         self.api.auth.check(cred, "remove")
35         self.api.auth.verify_object_permission(hrn)
36         auth_name = self.api.auth.get_authority(hrn)
37         table = self.api.auth.get_auth_table(auth_name)
38         record_list = table.resolve(type, hrn)
39         if not record_list:
40             raise RecordNotFound(hrn)
41         record = record_list[0]
42         
43         type = record['type']
44         if type == "user":
45             if self.api.plshell.GetPersons(self.api.plauth, record.get_pointer()):
46                 self.api.plshell.DeletePerson(self.api.plauth, record.get_pointer())
47         elif type == "slice":
48             if self.api.plshell.GetSlices(self.api.plauth, record.get_pointer()):
49                 self.api.plshell.DeleteSlice(self.api.plauth, record.get_pointer())
50         elif type == "node":
51             if self.api.plshell.GetNodes(self.api.plauth, record.get_pointer()):
52                 self.api.plshell.DeleteNode(self.api.plauth, record.get_pointer())
53         elif type == "authority":
54             if self.api.plshell.GetSites(self.api.plauth, record.get_pointer()):
55                 self.api.plshell.DeleteSite(self.api.plauth, record.get_pointer())
56         else:
57             raise UnknownGeniType(type)
58
59         table.remove(record)
60
61         return 1