465425d9fb3a5c44c40c0fe91d278d98066c5c2b
[plstackapi.git] / planetstack / openstack / client.py
1 import urlparse
2 try:
3     from keystoneclient.v2_0 import client as keystone_client
4     from glance import client as glance_client
5     import glanceclient
6     from novaclient.v1_1 import client as nova_client
7     from quantumclient.v2_0 import client as quantum_client
8     from nova.db.sqlalchemy import api as nova_db_api 
9     from nova.context import get_admin_context
10     from keystone.common.sql import core  
11     core.CONF(args=[], project='keystone', default_config_files=['/etc/keystone/keystone.conf'])
12     from keystone.identity.backends.sql import Metadata
13     has_openstack = True
14 except:
15     has_openstack = False
16
17 from planetstack.config import Config
18 from deployment_auth import deployment_auth
19
20 def require_enabled(callable):
21     def wrapper(*args, **kwds):
22         if has_openstack:
23             return callable(*args, **kwds)
24         else:
25             return None
26     return wrapper
27
28 def parse_novarc(filename):
29     opts = {}
30     f = open(filename, 'r')
31     for line in f:
32         try:
33             line = line.replace('export', '').strip()
34             parts = line.split('=')
35             if len(parts) > 1:
36                 value = parts[1].replace("\'", "")
37                 value = value.replace('\"', '')
38                 opts[parts[0]] = value
39         except:
40             pass
41     f.close()
42     return opts
43
44 class Client:
45     def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, deployment=None, admin=True, *args, **kwds):
46         
47             
48         if not deployment or deployment not in deployment_auth:
49             auth = deployment_auth['default']
50         else:
51             auth = deployment_auth[deployment]
52             
53         self.has_openstack = has_openstack
54
55         self.url = auth['url']
56         if admin:
57             self.username = auth['user']
58             self.password = auth['password']
59             self.tenant = auth['tenant']
60             self.endpoint = auth['endpoint']
61             self.token = auth['token']  
62         else:
63             self.username = None
64             self.password = None
65             self.tenant = None
66
67         if username:
68             self.username = username
69         if password:
70             self.password = password
71         if tenant:
72             self.tenant = tenant
73         if url:
74             self.url = url
75         if token:
76             self.token = token    
77         if endpoint:
78             self.endpoint = endpoint
79
80         #if '@' in self.username:
81         #    self.username = self.username[:self.username.index('@')]
82
83 class KeystoneDB:
84     @require_enabled
85     def get_session(self):
86         return core.Base().get_session()
87
88     @require_enabled
89     def get_metadata(self):
90         session = self.get_session()
91         return session.query(Metadata).all()     
92
93
94 class KeystoneClient(Client):
95     def __init__(self, *args, **kwds):
96         Client.__init__(self, *args, **kwds)
97         if has_openstack:
98             self.client = keystone_client.Client(username=self.username,
99                                                  password=self.password,
100                                                  tenant_name=self.tenant,
101                                                  auth_url=self.url,
102                                                  endpoint=self.endpoint,
103                                                  token=self.token
104                                                 )
105
106     @require_enabled
107     def connect(self, *args, **kwds):
108         self.__init__(*args, **kwds)
109
110     @require_enabled
111     def __getattr__(self, name):
112         return getattr(self.client, name)
113
114
115 class GlanceClient(Client):
116     def __init__(self, *args, **kwds):
117         Client.__init__(self, *args, **kwds)
118         if has_openstack:
119             self.client = glance_client.get_client(host='0.0.0.0',
120                                                    username=self.username,
121                                                    password=self.password,
122                                                    tenant=self.tenant,
123                                                    auth_url=self.url)
124     @require_enabled
125     def __getattr__(self, name):
126         return getattr(self.client, name)
127
128 class GlanceClientNew(Client):
129     def __init__(self, version, endpoint, token, *args, **kwds):
130         Client.__init__(self, *args, **kwds)
131         if has_openstack:
132             self.client = glanceclient.Client(version, endpoint=endpoint, token=token)
133
134     @require_enabled
135     def __getattr__(self, name):
136         return getattr(self.client, name)        
137
138 class NovaClient(Client):
139     def __init__(self, *args, **kwds):
140         Client.__init__(self, *args, **kwds)
141         if has_openstack:
142             self.client = nova_client.Client(username=self.username,
143                                              api_key=self.password,
144                                              project_id=self.tenant,
145                                              auth_url=self.url,
146                                              region_name='',
147                                              extensions=[],
148                                              service_type='compute',
149                                              service_name='',
150                                              )
151
152     @require_enabled
153     def connect(self, *args, **kwds):
154         self.__init__(*args, **kwds)
155
156     @require_enabled
157     def __getattr__(self, name):
158         return getattr(self.client, name)
159
160 class NovaDB(Client):
161     def __init__(self, *args, **kwds):
162         Client.__init__(self, *args, **kwds)
163         if has_openstack:
164             self.ctx = get_admin_context()
165             nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
166             self.client = nova_db_api
167
168
169     @require_enabled
170     def connect(self, *args, **kwds):
171         self.__init__(*args, **kwds)
172
173     @require_enabled
174     def __getattr__(self, name):
175         return getattr(self.client, name)
176
177 class QuantumClient(Client):
178     def __init__(self, *args, **kwds):
179         Client.__init__(self, *args, **kwds)
180         if has_openstack:
181             self.client = quantum_client.Client(username=self.username,
182                                                 password=self.password,
183                                                 tenant_name=self.tenant,
184                                                 auth_url=self.url)
185     @require_enabled
186     def connect(self, *args, **kwds):
187         self.__init__(*args, **kwds)
188
189     @require_enabled
190     def __getattr__(self, name):
191         return getattr(self.client, name)
192
193 class OpenStackClient:
194     """
195     A simple native shell to the openstack backend services.
196     This class can receive all nova calls to the underlying testbed
197     """
198
199     def __init__ ( self, *args, **kwds) :
200         # instantiate managers
201         self.keystone = KeystoneClient(*args, **kwds)
202         url_parsed = urlparse.urlparse(self.keystone.url)
203         hostname = url_parsed.netloc.split(':')[0]
204         token = self.keystone.client.tokens.authenticate(username=self.keystone.username, password=self.keystone.password, tenant_name=self.keystone.tenant)
205         self.keystone_db = KeystoneDB()
206         self.glance = GlanceClient(*args, **kwds)
207         
208         self.glanceclient = GlanceClientNew('1', endpoint='http://%s:9292' % hostname, token=token.id)
209         self.nova = NovaClient(*args, **kwds)
210         self.nova_db = NovaDB(*args, **kwds)
211         self.quantum = QuantumClient(*args, **kwds)
212     
213
214     @require_enabled
215     def connect(self, *args, **kwds):
216         self.__init__(*args, **kwds)
217
218     @require_enabled
219     def authenticate(self):
220         return self.keystone.authenticate()