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