dont forget to pass caller_cred to get_rspec()
[sfa.git] / sfa / managers / aggregate_manager_pl.py
1 ### $Id: slices.py 15842 2009-11-22 09:56:13Z anil $
2 ### $URL: https://svn.planet-lab.org/svn/sfa/trunk/sfa/plc/slices.py $
3
4 import datetime
5 import time
6 import traceback
7 import sys
8
9 from types import StringTypes
10 from sfa.util.misc import *
11 from sfa.util.rspec import *
12 from sfa.util.specdict import *
13 from sfa.util.faults import *
14 from sfa.util.record import GeniRecord
15 from sfa.util.policy import Policy
16 from sfa.util.prefixTree import prefixTree
17 from sfa.util.debug import log
18 from sfa.server.registry import Registries
19 import sfa.plc.peers as peers
20
21 def delete_slice(api, hrn):
22     slicename = hrn_to_pl_slicename(hrn)
23     slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
24     if not slices:
25         return 1
26     slice = slices[0]
27
28     # determine if this is a peer slice
29     peer = peers.get_peer(api, hrn)
30     if peer:
31         api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
32     api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
33     if peer:
34         api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
35     return 1
36
37 def create_slice(api, hrn, rspec):
38     # XX just import the legacy module and excute that until
39     # we transition the code to this module
40     from sfa.plc.slices import Slices
41     slices = Slices(api)
42     slices.create_slice(hrn, rspec)
43
44 def start_slice(api, hrn):
45     slicename = hrn_to_pl_slicename(hrn)
46     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
47     if not slices:
48         raise RecordNotFound(hrn)
49     slice_id = slices[0]
50     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
51     attribute_id = attreibutes[0]['slice_attribute_id']
52     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
53
54     return 1
55  
56 def stop_slice(api, hrn):
57     slicename = hrn_to_pl_slicename(hrn)
58     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
59     if not slices:
60         raise RecordNotFound(hrn)
61     slice_id = slices[0]['slice_id']
62     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
63     attribute_id = attributes[0]['slice_attribute_id']
64     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
65     return 1
66
67 def reset_slice(api, hrn):
68     # XX not implemented at this interface
69     return 1
70
71 def get_slices(api):
72     # XX just import the legacy module and excute that until
73     # we transition the code to this module
74     from sfa.plc.slices import Slices
75     slices = Slices(api)
76     slices.refresh()
77     return slices['hrn']
78      
79  
80 def get_rspec(api, hrn=None, caller_cred=None):
81     from sfa.plc.nodes import Nodes
82     nodes = Nodes(api, caller_cred=caller_cred)
83     if hrn:
84         rspec = nodes.get_rspec(hrn)
85     else:
86         nodes.refresh()
87         rspec = nodes['rspec'] 
88
89     return rspec
90
91 """
92 Returns the request context required by sfatables. At some point, this mechanism should be changed
93 to refer to "contexts", which is the information that sfatables is requesting. But for now, we just
94 return the basic information needed in a dict.
95 """
96 def fetch_context(slice_hrn, user_hrn, contexts):
97     base_context = {'sfa':{'user':{'hrn':user_hrn}}}
98     return base_context
99
100 def main():
101     r = RSpec()
102     r.parseFile(sys.argv[1])
103     rspec = r.toDict()
104     create_slice(None,'plc.princeton.tmacktestslice',rspec)
105
106 if __name__ == "__main__":
107     main()