Moving context extraction to the rspec manager
[sfa.git] / sfa / methods / remove_remote_object.py
1 from sfa.util.faults import *
2 from sfa.util.method import Method
3 from sfa.util.parameter import Parameter, Mixed
4 from sfa.trust.auth import Auth
5 from sfa.util.record import GeniRecord
6 from sfa.util.genitable import GeniTable
7 from sfa.util.debug import log
8 from sfa.trust.credential import Credential
9 from sfa.util.misc import *
10 from types import StringTypes
11
12 class remove_remote_object(Method):
13     """
14     Remove an object from the PLC records of a federated aggregate. 
15     This method will be called by registry.remove() while removing 
16     a record from the local aggreage's PLCDB and sfa table. This 
17     method need not be directly called by end-user.
18     
19     @param cred credential string
20     @param hrn human readbale name of record to be removed
21     @param record record as stored in the authoritative registry
22
23     @return 1 if successful, faults otherwise 
24     """
25
26     interfaces = ['registry']
27     
28     accepts = [
29         Parameter(str, "Credential string"),
30         Parameter(str, "Human readable name (hrn) of record to be removed"),
31         Parameter(dict, "Record dictionary as stored in the authoritative registry")
32         ]
33
34     returns = Parameter(int, "1 if successful")
35     
36     def call(self, cred, hrn, record, caller_cred=None):
37         self.api.auth.check(cred, "remove")
38         if caller_cred==None:
39            caller_cred=cred
40         
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         self.api.auth.verify_object_permission(hrn)
44         type = record['type']
45         peer=self.get_peer(hrn)
46         if peer:
47            if type == "user":
48                peer_records = self.api.plshell.GetPersons(self.api.plauth, {'peer_person_id' : record['pointer']})
49                if not peer_records:
50                   return 1
51                peer_record=peer_records[0]
52                self.api.plshell.UnBindObjectFromPeer(self.api.plauth, 'person', peer_record['person_id'], peer)
53                self.api.plshell.DeletePerson(self.api.plauth, peer_record['person_id'])
54            elif type == "slice":
55                peer_records=self.api.plshell.GetSlices(self.api.plauth, {'peer_slice_id' : record['pointer']})
56                if not peer_records:
57                   return 1
58                peer_record=peer_records[0]
59                self.api.plshell.UnBindObjectFromPeer(self.api.plauth, 'slice', peer_record['slice_id'], peer)
60                self.api.plshell.DeleteSlice(self.api.plauth, peer_record['slice_id'])
61            elif type == "authority":
62                peer_records=self.api.plshell.GetSites(self.api.plauth, {'peer_site_id' : record['pointer']})
63                if not peer_records:
64                   return 1
65                peer_record=peer_records[0]
66                self.api.plshell.UnBindObjectFromPeer(self.api.plauth, 'site', peer_record['site_id'], peer)
67                self.api.plshell.DeleteSite(self.api.plauth, peer_record['site_id'])
68            else:
69                raise UnknownGeniType(type)
70
71            return 1
72
73         else:
74           # Remove objects from remote agg. where no RefreshPeer Federation exists
75           # yet to figure out the mechanism to identify the correct object in the PLCDB
76           # on the remote aggregate
77              pass
78
79     def get_peer(self, hrn):
80         # Becaues of myplc federation,  we first need to determine if this
81         # slice belongs to out local plc or a myplc peer. We will assume it
82         # is a local site, unless we find out otherwise
83         peer = None
84         # get this slice's authority (site)
85         slice_authority = get_authority(hrn)
86
87         # get this site's authority (sfa root authority or sub authority)
88         site_authority = get_authority(slice_authority).lower()
89         if not site_authority:
90            site_authority = slice_authority             
91
92         # check if we are already peered with this site_authority, if so
93         peers = self.api.plshell.GetPeers(self.api.plauth, {}, ['peer_id', 'peername', 'shortname', 'hrn_root'])
94         for peer_record in peers:
95             names = [name.lower() for name in peer_record.values() if isinstance(name, StringTypes)]
96             if site_authority in names:
97                 peer = peer_record['shortname']
98
99         return peer
100
101