Removed hard coded login in GET requests (OARrestapi).
[sfa.git] / sfa / openstack / openstack_shell.py
1 import sys
2 import xmlrpclib
3 import socket
4 from urlparse import urlparse
5
6 from sfa.util.sfalogging import logger
7
8 class OpenstackShell:
9     """
10     A simple xmlrpc shell to a myplc instance
11     This class can receive all Openstack calls to the underlying testbed
12     """
13     
14     # dont care about limiting calls yet 
15     direct_calls = []
16     alias_calls = {}
17
18
19     # use the 'capability' auth mechanism for higher performance when the PLC db is local    
20     def __init__ ( self, config ) :
21         url = config.SFA_PLC_URL
22         # try to figure if the url is local
23         hostname=urlparse(url).hostname
24         is_local=False
25         if hostname == 'localhost': is_local=True
26         # otherwise compare IP addresses; 
27         # this might fail for any number of reasons, so let's harden that
28         try:
29             # xxx todo this seems to result in a DNS request for each incoming request to the AM
30             # should be cached or improved
31             url_ip=socket.gethostbyname(hostname)
32             local_ip=socket.gethostbyname(socket.gethostname())
33             if url_ip==local_ip: is_local=True
34         except:
35             pass
36
37
38         # Openstack provides a RESTful api but it is very limited, so we will
39         # ignore it for now and always use the native openstack (nova) library.
40         # This of course will not work if sfa is not installed on the same machine
41         # as the openstack-compute package.   
42         if is_local:
43             try:
44                 from nova.auth.manager import AuthManager, db, context
45                 direct_access=True
46             except:
47                 direct_access=False
48         if is_local and direct_access:
49             
50             logger.debug('openstack access - native')
51             self.auth = context.get_admin_context()
52             # AuthManager isnt' really useful for much yet but it's
53             # more convenient to use than the db reference which requires
54             # a context. Lets hold onto the AuthManager reference for now.
55             #self.proxy = AuthManager()
56             self.auth_manager = AuthManager()
57             self.proxy = db
58
59         else:
60             self.auth = None
61             self.proxy = None
62             logger.debug('openstack access - REST')
63             raise SfaNotImplemented('openstack access - Rest')
64
65     def __getattr__(self, name):
66         def func(*args, **kwds):
67             result=getattr(self.proxy, name)(self.auth, *args, **kwds)
68             return result
69         return func