3d7d5d3df1f6fa13befaa963c9246a31179aba37
[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 from sfa.server.registry import Registries
13
14 class remove(Method):
15     """
16     Remove an object from the registry. If the object represents a PLC object,
17     then the PLC records will also be removed.
18     
19     @param cred credential string
20     @param type record type
21     @param hrn human readable name of record to remove
22
23     @return 1 if successful, faults otherwise 
24     """
25
26     interfaces = ['registry']
27     
28     accepts = [
29         Parameter(str, "Credential string"),
30         Parameter(str, "Record type"),
31         Parameter(str, "Human readable name (hrn) of record to be removed"),
32         Mixed(Parameter(str, "Request hash"),
33               Parameter(None, "Request hash not specified"))
34         ]
35
36     returns = Parameter(int, "1 if successful")
37     
38     def call(self, cred, type, hrn, request_hash=None, origin_hrn=None):
39
40         if origin_hrn==None:
41             origin_hrn=Credential(string=cred).get_gid_caller().get_hrn()
42         #log the call
43         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, hrn, self.name))
44         # This cred will be an authority cred, not a user, so we cant use it to 
45         # authenticate the caller's request_hash. Let just get the caller's gid
46         # from the cred and authenticate using that
47         client_gid = Credential(string=cred).get_gid_caller()
48         client_gid_str = client_gid.save_to_string(save_parents=True)
49         self.api.auth.authenticateGid(client_gid_str, [cred, type, hrn], request_hash)
50         self.api.auth.check(cred, "remove")
51         self.api.auth.verify_object_permission(hrn)
52         table = GeniTable()
53         filter = {'hrn': hrn}
54         if type not in ['all', '*']:
55             filter['type'] = type
56         records = table.find(filter)
57         if not records:
58             raise RecordNotFound(hrn)
59         record = records[0]
60         type = record['type']
61
62         credential = self.api.getCredential()
63         registries = Registries(self.api) 
64
65         # Try to remove the object from the PLCDB of federated agg.
66         # This is attempted before removing the object from the local agg's PLCDB and sfa table
67         if hrn.startswith(self.api.hrn) and type in ['user', 'slice', 'authority']:
68             for registry in registries:
69                 if registry not in [self.api.hrn]:
70                     try:
71                         request_hash=None
72                         result=registries[registry].remove_peer_object(credential, record, request_hash, origin_hrn)
73                     except:
74                         pass
75         if type == "user":
76             persons = self.api.plshell.GetPersons(self.api.plauth, record['pointer'])
77             # only delete this person if he has site ids. if he doesnt, it probably means 
78             # he was just removed from a site, not actually deleted
79             if persons and persons[0]['site_ids']:
80                 self.api.plshell.DeletePerson(self.api.plauth, record['pointer'])
81         elif type == "slice":
82             if self.api.plshell.GetSlices(self.api.plauth, record['pointer']):
83                 self.api.plshell.DeleteSlice(self.api.plauth, record['pointer'])
84         elif type == "node":
85             if self.api.plshell.GetNodes(self.api.plauth, record['pointer']):
86                 self.api.plshell.DeleteNode(self.api.plauth, record['pointer'])
87         elif type == "authority":
88             if self.api.plshell.GetSites(self.api.plauth, record['pointer']):
89                 self.api.plshell.DeleteSite(self.api.plauth, record['pointer'])
90         else:
91             raise UnknownGeniType(type)
92
93         table.remove(record)
94            
95         # forward the call after replacing the root hrn
96
97         return 1