summaryrefslogtreecommitdiff
path: root/tuskar_ui/infrastructure/resource_management/flavors/forms.py
blob: 6e4eb3a11a8e93c67efa82876a165c9a4cb18da7 (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
# vim: tabstop=4 shiftwidth=4 softtabstop=4

#
#    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 _

from horizon import exceptions
from horizon import forms
from horizon import messages

from tuskar_ui import api as tuskar


class CreateFlavor(forms.SelfHandlingForm):
    name = forms.RegexField(label=_("Name"),
                            max_length=25,
                            regex=r'^[\w\.\- ]+$',
                            error_messages={'invalid': _('Name may only '
                                'contain letters, numbers, underscores, '
                                'periods and hyphens.')})
    cpu = forms.IntegerField(label=_("VCPU"),
                             min_value=0,
                             initial=0)
    memory = forms.IntegerField(label=_("RAM (MB)"),
                             min_value=0,
                             initial=0)
    storage = forms.IntegerField(label=_("Root Disk (GB)"),
                                 min_value=0,
                                 initial=0)
    ephemeral_disk = forms.IntegerField(label=_("Ephemeral Disk (GB)"),
                                        min_value=0,
                                        initial=0)
    swap_disk = forms.IntegerField(label=_("Swap Disk (MB)"),
                                   min_value=0,
                                   initial=0)

    def clean(self):
        cleaned_data = super(CreateFlavor, self).clean()
        name = cleaned_data.get('name')
        flavor_id = self.initial.get('flavor_id', None)
        try:
            flavors = tuskar.FlavorTemplate.list(self.request)
        except:
            flavors = []
            msg = _('Unable to get flavor list')
            exceptions.check_message(["Connection", "refused"], msg)
            raise
        # Check if there is no flavor with the same name
        for flavor in flavors:
            if flavor.name == name and flavor.id != flavor_id:
                raise forms.ValidationError(
                    _('The name "%s" is already used by another flavor.')
                    % name
                )
        return cleaned_data

    def handle(self, request, data):
        try:
            tuskar.FlavorTemplate.create(
                request,
                data['name'],
                data['cpu'],
                data['memory'],
                data['storage'],
                data['ephemeral_disk'],
                data['swap_disk']
            )
            msg = _('Created flavor "%s".') % data['name']
            messages.success(request, msg)
            return True
        except:
            exceptions.handle(request, _("Unable to create flavor."))


class EditFlavor(CreateFlavor):

    def handle(self, request, data):
        try:
            tuskar.FlavorTemplate.update(
                self.request,
                self.initial['flavor_id'],
                data['name'],
                data['cpu'],
                data['memory'],
                data['storage'],
                data['ephemeral_disk'],
                data['swap_disk']
            )

            msg = _('Updated flavor "%s".') % data['name']
            messages.success(request, msg)
            return True
        except:
            exceptions.handle(request, _("Unable to update flavor."))