def list_servers(nova_client, images): LOGGER.info("Executing list_servers with args : " + images) flavors = list_flavors(nova_client) try: servers = nova_client.servers.list() servers_list = [] for server in servers: server_flavor = None for flavor in flavors: if flavor[constants.FLAVOR_ID] == server.flavor['id']: server_flavor = flavor break server_image = None for image in images: if image[constants.IMAGE_ID] == server.image['id']: server_image = image break server_ips = load_server_ips(nova_client, server.id) servers_list.append({ constants.INSTANCE_ID: server.id, constants.INSTANCE_NAME: server.name, constants.INSTANCE_STATUS: server.status, constants.INSTANCE_FLAVOR: server_flavor, constants.INSTANCE_IMAGE: server_image, constants.INSTANCE_IPS: server_ips }) return servers_list except Exception as e: raise OpenstackException(message="Exception while listing servers : " + e.message, exception=e, logger=LOGGER)
def create_project(client, project_name, description, domain=None, project_id=None): """ If project_id is passed, method will update project. :param client: :param domain: :param project_name: :param description: :param project_id: :return: """ LOGGER.info("Executing create_project with args : " + str(project_name) + "\t" + str(description) + "\t" + str(domain) + "\t" + str(project_id)) try: if project_id: client.projects.update(project=project_id, name=project_name, description=description) else: client.projects.create(name=project_name, domain=domain, description=description) except Exception as e: raise OpenstackException( message="Exception while creating project : " + e.message, exception=e, logger=LOGGER)
def create_user(client, name, password, email, description, roles=None, project_id=None): LOGGER.info("Executing create_user with args : " + str(name) + "\t" + str(email) + "\t" + str(description) + "\t" + str(roles) + "\t" + str(project_id)) try: new_user = client.users.create(name=name, description=description, password=password, email=email, project=project_id) if project_id: assign_roles(client, new_user.id, roles, project_id) return new_user.id except Exception as e: raise OpenstackException( message="Exception while creating user in openstack : " + e.message, exception=e, logger=LOGGER)
def hypervisor_list(nova_client): try: return nova_client.hypervisors.list() except Exception as e: raise OpenstackException( message="Exception while getting hypervisors : " + e.message, exception=e, logger=LOGGER)
def stop_server(nova_client, server_id): LOGGER.info("Executing stop_server with args : " + str(server_id)) try: return nova_client.servers.stop(server=server_id) except Exception as e: raise OpenstackException(message="Exception while stopping server : " + e.message, exception=e, logger=LOGGER)
def get_hypervisor(nova_client, hypervisor): LOGGER.info("Executing get_hypervisor with args : " + str(hypervisor)) try: return nova_client.hypervisors.get(hypervisor) except Exception as e: raise OpenstackException( message="Exception while getting hypervisor : " + e.message, exception=e, logger=LOGGER)
def get_quota_details(nova_client, tenant_id): LOGGER.info("Executing get_quota_details with args : " + str(tenant_id)) try: return nova_client.quotas.get(tenant_id=tenant_id) except Exception as e: raise OpenstackException( message="Exception while getting quota details : " + e.message, exception=e, logger=LOGGER)
def delete_project(client, project_id): LOGGER.info("Executing delete_project with args : " + str(project_id)) try: client.projects.delete(project=project_id) except Exception as e: raise OpenstackException( message="Exception while deleting project : " + e.message, exception=e, logger=LOGGER)
def delete_user(client, user_id): LOGGER.info("Executing assign_roles with args : " + str(user_id)) try: client.users.delete(user=user_id) except Exception as e: raise OpenstackException(message="Exception while deleting user : " + e.message, exception=e, logger=LOGGER)
def is_admin_for_project(self): try: user_id, token, endpoint_urls = self.load_keystone_client(self.project_id) is_admin = False if endpoint_urls: is_admin = keystone.is_admin(self.keystone_client, user_id, self.project_id) return token, endpoint_urls, is_admin except Exception as e: raise OpenstackException(message=e.message, exception=e)
def create_neutron_client(endpoint, token): LOGGER.info("Executing create_neutron_client with args : " + endpoint + "\t" + token) try: return neutron_client.Client(endpoint_url=endpoint, token=token) except Exception as e: raise OpenstackException( message="Exception while creating neutron client : " + e.message, exception=e, logger=LOGGER)
def modify_flavor(nova_client, server_id, flavor_id): LOGGER.info("Executing modify_flavor with args : " + str(server_id) + "\t" + str(flavor_id)) try: nova_client.servers.resize(server=server_id, flavor=flavor_id) except Exception as e: raise OpenstackException( message="Exception while modifying flavor : " + e.message, exception=e, logger=LOGGER)
def revoke_roles(client, user_id, roles, project_id): LOGGER.info("Executing revoke_roles with args : " + str(user_id) + "\t" + str(roles) + "\t" + str(project_id)) try: for role in roles: client.roles.revoke(role=role, user=user_id, project=project_id) except Exception as e: raise OpenstackException(message="Exception while revoking role : " + e.message, exception=e, logger=LOGGER)
def load_console(nova_client, server_id): LOGGER.info("Executing load_console with args : " + str(server_id)) try: vnc_url_obj = nova_client.servers.get_vnc_console(server=server_id, console_type="novnc") return vnc_url_obj['console']['url'] except Exception as e: raise OpenstackException(message="Exception while loading console : " + e.message, exception=e, logger=LOGGER)
def generate_admin_auth(self): projects, unscoped_auth = self.get_projects_using_unscoped_login() for project in projects: scoped_auth = keystone.scoped_login_v3(self.protocol, self.host, self.port, unscoped_auth['token'], project['id']) if keystone.is_admin(scoped_auth['client'], unscoped_auth['user_id'], project['id']): self.keystone_client = scoped_auth['client'] break else: raise OpenstackException(message="Unable to find admin role for given user.") return scoped_auth['token'], project['id'], scoped_auth['endpoint_urls']
def create_glance_client(version, endpoint, token): LOGGER.info("Executing create_glance_client with args : " + version + "\t" + endpoint + "\t" + token) try: return glance_client.Client(version=version, endpoint=endpoint, token=token) except Exception as e: raise OpenstackException( message="Exception while creating glance client : " + e.message, exception=e, logger=LOGGER)
def get_tenant_usage(nova_client, tenant_id, start_date, end_date): LOGGER.info("Executing get_tenant_usage with args : " + str(tenant_id) + "\t" + str(start_date) + "\t" + str(end_date)) try: return nova_client.usage.get(tenant_id=tenant_id, start=start_date, end=end_date) except Exception as e: raise OpenstackException( message="Exception while getting usage details : " + e.message, exception=e, logger=LOGGER)
def list_projects(client, v2_api=True): LOGGER.info("Executing list_projects with args : " + str(v2_api)) try: if v2_api: projects_list = client.tenants.list() else: projects_list = client.projects.list() return projects_list except Exception as e: raise OpenstackException( message="Exception while getting list of projects : " + e.message, exception=e, logger=LOGGER)
def unscoped_login(protocol, host, port, username, password): LOGGER.info("Executing unscoped_login with args : " + str(protocol) + "\t" + str(host) + "\t" + str(port) + "\t" + str(username)) try: url = protocol + "://" + host + ":" + port + "/v2.0" auth = v2.Password(username=username, password=password, auth_url=url) session = keystone_session.Session(auth=auth) client = v2_client.Client(session=session) token = client.session.get_token(auth) user_id = client.session.get_user_id(auth) return {'token': token, 'client': client, 'user_id': user_id} except Exception as e: raise OpenstackException( message="Exception while performing unscoped login : " + e.message, exception=e, logger=LOGGER)
def get_roles(client): try: roles = client.roles.list() role_list = [] for role in roles: role_list.append({ constants.ROLE_ID: role.id, constants.ROLE_NAME: role.name }) return role_list except Exception as e: raise OpenstackException( message="Exception while getting roles from openstack: " + e.message, exception=e, logger=LOGGER)
def image_list(client): try: images = client.images.list() images_list = [] for image in images: images_list.append({ constants.IMAGE_ID: image.id, constants.IMAGE_NAME: image.name, constants.IMAGE_STATUS: image.status, constants.IMAGE_SIZE: image.size / 1000000 }) return images_list except Exception as e: raise OpenstackException(message="Exception while listing images : " + e.message, exception=e, logger=LOGGER)
def create_server(nova_client, server_name, image_id, flavor_id, network_id): LOGGER.info("Executing create_server with args : " + str(server_name) + "\t" + str(image_id) + "\t" + str(flavor_id) + "\t" + str(network_id)) try: server = nova_client.servers.create(name=server_name, image=image_id, flavor=flavor_id, nics=[{ 'net-id': network_id }]) return server.id except Exception as e: raise OpenstackException(message="Exception while creating server : " + e.message, exception=e, logger=LOGGER)
def list_flavors(nova_client): try: flavors = nova_client.flavors.list() flavors_list = [] for flavor in flavors: flavors_list.append({ constants.FLAVOR_ID: flavor.id, constants.FLAVOR_NAME: flavor.name, constants.FLAVOR_VCPU: flavor.vcpus, constants.FLAVOR_RAM: flavor.ram, constants.FLAVOR_DISK: flavor.disk }) return flavors_list except Exception as e: raise OpenstackException(message="Exception while listing flavor : " + e.message, exception=e, logger=LOGGER)
def get_nova_connection(protocol, host, port, domain, username, password, project_id): LOGGER.info("Executing get_nova_connection with args : " + str(protocol) + "\t" + str(host) + "\t" + str(port) + "\t" + str(domain) + "\t" + str(username) + "\t" + str(project_id)) try: auth_url = protocol + "://" + host + ":" + port + "/v3" return client.Client("2", username=username, password=password, project_id=project_id, auth_url=auth_url, user_domain_name=domain) except Exception as e: raise OpenstackException( message="Exception while loading nova client : " + e.message, exception=e, logger=LOGGER)
def network_list(client): try: networks = client.list_networks()['networks'] networks_list = [] for network in networks: networks_list.append({ constants.NETWORK_ID: str(network['id']), constants.NETWORK_NAME: str(network['name']), constants.NETWORK_STATUS: str(network['status']), }) return networks_list except Exception as e: raise OpenstackException( message="Exception while listing networks : " + e.message, exception=e, logger=LOGGER)
def get_project(client, project_id): LOGGER.info("Executing get_project with args : " + str(project_id)) try: project = client.projects.get(project=project_id) return { 'project_id': project_id, 'project_name': project.name, 'project_description': project.description, 'enabled': project.enabled, 'is_domain': project.is_domain, 'domain_id': project.domain_id, 'parent_id': project.parent_id } except Exception as e: raise OpenstackException( message="Exception while getting project information : " + e.message, exception=e, logger=LOGGER)
def get_user_roles(client, user_id, project_id): LOGGER.info("Executing get_user_roles with args : " + str(user_id) + "\t" + str(project_id)) try: user_role_ids = [] user_roles = [] users_role_list = client.roles.list(user=user_id, project=project_id) for role in users_role_list: user_role_ids.append(role.id) user_roles.append(role.name) return user_roles, user_role_ids except Exception as e: raise OpenstackException( message= "Exception while getting roles for specified project and user : " + e.message, exception=e, logger=LOGGER)
def list_users_in_project(client, users, project_id): LOGGER.info("Executing list_users_in_project with args : " + str(users) + "\t" + str(project_id)) try: if not users: users = list_user(client, with_sol_user=True) project_users = [] for user in users: roles, ids = get_user_roles(client, user[constants.USER_ID], project_id) if roles: user[constants.ROLES] = roles user['role_ids'] = ids project_users.append(user) return project_users except Exception as e: raise OpenstackException( message="Exception while listing users in project : " + e.message, exception=e, logger=LOGGER)
def set_quota_details(nova_client, tenant_id, quotas): LOGGER.info("Executing set_quota_details with args : " + str(tenant_id) + "\t" + str(quotas)) try: nova_client.quotas.update( tenant_id=tenant_id, cores=quotas[constants.TOTAL_CPU], fixed_ips=quotas[constants.FIXED_IPS], floating_ips=quotas[constants.FLOATING_IPS], instances=quotas[constants.INSTANCES], ram=quotas[constants.TOTAL_MEMORY], security_group_rules=quotas[constants.SECURITY_GROUP_RULES], security_groups=quotas[constants.SECURITY_GROUPS], server_group_members=quotas[constants.SERVER_GROUP_MEMBERS], server_groups=quotas[constants.SERVER_GROUPS]) except Exception as e: return OpenstackException(message="Exception while setting quota : " + e.message, exception=e, logger=LOGGER)
def list_user(client, with_sol_user=False): LOGGER.info("Executing list_user with args : " + str(with_sol_user)) try: users = client.users.list() user_list = [] for user in users: if user.name == constants.HYPERVISOR_SOLUSER_NAME and not with_sol_user: continue user_list.append({ constants.USER_ID: user.id, constants.USERNAME: user.name, constants.USER_FULL_NAME: None if not hasattr(user, 'description') else user.description }) return user_list except Exception as e: raise OpenstackException(message="Exception while lsting users : " + e.message, exception=e, logger=LOGGER)