~ubuntu-branches/ubuntu/raring/horizon/raring

« back to all changes in this revision

Viewing changes to horizon/dashboards/nova/images_and_snapshots/images/views.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-24 14:33:20 UTC
  • mfrom: (1.1.14)
  • Revision ID: package-import@ubuntu.com-20120524143320-i7eswfq6ecxlvh5a
Tags: 2012.2~f1-0ubuntu1
* New usptream release. 
* Prepare for quantal:
  - debian/patches/fix-coverage-binary-name.patch: Refreshed.
* Temporarily pass the testsuite.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
"""
24
24
 
25
25
import logging
 
26
 
26
27
from django.core.urlresolvers import reverse
27
28
from django.utils.translation import ugettext_lazy as _
28
29
 
30
31
from horizon import exceptions
31
32
from horizon import forms
32
33
from horizon import tabs
33
 
from .forms import UpdateImageForm, LaunchForm
 
34
from .forms import UpdateImageForm
34
35
from .tabs import ImageDetailTabs
35
36
 
36
37
 
37
38
LOG = logging.getLogger(__name__)
38
39
 
39
40
 
40
 
class LaunchView(forms.ModalFormView):
41
 
    form_class = LaunchForm
42
 
    template_name = 'nova/images_and_snapshots/images/launch.html'
43
 
    context_object_name = 'image'
44
 
 
45
 
    def get_form_kwargs(self):
46
 
        kwargs = super(LaunchView, self).get_form_kwargs()
47
 
        kwargs['flavor_list'] = self.flavor_list()
48
 
        kwargs['keypair_list'] = self.keypair_list()
49
 
        kwargs['security_group_list'] = self.security_group_list()
50
 
        kwargs['volume_list'] = self.volume_list()
51
 
        return kwargs
52
 
 
53
 
    def get_object(self, *args, **kwargs):
54
 
        image_id = self.kwargs["image_id"]
55
 
        try:
56
 
            self.object = api.image_get_meta(self.request, image_id)
57
 
        except:
58
 
            msg = _('Unable to retrieve image "%s".') % image_id
59
 
            redirect = reverse('horizon:nova:images_and_snapshots:index')
60
 
            exceptions.handle(self.request, msg, redirect=redirect)
61
 
        return self.object
62
 
 
63
 
    def get_context_data(self, **kwargs):
64
 
        context = super(LaunchView, self).get_context_data(**kwargs)
65
 
        try:
66
 
            context['usages'] = api.tenant_quota_usages(self.request)
67
 
        except:
68
 
            exceptions.handle(self.request)
69
 
        return context
70
 
 
71
 
    def get_initial(self):
72
 
        return {'image_id': self.kwargs["image_id"],
73
 
                'tenant_id': self.request.user.tenant_id}
74
 
 
75
 
    def flavor_list(self):
76
 
        display = '%(name)s (%(vcpus)sVCPU / %(disk)sGB Disk / %(ram)sMB Ram )'
77
 
        try:
78
 
            flavors = api.flavor_list(self.request)
79
 
            flavor_list = [(flavor.id, display % {"name": flavor.name,
80
 
                                                  "vcpus": flavor.vcpus,
81
 
                                                  "disk": flavor.disk,
82
 
                                                  "ram": flavor.ram})
83
 
                                                for flavor in flavors]
84
 
        except:
85
 
            flavor_list = []
86
 
            exceptions.handle(self.request,
87
 
                              _('Unable to retrieve instance flavors.'))
88
 
        return sorted(flavor_list)
89
 
 
90
 
    def keypair_list(self):
91
 
        try:
92
 
            keypairs = api.keypair_list(self.request)
93
 
            keypair_list = [(kp.name, kp.name) for kp in keypairs]
94
 
        except:
95
 
            keypair_list = []
96
 
            exceptions.handle(self.request,
97
 
                              _('Unable to retrieve keypairs.'))
98
 
        return keypair_list
99
 
 
100
 
    def security_group_list(self):
101
 
        try:
102
 
            groups = api.security_group_list(self.request)
103
 
            security_group_list = [(sg.name, sg.name) for sg in groups]
104
 
        except:
105
 
            exceptions.handle(self.request,
106
 
                              _('Unable to retrieve list of security groups'))
107
 
            security_group_list = []
108
 
        return security_group_list
109
 
 
110
 
    def volume_list(self):
111
 
        volume_options = [("", _("Select Volume"))]
112
 
 
113
 
        def _get_volume_select_item(volume):
114
 
            if hasattr(volume, "volume_id"):
115
 
                vol_type = "snap"
116
 
                visible_label = _("Snapshot")
117
 
            else:
118
 
                vol_type = "vol"
119
 
                visible_label = _("Volume")
120
 
            return (("%s:%s" % (volume.id, vol_type)),
121
 
                    ("%s - %s GB (%s)" % (volume.display_name,
122
 
                                         volume.size,
123
 
                                         visible_label)))
124
 
 
125
 
        # First add volumes to the list
126
 
        try:
127
 
            volumes = [v for v in api.volume_list(self.request) \
128
 
                       if v.status == api.VOLUME_STATE_AVAILABLE]
129
 
            volume_options.extend(
130
 
                    [_get_volume_select_item(vol) for vol in volumes])
131
 
        except:
132
 
            exceptions.handle(self.request,
133
 
                              _('Unable to retrieve list of volumes'))
134
 
 
135
 
        # Next add volume snapshots to the list
136
 
        try:
137
 
            snapshots = api.volume_snapshot_list(self.request)
138
 
            snapshots = [s for s in snapshots \
139
 
                         if s.status == api.VOLUME_STATE_AVAILABLE]
140
 
            volume_options.extend(
141
 
                    [_get_volume_select_item(snap) for snap in snapshots])
142
 
        except:
143
 
            exceptions.handle(self.request,
144
 
                              _('Unable to retrieve list of volumes'))
145
 
 
146
 
        return volume_options
147
 
 
148
 
 
149
41
class UpdateView(forms.ModalFormView):
150
42
    form_class = UpdateImageForm
151
43
    template_name = 'nova/images_and_snapshots/images/update.html'
153
45
 
154
46
    def get_object(self, *args, **kwargs):
155
47
        try:
156
 
            self.object = api.image_get_meta(self.request, kwargs['image_id'])
 
48
            self.object = api.image_get(self.request, kwargs['image_id'])
157
49
        except:
158
 
            msg = _('Unable to retrieve image "%s".') % kwargs['image_id']
 
50
            msg = _('Unable to retrieve image.')
159
51
            redirect = reverse('horizon:nova:images_and_snapshots:index')
160
52
            exceptions.handle(self.request, msg, redirect=redirect)
161
53
        return self.object
162
54
 
163
55
    def get_initial(self):
164
 
        properties = self.object['properties']
 
56
        properties = self.object.properties
 
57
        # NOTE(gabriel): glanceclient currently treats "is_public" as a string
 
58
        # rather than a boolean. This should be fixed in the client.
 
59
        public = self.object.is_public == "True"
165
60
        return {'image_id': self.kwargs['image_id'],
166
 
                'name': self.object.get('name', ''),
 
61
                'name': self.object.name,
167
62
                'kernel': properties.get('kernel_id', ''),
168
63
                'ramdisk': properties.get('ramdisk_id', ''),
169
64
                'architecture': properties.get('architecture', ''),
170
 
                'container_format': self.object.get('container_format', ''),
171
 
                'disk_format': self.object.get('disk_format', ''), }
 
65
                'container_format': self.object.container_format,
 
66
                'disk_format': self.object.disk_format,
 
67
                'public': public}
172
68
 
173
69
 
174
70
class DetailView(tabs.TabView):