diff --git a/libcloud/backup/drivers/ebs.py b/libcloud/backup/drivers/ebs.py index b0d667d2ef..53f956a157 100644 --- a/libcloud/backup/drivers/ebs.py +++ b/libcloud/backup/drivers/ebs.py @@ -26,10 +26,11 @@ ) from libcloud.backup.types import BackupTargetType, BackupTargetJobStatusType from libcloud.utils.iso8601 import parse_date +from libcloud.compute.constants.ec2_region_details_partial import REGION_DETAILS -VERSION = "2015-10-01" +VERSION = "2016-11-15" HOST = "ec2.amazonaws.com" -ROOT = "/%s/" % (VERSION) +ROOT = "/" NS = "http://ec2.amazonaws.com/doc/{}/".format(VERSION) @@ -47,7 +48,7 @@ class EBSConnection(SignedAWSConnection): version = VERSION host = HOST responseCls = EBSResponse - service_name = "backup" + service_name = "ec2" class EBSBackupDriver(BackupDriver): @@ -56,9 +57,12 @@ class EBSBackupDriver(BackupDriver): connectionCls = EBSConnection def __init__(self, access_id, secret, region): - super().__init__(access_id, secret) self.region = region - self.connection.host = HOST % (region) + self.region_name = region + super().__init__(access_id, secret, host=REGION_DETAILS[region]["endpoint"]) + + def _ex_connection_class_kwargs(self): + return {"signature_version": "4"} def get_supported_target_types(self): """ @@ -189,7 +193,7 @@ def list_recovery_points(self, target, start_date=None, end_date=None): params = { "Action": "DescribeSnapshots", "Filter.1.Name": "volume-id", - "Filter.1.Value": target.extra["volume-id"], + "Filter.1.Value.1": target.extra["volume-id"], } data = self.connection.request(ROOT, params=params).object return self._to_recovery_points(data, target) @@ -258,9 +262,9 @@ def list_target_jobs(self, target): params = { "Action": "DescribeSnapshots", "Filter.1.Name": "volume-id", - "Filter.1.Value": target.extra["volume-id"], + "Filter.1.Value.1": target.extra["volume-id"], "Filter.2.Name": "status", - "Filter.2.Value": "pending", + "Filter.2.Value.1": "pending", } data = self.connection.request(ROOT, params=params).object return self._to_jobs(data) @@ -316,7 +320,7 @@ def cancel_target_job(self, job): raise NotImplementedError("cancel_target_job not supported for this driver") def _to_recovery_points(self, data, target): - xpath = "DescribeSnapshotsResponse/snapshotSet/item" + xpath = "snapshotSet/item" return [ self._to_recovery_point(el, target) for el in findall(element=data, xpath=xpath, namespace=NS) @@ -336,7 +340,7 @@ def _to_recovery_point(self, el, target): return point def _to_jobs(self, data): - xpath = "DescribeSnapshotsResponse/snapshotSet/item" + xpath = "snapshotSet/item" return [self._to_job(el) for el in findall(element=data, xpath=xpath, namespace=NS)] def _to_job(self, el): diff --git a/libcloud/test/backup/test_ebs.py b/libcloud/test/backup/test_ebs.py new file mode 100644 index 0000000000..a745126a82 --- /dev/null +++ b/libcloud/test/backup/test_ebs.py @@ -0,0 +1,80 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from unittest import TestCase +from urllib.parse import parse_qs, urlsplit + +import requests_mock + +from libcloud.backup.drivers.ebs import EBSBackupDriver + + +class EBSBackupDriverTests(TestCase): + def test_regional_endpoint(self): + for region, host in [ + ("ap-south-1", "ec2.ap-south-1.amazonaws.com"), + ("cn-north-1", "ec2.cn-north-1.amazonaws.com.cn"), + ]: + with self.subTest(region=region): + driver = EBSBackupDriver("access-key", "secret-key", region) + self.assertEqual(driver.connection.host, host) + self.assertEqual(driver.connection.service_name, "ec2") + + def test_list_recovery_points_uses_ec2_query_api(self): + driver = EBSBackupDriver("access-key", "secret-key", "ap-south-1") + target = driver.ex_get_target_by_volume_id("vol-123") + xml = """ + snap-123vol-123 + 2026-11-01T12:00:00.000Z100% + Namebackup + """ + with requests_mock.Mocker() as transport: + transport.get("https://ec2.ap-south-1.amazonaws.com/", text=xml) + points = driver.list_recovery_points(target) + self.assertEqual(len(points), 1) + self.assertEqual(points[0].id, "snap-123") + self.assertEqual(points[0].extra["tags"], {"Name": "backup"}) + query = parse_qs(urlsplit(transport.last_request.url).query) + self.assertEqual(query["Action"], ["DescribeSnapshots"]) + self.assertEqual(query["Version"], ["2016-11-15"]) + self.assertEqual(query["Filter.1.Value.1"], ["vol-123"]) + self.assertIn( + "/ap-south-1/ec2/aws4_request", transport.last_request.headers["Authorization"] + ) + + def test_list_recovery_points_empty(self): + driver = EBSBackupDriver("access-key", "secret-key", "ap-south-1") + target = driver.ex_get_target_by_volume_id("vol-123") + xml = '' + with requests_mock.Mocker() as transport: + transport.get("https://ec2.ap-south-1.amazonaws.com/", text=xml) + self.assertEqual(driver.list_recovery_points(target), []) + + def test_list_target_jobs_filters_pending_snapshots(self): + driver = EBSBackupDriver("access-key", "secret-key", "ap-south-1") + target = driver.ex_get_target_by_volume_id("vol-123") + xml = """ + snap-123vol-123 + 25%""" + with requests_mock.Mocker() as transport: + transport.get("https://ec2.ap-south-1.amazonaws.com/", text=xml) + jobs = driver.list_target_jobs(target) + self.assertEqual(len(jobs), 1) + self.assertEqual(jobs[0].id, "snap-123") + self.assertEqual(jobs[0].progress, 25) + query = parse_qs(urlsplit(transport.last_request.url).query) + self.assertEqual(query["Filter.1.Value.1"], ["vol-123"]) + self.assertEqual(query["Filter.2.Name"], ["status"]) + self.assertEqual(query["Filter.2.Value.1"], ["pending"])