Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions libcloud/backup/drivers/ebs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -47,7 +48,7 @@ class EBSConnection(SignedAWSConnection):
version = VERSION
host = HOST
responseCls = EBSResponse
service_name = "backup"
service_name = "ec2"


class EBSBackupDriver(BackupDriver):
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
80 changes: 80 additions & 0 deletions libcloud/test/backup/test_ebs.py
Original file line number Diff line number Diff line change
@@ -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 = """<DescribeSnapshotsResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
<snapshotSet><item><snapshotId>snap-123</snapshotId><volumeId>vol-123</volumeId>
<startTime>2026-11-01T12:00:00.000Z</startTime><progress>100%</progress>
<tagSet><item><key>Name</key><value>backup</value></item></tagSet>
</item></snapshotSet></DescribeSnapshotsResponse>"""
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 = '<DescribeSnapshotsResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/"><snapshotSet/></DescribeSnapshotsResponse>'
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 = """<DescribeSnapshotsResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
<snapshotSet><item><snapshotId>snap-123</snapshotId><volumeId>vol-123</volumeId>
<progress>25%</progress></item></snapshotSet></DescribeSnapshotsResponse>"""
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"])