~ubuntu-branches/ubuntu/utopic/horizon/utopic-updates

« back to all changes in this revision

Viewing changes to openstack_dashboard/dashboards/admin/volumes/volume_types/views.py

  • Committer: Package Import Robot
  • Author(s): James Page, Chris Johnston, James Page
  • Date: 2014-10-03 17:54:18 UTC
  • mfrom: (0.4.1) (1.1.44) (70.1.2 utopic)
  • Revision ID: package-import@ubuntu.com-20141003175418-1jomx0azdvnl5fxz
Tags: 1:2014.2~rc1-0ubuntu1
[ Chris Johnston ]
* d/theme/css/ubuntu.css: Fix Ubuntu theme for Instances "more" dropdown
  (LP: #1308651).

[ James Page ]
* New upstream release candidate:
  - d/p/*: Refresh.
* d/watch: Use tarballs.openstack.org for upstream releases. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
2
# not use this file except in compliance with the License. You may obtain
 
3
# a copy of the License at
 
4
#
 
5
#   http://www.apache.org/licenses/LICENSE-2.0
 
6
#
 
7
# Unless required by applicable law or agreed to in writing, software
 
8
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
9
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
10
# License for the specific language governing permissions and limitations
 
11
# under the License.
 
12
 
 
13
"""
 
14
Admin views for managing volumes.
 
15
"""
 
16
 
 
17
from django.core.urlresolvers import reverse
 
18
from django.utils.translation import ugettext_lazy as _
 
19
 
 
20
from horizon import exceptions
 
21
from horizon import forms
 
22
from horizon.utils import memoized
 
23
 
 
24
from openstack_dashboard import api
 
25
from openstack_dashboard.dashboards.admin.volumes.volume_types \
 
26
    import forms as volume_type_forms
 
27
from openstack_dashboard.dashboards.admin.volumes.volumes \
 
28
    import forms as volumes_forms
 
29
 
 
30
 
 
31
class CreateVolumeTypeView(forms.ModalFormView):
 
32
    form_class = volumes_forms.CreateVolumeType
 
33
    template_name = 'admin/volumes/volume_types/create_volume_type.html'
 
34
    success_url = 'horizon:admin:volumes:volume_types_tab'
 
35
 
 
36
    def get_success_url(self):
 
37
        return reverse(self.success_url)
 
38
 
 
39
 
 
40
class CreateQosSpecView(forms.ModalFormView):
 
41
    form_class = volumes_forms.CreateQosSpec
 
42
    template_name = 'admin/volumes/volume_types/create_qos_spec.html'
 
43
    success_url = 'horizon:admin:volumes:volume_types_tab'
 
44
 
 
45
    def get_success_url(self):
 
46
        return reverse(self.success_url)
 
47
 
 
48
 
 
49
class EditQosSpecConsumerView(forms.ModalFormView):
 
50
    form_class = volume_type_forms.EditQosSpecConsumer
 
51
    template_name = 'admin/volumes/volume_types/edit_qos_spec_consumer.html'
 
52
    success_url = 'horizon:admin:volumes:volume_types_tab'
 
53
 
 
54
    def get_success_url(self):
 
55
        return reverse(self.success_url)
 
56
 
 
57
    def get_context_data(self, **kwargs):
 
58
        context = super(EditQosSpecConsumerView, self).\
 
59
            get_context_data(**kwargs)
 
60
        context['qos_spec_id'] = self.kwargs["qos_spec_id"]
 
61
        return context
 
62
 
 
63
    @memoized.memoized_method
 
64
    def get_object(self, *args, **kwargs):
 
65
        qos_spec_id = self.kwargs['qos_spec_id']
 
66
        try:
 
67
            self._object = api.cinder.qos_spec_get(self.request, qos_spec_id)
 
68
        except Exception:
 
69
            msg = _('Unable to retrieve QOS Spec details.')
 
70
            exceptions.handle(self.request, msg)
 
71
        return self._object
 
72
 
 
73
    def get_initial(self):
 
74
        qos_spec = self.get_object()
 
75
        qos_spec_id = self.kwargs['qos_spec_id']
 
76
 
 
77
        return {'qos_spec_id': qos_spec_id,
 
78
                'qos_spec': qos_spec}
 
79
 
 
80
 
 
81
class ManageQosSpecAssociationView(forms.ModalFormView):
 
82
    form_class = volume_type_forms.ManageQosSpecAssociation
 
83
    template_name = 'admin/volumes/volume_types/associate_qos_spec.html'
 
84
    success_url = 'horizon:admin:volumes:volume_types_tab'
 
85
 
 
86
    def get_success_url(self):
 
87
        return reverse(self.success_url)
 
88
 
 
89
    def get_context_data(self, **kwargs):
 
90
        context = super(ManageQosSpecAssociationView, self).\
 
91
            get_context_data(**kwargs)
 
92
        context['type_id'] = self.kwargs["type_id"]
 
93
        return context
 
94
 
 
95
    @memoized.memoized_method
 
96
    def get_object(self, *args, **kwargs):
 
97
        type_id = self.kwargs['type_id']
 
98
        try:
 
99
            self._object = api.cinder.volume_type_get(self.request, type_id)
 
100
        except Exception:
 
101
            msg = _('Unable to retrieve volume type details.')
 
102
            exceptions.handle(self.request, msg)
 
103
        return self._object
 
104
 
 
105
    @memoized.memoized_method
 
106
    def get_qos_specs(self, *args, **kwargs):
 
107
        try:
 
108
            return api.cinder.qos_spec_list(self.request)
 
109
        except Exception:
 
110
            exceptions.handle(self.request,
 
111
                              _('Unable to retrieve QOS Specs.'))
 
112
 
 
113
    def find_current_qos_spec_association(self, vol_type_id):
 
114
        qos_specs = self.get_qos_specs()
 
115
        if qos_specs:
 
116
            try:
 
117
                # find out which QOS Spec is currently associated with this
 
118
                # volume type, if any
 
119
                # NOTE - volume type can only have ONE QOS Spec association
 
120
                for qos_spec in qos_specs:
 
121
                    type_ids = \
 
122
                        api.cinder.qos_spec_get_associations(self.request,
 
123
                                                              qos_spec.id)
 
124
                    for vtype in type_ids:
 
125
                        if vtype.id == vol_type_id:
 
126
                            return qos_spec
 
127
 
 
128
            except Exception:
 
129
                exceptions.handle(self.request,
 
130
                        _('Unable to retrieve QOS Spec association.'))
 
131
 
 
132
        return None
 
133
 
 
134
    def get_initial(self):
 
135
        volume_type = self.get_object()
 
136
        vol_type_id = self.kwargs['type_id']
 
137
 
 
138
        cur_qos_spec_id = None
 
139
        cur_qos_spec_name = None
 
140
 
 
141
        qos_spec = self.find_current_qos_spec_association(vol_type_id)
 
142
        if qos_spec:
 
143
            cur_qos_spec_id = qos_spec.id
 
144
            cur_qos_spec_name = qos_spec.name
 
145
 
 
146
        return {'type_id': vol_type_id,
 
147
                'name': getattr(volume_type, 'name', None),
 
148
                'cur_qos_spec_id': cur_qos_spec_id,
 
149
                'cur_qos_spec_name': cur_qos_spec_name,
 
150
                'qos_specs': self.get_qos_specs()}