3 from keystoneclient.v2_0 import client as keystone_client
4 #from glance import client as glance_client
6 from novaclient.v1_1 import client as nova_client
7 from neutronclient.v2_0 import client as quantum_client
12 from planetstack.config import Config
14 def require_enabled(callable):
15 def wrapper(*args, **kwds):
17 return callable(*args, **kwds)
22 def parse_novarc(filename):
24 f = open(filename, 'r')
27 line = line.replace('export', '').strip()
28 parts = line.split('=')
30 value = parts[1].replace("\'", "")
31 value = value.replace('\"', '')
32 opts[parts[0]] = value
39 def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=None, admin=True, *args, **kwds):
41 self.has_openstack = has_openstack
42 self.url = controller.auth_url
44 self.username = controller.admin_user
45 self.password = controller.admin_password
46 self.tenant = controller.admin_tenant
53 self.username = username
55 self.password = password
63 self.endpoint = endpoint
65 #if '@' in self.username:
66 # self.username = self.username[:self.username.index('@')]
68 class KeystoneClient(Client):
69 def __init__(self, *args, **kwds):
70 Client.__init__(self, *args, **kwds)
72 self.client = keystone_client.Client(username=self.username,
73 password=self.password,
74 tenant_name=self.tenant,
79 def connect(self, *args, **kwds):
80 self.__init__(*args, **kwds)
83 def __getattr__(self, name):
84 return getattr(self.client, name)
87 class GlanceClient(Client):
88 def __init__(self, *args, **kwds):
89 Client.__init__(self, *args, **kwds)
91 self.client = glanceclient.get_client(host='0.0.0.0',
92 username=self.username,
93 password=self.password,
97 def __getattr__(self, name):
98 return getattr(self.client, name)
100 class GlanceClientNew(Client):
101 def __init__(self, version, endpoint, token, *args, **kwds):
102 Client.__init__(self, *args, **kwds)
104 self.client = glanceclient.Client(version, endpoint=endpoint, token=token)
107 def __getattr__(self, name):
108 return getattr(self.client, name)
110 class NovaClient(Client):
111 def __init__(self, *args, **kwds):
112 Client.__init__(self, *args, **kwds)
114 self.client = nova_client.Client(username=self.username,
115 api_key=self.password,
116 project_id=self.tenant,
120 service_type='compute',
125 def connect(self, *args, **kwds):
126 self.__init__(*args, **kwds)
129 def __getattr__(self, name):
130 return getattr(self.client, name)
132 class NovaDB(Client):
133 def __init__(self, *args, **kwds):
134 Client.__init__(self, *args, **kwds)
136 self.ctx = get_admin_context()
137 nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf'])
138 self.client = nova_db_api
142 def connect(self, *args, **kwds):
143 self.__init__(*args, **kwds)
146 def __getattr__(self, name):
147 return getattr(self.client, name)
149 class QuantumClient(Client):
150 def __init__(self, *args, **kwds):
151 Client.__init__(self, *args, **kwds)
153 self.client = quantum_client.Client(username=self.username,
154 password=self.password,
155 tenant_name=self.tenant,
158 def connect(self, *args, **kwds):
159 self.__init__(*args, **kwds)
162 def __getattr__(self, name):
163 return getattr(self.client, name)
165 class OpenStackClient:
167 A simple native shell to the openstack backend services.
168 This class can receive all nova calls to the underlying testbed
171 def __init__ ( self, *args, **kwds) :
172 # instantiate managers
173 self.keystone = KeystoneClient(*args, **kwds)
174 url_parsed = urlparse.urlparse(self.keystone.url)
175 hostname = url_parsed.netloc.split(':')[0]
176 token = self.keystone.client.tokens.authenticate(username=self.keystone.username, password=self.keystone.password, tenant_name=self.keystone.tenant)
177 #self.glance = GlanceClient(*args, **kwds)
179 self.glanceclient = GlanceClientNew('1', endpoint='https://%s:9292' % hostname, token=token.id, **kwds)
180 self.nova = NovaClient(*args, **kwds)
181 # self.nova_db = NovaDB(*args, **kwds)
182 self.quantum = QuantumClient(*args, **kwds)
186 def connect(self, *args, **kwds):
187 self.__init__(*args, **kwds)
190 def authenticate(self):
191 return self.keystone.authenticate()