def generate_images(self, nof_images): if len(self.default_images) + len(self.created_images) < nof_images: session = self._authenticate() glance = GlanceClient('1', endpoint=self.glance_url, token=session.get_token()) create_kw = { "container_format": "bare", "disk_format": "qcow2", "name": "horizon_load_test_image", "copy_from": "http://172.16.44.5/cirros-0.3.1-x86_64-disk.img" } nof_images_to_create = nof_images - len(self.default_images) - len(self.created_images) for i in range(0, nof_images_to_create): image = glance.images.create(**create_kw) self._wait_for_image_status(glance, image.id, "active") self.created_images.append(image) elif len(self.default_images) + len(self.created_images) > nof_images: nof_images_to_delete = len(self.default_images) + len(self.created_images) - nof_images if nof_images_to_delete > len(self.created_images): raise Exception("Cannot delete such number of images") for i in range(nof_images_to_delete): image_to_delete = self.created_images.pop() self._delete_image(image_to_delete.id)
def get_swiftclient(self): self._require_module('swift') session = self.get_session() endpoint = self.get_public_endpoint('object-store', self.region) token = session.get_token() return self._modules_imported['swift'].Connection( preauthurl=endpoint, preauthtoken=token)
def get_swiftclient(self): self._require_module('swift') session = self.get_session() endpoint = self.get_public_endpoint('object-store', self.region) token = session.get_token() return self._modules_imported['swift'].Connection(preauthurl=endpoint, preauthtoken=token)
def get_glanceclient(): session = get_session() token = session.get_token() region = None if 'OS_REGION_NAME' in env: region = env['OS_REGION_NAME'] endpoint = session.get_endpoint(service_type='image', region_name=region) return glanceclient.Client(version='1', endpoint=endpoint, token=token)
def get_session_info(output=False): session = get_session(output=True) print "=== Session Information ===" print "Domain :", session.auth.project_domain_name print "Project ID :", session.get_project_id() print "User ID :", session.get_user_id() print "Raw Token :", session.get_token() return {}
def _create_test_image(self): session = self._authenticate() glance = GlanceClient('1', endpoint=self.glance_url, token=session.get_token()) create_kw = { "container_format": "bare", "disk_format": "qcow2", "name": "horizon_load_test_image_for_servers", "copy_from": "http://172.16.44.5/cirros-0.3.1-x86_64-disk.img" } image = glance.images.create(**create_kw) self._wait_for_image_status(glance, image.id, "active") return image
def generate_users(self, nof_users): session = self._authenticate() keystone = KeystoneClientFactory.Client(token=session.get_token(), endpoint=self.keystone_url) if len(self.default_users) + len(self.created_users) < nof_users: nof_users_to_create = nof_users - len(self.default_users) - len(self.created_users) for i in range(0, nof_users_to_create): rand_hash = random.getrandbits(128) user = keystone.users.create(name="horizon_load_test_user_%032x" % rand_hash) self.created_users.append(user) elif len(self.default_users) + len(self.created_users) > nof_users: nof_users_to_delete = len(self.default_users) + len(self.created_users) - nof_users if nof_users_to_delete > len(self.created_users): raise Exception("Cannot delete such number of users") for i in range(nof_users_to_delete): user_to_delete = self.created_users.pop() keystone.users.delete(user_to_delete.id)
def generate_projects(self, nof_projects): session = self._authenticate() keystone = KeystoneClientFactory.Client(token=session.get_token(), endpoint=self.keystone_url) if len(self.default_projects) + len(self.created_projects) < nof_projects: nof_projects_to_create = nof_projects - len(self.default_projects) - len(self.created_projects) for i in range(0, nof_projects_to_create): rand_hash = random.getrandbits(128) project = keystone.projects.create(name="horizon_load_test_project_%032x" % rand_hash, domain="default") self.created_projects.append(project) elif len(self.default_projects) + len(self.created_projects) > nof_projects: nof_projects_to_delete = len(self.default_projects) + len(self.created_projects) - nof_projects if nof_projects_to_delete > len(self.created_projects): raise Exception("Cannot delete such number of projects") for i in range(nof_projects_to_delete): project_to_delete = self.created_users.pop() keystone.users.delete(project_to_delete)
def get_glanceclient(self): """Get a glance client. A client is different for each region (although all clients share the same session and it is possible to have simultaneously clients to several regions). Before calling this method, the credential must be provided. The constructor obtain the credential for environment variables if present but also the method set_credential is available. Be aware that calling the method set_credential invalidate the old session if already existed and therefore can affect the old clients. :return: a glance client valid for a region. """ session = self.get_session() token = session.get_token() endpoint = session.get_endpoint(service_type='image', region_name=self.region) return glanceclient.Client(version='1', endpoint=endpoint, token=token)
def get_glanceclient(self): """Get a glance client. A client is different for each region (although all clients share the same session and it is possible to have simultaneously clients to several regions). Before calling this method, the credential must be provided. The constructor obtain the credential for environment variables if present but also the method set_credential is available. Be aware that calling the method set_credential invalidate the old session if already existed and therefore can affect the old clients. :return: a glance client valid for a region. """ self._require_module('glance') session = self.get_session() token = session.get_token() endpoint = session.get_endpoint(service_type='image', region_name=self.region) return self._modules_imported['glance'].Client( version='1', endpoint=endpoint, token=token)
def main(): """ 1. 从username、password获取unscoped token 2. 使用unscoped token获取该用户的projects列表 3. 使用该用户的第一个可用project生成scoped token 4. 试试这个scoped token吧! """ keystone = get_unscoped_client() keystone.management_url = local_settings.auth_url_v3 projects = list_user_projects(keystone) auth = keystoneclient.auth.identity.v3.Token(auth_url=local_settings.auth_url_v3, token=keystone.auth_token, project_id=projects[0].id) session = keystoneclient.session.Session(auth=auth) nova = novaclient.client.Client('2', session=session) print json.dumps([i.to_dict() for i in nova.flavors.list()]) auth2 = keystoneclient.auth.identity.v3.Token(auth_url=local_settings.auth_url_v3, token=session.get_token(), project_id=projects[1].id) session2 = keystoneclient.session.Session(auth=auth2) nova2 = novaclient.client.Client('2', session=session2) print json.dumps([i.to_dict() for i in nova2.flavors.list()])
def main(): """ 1. 从username、password获取unscoped token 2. 使用unscoped token获取该用户的projects列表 3. 使用该用户的第一个可用project生成scoped token 4. 试试这个scoped token吧! """ keystone = get_unscoped_client() keystone.management_url = local_settings.auth_url_v3 projects = list_user_projects(keystone) auth = keystoneclient.auth.identity.v3.Token( auth_url=local_settings.auth_url_v3, token=keystone.auth_token, project_id=projects[0].id) session = keystoneclient.session.Session(auth=auth) nova = novaclient.client.Client('2', session=session) print json.dumps([i.to_dict() for i in nova.flavors.list()]) auth2 = keystoneclient.auth.identity.v3.Token( auth_url=local_settings.auth_url_v3, token=session.get_token(), project_id=projects[1].id) session2 = keystoneclient.session.Session(auth=auth2) nova2 = novaclient.client.Client('2', session=session2) print json.dumps([i.to_dict() for i in nova2.flavors.list()])
def getToken(self): session = self.getSession() return session.get_token()
#!/usr/bin/env python # -*- coding: utf-8 -*- import json import keystoneclient import keystoneclient.auth.identity.v3 import keystoneclient.session import keystoneclient.v3.client import ceilometerclient.client import local_settings keystone = keystoneclient.v3.client.Client(auth_url=local_settings.auth_url_v3, username=local_settings.username, password=local_settings.password, unscoped=True) keystone.management_url = local_settings.auth_url_v3 projects = keystone.projects.list(user=keystone.user_id) auth = keystoneclient.auth.identity.v3.Token( auth_url=local_settings.auth_url_v3, token=keystone.auth_token, project_id=projects[0].id) session = keystoneclient.session.Session(auth=auth) client = ceilometerclient.client.get_client( '2', token=session.get_token(), ceilometer_url='http://10.202.19.11:8777') print json.dumps([i.to_dict() for i in client.resources.list()])
def _delete_image(self, image_id): session = self._authenticate() glance = GlanceClient('1', endpoint=self.glance_url, token=session.get_token()) glance.images.delete(image_id)
def _get_users(self): session = self._authenticate() keystone = KeystoneClientFactory.Client(token=session.get_token(), endpoint=self.keystone_url) users = keystone.users.list() return [user for user in users]
def _get_neutron_client(self): session = self._authenticate() neutron = NeutronClientFactory.Client(version="2", endpoint_url=self.neutron_url, token=session.get_token()) return neutron
def _cleanup_projects(self): session = self._authenticate() keystone = KeystoneClientFactory.Client(token=session.get_token(), endpoint=self.keystone_url) for project in self.created_projects: keystone.projects.delete(project)
def _cleanup_images(self): session = self._authenticate() glance = GlanceClient('1', endpoint=self.glance_url, token=session.get_token()) glance.images.delete(self.test_image.id) for image in self.created_images: glance.images.delete(image.id)
def get_swiftclient(self): session = self.get_session() token = session.get_token() endpoint = self.get_public_endpoint('object-store', self.region) token = session.get_token() return swiftclient.Connection(preauthurl=endpoint, preauthtoken=token)
def _get_projects(self): session = self._authenticate() keystone = KeystoneClientFactory.Client(token=session.get_token(), endpoint=self.keystone_url) projects = keystone.projects.list() return [project for project in projects]
#!/usr/bin/env python # -*- coding: utf-8 -*- import json import keystoneclient import keystoneclient.auth.identity.v3 import keystoneclient.session import keystoneclient.v3.client import ceilometerclient.client import local_settings keystone = keystoneclient.v3.client.Client(auth_url=local_settings.auth_url_v3, username=local_settings.username, password=local_settings.password, unscoped=True) keystone.management_url = local_settings.auth_url_v3 projects = keystone.projects.list(user=keystone.user_id) auth = keystoneclient.auth.identity.v3.Token(auth_url=local_settings.auth_url_v3, token=keystone.auth_token, project_id=projects[0].id) session = keystoneclient.session.Session(auth=auth) client = ceilometerclient.client.get_client('2', token=session.get_token(), ceilometer_url='http://10.202.19.11:8777') print json.dumps([i.to_dict() for i in client.alarms.list()])
def _get_images(self): session = self._authenticate() glance = GlanceClient('1', endpoint=self.glance_url, token=session.get_token()) images = glance.images.list() return [image for image in images]
from keystoneclient import session from keystoneclient.v3 import client from keystoneclient.auth.identity import v3 # http://docs.openstack.org/developer/python-keystoneclient/using-api-v3.html def authenticate(): auth_url = '{PROTO}://{HOST}:{IDENTITY_PORT}/v3'.format(**CONNECTION) auth = v3.Password(auth_url=auth_url, username=USERNAME, password=PASSWORD, project_name=TENANT) return session.Session(auth=auth) if __name__ == '__main__': session = authenticate() keystone = client.Client(session=session) print(' TOKEN: {0}'.format(session.get_token())) print('-SERVICE CATALOG-') for service in keystone.services.list(): print('--') print("NAME: {0}".format(service.name)) print("TYPE: {0}".format(service.type))
def _cleanup_users(self): session = self._authenticate() keystone = KeystoneClientFactory.Client(token=session.get_token(), endpoint=self.keystone_url) for user in self.created_users: keystone.users.delete(user.id)