From f28ad8ee293e239c0b743fcd8cfbb4617090811f Mon Sep 17 00:00:00 2001 From: Peter Van Bouwel Date: Tue, 11 Nov 2014 10:26:02 +0100 Subject: [PATCH] Alter get_tags to use the backend associated with the taggable object. Also give volume and snapshot an additional backend attribute. --- moto/ec2/models.py | 20 +++++++++++++++----- tests/test_ec2/test_tags.py | 4 ++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 4684e02b3..8f9220142 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -109,7 +109,15 @@ class StateReason(object): class TaggedEC2Resource(object): def get_tags(self, *args, **kwargs): - tags = ec2_backend.describe_tags(filters={'resource-id': [self.id]}) + if hasattr(self,"ec2_backend"): + backend = self.ec2_backend + elif hasattr(self,"ebs_backend"): + backend = self.ebs_backend + else: + raise NotImplementedError("Tagging of an object with backend that differs from ec2_backend or ebs_backend.") + + + tags = backend.describe_tags(filters={'resource-id': [self.id]}) return tags def get_filter_value(self, filter_name): @@ -1224,11 +1232,12 @@ class VolumeAttachment(object): class Volume(TaggedEC2Resource): - def __init__(self, volume_id, size, zone): + def __init__(self, ebs_backend, volume_id, size, zone): self.id = volume_id self.size = size self.zone = zone self.attachment = None + self.ebs_backend = ebs_backend @classmethod def create_from_cloudformation_json(cls, resource_name, cloudformation_json): @@ -1253,11 +1262,12 @@ class Volume(TaggedEC2Resource): class Snapshot(TaggedEC2Resource): - def __init__(self, snapshot_id, volume, description): + def __init__(self, ebs_backend, snapshot_id, volume, description): self.id = snapshot_id self.volume = volume self.description = description self.create_volume_permission_groups = set() + self.ebs_backend = ebs_backend class EBSBackend(object): @@ -1270,7 +1280,7 @@ class EBSBackend(object): def create_volume(self, size, zone_name): volume_id = random_volume_id() zone = self.get_zone_by_name(zone_name) - volume = Volume(volume_id, size, zone) + volume = Volume(self, volume_id, size, zone) self.volumes[volume_id] = volume return volume @@ -1312,7 +1322,7 @@ class EBSBackend(object): def create_snapshot(self, volume_id, description): snapshot_id = random_snapshot_id() volume = self.get_volume(volume_id) - snapshot = Snapshot(snapshot_id, volume, description) + snapshot = Snapshot(self, snapshot_id, volume, description) self.snapshots[snapshot_id] = snapshot return snapshot diff --git a/tests/test_ec2/test_tags.py b/tests/test_ec2/test_tags.py index 15717519d..72e72fb86 100644 --- a/tests/test_ec2/test_tags.py +++ b/tests/test_ec2/test_tags.py @@ -315,8 +315,8 @@ def test_retrieved_snapshots_must_contain_their_tags(): tag_key = 'Tag name' tag_value = 'Tag value' tags_to_be_set = {tag_key: tag_value} - conn = boto.connect_ec2('the_key', 'the_secret') - volume = conn.create_volume(80, "us-east-1a") + conn = boto.connect_ec2(aws_access_key_id='the_key', aws_secret_access_key='the_secret') + volume = conn.create_volume(80, "eu-west-1a") snapshot = conn.create_snapshot(volume.id) conn.create_tags([snapshot.id], tags_to_be_set)