Support json requests for subcloud backup API

Add support for requests using application/json content-type on
subcloud-backup.

This is needed to support the subcloud-backup delete operation, as well
as a change on backup create CLI [1] to fix an issue seen in Debian.

Test Plan:

PASS:
- Ensure tox tests are passing
- Issue backup request without overrides file using json content
- Issue backup request with overrides file using multipart content

[1] Review for the Debian fix on the CLI side
https://review.opendev.org/c/starlingx/distcloud-client/+/858441

Story: 2010116
Task: 45696

Signed-off-by: Gabriel Silva Trevisan <gabriel.silvatrevisan@windriver.com>
Change-Id: Ieafd1a213c9c505aa35b07cf8490abedf2c56a27
This commit is contained in:
Gabriel Silva Trevisan
2022-09-19 20:02:27 -03:00
committed by BoYuan Chang
parent c74b1be2cb
commit be55504cf3
2 changed files with 36 additions and 0 deletions

View File

@@ -3,6 +3,8 @@
#
# SPDX-License-Identifier: Apache-2.0
#
import json
from requests_toolbelt.multipart import decoder
import base64
@@ -56,6 +58,19 @@ class SubcloudBackupController(object):
@staticmethod
def _get_payload(request, expected_params):
content_type = pecan.request.headers.get('Content-Type')
if 'multipart' in content_type:
return SubcloudBackupController._get_multipart_payload(
request, expected_params)
elif 'json' in content_type:
return SubcloudBackupController._get_json_payload(
request, expected_params)
else:
pecan.abort(400, _("Unexpected request content type"))
@staticmethod
def _get_multipart_payload(request, expected_params):
payload = dict()
multipart_data = \
@@ -77,6 +92,23 @@ class SubcloudBackupController(object):
return payload
@staticmethod
def _get_json_payload(request, expected_params):
try:
payload = json.loads(request.body)
except Exception:
error_msg = 'Request body is malformed.'
LOG.exception(error_msg)
pecan.abort(400, _(error_msg))
return
if not isinstance(payload, dict):
pecan.abort(400, _('Invalid request body format'))
if not set(payload.keys()).issubset(expected_params.keys()):
LOG.info(payload.keys())
pecan.abort(400, _("Unexpected parameter received"))
return payload
@staticmethod
def read_yaml_param(param, request):
invalid_yaml_msg = "Invalid format received on yaml parameter %s"

View File

@@ -39,6 +39,10 @@ from dcorch.objects import subcloud_resource
from keystoneclient import client as keystoneclient
# Some of the variables defined in this file cannot be recognized by the
# current pylint check, thus will raise error which will fail tox check
# The pylint check is temporarily skipped on this file
# pylint: skip-file
LOG = logging.getLogger(__name__)
# sync request states, should be in SyncRequest class