Updated to new IceHouse clients
[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 neutronclient.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     has_openstack = True
11 except:
12     has_openstack = False
13
14 from planetstack.config import Config
15
16 def require_enabled(callable):
17     def wrapper(*args, **kwds):
18         if has_openstack:
19             return callable(*args, **kwds)
20         else:
21             return None
22     return wrapper
23
24 def parse_novarc(filename):
25     opts = {}
26     f = open(filename, 'r')
27     for line in f:
28         try:
29             line = line.replace('export', '').strip()
30             parts = line.split('=')
31             if len(parts) > 1:
32                 value = parts[1].replace("\'", "")
33                 value = value.replace('\"', '')
34                 opts[parts[0]] = value
35         except:
36             pass
37     f.close()
38     return opts
39
40 class Client:
41     def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, deployment=None, admin=True, *args, **kwds):
42        
43         self.has_openstack = has_openstack
44         self.url = deployment.auth_url
45         if admin:
46             self.username = deployment.admin_user
47             self.password = deployment.admin_password
48             self.tenant = deployment.admin_tenant
49         else:
50             self.username = None
51             self.password = None
52             self.tenant = None
53
54         if username:
55             self.username = username
56         if password:
57             self.password = password
58         if tenant:
59             self.tenant = tenant
60         if url:
61             self.url = url
62         if token:
63             self.token = token    
64         if endpoint:
65             self.endpoint = endpoint
66
67         #if '@' in self.username:
68         #    self.username = self.username[:self.username.index('@')]
69
70 class KeystoneClient(Client):
71     def __init__(self, *args, **kwds):
72         Client.__init__(self, *args, **kwds)
73         if has_openstack:
74             self.client = keystone_client.Client(username=self.username,
75                                                  password=self.password,
76                                                  tenant_name=self.tenant,
77                                                  auth_url=self.url
78                                                 )
79
80     @require_enabled
81     def connect(self, *args, **kwds):
82         self.__init__(*args, **kwds)
83
84     @require_enabled
85     def __getattr__(self, name):
86         return getattr(self.client, name)
87
88
89 class GlanceClient(Client):
90     def __init__(self, *args, **kwds):
91         Client.__init__(self, *args, **kwds)
92         if has_openstack:
93             self.client = glanceclient.get_client(host='0.0.0.0',
94                                                    username=self.username,
95                                                    password=self.password,
96                                                    tenant=self.tenant,
97                                                    auth_url=self.url)
98     @require_enabled
99     def __getattr__(self, name):
100         return getattr(self.client, name)
101
102 class GlanceClientNew(Client):
103     def __init__(self, version, endpoint, token, *args, **kwds):
104         Client.__init__(self, *args, **kwds)
105         if has_openstack:
106             self.client = glanceclient.Client(version, endpoint=endpoint, token=token)
107
108     @require_enabled
109     def __getattr__(self, name):
110         return getattr(self.client, name)        
111
112 class NovaClient(Client):
113     def __init__(self, *args, **kwds):
114         Client.__init__(self, *args, **kwds)
115         if has_openstack:
116             self.client = nova_client.Client(username=self.username,
117                                              api_key=self.password,
118                                              project_id=self.tenant,
119                                              auth_url=self.url,
120                                              region_name='',
121                                              extensions=[],
122                                              service_type='compute',
123                                              service_name='',
124                                              )
125
126     @require_enabled
127     def connect(self, *args, **kwds):
128         self.__init__(*args, **kwds)
129
130     @require_enabled
131     def __getattr__(self, name):
132         return getattr(self.client, name)
133
134 class NovaDB(Client):
135     def __init__(self, *args, **kwds):
136         Client.__init__(self, *args, **kwds)
137         if has_openstack:
138             self.ctx = get_admin_context()
139             nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
140             self.client = nova_db_api
141
142
143     @require_enabled
144     def connect(self, *args, **kwds):
145         self.__init__(*args, **kwds)
146
147     @require_enabled
148     def __getattr__(self, name):
149         return getattr(self.client, name)
150
151 class QuantumClient(Client):
152     def __init__(self, *args, **kwds):
153         Client.__init__(self, *args, **kwds)
154         if has_openstack:
155             self.client = quantum_client.Client(username=self.username,
156                                                 password=self.password,
157                                                 tenant_name=self.tenant,
158                                                 auth_url=self.url)
159     @require_enabled
160     def connect(self, *args, **kwds):
161         self.__init__(*args, **kwds)
162
163     @require_enabled
164     def __getattr__(self, name):
165         return getattr(self.client, name)
166
167 class OpenStackClient:
168     """
169     A simple native shell to the openstack backend services.
170     This class can receive all nova calls to the underlying testbed
171     """
172
173     def __init__ ( self, *args, **kwds) :
174         # instantiate managers
175         self.keystone = KeystoneClient(*args, **kwds)
176         url_parsed = urlparse.urlparse(self.keystone.url)
177         hostname = url_parsed.netloc.split(':')[0]
178         token = self.keystone.client.tokens.authenticate(username=self.keystone.username, password=self.keystone.password, tenant_name=self.keystone.tenant)
179         #self.glance = GlanceClient(*args, **kwds)
180         
181         self.glanceclient = GlanceClientNew('1', endpoint='http://%s:9292' % hostname, token=token.id, **kwds)
182         self.nova = NovaClient(*args, **kwds)
183         # self.nova_db = NovaDB(*args, **kwds)
184         self.quantum = QuantumClient(*args, **kwds)
185     
186
187     @require_enabled
188     def connect(self, *args, **kwds):
189         self.__init__(*args, **kwds)
190
191     @require_enabled
192     def authenticate(self):
193         return self.keystone.authenticate()