Tzu-Mainn Chen d010384fd3 Rename node profile to flavor
Implements: blueprint rename-node-profiles-to-flavors
Change-Id: Ic530041615d04878ff9586038abaf9e8dc7f707a
2014-04-04 13:42:40 +02:00

141 lines
4.1 KiB
Python

# -*- coding: utf8 -*-
#
# 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.
from django.utils.translation import ugettext_lazy as _
import horizon.tabs
from tuskar_ui import api
from tuskar_ui.infrastructure.flavors import tables
def _get_unmatched_suggestions(request):
unmatched_suggestions = []
flavor_suggestions = [FlavorSuggestion.from_flavor(flavor)
for flavor in api.Flavor.list(request)]
for node in api.Node.list(request):
node_suggestion = FlavorSuggestion.from_node(node)
for flavor_suggestion in flavor_suggestions:
if flavor_suggestion == node_suggestion:
break
else:
unmatched_suggestions.append(node_suggestion)
return unmatched_suggestions
def get_flavor_suggestions(request):
return set(_get_unmatched_suggestions(request))
class FlavorsTab(horizon.tabs.TableTab):
name = _("Flavors")
slug = 'flavors'
table_classes = (tables.FlavorsTable,)
template_name = ("horizon/common/_detail_table.html")
preload = False
def get_flavors_data(self):
flavors = api.Flavor.list(self.request)
flavors.sort(key=lambda np: (np.vcpus, np.ram, np.disk))
return flavors
class FlavorSuggestion(object):
"""Describe node parameters in a way that is easy to compare."""
def __init__(self, vcpus=None, ram=None, disk=None, cpu_arch=None,
ram_bytes=None, disk_bytes=None, node_id=None):
self.vcpus = vcpus
self.ram_bytes = ram_bytes or ram * 1024 * 1024 or 0
self.disk_bytes = disk_bytes or disk * 1024 * 1024 * 1024 or 0
self.cpu_arch = cpu_arch
self.id = node_id
@classmethod
def from_node(cls, node):
return cls(
node_id=node.id,
vcpus=int(node.properties['cpu']),
ram_bytes=int(node.properties['ram']),
disk_bytes=int(node.properties['local_disk']),
# TODO(rdopieralski) Add architecture when available.
)
@classmethod
def from_flavor(cls, flavor):
return cls(
vcpus=flavor.vcpus,
ram_bytes=flavor.ram_bytes,
disk_bytes=flavor.disk_bytes,
# TODO(rdopieralski) Add architecture when available.
)
@property
def name(self):
return '%s-%s-%s-%s' % (
self.vcpus or '0',
self.cpu_arch or '',
self.ram or '0',
self.disk or '0',
)
@property
def ram(self):
return self.ram_bytes / 1024 / 1024
@property
def disk(self):
return self.disk_bytes / 1024 / 1024 / 1024
def __hash__(self):
return self.name.__hash__()
def __eq__(self, other):
return self.name == other.name
def __ne__(self, other):
return not self == other
def __repr__(self):
return (
'%s(vcpus=%r, ram_bytes=%r, disk_bytes=%r, '
'cpu_arch=%r, node_id=%r)' % (
self.__class__.__name__,
self.vcpus,
self.ram_bytes,
self.disk_bytes,
self.cpu_arch,
self.id,
)
)
class FlavorSuggestionsTab(horizon.tabs.TableTab):
name = _("Flavor Suggestions")
slug = 'flavor_suggestions'
table_classes = (tables.FlavorSuggestionsTable,)
template_name = ("horizon/common/_detail_table.html")
preload = False
def get_flavor_suggestions_data(self):
return list(get_flavor_suggestions(self.request))
class FlavorTabs(horizon.tabs.TabGroup):
slug = 'flavor_tabs'
tabs = (
FlavorsTab,
FlavorSuggestionsTab,
)
sticky = True