added request_hash argumet some more calls
[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         Parameter(str, "Request hash")
33         ]
34
35     returns = Parameter(int, "1 if successful")
36     
37     def call(self, cred, type, hrn, request_hash, caller_cred=None):
38
39         if caller_cred==None:
40             caller_cred=cred
41         #log the call
42         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))
43         # This cred will be an authority cred, not a user, so we cant use it to 
44         # authenticate the caller's request_hash. Let just get the caller's gid
45         # from the cred and authenticate using that
46         client_gid = Credential(string=cred).get_gid_caller()
47         client_gid_str = client_gid.save_to_string(save_parents=True)
48         self.api.auth.authenticateGid(client_gid_str, [cred, type, hrn], request_hash)
49         self.api.auth.check(cred, "remove")
50         self.api.auth.verify_object_permission(hrn)
51         table = GeniTable()
52         filter = {'hrn': hrn}
53         if type not in ['all', '*']:
54             filter['type'] = type
55         records = table.find(filter)
56         if not records:
57             raise RecordNotFound(hrn)
58         record = records[0]
59         type = record['type']
60
61         credential = self.api.getCredential()
62         registries = Registries(self.api) 
63
64         # Try to remove the object from the PLCDB of federated agg.
65         # This is attempted before removing the object from the local agg's PLCDB and sfa table
66         if hrn.startswith(self.api.hrn) and type in ['user', 'slice', 'authority']:
67             for registry in registries:
68                 if registry not in [self.api.hrn]:
69                     try:
70                         result=registries[registry].remove_peer_object(credential, record)
71                     except:
72                         pass
73         if type == "user":
74             persons = self.api.plshell.GetPersons(self.api.plauth, record['pointer'])
75             # only delete this person if he has site ids. if he doesnt, it probably means 
76             # he was just removed from a site, not actually deleted
77             if persons and persons[0]['site_ids']:
78                 self.api.plshell.DeletePerson(self.api.plauth, record['pointer'])
79         elif type == "slice":
80             if self.api.plshell.GetSlices(self.api.plauth, record['pointer']):
81                 self.api.plshell.DeleteSlice(self.api.plauth, record['pointer'])
82         elif type == "node":
83             if self.api.plshell.GetNodes(self.api.plauth, record['pointer']):
84                 self.api.plshell.DeleteNode(self.api.plauth, record['pointer'])
85         elif type == "authority":
86             if self.api.plshell.GetSites(self.api.plauth, record['pointer']):
87                 self.api.plshell.DeleteSite(self.api.plauth, record['pointer'])
88         else:
89             raise UnknownGeniType(type)
90
91         table.remove(record)
92            
93         # forward the call after replacing the root hrn
94
95         return 1