215d33305412e2a24ddaf547faa59181b5f69428
[sfa.git] / sfa / openstack / client.py
1 from sfa.util.sfalogging import logger
2 from keystoneclient.v2_0 import client as keystone_client
3 from glance import client as glance_client
4 from novaclient.v1_1 import client as nova_client
5 from sfa.util.config import Config
6
7
8 def parse_novarc(filename):
9     opts = {}
10     f = open(filename, 'r')
11     for line in f:
12         try:
13             line = line.replace('export', '').strip()
14             parts = line.split('=')
15             if len(parts) > 1:
16                 value = parts[1].replace("\'", "")
17                 value = value.replace('\"', '') 
18                 opts[parts[0]] = value
19         except:
20             pass
21     f.close()
22     return opts
23
24
25 class KeystoneClient:
26     def __init__(self, username=None, password=None, tenant=None, url=None, config=None):
27         if not config:
28             config = Config()
29         opts = parse_novarc(config.SFA_NOVA_NOVARC)
30         if username:
31             opts['OS_USERNAME'] = username
32         if password:
33             opts['OS_PASSWORD'] = password
34         if tenant:
35             opts['OS_TENANT_NAME'] = tenant
36         if url:
37             opts['OS_AUTH_URL'] = url
38         self.opts = opts 
39         self.client = keystone_client.Client(username=opts.get('OS_USERNAME'),
40                                              password=opts.get('OS_PASSWORD'),
41                                              tenant_name=opts.get('OS_TENANT_NAME'),
42                                              auth_url=opts.get('OS_AUTH_URL'))
43
44     def connect(self, *args, **kwds):
45         self.__init__(*args, **kwds)
46    
47     def __getattr__(self, name):
48         return getattr(self.client, name) 
49
50
51 class GlanceClient:
52     def __init__(self, config=None):
53         if not config:
54             config = Config()
55         opts = parse_novarc(config.SFA_NOVA_NOVARC)
56         self.client = glance_client.get_client(host='0.0.0.0',
57                                                username=opts.get('OS_USERNAME'),
58                                                password=opts.get('OS_PASSWORD'),
59                                                tenant=opts.get('OS_TENANT_NAME'),
60                                                auth_url=opts.get('OS_AUTH_URL'))
61     def __getattr__(self, name):
62         return getattr(self.client, name)
63
64 class NovaClient:
65     def __init__(self, username=None, password=None, tenant=None, url=None, config=None):
66         if not config:
67             config = Config()
68         opts = parse_novarc(config.SFA_NOVA_NOVARC)
69         if username:
70             opts['OS_USERNAME'] = username
71         if password:
72             opts['OS_PASSWORD'] = password
73         if tenant:
74             opts['OS_TENANT_NAME'] = tenant
75         if url:
76             opts['OS_AUTH_URL'] = url
77         self.opts = opts
78         self.client = nova_client.Client(username=opts.get('OS_USERNAME'),
79                                          api_key=opts.get('OS_PASSWORD'),
80                                          project_id=opts.get('OS_TENANT_NAME'),
81                                          auth_url=opts.get('OS_AUTH_URL'),
82                                          region_name='',
83                                          extensions=[],
84                                          service_type='compute',
85                                          service_name='',  
86                                          )
87
88     def connect(self, *args, **kwds):
89         self.__init__(*args, **kwds)
90                               
91     def __getattr__(self, name):
92         return getattr(self.client, name)