<commit_before># Copyright 2015,  A10 Networks
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import logging
import pprint

import glanceclient.client as glance_client
import keystoneclient.auth.identity.generic as auth_plugin
import keystoneclient.client as keystone_client
import keystoneclient.session as keystone_session
import neutronclient.neutron.client as neutron_client
import novaclient.client as nova_client

import a10_neutron_lbaas.a10_exceptions as a10_ex


pp = pprint.PrettyPrinter(indent=4)


LOG = logging.getLogger(__name__)

# TODO(mdurrant) - These may need to go into a configuration file.
GLANCE_VERSION = 1
KEYSTONE_VERSION = "2.0"
NOVA_VERSION = "2.1"
NEUTRON_VERSION = "2.0"
OS_INTERFACE_URL = "public"

_default_server = {
    "id": None,
    "name": None,
    "image": None,
    "flavor": None,
    "meta": {},
    "files": {},
    "min_count": 1,  # optional extension
    "max_count": 1,  # optional extension
    "security_groups": [],
    "userdata": None,
    "key_name": None,  # optional extension
    "availability_zone": None,
    "block_device_mapping": None,  # optional extension
    "block_device_mapping_v2": None,  # optional extension
    "networks": None,  # optional extension
    "scheduler_hints": {},  # optional extension
    "config_drive": False,  # optional extension
    "disk_config": "AUTO",   # AUTO or MANUAL # optional extension
    "admin_pass": None  # optional extension
}

MISSING_ERR_FORMAT = "{0} with name or id {1} could not be found"


class InstanceManager(object):
    def __init__(self, context=None, request=None, nova_api=None,
                 glance_api=None, neutron_api=None, keystone_api=None):
        self.context = context
        self.user = request.user
        self.tenant_id = request.user.token.project["id"]
        self.endpoints = self._get_services_from_token(self.user.token)
        (self.auth_url, self.auth_token) = self._get_auth_from_token(self.user.token)
        self._validate_auth()
        session = keystone_session.Session(auth=self.token)
        self._keystone_api = (keystone_api or keystone_client.Client(KEYSTONE_VERSION,
                              session=session, auth_url=self.auth_url))

        self._nova_api = nova_api or nova_client.Client(NOVA_VERSION, session=session,
                                                        auth_url=self.auth_url)

        self._neutron_api = neutron_api or neutron_client.Client(NEUTRON_VERSION,
                                                                 session=session,
                                                                 auth_url=self.auth_url)

        self._glance_api = glance_api or glance_client.Client(GLANCE_VERSION, session=session)

    def _validate_auth(self):
        pass
        # if self.auth_url is None or self.auth_token is None:
        #     raise AttributeError("Auth URL and Token must be provided for authentication.")

    def _get_services_from_token(self, token):
        # This changes between keystone versions.
        res = {}
        if not token.serviceCatalog or len(token.serviceCatalog) < 1:
            raise AttributeError("FATAL: Service catalog not populated.")
        for x in token.serviceCatalog:
            # This is always returned as an array.
            endpoints = x.get("endpoints", [{}])
            endpoint_filter = lambda x: x.get("interface") == OS_INTERFACE_URL
            endpoint = filter(endpoint_filter, endpoints)
            if len(endpoint) > 0:
                res[x["type"]] = endpoint[0].get("url")
        return res

    def _get_auth_from_token(self, token):
        auth_url = None
        auth_token = token.unscoped_token

        if self.endpoints:
            auth_url = self.endpoints.get("identity", None)
        else:
            LOG.exception("Identity Service discovery failed.")

        self.token = auth_plugin.Token(token=auth_token,
                                       project_name=self.user.token.project["name"],
                                       auth_url=auth_url)

        return (auth_url, auth_token)

    def _build_server(self, instance):
        retval = {}
        for k in _default_server:
            retval[k] = instance.get(k, _default_server[k])

        return retval

    def list_instances(self, detailed=True, search_opts=None,<commit_msg>Hopefully fixes endpoint issue that works on my box not Cedrics doh<commit_after># Copyright 2015,  A10 Networks
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import logging
import pprint

import glanceclient.client as glance_client
import keystoneclient.auth.identity.generic as auth_plugin
import keystoneclient.client as keystone_client
import keystoneclient.session as keystone_session
import neutronclient.neutron.client as neutron_client
import novaclient.client as nova_client

import a10_neutron_lbaas.a10_exceptions as a10_ex


pp = pprint.PrettyPrinter(indent=4)


LOG = logging.getLogger(__name__)

# TODO(mdurrant) - These may need to go into a configuration file.
GLANCE_VERSION = 1
KEYSTONE_VERSION = "2.0"
NOVA_VERSION = "2.1"
NEUTRON_VERSION = "2.0"
OS_INTERFACE_URLS = ["public", "publicURL"]

_default_server = {
    "id": None,
    "name": None,
    "image": None,
    "flavor": None,
    "meta": {},
    "files": {},
    "min_count": 1,  # optional extension
    "max_count": 1,  # optional extension
    "security_groups": [],
    "userdata": None,
    "key_name": None,  # optional extension
    "availability_zone": None,
    "block_device_mapping": None,  # optional extension
    "block_device_mapping_v2": None,  # optional extension
    "networks": None,  # optional extension
    "scheduler_hints": {},  # optional extension
    "config_drive": False,  # optional extension
    "disk_config": "AUTO",   # AUTO or MANUAL # optional extension
    "admin_pass": None  # optional extension
}

MISSING_ERR_FORMAT = "{0} with name or id {1} could not be found"


class InstanceManager(object):
    def __init__(self, context=None, request=None, nova_api=None,
                 glance_api=None, neutron_api=None, keystone_api=None):
        self.context = context
        self.user = request.user
        self.tenant_id = request.user.token.project["id"]
        self.endpoints = self._get_services_from_token(self.user.token)
        (self.auth_url, self.auth_token) = self._get_auth_from_token(self.user.token)
        self._validate_auth()
        session = keystone_session.Session(auth=self.token)
        self._keystone_api = (keystone_api or keystone_client.Client(KEYSTONE_VERSION,
                              session=session, auth_url=self.auth_url))

        self._nova_api = nova_api or nova_client.Client(NOVA_VERSION, session=session,
                                                        auth_url=self.auth_url)

        self._neutron_api = neutron_api or neutron_client.Client(NEUTRON_VERSION,
                                                                 session=session,
                                                                 auth_url=self.auth_url)

        self._glance_api = glance_api or glance_client.Client(GLANCE_VERSION, session=session)

    def _validate_auth(self):
        pass
        # if self.auth_url is None or self.auth_token is None:
        #     raise AttributeError("Auth URL and Token must be provided for authentication.")

    def _get_services_from_token(self, token):
        # This changes between keystone versions.
        res = {}
        if not token.serviceCatalog or len(token.serviceCatalog) < 1:
            raise AttributeError("FATAL: Service catalog not populated.")
        for x in token.serviceCatalog:
            # This is always returned as an array.
            endpoints = x.get("endpoints", [{}])
            endpoint_filter = lambda x: x.get("interface") in OS_INTERFACE_URLS
            endpoint = filter(endpoint_filter, endpoints)
            if len(endpoint) > 0:
                res[x["type"]] = endpoint[0].get("url")
        return res

    def _get_auth_from_token(self, token):
        auth_url = None
        auth_token = token.unscoped_token

        if self.endpoints:
            auth_url = self.endpoints.get("identity", None)
        else:
            LOG.exception("Identity Service discovery failed.")

        self.token = auth_plugin.Token(token=auth_token,
                                       project_name=self.user.token.project["name"],
                                       auth_url=auth_url)

        return (auth_url, auth_token)

    def _build_server(self, instance):
        retval = {}
        for k in _default_server:
            retval[k] = instance.get(k, _default_server[k])

        return retval

    def list_instances(self, detailed=True, search_opts=None,
{'fraction_non_alphanumeric': 0.08855070983556328, 'fraction_numerical': 0.006536615258911245, 'mean_word_length': 2.6092886103943975, 'pattern_counts': {'":': 38, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3815225', 'n_tokens_mistral': 2832, 'n_tokens_neox': 2737, 'n_words': 744}