X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fopenstack%2Feuca_shell.py;h=90c222258ba02b9058336960a0e7d95044164614;hb=1505f3a42ea06ef30959e9e54fa7298eb55d83ee;hp=616057fde562b574880671d805dd7634dcb4f83f;hpb=3f24252a757975373ed2447186d317493372907d;p=sfa.git diff --git a/sfa/openstack/euca_shell.py b/sfa/openstack/euca_shell.py index 616057fd..90c22225 100644 --- a/sfa/openstack/euca_shell.py +++ b/sfa/openstack/euca_shell.py @@ -2,41 +2,62 @@ try: import boto from boto.ec2.regioninfo import RegionInfo from boto.exception import EC2ResponseError - has_boto=True + has_boto = True except: - has_boto=False + has_boto = False from sfa.util.sfalogging import logger from sfa.openstack.nova_shell import NovaShell from sfa.util.config import Config + class EucaShell: """ A xmlrpc connection to the euca api. - """ + """ def __init__(self, config): self.config = config + self.nova_shell = NovaShell(config) + self.access_key = None + self.secret_key = None + + def init_context(self, project_name=None): + + # use the context of the specified project's project + # manager. + if project_name: + project = self.nova_shell.auth_manager.get_project(project_name) + self.access_key = "%s:%s" % ( + project.project_manager.name, project_name) + self.secret_key = project.project_manager.secret + else: + # use admin user's context + admin_user = self.nova_shell.auth_manager.get_user( + self.config.SFA_NOVA_USER) + #access_key = admin_user.access + self.access_key = '%s' % admin_user.name + self.secret_key = admin_user.secret - def get_euca_connection(self): + def get_euca_connection(self, project_name=None): if not has_boto: logger.info('Unable to access EC2 API - boto library not found.') return None - nova = NovaShell(self.config) - admin_user = nova.auth_manager.get_user(self.config.SFA_NOVA_USER) - access_key = admin_user.access - secret_key = admin_user.secret + + if not self.access_key or not self.secret_key: + self.init_context(project_name) + url = self.config.SFA_NOVA_API_URL host = None - port = None + port = None path = "/" use_ssl = False - # Split the url into parts + # Split the url into parts if url.find('https://') >= 0: - use_ssl = True + use_ssl = True url = url.replace('https://', '') elif url.find('http://') >= 0: - use_ssl = False + use_ssl = False url = url.replace('http://', '') parts = url.split(':') host = parts[0] @@ -44,18 +65,15 @@ class EucaShell: parts = parts[1].split('/') port = int(parts[0]) parts = parts[1:] - path = '/'.join(parts) - - return boto.connect_ec2(aws_access_key_id=access_key, - aws_secret_access_key=secret_key, + path = '/' + '/'.join(parts) + return boto.connect_ec2(aws_access_key_id=self.access_key, + aws_secret_access_key=self.secret_key, is_secure=use_ssl, region=RegionInfo(None, 'eucalyptus', host), host=host, port=port, - path=path) + path=path) def __getattr__(self, name): def func(*args, **kwds): conn = self.get_euca_connection() - return getattr(conn, name)(*args, **kwds) - return func