Use multipart headers on subcloud-backup API calls

This change replaces JSON content types for subcloud-backup API calls
with multipart content. This aims to keep the uniformity with all
other API calls.

Test Plan:
1. PASS - Validate that the API is called with multipart
content types and all parameters are read by the API.
2. PASS - Validate that the files uploaded for restore_values and
backup_values are read by the API.
3. PASS - Repeat steps 1 and 2 for backup create, delete and restore
operation.

Story: 2010116
Task: 47020

Signed-off-by: Andre Carneiro <Andre.DexheimerCarneiro@windriver.com>
Change-Id: Iba4ceab4a541d308ee4f49c1387298828cf02694
This commit is contained in:
Andre Carneiro 2022-11-07 18:01:15 -03:00
parent 40459e1dc9
commit ffbbaaa5cb
4 changed files with 30 additions and 38 deletions

View File

@ -7,6 +7,8 @@
import json
from requests_toolbelt import MultipartEncoder
from dcmanagerclient.api import base
from dcmanagerclient.api.base import get_json
@ -18,13 +20,17 @@ class subcloud_backup_manager(base.ResourceManager):
def json_to_resource(self, json_object):
return self.resource_class.from_payload(self, json_object)
def subcloud_backup_create(self, url, body, data):
if body:
data.update(body)
enc = json.dumps(data)
headers = {'Content-Type': 'application/json'}
def subcloud_backup_create(self, url, files, data):
fields = dict()
if files:
for k, v in files.items():
fields.update({k: (v, open(v, 'rb'),)})
fields.update(data)
enc = MultipartEncoder(fields=fields)
headers = {'content-type': enc.content_type}
resp = self.http_client.post(url, enc, headers=headers)
if resp.status_code != 200:
self._raise_api_exception(resp)
json_response_key = get_json(resp)
@ -36,19 +42,29 @@ class subcloud_backup_manager(base.ResourceManager):
return resource
def subcloud_backup_delete(self, url, data):
data = json.dumps(data)
resp = self.http_client.patch(url, data)
fields = dict()
fields.update(data)
enc = MultipartEncoder(fields=fields)
headers = {'content-type': enc.content_type}
resp = self.http_client.patch(url, enc, headers=headers)
if resp.status_code not in {204, 207}:
self._raise_api_exception(resp)
elif resp.status_code == 207:
return json.loads(resp.content)
return None
def subcloud_backup_restore(self, url, body, data):
if body:
data.update(body)
headers = {'Content-Type': 'application/json'}
enc = json.dumps(data)
def subcloud_backup_restore(self, url, files, data):
fields = dict()
if files:
for k, v in files.items():
fields.update({k: (v, open(v, 'rb'),)})
fields.update(data)
enc = MultipartEncoder(fields=fields)
headers = {'content-type': enc.content_type}
resp = self.http_client.patch(url, enc, headers=headers)
if resp.status_code != 200:

View File

@ -183,7 +183,7 @@ class CreateSubcloudBackup(base.DCManagerShow):
error_msg = "Backup-values file does not exist: %s" % \
parsed_args.backup_values
raise exceptions.DCManagerClientException(error_msg)
files['backup_values'] = utils.load_file(parsed_args.backup_values)
files['backup_values'] = parsed_args.backup_values
try:
return dcmanager_client.subcloud_backup_manager.\
@ -410,8 +410,7 @@ class RestoreSubcloudBackup(base.DCManagerShow):
error_msg = "Restore_values file does not exist: %s" % \
parsed_args.restore_values
raise exceptions.DCManagerClientException(error_msg)
files['restore_values'] = utils.load_file(
parsed_args.restore_values)
files['restore_values'] = parsed_args.restore_values
try:
return dcmanager_client.subcloud_backup_manager.\
backup_subcloud_restore(data=data, files=files)

View File

@ -15,8 +15,6 @@
#
import json
import os.path
import tempfile
import testtools
import yaml
@ -39,21 +37,5 @@ class UtilityTest(testtools.TestCase):
def test_load_json_content(self):
self.assertDictEqual(ENV_DICT, utils.load_content(ENV_STR))
def test_load_json_file(self):
with tempfile.NamedTemporaryFile() as f:
f.write(ENV_STR.encode('utf-8'))
f.flush()
file_path = os.path.abspath(f.name)
self.assertDictEqual(ENV_DICT, utils.load_file(file_path))
def test_load_yaml_content(self):
self.assertDictEqual(ENV_DICT, utils.load_content(ENV_YAML))
def test_load_yaml_file(self):
with tempfile.NamedTemporaryFile() as f:
f.write(ENV_YAML.encode('utf-8'))
f.flush()
file_path = os.path.abspath(f.name)
self.assertDictEqual(ENV_DICT, utils.load_file(file_path))

View File

@ -55,11 +55,6 @@ def load_content(content):
return data
def load_file(path):
with open(path, 'r') as f:
return load_content(f.read())
def get_contents_if_file(contents_or_file_name):
"""Get the contents of a file.