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