Skip to content

Commit 2a0129e

Browse files
authored
Merge pull request #78 from svt-pchavan/feature/restore_files
Add support for feature restore files from backup
2 parents 83ee3bd + 99c44e2 commit 2a0129e

5 files changed

Lines changed: 49 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- endpoint support for /backups/{bkpId}/copy <POST>
1717
- endpoint support for /backups/{bkpId}/rename <POST>
1818
- endpoint support for /backups/{bkpId}/restore <POST>
19+
- endpoint support for /backups/{bkpId}/restore_files <POST>
1920
- endpoint support for /backups/{bkpId}/virtual_disk_partitions <GET>
2021
- endpoint support for /backups/{bkpId}/virtual_disk_partition_files <GET>
2122
- endpoint support for /cluster_groups <GET>

endpoints-support.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Refer SimpliVity REST API doc for the resource endpoints documentation [HPE Simp
1616
|<sub>/backups/{bkpId}/lock</sub> |POST |
1717
|<sub>/backups/{bkpId}/rename</sub> |POST |
1818
|<sub>/backups/{bkpId}/restore</sub> |POST |
19+
|<sub>/backups/{bkpId}/restore_files</sub> |POST |
1920
|<sub>/backups/{bkpId}/virtual_disk_partitions</sub> |GET |
2021
|<sub>/backups/{bkpId}/virtual_disk_partition_files</sub> |GET |
2122
| **Cluster Groups**

examples/backups.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
cluster1_name = "CC_Virt_0001"
4040
cluster2_name = "CC_Virt_0000"
4141
virtual_disk = "tinyvm32_ATF_0.vmdk"
42+
# The virtual machine where you want to restore the files
43+
target_vm_name = "win2k8_template"
44+
# List of paths to the files in this format: virtual_machine_disk_name/partition_number/path_to_file
45+
paths = ['win2k8_template_test.vmdk/2/Users/desktop.ini', 'win2k8_template_test.vmdk/2/guestvmtest.csv', 'win2k8_template_test.vmdk/2/Windows/win.ini']
4246

4347
print("\n\nget_all with default params")
4448
all_backups = backups.get_all()
@@ -177,3 +181,16 @@
177181
print("\n\nget virtual disk partition files")
178182
partition_data = backup_object.get_virtual_disk_partition_files(virtual_disk, 1, "/")
179183
print(f"{pp.pformat(partition_data)}")
184+
185+
vm = machines.get_by_name(test_vm_name)
186+
backup = vm.create_backup("backup_test_from_sdk_" + str(time.time()))
187+
print(f"{backup}")
188+
print(f"{pp.pformat(backup.data)} \n")
189+
try:
190+
print("\n\nbackup restore files")
191+
target_vm = machines.get_by_name(target_vm_name)
192+
target_vm_id = target_vm.data["id"]
193+
backup.restore_files(target_vm_id, paths)
194+
except HPESimpliVityException as e:
195+
print(f"{e}")
196+
backup.delete()

simplivity/resources/backups.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,20 @@ def get_virtual_disk_partition_files(self, virtual_disk, partition_number, file_
321321
'file_path': file_path}
322322

323323
return self._client.do_get(resource_uri, data)
324+
325+
def restore_files(self, virtual_machine_id, paths, timeout=-1):
326+
"""Restores files from specific partition
327+
328+
Args:
329+
virtual_machine_id: The identification number of the virtual machine where you want to restore the files.
330+
paths: List of path to the files in this format: virtual_machine_disk_name/partition_number/path_to_file.
331+
332+
Returns:
333+
None
334+
"""
335+
resource_uri = "{}/{}/restore_files".format(URL, self.data["id"])
336+
custom_headers = {'Content-type': 'application/vnd.simplivity.v1.9+json'}
337+
data = {"virtual_machine_id": virtual_machine_id,
338+
"paths": paths}
339+
340+
self._client.do_post(resource_uri, data, timeout, custom_headers)

tests/unit/resources/test_backups.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,19 @@ def test_get_virtual_disk_partition_files(self, mock_get):
405405
self.assertEqual(partition_data, resource_data)
406406
mock_get.assert_has_calls([call('/backups/12345/virtual_disk_partition_files?file_path=%2F&partition_number=1&virtual_disk=tinyvm32_ATF_0.vmdk')])
407407

408+
@mock.patch.object(Connection, "post")
409+
@mock.patch.object(Connection, "get")
410+
def test_restore_files(self, mock_get, mock_post):
411+
mock_post.return_value = None, [{'object_id': '12345'}]
412+
resource_data = {'name': 'name1', 'id': '12345', 'state': 'PROTECTED'}
413+
mock_get.return_value = {backups.DATA_FIELD: [resource_data]}
414+
backup_data = {'name': 'name1', 'id': '12345', 'state': 'PROTECTED'}
415+
backup = self.backups.get_by_data(backup_data)
416+
paths = ['vmdkName/partition/file1', 'vmdkName/partition/directory/file2']
417+
backup.restore_files("12345", paths)
418+
data = {"virtual_machine_id": "12345", "paths": ['vmdkName/partition/file1', 'vmdkName/partition/directory/file2']}
419+
mock_post.assert_called_once_with('/backups/12345/restore_files', data, custom_headers={'Content-type': 'application/vnd.simplivity.v1.9+json'})
420+
408421

409422
if __name__ == '__main__':
410423
unittest.main()

0 commit comments

Comments
 (0)