Ejemplo n.º 1
0
 def handle(self, request, data):
     instance_choices = dict(self.fields["instance"].choices)
     instance_name = instance_choices.get(data["instance"], _("Unknown instance (None)"))
     # The name of the instance in the choices list has the ID appended to
     # it, so let's slice that off...
     instance_name = instance_name.rsplit(" (")[0]
     try:
         attach = api.nova.instance_volume_attach(
             request, data["volume_id"], data["instance"], data.get("device", "")
         )
         volume = cinder.volume_get(request, data["volume_id"])
         if not volume.display_name:
             volume_name = volume.id
         else:
             volume_name = volume.display_name
         message = _("Attaching volume %(vol)s to instance " "%(inst)s on %(dev)s.") % {
             "vol": volume_name,
             "inst": instance_name,
             "dev": attach.device,
         }
         messages.info(request, message)
         return True
     except Exception:
         redirect = reverse("horizon:hpcloud:volumes:index")
         exceptions.handle(request, _("Unable to attach volume."), redirect=redirect)
Ejemplo n.º 2
0
 def handle(self, request, data):
     instance_choices = dict(self.fields['instance'].choices)
     instance_name = instance_choices.get(data['instance'],
                                          _("Unknown instance (None)"))
     # The name of the instance in the choices list has the ID appended to
     # it, so let's slice that off...
     instance_name = instance_name.rsplit(" (")[0]
     try:
         attach = api.nova.instance_volume_attach(request,
                                                  data['volume_id'],
                                                  data['instance'],
                                                  data.get('device', ''))
         volume = cinder.volume_get(request, data['volume_id'])
         if not volume.display_name:
             volume_name = volume.id
         else:
             volume_name = volume.display_name
         message = _('Attaching volume %(vol)s to instance '
                     '%(inst)s on %(dev)s.') % {
                         "vol": volume_name,
                         "inst": instance_name,
                         "dev": attach.device
                     }
         messages.info(request, message)
         return True
     except Exception:
         redirect = reverse("horizon:hpcloud:volumes:index")
         exceptions.handle(request,
                           _('Unable to attach volume.'),
                           redirect=redirect)
Ejemplo n.º 3
0
 def get_object(self):
     if not hasattr(self, "_object"):
         volume_id = self.kwargs['volume_id']
         try:
             self._object = cinder.volume_get(self.request, volume_id)
         except Exception:
             self._object = None
             exceptions.handle(self.request,
                               _('Unable to retrieve volume information.'))
     return self._object
Ejemplo n.º 4
0
 def get_object(self):
     if not hasattr(self, "_object"):
         volume_id = self.kwargs['volume_id']
         try:
             self._object = cinder.volume_get(self.request, volume_id)
         except Exception:
             self._object = None
             exceptions.handle(self.request,
                               _('Unable to retrieve volume information.'))
     return self._object
Ejemplo n.º 5
0
 def get_context_data(self, request):
     snapshot_id = self.tab_group.kwargs['snapshot_id']
     try:
         snapshot = cinder.volume_snapshot_get(request, snapshot_id)
         volume = cinder.volume_get(request, snapshot.volume_id)
         volume.display_name = None
     except Exception:
         redirect = reverse('horizon:hpcloud:images_and_snapshots:index')
         exceptions.handle(self.request,
                           _('Unable to retrieve snapshot details.'),
                           redirect=redirect)
     return {'snapshot': snapshot, 'volume': volume}
Ejemplo n.º 6
0
 def get_context_data(self, request):
     volume_id = self.tab_group.kwargs['volume_id']
     try:
         volume = cinder.volume_get(request, volume_id)
         for att in volume.attachments:
             att['instance'] = nova.server_get(request, att['server_id'])
     except Exception:
         redirect = reverse('horizon:hpcloud:volumes:index')
         exceptions.handle(self.request,
                           _('Unable to retrieve volume details.'),
                           redirect=redirect)
     return {'volume': volume}
Ejemplo n.º 7
0
 def get_context_data(self, request):
     snapshot_id = self.tab_group.kwargs['snapshot_id']
     try:
         snapshot = cinder.volume_snapshot_get(request, snapshot_id)
         volume = cinder.volume_get(request, snapshot.volume_id)
         volume.display_name = None
     except Exception:
         redirect = reverse('horizon:hpcloud:images_and_snapshots:index')
         exceptions.handle(self.request,
                           _('Unable to retrieve snapshot details.'),
                           redirect=redirect)
     return {'snapshot': snapshot,
             'volume': volume}
Ejemplo n.º 8
0
    def __init__(self, request, *args, **kwargs):
        super(CreateForm, self).__init__(request, *args, **kwargs)
        volume_types = cinder.volume_type_list(request)
        self.fields["type"].choices = [("", "")] + [(type.name, type.name) for type in volume_types]

        if "snapshot_id" in request.GET:
            try:
                snapshot = self.get_snapshot(request, request.GET["snapshot_id"])
                self.fields["name"].initial = snapshot.display_name
                self.fields["size"].initial = snapshot.size
                self.fields["snapshot_source"].choices = ((snapshot.id, snapshot),)
                try:
                    # Set the volume type from the original volume
                    orig_volume = cinder.volume_get(request, snapshot.volume_id)
                    self.fields["type"].initial = orig_volume.volume_type
                except Exception:
                    pass
                self.fields["size"].help_text = _(
                    "Volume size must be equal " "to or greater than the snapshot size (%sGB)" % snapshot.size
                )
                del self.fields["image_source"]
                del self.fields["volume_source_type"]
            except Exception:
                exceptions.handle(request, _("Unable to load the specified snapshot."))
        elif "image_id" in request.GET:
            try:
                image = self.get_image(request, request.GET["image_id"])
                image.bytes = image.size
                self.fields["name"].initial = image.name
                self.fields["size"].initial = functions.bytes_to_gigabytes(image.size)
                self.fields["image_source"].choices = ((image.id, image),)
                self.fields["size"].help_text = _(
                    "Volume size must be equal " "to or greater than the image size (%s)" % filesizeformat(image.size)
                )
                del self.fields["snapshot_source"]
                del self.fields["volume_source_type"]
            except Exception:
                msg = _("Unable to load the specified image. %s")
                exceptions.handle(request, msg % request.GET["image_id"])
        else:
            source_type_choices = []

            try:
                snapshots = cinder.volume_snapshot_list(request)
                if snapshots:
                    source_type_choices.append(("snapshot_source", _("Snapshot")))
                    choices = [("", _("Choose a snapshot"))] + [(s.id, s) for s in snapshots]
                    self.fields["snapshot_source"].choices = choices
                else:
                    del self.fields["snapshot_source"]
            except Exception:
                exceptions.handle(request, _("Unable to retrieve " "volume snapshots."))

            images = utils.get_available_images(request, request.user.tenant_id)
            if images:
                source_type_choices.append(("image_source", _("Image")))
                choices = [("", _("Choose an image"))]
                for image in images:
                    image.bytes = image.size
                    image.size = functions.bytes_to_gigabytes(image.bytes)
                    choices.append((image.id, image))
                self.fields["image_source"].choices = choices
            else:
                del self.fields["image_source"]

            if source_type_choices:
                choices = [("no_source_type", _("No source, empty volume."))] + source_type_choices
                self.fields["volume_source_type"].choices = choices
            else:
                del self.fields["volume_source_type"]
Ejemplo n.º 9
0
    def __init__(self, request, *args, **kwargs):
        super(CreateForm, self).__init__(request, *args, **kwargs)
        volume_types = cinder.volume_type_list(request)
        self.fields['type'].choices = [("", "")] + \
                                      [(type.name, type.name)
                                       for type in volume_types]

        if ("snapshot_id" in request.GET):
            try:
                snapshot = self.get_snapshot(request,
                                             request.GET["snapshot_id"])
                self.fields['name'].initial = snapshot.display_name
                self.fields['size'].initial = snapshot.size
                self.fields['snapshot_source'].choices = ((snapshot.id,
                                                           snapshot), )
                try:
                    # Set the volume type from the original volume
                    orig_volume = cinder.volume_get(request,
                                                    snapshot.volume_id)
                    self.fields['type'].initial = orig_volume.volume_type
                except Exception:
                    pass
                self.fields['size'].help_text = _(
                    'Volume size must be equal '
                    'to or greater than the snapshot size (%sGB)' %
                    snapshot.size)
                del self.fields['image_source']
                del self.fields['volume_source_type']
            except Exception:
                exceptions.handle(request,
                                  _('Unable to load the specified snapshot.'))
        elif ('image_id' in request.GET):
            try:
                image = self.get_image(request, request.GET["image_id"])
                image.bytes = image.size
                self.fields['name'].initial = image.name
                self.fields['size'].initial = functions.bytes_to_gigabytes(
                    image.size)
                self.fields['image_source'].choices = ((image.id, image), )
                self.fields['size'].help_text = _(
                    'Volume size must be equal '
                    'to or greater than the image size (%s)' %
                    filesizeformat(image.size))
                del self.fields['snapshot_source']
                del self.fields['volume_source_type']
            except Exception:
                msg = _('Unable to load the specified image. %s')
                exceptions.handle(request, msg % request.GET['image_id'])
        else:
            source_type_choices = []

            try:
                snapshots = cinder.volume_snapshot_list(request)
                if snapshots:
                    source_type_choices.append(
                        ("snapshot_source", _("Snapshot")))
                    choices = [('', _("Choose a snapshot"))] + \
                              [(s.id, s) for s in snapshots]
                    self.fields['snapshot_source'].choices = choices
                else:
                    del self.fields['snapshot_source']
            except Exception:
                exceptions.handle(request,
                                  _("Unable to retrieve "
                                    "volume snapshots."))

            images = utils.get_available_images(request,
                                                request.user.tenant_id)
            if images:
                source_type_choices.append(("image_source", _("Image")))
                choices = [('', _("Choose an image"))]
                for image in images:
                    image.bytes = image.size
                    image.size = functions.bytes_to_gigabytes(image.bytes)
                    choices.append((image.id, image))
                self.fields['image_source'].choices = choices
            else:
                del self.fields['image_source']

            if source_type_choices:
                choices = (
                    [('no_source_type', _("No source, empty volume."))] +
                    source_type_choices)
                self.fields['volume_source_type'].choices = choices
            else:
                del self.fields['volume_source_type']
Ejemplo n.º 10
0
 def get_data(self, request, volume_id):
     volume = cinder.volume_get(request, volume_id)
     if not volume.display_name:
         volume.display_name = volume_id
     return volume
Ejemplo n.º 11
0
 def get_data(self, request, snapshot_id):
     snapshot = cinder.volume_snapshot_get(request, snapshot_id)
     snapshot._volume = cinder.volume_get(request, snapshot.volume_id)
     return snapshot
Ejemplo n.º 12
0
 def get_data(self, request, volume_id):
     volume = cinder.volume_get(request, volume_id)
     if not volume.display_name:
         volume.display_name = volume_id
     return volume
Ejemplo n.º 13
0
 def get_data(self, request, snapshot_id):
     snapshot = cinder.volume_snapshot_get(request, snapshot_id)
     snapshot._volume = cinder.volume_get(request, snapshot.volume_id)
     return snapshot