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
96 lines
2.9 KiB
Python
96 lines
2.9 KiB
Python
|
|
#
|
|
# Copyright (c) 2022 Wind River Systems, Inc.
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
#
|
|
|
|
import json
|
|
|
|
from requests_toolbelt import MultipartEncoder
|
|
|
|
from dcmanagerclient.api import base
|
|
from dcmanagerclient.api.base import get_json
|
|
|
|
|
|
class subcloud_backup_manager(base.ResourceManager):
|
|
|
|
resource_class = base.Subcloud
|
|
|
|
def json_to_resource(self, json_object):
|
|
return self.resource_class.from_payload(self, json_object)
|
|
|
|
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)
|
|
json_objects = json_response_key['subclouds']
|
|
resource = []
|
|
for json_object in json_objects:
|
|
resource.append(
|
|
self.json_to_resource(json_object))
|
|
return resource
|
|
|
|
def subcloud_backup_delete(self, 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, 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:
|
|
self._raise_api_exception(resp)
|
|
|
|
json_response_key = get_json(resp)
|
|
json_objects = json_response_key['subclouds']
|
|
resource = []
|
|
for json_object in json_objects:
|
|
resource.append(self.json_to_resource(json_object))
|
|
return resource
|
|
|
|
def backup_subcloud_create(self, **kwargs):
|
|
files = kwargs.get('files')
|
|
data = kwargs.get('data')
|
|
url = '/subcloud-backup/'
|
|
return self.subcloud_backup_create(url, files, data)
|
|
|
|
def backup_subcloud_delete(self, release_version, **kwargs):
|
|
data = kwargs.get('data')
|
|
url = '/subcloud-backup/delete/%s' % release_version
|
|
return self.subcloud_backup_delete(url, data)
|
|
|
|
def backup_subcloud_restore(self, **kwargs):
|
|
files = kwargs.get('files')
|
|
data = kwargs.get('data')
|
|
url = '/subcloud-backup/restore'
|
|
return self.subcloud_backup_restore(url, files, data)
|