from botocore.exceptions import ClientError


class VPCPeeringRelationship(object):
    def __init__(self, vpc1, vpc2, ec2_client, logger):
        self.vpc1 = vpc1
        self.vpc2 = vpc2
        self.ec2_client = ec2_client
        self.logger = logger

    def fetch(self):
        peering_connection = next(
            self.ec2_client.vpc_peering_connections.filter(
                Filters=[
                    {'Name': 'accepter-vpc-info.vpc-id',
                     'Values': [self.vpc1.id]},
                    {'Name': 'requester-vpc-info.vpc-id',
                     'Values': [self.vpc2.id]}]),
            None)
        if peering_connection:
            return peering_connection

        peering_connection = next(
            self.ec2_client.vpc_peering_connections.filter(
                Filters=[
                    {'Name': 'accepter-vpc-info.vpc-id',
                     'Values': [self.vpc2.id]},
                    {'Name': 'requester-vpc-info.vpc-id',
                     'Values': [self.vpc1.id]}]),
            None)
        if peering_connection:
            return peering_connection

        return None

    def provision(self):
        vpc1_id = self.vpc1.id
        vpc2_id = self.vpc2.id

        self.logger.debug(
            "Requesting peering connection between: '%s' and: '%s'.",
            vpc1_id, vpc2_id)
        vpc_peering_connection = self.vpc1. \
            request_vpc_peering_connection(PeerVpcId=vpc2_id)

        try:
            self.logger.debug(
                "Accepting peering connection between: '%s' and: '%s'.",
                vpc1_id, vpc2_id)
            vpc_peering_connection.accept()
        except ClientError:
            self.logger.warn(
                "Could not accept peering connection. This may be because one "
                "already exists between '%s' and: '%s'. Continuing.",
                vpc1_id, vpc2_id)
            vpc_peering_connection.delete()

    def destroy(self):
        vpc_peering_connection = self.fetch()
        if vpc_peering_connection:
            self.logger.debug(
                "Destroying peering connection between: '%s' and: '%s'",
                    vpc_peering_connection.requester_vpc.id,
                    vpc_peering_connection.accepter_vpc.id)
            vpc_peering_connection.delete()
        else:
            self.logger.debug(
                "No peering connection to destroy between: '%s' and: '%s'",
                self.vpc1.id,
                self.vpc2.id)

@@ -262,49 +262,66 @@
 def 
-fetch(self):%0A        peering_connection =
+__peering_connection_for(self, vpc1, vpc2):%0A        return
  nex
@@ -490,37 +490,32 @@
      'Values': %5B
-self.
 vpc1.id%5D%7D,%0A     
@@ -591,37 +591,32 @@
      'Values': %5B
-self.
 vpc2.id%5D%7D%5D),%0A   
@@ -634,57 +634,37 @@
 ne)%0A
+%0A
     
-    if peering_connection:%0A            return
+def fetch(self):%0A       
  pee
@@ -678,26 +678,26 @@
 nnection
-%0A%0A        
+ = self.__
 peering_
@@ -710,15 +710,12 @@
 tion
- = next
+_for
 (%0A  
@@ -725,39 +725,52 @@
        self.
-ec2_client.vpc_
+vpc1, self.vpc2)%0A        if 
 peering_conn
@@ -771,33 +771,25 @@
 g_connection
-s.filter(
+:
 %0A           
@@ -793,251 +793,127 @@
     
-    Filters=%5B%0A                    %7B'Name': 'accepter-vpc-info.vpc-id',%0A                     'Values': %5Bself.vpc2.id%5D%7D,%0A                    %7B'Name': 'requester-vpc-info.vpc-id',%0A                     'Values': %5Bself.vpc1.id%5D%7D%5D),%0A            None
+return peering_connection%0A%0A        peering_connection = self.__peering_connection_for(%0A            self.vpc2, self.vpc1
 )%0A  

Factor out duplication in VPCPeeringRelationship.