Dont try to validate ip or protocol here. Assume that will be handled by the ec2 api
[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 from sfa.util.config import Config
12
13 class EucaShell:
14     """
15     A xmlrpc connection to the euca api. 
16     """    
17
18     def __init__(self, config):
19         self.config = config
20
21     def get_euca_connection(self):
22         if not has_boto:
23             logger.info('Unable to access EC2 API - boto library not found.')
24             return None
25         nova = NovaShell(self.config)
26         admin_user = nova.auth_manager.get_user(self.config.SFA_NOVA_USER)
27         access_key = admin_user.access
28         secret_key = admin_user.secret
29         url = self.config.SFA_NOVA_API_URL
30         host = None
31         port = None    
32         path = "/"
33         use_ssl = False
34         # Split the url into parts 
35         if url.find('https://') >= 0:
36             use_ssl  = True
37             url = url.replace('https://', '')
38         elif url.find('http://') >= 0:
39             use_ssl  = False
40             url = url.replace('http://', '')
41         parts = url.split(':')
42         host = parts[0]
43         if len(parts) > 1:
44             parts = parts[1].split('/')
45             port = int(parts[0])
46             parts = parts[1:]
47             path = '/'.join(parts)
48
49         return boto.connect_ec2(aws_access_key_id=access_key,
50                                 aws_secret_access_key=secret_key,
51                                 is_secure=use_ssl,
52                                 region=RegionInfo(None, 'eucalyptus', host),
53                                 host=host,
54                                 port=port,
55                                 path=path) 
56
57     def __getattr__(self, name):
58         def func(*args, **kwds):
59             conn = self.get_euca_connection()
60             return getattr(conn, name)(*args, **kwds)
61         return func