<commit_before>#  -*- coding: utf-8 -*-
"""
This module manage interaction between application and
OpenStack cloud infrastructure
"""
import math

from datetime import date
from dateutil.relativedelta import relativedelta

from django.core.cache import cache
from django.utils.dateparse import parse_datetime

from keystoneauth1.identity import v3
from keystoneauth1 import session, exceptions as ksexceptions
from keystoneclient.v3 import client as ksclient
from novaclient import client as nvclient

from openstack_lease_it.settings import GLOBAL_CONFIG
from lease_it.datastore import InstancesAccess, LEASE_DURATION
from lease_it.backend.Exceptions import PermissionDenied

# Define nova client version as a constant
NOVA_VERSION = 2

# Default cache timeout (in sec)
FLAVOR_CACHE_TIMEOUT = 86400
USERS_CACHE_TIMEOUT = 86400
PROJECTS_CACHE_TIMEOUT = 86400
INSTANCES_CACHE_TIMEOUT = 86400


class OpenstackConnection(object):  # pylint: disable=too-few-public-methods
    """
    This class manage interface between OpenStack Cloud infrastructure and
    views.
    """
    def __init__(self):
        """
        During class initialization, we create a connection to
        OpenStack Cloud
        """
        super(OpenstackConnection, self).__init__()
        # We need to be admin to have access to hypervisor list
        credentials = dict()
        credentials['username'] = GLOBAL_CONFIG['OS_USERNAME']
        credentials['password'] = GLOBAL_CONFIG['<PASSWORD>']
        credentials['auth_url'] = GLOBAL_CONFIG['OS_AUTH_URL']
        credentials['project_name'] = GLOBAL_CONFIG['OS_PROJECT_NAME']
        credentials['project_domain_name'] = GLOBAL_CONFIG['OS_PROJECT_DOMAIN_NAME']
        credentials['user_domain_name'] = GLOBAL_CONFIG['OS_USER_DOMAIN_NAME']

        try:
            auth = v3.Password(**credentials)
            self.session = session.Session(auth=auth,
                                           verify=GLOBAL_CONFIG['OS_CACERT'])
        except:  # pylint: disable=bare-except
            pass

    def _instances(self):
        """
        List of instances actually launched
        :return: dict()
        """
        response = cache.get('instances')
        if not response:
            response = dict()
            nova = nvclient.Client(NOVA_VERSION, session=self.session)
            data_instances = nova.servers.list(search_opts={'all_tenants': 'true'})
            for instance in data_instances:
                response[instance.id] = {
                    'user_id': instance.user_id,
                    'project_id': instance.tenant_id,
                    'id': instance.id,
                    'name': instance.name,
                    'created_at': parse_datetime(instance.created).date()
                }
            cache.set('instances', response, INSTANCES_CACHE_TIMEOUT)
        return response

    def _hypervisors(self):
        """
        List of hypervisors and their details
        :return: dict()
        """
        nova = nvclient.Client(NOVA_VERSION, session=self.session)
        hypervisors = nova.hypervisors.list()
        response = list()
        for hypervisor in hypervisors:
            response.append( {
                'status': hypervisor.status,
                'state': hypervisor.state,
                'vcpus': hypervisor.vcpus,
                'vcpus_used': hypervisor.vcpus_used,
                'free_ram': hypervisor.free_disk_gb,
                'memory': hypervisor.memory_mb,
                'free_disk': hypervisor.free_disk_gb,
                'local_disk': hypervisor.local_gb
            })
        return response

    def _flavors(self):
        """
        List of flavors and their details
        """<commit_msg>Some PEP8 fixes<commit_after>#  -*- coding: utf-8 -*-
"""
This module manage interaction between application and
OpenStack cloud infrastructure
"""
import math

from datetime import date
from dateutil.relativedelta import relativedelta

from django.core.cache import cache
from django.utils.dateparse import parse_datetime

from keystoneauth1.identity import v3
from keystoneauth1 import session, exceptions as ksexceptions
from keystoneclient.v3 import client as ksclient
from novaclient import client as nvclient

from openstack_lease_it.settings import GLOBAL_CONFIG
from lease_it.datastore import InstancesAccess, LEASE_DURATION
from lease_it.backend.Exceptions import PermissionDenied

# Define nova client version as a constant
NOVA_VERSION = 2

# Default cache timeout (in sec)
FLAVOR_CACHE_TIMEOUT = 86400
USERS_CACHE_TIMEOUT = 86400
PROJECTS_CACHE_TIMEOUT = 86400
INSTANCES_CACHE_TIMEOUT = 86400


class OpenstackConnection(object):  # pylint: disable=too-few-public-methods
    """
    This class manage interface between OpenStack Cloud infrastructure and
    views.
    """
    def __init__(self):
        """
        During class initialization, we create a connection to
        OpenStack Cloud
        """
        super(OpenstackConnection, self).__init__()
        # We need to be admin to have access to hypervisor list
        credentials = dict()
        credentials['username'] = GLOBAL_CONFIG['OS_USERNAME']
        credentials['password'] = GLOBAL_CONFIG['OS_PASSWORD']
        credentials['auth_url'] = GLOBAL_CONFIG['OS_AUTH_URL']
        credentials['project_name'] = GLOBAL_CONFIG['OS_PROJECT_NAME']
        credentials['project_domain_name'] = GLOBAL_CONFIG['OS_PROJECT_DOMAIN_NAME']
        credentials['user_domain_name'] = GLOBAL_CONFIG['OS_USER_DOMAIN_NAME']

        try:
            auth = v3.Password(**credentials)
            self.session = session.Session(auth=auth,
                                           verify=GLOBAL_CONFIG['OS_CACERT'])
        except:  # pylint: disable=bare-except
            pass

    def _instances(self):
        """
        List of instances actually launched
        :return: dict()
        """
        response = cache.get('instances')
        if not response:
            response = dict()
            nova = nvclient.Client(NOVA_VERSION, session=self.session)
            data_instances = nova.servers.list(search_opts={'all_tenants': 'true'})
            for instance in data_instances:
                response[instance.id] = {
                    'user_id': instance.user_id,
                    'project_id': instance.tenant_id,
                    'id': instance.id,
                    'name': instance.name,
                    'created_at': parse_datetime(instance.created).date()
                }
            cache.set('instances', response, INSTANCES_CACHE_TIMEOUT)
        return response

    def _hypervisors(self):
        """
        List of hypervisors and their details
        :return: dict()
        """
        nova = nvclient.Client(NOVA_VERSION, session=self.session)
        hypervisors = nova.hypervisors.list()
        response = list()
        for hypervisor in hypervisors:
            response.append({
                'status': hypervisor.status,
                'state': hypervisor.state,
                'vcpus': hypervisor.vcpus,
                'vcpus_used': hypervisor.vcpus_used,
                'free_ram': hypervisor.free_disk_gb,
                'memory': hypervisor.memory_mb,
                'free_disk': hypervisor.free_disk_gb,
                'local_disk': hypervisor.local_gb
            })
        return response

    def _flavors(self):
        """
        List of flavors and their details
        """
{'fraction_non_alphanumeric': 0.08678130037614186, 'fraction_numerical': 0.00738850080601827, 'mean_word_length': 2.446759259259259, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5560860', 'n_tokens_mistral': 2075, 'n_tokens_neox': 2025, 'n_words': 486}