added NovaClient
[sfa.git] / sfa / openstack / client.py
1 from sfa.util.sfalogging import logger
2 from glance import client as glance_client
3 from novaclient.v1_1.client import Client
4 from sfa.util.config import Config
5
6
7 def parse_novarc(filename):
8     opts = {}
9     f = open(filename, 'r')
10     for line in f:
11         try:
12             line = line.replace('export', '').strip()
13             parts = line.split('=')
14             if len(parts) > 1:
15                 value = parts[1].replace("\'", "")
16                 value = value.replace('\"', '') 
17                 opts[parts[0]] = value
18         except:
19             pass
20     f.close()
21     return opts
22
23
24 class GlanceClient:
25     def __init__(self, config=None):
26         if not config:
27             config = Config()
28         opts = parse_novarc(config.SFA_NOVA_NOVARC)
29
30         self.client = glance_client.get_client(host='0.0.0.0',
31                                                username=opts.get('OS_USERNAME'),
32                                                password=opts.get('OS_PASSWORD'),
33                                                tenant=opts.get('OS_TENANT_NAME'),
34                                                auth_url=opts.get('OS_AUTH_URL'))
35
36     def __getattr__(self, name):
37         return getattr(self.client, name)
38
39
40 class NovaClient:
41     def __init__(self, config=None):
42         if not config:
43             config = Config()
44         opts = parse_novarc(config.SFA_NOVA_NOVARC)
45         
46         self.client = Client(username=opts.get('OS_USERNAME'),
47                              api_key=opts.get('OS_PASSWORD'),
48                              project_id=opts.get('OS_TENANT_NAME'),
49                              auth_url=opts.get('OS_AUTH_URL'),
50                              region_name='',
51                              extensions=[],
52                              service_type='compute',
53                              service_name='',  
54                             )
55                               
56
57     def __getattr__(self, name):
58         return getattr(self.client, name)
59
60