summaryrefslogtreecommitdiff
path: root/tuskar_ui/infrastructure/flavors/tables.py
blob: 68431a5bbe5556a233c6e05cc89e76abf3fb5506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- 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.
import django.shortcuts
from django.utils.translation import ugettext_lazy as _
import horizon.exceptions
import horizon.messages
import horizon.tables
from openstack_dashboard.dashboards.admin.flavors import (
    tables as flavor_tables)

from tuskar_ui import api
from tuskar_ui.infrastructure.flavors import utils


class CreateFlavor(flavor_tables.CreateFlavor):
    verbose_name = _(u"New Flavor")
    url = "horizon:infrastructure:flavors:create"


class CreateSuggestedFlavor(horizon.tables.Action):
    name = 'create'
    verbose_name = _(u"Create")
    verbose_name_plural = _(u"Create Suggested Flavors")
    method = 'POST'
    icon = 'plus'

    def create_flavor(self, request, node_id):
        node = api.node.Node.get(request, node_id)
        suggestion = utils.FlavorSuggestion.from_node(node)
        return suggestion.create_flavor(request)

    def handle(self, data_table, request, node_ids):
        for node_id in node_ids:
            try:
                self.create_flavor(request, node_id)
            except Exception:
                horizon.exceptions.handle(
                    request,
                    _(u"Unable to create flavor for node %r") % node_id,
                )
        return django.shortcuts.redirect(request.get_full_path())


class EditAndCreateSuggestedFlavor(CreateFlavor):
    name = 'edit_and_create'
    verbose_name = _(u"Edit before creating")
    icon = 'pencil'


class DeleteFlavor(flavor_tables.DeleteFlavor):

    def __init__(self, **kwargs):
        super(DeleteFlavor, self).__init__(**kwargs)
        # NOTE(dtantsur): setting class attributes doesn't work
        # probably due to metaclass magic in actions
        self.data_type_singular = _("Flavor")
        self.data_type_plural = _("Flavors")

    def allowed(self, request, datum=None):
        """Check that action is allowed on flavor

        This is overridden method from horizon.tables.BaseAction.

        :param datum: flavor we're operating on
        :type  datum: tuskar_ui.api.Flavor
        """
        if datum is not None:
            deployed_flavors = api.flavor.Flavor.list_deployed_ids(
                request, _error_default=None)
            if deployed_flavors is None or datum.id in deployed_flavors:
                return False
        return super(DeleteFlavor, self).allowed(request, datum)


class FlavorsTable(horizon.tables.DataTable):
    name = horizon.tables.Column('name',
                                 link="horizon:infrastructure:flavors:details")
    arch = horizon.tables.Column('cpu_arch', verbose_name=_('Architecture'))
    vcpus = horizon.tables.Column('vcpus', verbose_name=_('CPUs'))
    ram = horizon.tables.Column(flavor_tables.get_size,
                                verbose_name=_('Memory'),
                                attrs={'data-type': 'size'})
    disk = horizon.tables.Column(flavor_tables.get_disk_size,
                                 verbose_name=_('Disk'),
                                 attrs={'data-type': 'size'})

    class Meta(object):
        name = "flavors"
        verbose_name = _("Available")
        table_actions = (
            DeleteFlavor,
            flavor_tables.FlavorFilterAction,
        )
        row_actions = (
            DeleteFlavor,
        )
        template = "horizon/common/_enhanced_data_table.html"


class FlavorRolesTable(horizon.tables.DataTable):
    name = horizon.tables.Column('name', verbose_name=_('Role Name'))

    def __init__(self, request, *args, **kwargs):
        # TODO(dtantsur): support multiple overclouds
        plan = api.tuskar.Plan.get_the_plan(request)
        stack = api.heat.Stack.get_by_plan(request, plan)

        if stack is None:
            count = lambda role: _('Not Deployed')
        else:
            count = stack.resources_count

        self._columns['count'] = horizon.tables.Column(
            count,
            verbose_name=_("Instances Count")
        )
        super(FlavorRolesTable, self).__init__(request, *args, **kwargs)

    class Meta(object):
        name = "flavor_roles"
        verbose_name = _("Overcloud Roles")
        table_actions = ()
        row_actions = ()
        hidden_title = False
        template = "horizon/common/_enhanced_data_table.html"


class FlavorSuggestionsTable(horizon.tables.DataTable):
    name = horizon.tables.Column('name',)
    arch = horizon.tables.Column('cpu_arch', verbose_name=_('Architecture'))
    vcpus = horizon.tables.Column('vcpus', verbose_name=_('CPUs'))
    ram = horizon.tables.Column(flavor_tables.get_size,
                                verbose_name=_('Memory'),
                                attrs={'data-type': 'size'})
    disk = horizon.tables.Column(flavor_tables.get_disk_size,
                                 verbose_name=_('Disk'),
                                 attrs={'data-type': 'size'})

    class Meta(object):
        name = "suggested_flavors"
        verbose_name = _("Suggested")
        row_actions = (
            CreateSuggestedFlavor,
            EditAndCreateSuggestedFlavor,
        )
        template = "horizon/common/_enhanced_data_table.html"