1# Copyright (c) 2014 Skytap http://skytap.com/
2#
3# Permission is hereby granted, free of charge, to any person obtaining a
4# copy of this software and associated documentation files (the
5# "Software"), to deal in the Software without restriction, including
6# without limitation the rights to use, copy, modify, merge, publish, dis-
7# tribute, sublicense, and/or sell copies of the Software, and to permit
8# persons to whom the Software is furnished to do so, subject to the fol-
9# lowing conditions:
10#
11# The above copyright notice and this permission notice shall be included
12# in all copies or substantial portions of the Software.
13#
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20# IN THE SOFTWARE.
21
22"""
23Represents a VPC Peering Connection.
24"""
25
26from boto.ec2.ec2object import TaggedEC2Object
27
28class VpcInfo(object):
29    def __init__(self):
30        """
31        Information on peer Vpc.
32
33        :ivar id: The unique ID of peer Vpc.
34        :ivar owner_id: Owner of peer Vpc.
35        :ivar cidr_block: CIDR Block of peer Vpc.
36        """
37
38        self.vpc_id = None
39        self.owner_id = None
40        self.cidr_block = None
41
42    def __repr__(self):
43        return 'VpcInfo:%s' % self.vpc_id
44
45    def startElement(self, name, attrs, connection):
46        pass
47
48    def endElement(self, name, value, connection):
49        if name == 'vpcId':
50            self.vpc_id = value
51        elif name == 'ownerId':
52            self.owner_id = value
53        elif name == 'cidrBlock':
54            self.cidr_block = value
55        else:
56            setattr(self, name, value)
57
58class VpcPeeringConnectionStatus(object):
59    """
60    The status of VPC peering connection.
61
62    :ivar code: The status of the VPC peering connection. Valid values are:
63
64        * pending-acceptance
65        * failed
66        * expired
67        * provisioning
68        * active
69        * deleted
70        * rejected
71
72    :ivar message: A message that provides more information about the status of the VPC peering connection, if applicable.
73    """
74    def __init__(self, code=0, message=None):
75        self.code = code
76        self.message = message
77
78    def __repr__(self):
79        return '%s(%d)' % (self.code, self.message)
80
81    def startElement(self, name, attrs, connection):
82        pass
83
84    def endElement(self, name, value, connection):
85        if name == 'code':
86            self.code = value
87        elif name == 'message':
88            self.message = value
89        else:
90            setattr(self, name, value)
91
92
93
94class VpcPeeringConnection(TaggedEC2Object):
95
96    def __init__(self, connection=None):
97        """
98        Represents a VPC peering connection.
99
100        :ivar id: The unique ID of the VPC peering connection.
101        :ivar accepter_vpc_info: Information on peer Vpc.
102        :ivar requester_vpc_info: Information on requester Vpc.
103        :ivar expiration_time: The expiration date and time for the VPC peering connection.
104        :ivar status_code: The status of the VPC peering connection.
105        :ivar status_message: A message that provides more information about the status of the VPC peering connection, if applicable.
106        """
107        super(VpcPeeringConnection, self).__init__(connection)
108        self.id = None
109        self.accepter_vpc_info = VpcInfo()
110        self.requester_vpc_info = VpcInfo()
111        self.expiration_time = None
112        self._status = VpcPeeringConnectionStatus()
113
114    @property
115    def status_code(self):
116        return self._status.code
117
118    @property
119    def status_message(self):
120        return self._status.message
121
122    def __repr__(self):
123        return 'VpcPeeringConnection:%s' % self.id
124
125    def startElement(self, name, attrs, connection):
126        retval = super(VpcPeeringConnection, self).startElement(name, attrs, connection)
127        if retval is not None:
128            return retval
129
130        if name == 'requesterVpcInfo':
131            return self.requester_vpc_info
132        elif name == 'accepterVpcInfo':
133            return self.accepter_vpc_info
134        elif name == 'status':
135            return self._status
136
137        return None
138
139    def endElement(self, name, value, connection):
140        if name == 'vpcPeeringConnectionId':
141            self.id = value
142        elif name == 'expirationTime':
143            self.expiration_time = value
144        else:
145            setattr(self, name, value)
146
147    def delete(self):
148        return self.connection.delete_vpc_peering_connection(self.id)
149
150    def _update(self, updated):
151        self.__dict__.update(updated.__dict__)
152
153    def update(self, validate=False, dry_run=False):
154        vpc_peering_connection_list = self.connection.get_all_vpc_peering_connections(
155            [self.id],
156            dry_run=dry_run
157        )
158        if len(vpc_peering_connection_list):
159            updated_vpc_peering_connection = vpc_peering_connection_list[0]
160            self._update(updated_vpc_peering_connection)
161        elif validate:
162            raise ValueError('%s is not a valid VpcPeeringConnection ID' % (self.id,))
163        return self.status_code
164