]>
Commit | Line | Data |
---|---|---|
dcea9012 JB |
1 | Index: boto-2.40.0/boto/vpc/__init__.py |
2 | =================================================================== | |
3 | --- boto-2.40.0.orig/boto/vpc/__init__.py | |
4 | +++ boto-2.40.0/boto/vpc/__init__.py | |
5 | @@ -29,6 +29,7 @@ from boto.vpc.vpc import VPC | |
6 | from boto.vpc.customergateway import CustomerGateway | |
7 | from boto.vpc.networkacl import NetworkAcl | |
8 | from boto.vpc.routetable import RouteTable | |
9 | +from boto.vpc.natgateway import NatGateway | |
10 | from boto.vpc.internetgateway import InternetGateway | |
11 | from boto.vpc.vpngateway import VpnGateway, Attachment | |
12 | from boto.vpc.dhcpoptions import DhcpOptions | |
13 | @@ -783,6 +784,76 @@ class VPCConnection(EC2Connection): | |
14 | ||
15 | return self.get_status('DeleteNetworkAclEntry', params) | |
16 | ||
17 | + # NAT Gateways | |
18 | + | |
19 | + def get_all_nat_gateways(self, nat_gateway_ids=None, filters=None, dry_run=False): | |
20 | + """ | |
21 | + Get a list of NAT gateways. You can filter results to return information | |
22 | + about only those gateways that you're interested in. | |
23 | + | |
24 | + :type nat_gateway_ids: list | |
25 | + :param nat_gateway_ids: A list of strings with the desired gateway IDs. | |
26 | + | |
27 | + :type filters: list of tuples or dict | |
28 | + :param filters: A list of tuples or dict containing filters. Each tuple | |
29 | + or dict item consists of a filter key and a filter value. | |
30 | + | |
31 | + :type dry_run: bool | |
32 | + :param dry_run: Set to True if the operation should not actually run. | |
33 | + | |
34 | + """ | |
35 | + params = {} | |
36 | + | |
37 | + if nat_gateway_ids: | |
38 | + self.build_list_params(params, nat_gateway_ids, | |
39 | + 'NatGatewayId') | |
40 | + if filters: | |
41 | + self.build_filter_params(params, filters) | |
42 | + if dry_run: | |
43 | + params['DryRun'] = 'true' | |
44 | + return self.get_list('DescribeNatGateways', params, | |
45 | + [('item', NatGateway)]) | |
46 | + | |
47 | + def create_nat_gateway(self, subnet_id, allocation_id, dry_run=False): | |
48 | + """ | |
49 | + Creates a NAT gateway for VPC. | |
50 | + | |
51 | + :type subnet_id: str | |
52 | + :param subnet_id: The subnet in which the NAT gateway should be launched. | |
53 | + | |
54 | + :type allocation_id: str | |
55 | + :param allocation_id: The allocation ID of an elastic IP address for the public side of the gateway. | |
56 | + | |
57 | + :type dry_run: bool | |
58 | + :param dry_run: Set to True if the operation should not actually run. | |
59 | + | |
60 | + :rtype: Newly created nat gateway. | |
61 | + :return: `boto.vpc.natgateway.NATGateway` | |
62 | + """ | |
63 | + params = {'SubnetId': subnet_id, | |
64 | + 'AllocationId': allocation_id} | |
65 | + if dry_run: | |
66 | + params['DryRun'] = 'true' | |
67 | + return self.get_object('CreateNatGateway', params, NatGateway) | |
68 | + | |
69 | + def delete_nat_gateway(self, nat_gateway_id, dry_run=False): | |
70 | + """ | |
71 | + Deletes a NAT gateway from the VPC. | |
72 | + | |
73 | + :type nat_gateway_id: str | |
74 | + :param nat_gateway_id: The ID of the NAT gateway to delete. | |
75 | + | |
76 | + :type dry_run: bool | |
77 | + :param dry_run: Set to True if the operation should not actually run. | |
78 | + | |
79 | + :rtype: Bool | |
80 | + :return: True if successful | |
81 | + """ | |
82 | + params = {'NatGatewayId': nat_gateway_id} | |
83 | + if dry_run: | |
84 | + params['DryRun'] = 'true' | |
85 | + return self.get_status('DeleteNatGateway', params) | |
86 | + | |
87 | # Internet Gateways | |
88 | ||
89 | def get_all_internet_gateways(self, internet_gateway_ids=None, | |
90 | Index: boto-2.40.0/boto/vpc/natgateway.py | |
91 | =================================================================== | |
92 | --- /dev/null | |
93 | +++ boto-2.40.0/boto/vpc/natgateway.py | |
94 | @@ -0,0 +1,89 @@ | |
95 | +# Copyright (c) 2009-2010 Mitch Garnaat http://garnaat.org/ | |
96 | +# | |
97 | +# Permission is hereby granted, free of charge, to any person obtaining a | |
98 | +# copy of this software and associated documentation files (the | |
99 | +# "Software"), to deal in the Software without restriction, including | |
100 | +# without limitation the rights to use, copy, modify, merge, publish, dis- | |
101 | +# tribute, sublicense, and/or sell copies of the Software, and to permit | |
102 | +# persons to whom the Software is furnished to do so, subject to the fol- | |
103 | +# lowing conditions: | |
104 | +# | |
105 | +# The above copyright notice and this permission notice shall be included | |
106 | +# in all copies or substantial portions of the Software. | |
107 | +# | |
108 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
109 | +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | |
110 | +# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | |
111 | +# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
112 | +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
113 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
114 | +# IN THE SOFTWARE. | |
115 | + | |
116 | +""" | |
117 | +Represents a NAT Gateway | |
118 | +""" | |
119 | + | |
120 | +from boto.ec2.ec2object import TaggedEC2Object | |
121 | +from boto.resultset import ResultSet | |
122 | + | |
123 | + | |
124 | +class NatGateway(TaggedEC2Object): | |
125 | + def __init__(self, connection=None): | |
126 | + super(NatGateway, self).__init__(connection) | |
127 | + self.id = None | |
128 | + self.vpc_id = None | |
129 | + self.subnet_id = None | |
130 | + self.state = None | |
131 | + self.addresses = [] | |
132 | + | |
133 | + def __repr__(self): | |
134 | + return 'NatGateway:%s' % self.id | |
135 | + | |
136 | + def startElement(self, name, attrs, connection): | |
137 | + result = super(NatGateway, self).startElement(name, attrs, connection) | |
138 | + | |
139 | + if result is not None: | |
140 | + # Parent found an interested element, just return it | |
141 | + return result | |
142 | + | |
143 | + if name == 'natGatewayAddressSet': | |
144 | + self.addresses = ResultSet([('item', NatGatewayAddress)]) | |
145 | + return self.addresses | |
146 | + else: | |
147 | + return None | |
148 | + | |
149 | + def endElement(self, name, value, connection): | |
150 | + if name == 'natGatewayId': | |
151 | + self.id = value | |
152 | + elif name == 'vpcId': | |
153 | + self.vpc_id = value | |
154 | + elif name == 'subnetId': | |
155 | + self.subnet_id = value | |
156 | + elif name == 'state': | |
157 | + self.state = value | |
158 | + else: | |
159 | + setattr(self, name, value) | |
160 | + | |
161 | + | |
162 | +class NatGatewayAddress(object): | |
163 | + def __init__(self, connection=None): | |
164 | + self.interface_id = None | |
165 | + self.allocation_id = None | |
166 | + self.ip_public = None | |
167 | + self.ip_private = None | |
168 | + | |
169 | + def __repr__(self): | |
170 | + return 'NatGatewayAddress:%s' % self.interface_id | |
171 | + | |
172 | + def startElement(self, name, attrs, connection): | |
173 | + return None | |
174 | + | |
175 | + def endElement(self, name, value, connection): | |
176 | + if name == 'networkInterfaceId': | |
177 | + self.interface_id = value | |
178 | + elif name == 'publicIp': | |
179 | + self.ip_public = value | |
180 | + elif name == 'allocationId': | |
181 | + self.allocation_id = value | |
182 | + elif name == 'privateIp': | |
183 | + self.ip_private = value | |
184 | Index: boto-2.40.0/docs/source/ref/vpc.rst | |
185 | =================================================================== | |
186 | --- boto-2.40.0.orig/docs/source/ref/vpc.rst | |
187 | +++ boto-2.40.0/docs/source/ref/vpc.rst | |
188 | @@ -32,6 +32,13 @@ boto.vpc.internetgateway | |
189 | :members: | |
190 | :undoc-members: | |
191 | ||
192 | +boto.vpc.natgateway | |
193 | +------------------- | |
194 | + | |
195 | +.. automodule:: boto.vpc.natgateway | |
196 | + :members: | |
197 | + :undoc-members: | |
198 | + | |
199 | boto.vpc.routetable | |
200 | ------------------- | |
201 | ||
202 | Index: boto-2.40.0/tests/unit/vpc/test_natgateway.py | |
203 | =================================================================== | |
204 | --- /dev/null | |
205 | +++ boto-2.40.0/tests/unit/vpc/test_natgateway.py | |
206 | @@ -0,0 +1,113 @@ | |
207 | +from tests.unit import unittest | |
208 | +from tests.unit import AWSMockServiceTestCase | |
209 | + | |
210 | +from boto.vpc import VPCConnection, NatGateway | |
211 | + | |
212 | + | |
213 | +class TestDescribeNatGateway(AWSMockServiceTestCase): | |
214 | + | |
215 | + connection_class = VPCConnection | |
216 | + | |
217 | + def default_body(self): | |
218 | + return b""" | |
219 | + <DescribeNatGatewaysResponse xmlns="http://ec2.amazonaws.com/doc/2015-10-01/"> | |
220 | + <requestId>bfed02c6-dae9-47c0-86a2-example</requestId> | |
221 | + <natGatewaySet> | |
222 | + <item> | |
223 | + <subnetId>subnet-1a2a3a4a</subnetId> | |
224 | + <natGatewayAddressSet> | |
225 | + <item> | |
226 | + <networkInterfaceId>eni-00e37850</networkInterfaceId> | |
227 | + <publicIp>198.18.125.129</publicIp> | |
228 | + <allocationId>eipalloc-37fc1a52</allocationId> | |
229 | + <privateIp>10.0.2.147</privateIp> | |
230 | + </item> | |
231 | + </natGatewayAddressSet> | |
232 | + <createTime>2015-11-25T14:00:55.416Z</createTime> | |
233 | + <vpcId>vpc-4e20d42b</vpcId> | |
234 | + <natGatewayId>nat-04e77a5e9c34432f9</natGatewayId> | |
235 | + <state>available</state> | |
236 | + </item> | |
237 | + </natGatewaySet> | |
238 | + </DescribeNatGatewaysResponse> | |
239 | + """ | |
240 | + | |
241 | + def test_describe_nat_gateway(self): | |
242 | + self.set_http_response(status_code=200) | |
243 | + api_response = self.service_connection.get_all_nat_gateways( | |
244 | + 'nat-04e77a5e9c34432f9', filters=[('natGatewayAddress.allocationId', ['eipalloc-37fc1a52'])]) | |
245 | + self.assert_request_parameters({ | |
246 | + 'Action': 'DescribeNatGateways', | |
247 | + 'NatGatewayId.1': 'nat-04e77a5e9c34432f9', | |
248 | + 'Filter.1.Name': 'natGatewayAddress.allocationId', | |
249 | + 'Filter.1.Value.1': 'eipalloc-37fc1a52'}, | |
250 | + ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', | |
251 | + 'SignatureVersion', 'Timestamp', | |
252 | + 'Version']) | |
253 | + self.assertEquals(len(api_response), 1) | |
254 | + self.assertIsInstance(api_response[0], NatGateway) | |
255 | + self.assertEqual(api_response[0].id, 'nat-04e77a5e9c34432f9') | |
256 | + | |
257 | + | |
258 | +class TestCreateNatGateway(AWSMockServiceTestCase): | |
259 | + | |
260 | + connection_class = VPCConnection | |
261 | + | |
262 | + def default_body(self): | |
263 | + return b""" | |
264 | + <CreateNatGatewayResponse xmlns="http://ec2.amazonaws.com/doc/2015-10-01/"> | |
265 | + <requestId>1b74dc5c-bcda-403f-867d-example</requestId> | |
266 | + <natGateway> | |
267 | + <subnetId>subnet-1a2b3c4d</subnetId> | |
268 | + <natGatewayAddressSet> | |
269 | + <item> | |
270 | + <allocationId>eipalloc-37fc1a52</allocationId> | |
271 | + </item> | |
272 | + </natGatewayAddressSet> | |
273 | + <createTime>2015-11-25T14:00:55.416Z</createTime> | |
274 | + <vpcId>vpc-4e20d42b</vpcId> | |
275 | + <natGatewayId>nat-04e77a5e9c34432f9</natGatewayId> | |
276 | + <state>pending</state> | |
277 | + </natGateway> | |
278 | + </CreateNatGatewayResponse> | |
279 | + """ | |
280 | + | |
281 | + def test_create_nat_gateway(self): | |
282 | + self.set_http_response(status_code=200) | |
283 | + api_response = self.service_connection.create_nat_gateway('subnet-1a2b3c4d', 'eipalloc-37fc1a52') | |
284 | + self.assert_request_parameters({ | |
285 | + 'Action': 'CreateNatGateway', | |
286 | + 'SubnetId': 'subnet-1a2b3c4d', | |
287 | + 'AllocationId': 'eipalloc-37fc1a52'}, | |
288 | + ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', | |
289 | + 'SignatureVersion', 'Timestamp', | |
290 | + 'Version']) | |
291 | + self.assertIsInstance(api_response, NatGateway) | |
292 | + self.assertEqual(api_response.id, 'nat-04e77a5e9c34432f9') | |
293 | + | |
294 | + | |
295 | +class TestDeleteNatGateway(AWSMockServiceTestCase): | |
296 | + | |
297 | + connection_class = VPCConnection | |
298 | + | |
299 | + def default_body(self): | |
300 | + return b""" | |
301 | + <DeleteNatGatewayResponse xmlns="http://ec2.amazonaws.com/doc/2015-10-01/"> | |
302 | + <requestId>741fc8ab-6ebe-452b-b92b-example</requestId> | |
303 | + <natGatewayId>nat-04ae55e711cec5680</natGatewayId> | |
304 | + </DeleteNatGatewayResponse> | |
305 | + """ | |
306 | + | |
307 | + def test_delete_nat_gateway(self): | |
308 | + self.set_http_response(status_code=200) | |
309 | + api_response = self.service_connection.delete_nat_gateway('nat-04ae55e711cec5680') | |
310 | + self.assert_request_parameters({ | |
311 | + 'Action': 'DeleteNatGateway', | |
312 | + 'NatGatewayId': 'nat-04ae55e711cec5680'}, | |
313 | + ignore_params_values=['AWSAccessKeyId', 'SignatureMethod', | |
314 | + 'SignatureVersion', 'Timestamp', | |
315 | + 'Version']) | |
316 | + self.assertEquals(api_response, True) | |
317 | + | |
318 | +if __name__ == '__main__': | |
319 | + unittest.main() |