changed some config options
[sfa.git] / sfa / openstack / euca_shell.py
1 try:
2     import boto
3     from boto.ec2.regioninfo import RegionInfo
4     from boto.exception import EC2ResponseError
5     has_boto=True
6 except:
7     has_boto=False    
8
9 from sfa.util.sfalogging import logger
10 from sfa.openstack.nova_shell import NovaShell
11
12 class EucaShell:
13     """
14     A xmlrpc connection to the euca api. 
15     """    
16
17     def __init__(self, config):
18         self.config = Config
19
20     def get_euca_connection(self):
21         if not has_boto:
22             logger.info('Unable to access EC2 API - boto library not found.')
23             return None
24         nova = NovaShell(self.config)
25         admin_user = nova.auth_manager.get_user(self.config.SFA_NOVA_USER)
26         access_key = admin_user.access
27         secret_key = admin_user.secret
28         url = self.config.SFA_NOVA_API_URL
29         path = "/"
30         euca_port = self.config.SFA_NOVA_API_PORT        
31         use_ssl = False
32
33         # Split the url into parts 
34         if url.find('https://') >= 0:
35             use_ssl  = True
36             url = url.replace('https://', '')
37         elif url.find('http://') >= 0:
38             use_ssl  = False
39             url = url.replace('http://', '')
40         (host, parts) = url.split(':')
41         if len(parts) > 1:
42             parts = parts.split('/')
43             port = int(parts[0])
44             parts = parts[1:]
45             path = '/'.join(parts)
46         
47         return boto.connect_ec2(aws_access_key_id=access_key,
48                                 aws_secret_access_key=secret_key,
49                                 is_secure=use_ssl,
50                                 region=RegionInfo(None, 'eucalyptus', host),
51                                 port=port,
52                                 path=path) 
53
54     def __getattr__(self, name):
55         def func(*args, **kwds):
56             conn = self.get_euca_connection()
57             return getattr(conn, name)(*args, **kwds)
58         return func