"Fossies" - the Fresh Open Source Software Archive

Member "horizon-20.1.2/openstack_dashboard/dashboards/project/instances/workflows/resize_instance.py" (29 Apr 2022, 4445 Bytes) of package /linux/misc/openstack/horizon-20.1.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright 2013 CentRin Data, Inc.
    2 #
    3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 #    not use this file except in compliance with the License. You may obtain
    5 #    a copy of the License at
    6 #
    7 #         http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 #    Unless required by applicable law or agreed to in writing, software
   10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 #    License for the specific language governing permissions and limitations
   13 #    under the License.
   14 
   15 
   16 import json
   17 
   18 from django.utils import encoding
   19 from django.utils.translation import ugettext_lazy as _
   20 from django.views.decorators.debug import sensitive_variables
   21 
   22 from horizon import exceptions
   23 from horizon import forms
   24 from horizon import workflows
   25 from openstack_dashboard import api
   26 from openstack_dashboard.dashboards.project.instances \
   27     import utils as instance_utils
   28 from openstack_dashboard.dashboards.project.instances.workflows \
   29     import create_instance
   30 
   31 
   32 class SetFlavorChoiceAction(workflows.Action):
   33     old_flavor_id = forms.CharField(required=False, widget=forms.HiddenInput())
   34     old_flavor_name = forms.CharField(
   35         label=_("Old Flavor"),
   36         widget=forms.TextInput(attrs={'readonly': 'readonly'}),
   37         required=False,
   38     )
   39     flavor = forms.ThemableChoiceField(
   40         label=_("New Flavor"),
   41         help_text=_("Choose the flavor to launch."))
   42 
   43     class Meta(object):
   44         name = _("Flavor Choice")
   45         slug = 'flavor_choice'
   46         help_text_template = ("project/instances/"
   47                               "_flavors_and_quotas.html")
   48 
   49     def populate_flavor_choices(self, request, context):
   50         old_flavor_name = context.get('old_flavor_name')
   51         flavors = context.get('flavors').values()
   52 
   53         # Remove current flavor from the list of flavor choices
   54         flavors = [flavor for flavor in flavors
   55                    if flavor.name != old_flavor_name]
   56 
   57         if flavors:
   58             if len(flavors) > 1:
   59                 flavors = instance_utils.sort_flavor_list(request, flavors)
   60             else:
   61                 flavor = flavors[0]
   62                 flavors = [(flavor.id, flavor.name)]
   63             flavors.insert(0, ("", _("Select a New Flavor")))
   64         else:
   65             flavors.insert(0, ("", _("No flavors available")))
   66         return flavors
   67 
   68     def get_help_text(self, extra_context=None):
   69         extra = {} if extra_context is None else dict(extra_context)
   70         try:
   71             extra['usages'] = api.nova.tenant_absolute_limits(self.request,
   72                                                               reserved=True)
   73             extra['usages_json'] = json.dumps(extra['usages'])
   74             flavors = json.dumps([f._info for f in
   75                                   instance_utils.flavor_list(self.request)])
   76             extra['flavors'] = flavors
   77             extra['resize_instance'] = True
   78         except Exception:
   79             exceptions.handle(self.request,
   80                               _("Unable to retrieve quota information."))
   81         return super().get_help_text(extra)
   82 
   83 
   84 class SetFlavorChoice(workflows.Step):
   85     action_class = SetFlavorChoiceAction
   86     depends_on = ("instance_id", "name")
   87     contributes = ("old_flavor_id", "old_flavor_name", "flavors", "flavor")
   88 
   89 
   90 class ResizeInstance(workflows.Workflow):
   91     slug = "resize_instance"
   92     name = _("Resize Instance")
   93     finalize_button_name = _("Resize")
   94     success_message = _('Request for resizing of instance "%s" '
   95                         'has been submitted.')
   96     failure_message = _('Unable to resize instance "%s".')
   97     success_url = "horizon:project:instances:index"
   98     default_steps = (SetFlavorChoice, create_instance.SetAdvanced)
   99 
  100     def format_status_message(self, message):
  101         if "%s" in message:
  102             return message % self.context.get('name', 'unknown instance')
  103         return message
  104 
  105     @sensitive_variables('context')
  106     def handle(self, request, context):
  107         instance_id = context.get('instance_id', None)
  108         flavor = context.get('flavor', None)
  109         disk_config = context.get('disk_config', None)
  110         try:
  111             api.nova.server_resize(request, instance_id, flavor, disk_config)
  112             return True
  113         except Exception as e:
  114             self.failure_message = encoding.force_text(e)
  115             return False