# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2012 Nebula, Inc.
# Copyright 2012 OpenStack LLC
#
#    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.

"""
Views for Instances and Volumes.
"""
import logging

from django.utils.translation import ugettext_lazy as _
from django.utils.datastructures import SortedDict

from horizon import api
from horizon import exceptions
from horizon import tables
from .instances.tables import InstancesTable
from .volumes.tables import VolumesTable


LOG = logging.getLogger(__name__)


class IndexView(tables.MultiTableView):
    table_classes = (InstancesTable, VolumesTable)
    template_name = 'nova/instances_and_volumes/index.html'

    def get_instances_data(self):
        if not hasattr(self, "_instances"):
            # Gather our instances
            try:
                instances = self._get_instances()
            except:
                instances = []
                exceptions.handle(self.request,
                                  _('Unable to retrieve instances.'))
            # Gather our flavors and correlate our instances to them
            if instances:
                try:
                    flavors = api.flavor_list(self.request)
                except:
                    # If fails to retrieve flavor list, creates an empty list.
                    flavors = []

                full_flavors = SortedDict([(str(flavor.id), flavor)
                                            for flavor in flavors])
                # Loop through instances to get flavor info.
                for instance in instances:
                    try:
                        flavor_id = instance.flavor["id"]
                        if flavor_id in full_flavors:
                            instance.full_flavor = full_flavors[flavor_id]
                        else:
                            # If the flavor_id is not in full_flavors list,
                            # gets it via nova api.
                            instance.full_flavor = api.flavor_get(
                                                    self.request, flavor_id)
                    except:
                        msg = _('Unable to retrieve instance \
                                size information.')
                        exceptions.handle(self.request, msg)
            self._instances = instances
        return self._instances

    def get_volumes_data(self):
        # Gather our volumes
        try:
            volumes = api.volume_list(self.request)
            instances = SortedDict([(inst.id, inst) for inst in
                                    self._get_instances()])
            for volume in volumes:
                # It is possible to create a volume with no name through the
                # EC2 API, use the ID in those cases.
                if not volume.display_name:
                    volume.display_name = volume.id

                description = getattr(volume, 'display_description', '')

                for att in volume.attachments:
                    server_id = att.get('server_id', None)
                    att['instance'] = instances.get(server_id, None)
        except:
            volumes = []
            exceptions.handle(self.request,
                              _('Unable to retrieve volume list.'))
        return volumes

    def _get_instances(self):
        if not hasattr(self, "_instances_list"):
            self._instances_list = api.server_list(self.request)
        return self._instances_list
