fix bugs
[plcapi.git] / PLC / NovaShell.py
1 from keystoneclient.v2_0 import client as keystone_client
2 from glance import client as glance_client
3 from novaclient.v1_1 import client as nova_client
4 from PLC.Config import Config
5 from PLC.Logger import logger
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 Client:
25     def __init__(self, username=None, password=None, tenant=None, url=None, config=None, *args, **kwds):
26         if config:
27             config = Config(config)
28         else:
29             config = Config() 
30         self.username = config.nova_admin_user
31         self.password = config.nova_admin_password
32         self.tenant = config.nova_admin_tenant
33         self.url = config.nova_url
34
35         if username:
36             self.username = username
37         if password:
38             self.password = password
39         if tenant:
40             self.tenant = tenant
41         if url:
42             self.url = url  
43
44 class KeystoneClient(Client):
45     def __init__(self, *args, **kwds):
46         Client.__init__(self, *args, **kwds)
47         self.client = keystone_client.Client(username=self.username,
48                                              password=self.password,
49                                              tenant_name=self.tenant,
50                                              auth_url=self.url)
51
52     def connect(self, *args, **kwds):
53         self.__init__(*args, **kwds)
54    
55     def __getattr__(self, name):
56         return getattr(self.client, name) 
57
58
59 class GlanceClient(Client):
60     def __init__(self, *args, **kwds):
61         Client.__init__(self, *args, **kwds)
62         self.client = glance_client.get_client(host='0.0.0.0',
63                                                username=self.username,
64                                                password=self.password,
65                                                tenant=self.tenant,
66                                                auth_url=self.url)
67     def __getattr__(self, name):
68         return getattr(self.client, name)
69
70 class NovaClient(Client):
71     def __init__(self, *args, **kwds):
72         Client.__init__(self, *args, **kwds)
73         self.client = nova_client.Client(username=self.username,
74                                          api_key=self.password,
75                                          project_id=self.tenant,
76                                          auth_url=self.url,
77                                          region_name='',
78                                          extensions=[],
79                                          service_type='compute',
80                                          service_name='',  
81                                          )
82
83     def connect(self, *args, **kwds):
84         self.__init__(*args, **kwds)
85                               
86     def __getattr__(self, name):
87         return getattr(self.client, name)       
88
89 class NovaShell:
90     """
91     A simple native shell to a nova backend.
92     This class can receive all nova calls to the underlying testbed
93     """
94
95     def __init__ ( self, *args, **kwds) :
96         # instantiate managers
97         self.keystone = KeystoneClient(*args, **kwds)
98         self.glance = GlanceClient(*args, **kwds)
99         self.nova = NovaClient(*args, **kwds)
100
101     def authenticate(self):
102         return self.keystone.authenticate()