4c3c58d6e07b296c768541188d60627d28928f8c
[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 of slice to instantiate"),
32         Mixed(Parameter(str, "Human readable name of the original caller"),
33               Parameter(None, "Origin hrn not specified"))
34         ]
35
36     returns = Parameter(int, "1 if successful")
37     
38     def call(self, cred, type, hrn, origin_hrn=None):
39         user_cred = Credential(string=cred)
40
41         #log the call
42         if not origin_hrn:
43             origin_hrn = user_cred.get_gid_caller().get_hrn()
44         self.api.logger.info("interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s"%(self.api.interface, origin_hrn, hrn, self.name))
45
46         # validate the cred
47         self.api.auth.check(cred, "remove")
48         self.api.auth.verify_object_permission(hrn)
49         
50         table = GeniTable()
51         filter = {'hrn': hrn}
52         if type not in ['all', '*']:
53             filter['type'] = type
54         records = table.find(filter)
55         if not records:
56             raise RecordNotFound(hrn)
57         record = records[0]
58         type = record['type']
59
60         credential = self.api.getCredential()
61         registries = Registries(self.api) 
62
63         # Try to remove the object from the PLCDB of federated agg.
64         # This is attempted before removing the object from the local agg's PLCDB and sfa table
65         if hrn.startswith(self.api.hrn) and type in ['user', 'slice', 'authority']:
66             for registry in registries:
67                 if registry not in [self.api.hrn]:
68                     result=registries[registry].remove_peer_object(credential, record, origin_hrn)
69                         pass
70         if type == "user":
71             persons = self.api.plshell.GetPersons(self.api.plauth, record['pointer'])
72             # only delete this person if he has site ids. if he doesnt, it probably means 
73             # he was just removed from a site, not actually deleted
74             if persons and persons[0]['site_ids']:
75                 self.api.plshell.DeletePerson(self.api.plauth, record['pointer'])
76         elif type == "slice":
77             if self.api.plshell.GetSlices(self.api.plauth, record['pointer']):
78                 self.api.plshell.DeleteSlice(self.api.plauth, record['pointer'])
79         elif type == "node":
80             if self.api.plshell.GetNodes(self.api.plauth, record['pointer']):
81                 self.api.plshell.DeleteNode(self.api.plauth, record['pointer'])
82         elif type == "authority":
83             if self.api.plshell.GetSites(self.api.plauth, record['pointer']):
84                 self.api.plshell.DeleteSite(self.api.plauth, record['pointer'])
85         else:
86             raise UnknownGeniType(type)
87
88         table.remove(record)
89            
90         return 1