From fcce68a914f49938137785a4635d781b5a1741df Mon Sep 17 00:00:00 2001 From: MinhNLH2 Date: Sun, 19 May 2024 20:58:47 +0700 Subject: [PATCH] Prevent KeyError when getting value of optional key Closes-Bug: #2066115 Change-Id: Ica10eb749b48410583cb34bfa2fda0433a26c664 Signed-off-by: MinhNLH2 --- .../dashboards/admin/aggregates/workflows.py | 2 +- .../dashboards/identity/application_credentials/forms.py | 4 ++-- openstack_dashboard/dashboards/identity/users/forms.py | 6 ++---- .../project/instances/workflows/update_instance.py | 2 +- .../dashboards/project/networks/subnets/views.py | 5 ++--- 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/openstack_dashboard/dashboards/admin/aggregates/workflows.py b/openstack_dashboard/dashboards/admin/aggregates/workflows.py index 704fbc4ebb..7505199c5f 100644 --- a/openstack_dashboard/dashboards/admin/aggregates/workflows.py +++ b/openstack_dashboard/dashboards/admin/aggregates/workflows.py @@ -180,7 +180,7 @@ class CreateAggregateWorkflow(workflows.Workflow): api.nova.aggregate_create( request, name=context['name'], - availability_zone=context['availability_zone'] or None) + availability_zone=context.get('availability_zone') or None) except Exception: exceptions.handle(request, _('Unable to create host aggregate.')) return False diff --git a/openstack_dashboard/dashboards/identity/application_credentials/forms.py b/openstack_dashboard/dashboards/identity/application_credentials/forms.py index 62c6fdf4f6..28afd73221 100644 --- a/openstack_dashboard/dashboards/identity/application_credentials/forms.py +++ b/openstack_dashboard/dashboards/identity/application_credentials/forms.py @@ -109,8 +109,8 @@ class CreateApplicationCredentialForm(forms.SelfHandlingForm): new_app_cred = api.keystone.application_credential_create( request, name=data['name'], - description=data['description'] or None, - secret=data['secret'] or None, + description=data.get('description') or None, + secret=data.get('secret') or None, expires_at=expiration or None, roles=roles, access_rules=access_rules, diff --git a/openstack_dashboard/dashboards/identity/users/forms.py b/openstack_dashboard/dashboards/identity/users/forms.py index dbf3f0450e..fb87dee40c 100644 --- a/openstack_dashboard/dashboards/identity/users/forms.py +++ b/openstack_dashboard/dashboards/identity/users/forms.py @@ -162,8 +162,6 @@ class CreateUserForm(PasswordMixin, BaseUserForm, AddExtraColumnMixIn): try: LOG.info('Creating user with name "%s"', data['name']) desc = data["description"] - if "email" in data: - data['email'] = data['email'] or None # add extra information EXTRA_INFO = settings.USER_TABLE_EXTRA_INFO @@ -176,10 +174,10 @@ class CreateUserForm(PasswordMixin, BaseUserForm, AddExtraColumnMixIn): new_user = \ api.keystone.user_create(request, name=data['name'], - email=data['email'], + email=data.get('email') or None, description=desc or None, password=data['password'], - project=data['project'] or None, + project=data.get('project') or None, enabled=data['enabled'], domain=domain.id, **kwargs) diff --git a/openstack_dashboard/dashboards/project/instances/workflows/update_instance.py b/openstack_dashboard/dashboards/project/instances/workflows/update_instance.py index 4e06635411..e7afb58c84 100644 --- a/openstack_dashboard/dashboards/project/instances/workflows/update_instance.py +++ b/openstack_dashboard/dashboards/project/instances/workflows/update_instance.py @@ -87,7 +87,7 @@ class UpdateInstanceInfoAction(workflows.Action): api.nova.server_update(request, data['instance_id'], data['name'], - description=data.get('description')) + description=data.get('description') or None) except Exception: exceptions.handle(request, ignore=True) return False diff --git a/openstack_dashboard/dashboards/project/networks/subnets/views.py b/openstack_dashboard/dashboards/project/networks/subnets/views.py index 3ada8429e7..39921eae4d 100644 --- a/openstack_dashboard/dashboards/project/networks/subnets/views.py +++ b/openstack_dashboard/dashboards/project/networks/subnets/views.py @@ -81,9 +81,8 @@ class UpdateView(workflows.WorkflowView): for key in ('cidr', 'ip_version', 'enable_dhcp'): initial[key] = subnet[key] - initial['gateway_ip'] = subnet['gateway_ip'] or '' - initial['no_gateway'] = (subnet['gateway_ip'] is None) - + initial['gateway_ip'] = subnet.get('gateway_ip', '') + initial['no_gateway'] = not initial['gateway_ip'] if initial['ip_version'] == 6: initial['ipv6_modes'] = utils.get_ipv6_modes_menu_from_attrs( subnet['ipv6_ra_mode'], subnet['ipv6_address_mode'])