forgot to check in
[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         if '@' in self.username:
45             self.username = self.username[:self.username.index('@')]             
46
47 class KeystoneClient(Client):
48     def __init__(self, *args, **kwds):
49         Client.__init__(self, *args, **kwds)
50         self.client = keystone_client.Client(username=self.username,
51                                              password=self.password,
52                                              tenant_name=self.tenant,
53                                              auth_url=self.url)
54
55     def connect(self, *args, **kwds):
56         self.__init__(*args, **kwds)
57    
58     def __getattr__(self, name):
59         return getattr(self.client, name) 
60
61
62 class GlanceClient(Client):
63     def __init__(self, *args, **kwds):
64         Client.__init__(self, *args, **kwds)
65         self.client = glance_client.get_client(host='0.0.0.0',
66                                                username=self.username,
67                                                password=self.password,
68                                                tenant=self.tenant,
69                                                auth_url=self.url)
70     def __getattr__(self, name):
71         return getattr(self.client, name)
72
73 class NovaClient(Client):
74     def __init__(self, *args, **kwds):
75         Client.__init__(self, *args, **kwds)
76         self.client = nova_client.Client(username=self.username,
77                                          api_key=self.password,
78                                          project_id=self.tenant,
79                                          auth_url=self.url,
80                                          region_name='',
81                                          extensions=[],
82                                          service_type='compute',
83                                          service_name='',  
84                                          )
85
86     def connect(self, *args, **kwds):
87         self.__init__(*args, **kwds)
88                               
89     def __getattr__(self, name):
90         return getattr(self.client, name)       
91
92 class NovaShell:
93     """
94     A simple native shell to a nova backend.
95     This class can receive all nova calls to the underlying testbed
96     """
97
98     def __init__ ( self, *args, **kwds) :
99         # instantiate managers
100         self.keystone = KeystoneClient(*args, **kwds)
101         self.glance = GlanceClient(*args, **kwds)
102         self.nova = NovaClient(*args, **kwds)
103
104     def authenticate(self):
105         return self.keystone.authenticate()