def populate_instance_snapshot_id_choices(self, request, context):
     images = utils.get_available_images(request, context.get("project_id"), self._images_cache)
     choices = [(image.id, image.name) for image in images if image.properties.get("image_type", "") == "snapshot"]
     if choices:
         choices.insert(0, ("", _("Select Instance Snapshot")))
     else:
         choices.insert(0, ("", _("No snapshots available.")))
     return choices
    def __init__(self, request, *args, **kwargs):
        super(RebuildInstanceForm, self).__init__(request, *args, **kwargs)
        instance_id = kwargs.get('initial', {}).get('instance_id')
        self.fields['instance_id'].initial = instance_id

        images = utils.get_available_images(request, request.user.tenant_id)
        choices = [(image.id, image.name) for image in images]
        if choices:
            choices.insert(0, ("", _("Select Image")))
        else:
            choices.insert(0, ("", _("No images available.")))
        self.fields['image'].choices = choices
 def populate_image_id_choices(self, request, context):
     choices = []
     images = utils.get_available_images(request, context.get("project_id"), self._images_cache)
     for image in images:
         image.bytes = image.size
         image.volume_size = functions.bytes_to_gigabytes(image.bytes)
         choices.append((image.id, image))
     if choices:
         choices.insert(0, ("", _("Select Image")))
     else:
         choices.insert(0, ("", _("No images available")))
     return choices
 def populate_instance_snapshot_id_choices(self, request, context):
     images = utils.get_available_images(request,
                                         context.get('project_id'),
                                         self._images_cache)
     choices = [(image.id, image.name)
                for image in images
                if image.properties.get("image_type", '') == "snapshot"]
     if choices:
         choices.insert(0, ("", _("Select Instance Snapshot")))
     else:
         choices.insert(0, ("", _("No snapshots available.")))
     return choices
 def populate_image_id_choices(self, request, context):
     choices = []
     images = utils.get_available_images(request,
                                         context.get('project_id'),
                                         self._images_cache)
     for image in images:
         image.bytes = image.size
         image.volume_size = functions.bytes_to_gigabytes(image.bytes)
         choices.append((image.id, image))
     if choices:
         choices.insert(0, ("", _("Select Image")))
     else:
         choices.insert(0, ("", _("No images available")))
     return choices
    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"]
    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']