1
# Copyright (c) 2009 Mitch Garnaat http://garnaat.org/
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-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
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
23
Represents a connection to the EC2 service.
26
from boto.ec2.connection import EC2Connection
27
from boto.vpc.vpc import VPC
28
from boto.vpc.customergateway import CustomerGateway
29
from boto.vpc.vpngateway import VpnGateway, Attachment
30
from boto.vpc.dhcpoptions import DhcpOptions
31
from boto.vpc.subnet import Subnet
32
from boto.vpc.vpnconnection import VpnConnection
34
class VPCConnection(EC2Connection):
38
def get_all_vpcs(self, vpc_ids=None, filters=None):
40
Retrieve information about your VPCs. You can filter results to
41
return information only about those VPCs that match your search
42
parameters. Otherwise, all VPCs associated with your account
46
:param vpc_ids: A list of strings with the desired VPC ID's
48
:type filters: list of tuples
49
:param filters: A list of tuples containing filters. Each tuple
50
consists of a filter key and a filter value.
51
Possible filter keys are:
53
- *state*, the state of the VPC (pending or available)
54
- *cidrBlock*, CIDR block of the VPC
55
- *dhcpOptionsId*, the ID of a set of DHCP options
58
:return: A list of :class:`boto.vpc.vpc.VPC`
62
self.build_list_params(params, vpc_ids, 'VpcId')
65
for filter in filters:
66
params[('Filter.%d.Key' % i)] = filter[0]
67
params[('Filter.%d.Value.1')] = filter[1]
69
return self.get_list('DescribeVpcs', params, [('item', VPC)])
71
def create_vpc(self, cidr_block):
73
Create a new Virtual Private Cloud.
76
:param cidr_block: A valid CIDR block
78
:rtype: The newly created VPC
79
:return: A :class:`boto.vpc.vpc.VPC` object
81
params = {'CidrBlock' : cidr_block}
82
return self.get_object('CreateVpc', params, VPC)
84
def delete_vpc(self, vpc_id):
86
Delete a Virtual Private Cloud.
89
:param vpc_id: The ID of the vpc to be deleted.
92
:return: True if successful
94
params = {'VpcId': vpc_id}
95
return self.get_status('DeleteVpc', params)
99
def get_all_customer_gateways(self, customer_gateway_ids=None, filters=None):
101
Retrieve information about your CustomerGateways. You can filter results to
102
return information only about those CustomerGateways that match your search
103
parameters. Otherwise, all CustomerGateways associated with your account
106
:type customer_gateway_ids: list
107
:param customer_gateway_ids: A list of strings with the desired CustomerGateway ID's
109
:type filters: list of tuples
110
:param filters: A list of tuples containing filters. Each tuple
111
consists of a filter key and a filter value.
112
Possible filter keys are:
114
- *state*, the state of the CustomerGateway
115
(pending,available,deleting,deleted)
116
- *type*, the type of customer gateway (ipsec.1)
117
- *ipAddress* the IP address of customer gateway's
118
internet-routable external inteface
121
:return: A list of :class:`boto.vpc.customergateway.CustomerGateway`
124
if customer_gateway_ids:
125
self.build_list_params(params, customer_gateway_ids, 'CustomerGatewayId')
128
for filter in filters:
129
params[('Filter.%d.Key' % i)] = filter[0]
130
params[('Filter.%d.Value.1')] = filter[1]
132
return self.get_list('DescribeCustomerGateways', params, [('item', CustomerGateway)])
134
def create_customer_gateway(self, type, ip_address, bgp_asn):
136
Create a new Customer Gateway
139
:param type: Type of VPN Connection. Only valid valid currently is 'ipsec.1'
141
:type ip_address: str
142
:param ip_address: Internet-routable IP address for customer's gateway.
143
Must be a static address.
146
:param bgp_asn: Customer gateway's Border Gateway Protocol (BGP)
147
Autonomous System Number (ASN)
149
:rtype: The newly created CustomerGateway
150
:return: A :class:`boto.vpc.customergateway.CustomerGateway` object
152
params = {'Type' : type,
153
'IpAddress' : ip_address,
155
return self.get_object('CreateCustomerGateway', params, CustomerGateway)
157
def delete_customer_gateway(self, customer_gateway_id):
159
Delete a Customer Gateway.
161
:type customer_gateway_id: str
162
:param customer_gateway_id: The ID of the customer_gateway to be deleted.
165
:return: True if successful
167
params = {'CustomerGatewayId': customer_gateway_id}
168
return self.get_status('DeleteCustomerGateway', params)
172
def get_all_vpn_gateways(self, vpn_gateway_ids=None, filters=None):
174
Retrieve information about your VpnGateways. You can filter results to
175
return information only about those VpnGateways that match your search
176
parameters. Otherwise, all VpnGateways associated with your account
179
:type vpn_gateway_ids: list
180
:param vpn_gateway_ids: A list of strings with the desired VpnGateway ID's
182
:type filters: list of tuples
183
:param filters: A list of tuples containing filters. Each tuple
184
consists of a filter key and a filter value.
185
Possible filter keys are:
187
- *state*, the state of the VpnGateway
188
(pending,available,deleting,deleted)
189
- *type*, the type of customer gateway (ipsec.1)
190
- *availabilityZone*, the Availability zone the
194
:return: A list of :class:`boto.vpc.customergateway.VpnGateway`
198
self.build_list_params(params, vpn_gateway_ids, 'VpnGatewayId')
201
for filter in filters:
202
params[('Filter.%d.Key' % i)] = filter[0]
203
params[('Filter.%d.Value.1')] = filter[1]
205
return self.get_list('DescribeVpnGateways', params, [('item', VpnGateway)])
207
def create_vpn_gateway(self, type, availability_zone=None):
209
Create a new Vpn Gateway
212
:param type: Type of VPN Connection. Only valid valid currently is 'ipsec.1'
214
:type availability_zone: str
215
:param availability_zone: The Availability Zone where you want the VPN gateway.
217
:rtype: The newly created VpnGateway
218
:return: A :class:`boto.vpc.vpngateway.VpnGateway` object
220
params = {'Type' : type}
221
if availability_zone:
222
params['AvailabilityZone'] = availability_zone
223
return self.get_object('CreateVpnGateway', params, VpnGateway)
225
def delete_vpn_gateway(self, vpn_gateway_id):
227
Delete a Vpn Gateway.
229
:type vpn_gateway_id: str
230
:param vpn_gateway_id: The ID of the vpn_gateway to be deleted.
233
:return: True if successful
235
params = {'VpnGatewayId': vpn_gateway_id}
236
return self.get_status('DeleteVpnGateway', params)
238
def attach_vpn_gateway(self, vpn_gateway_id, vpc_id):
240
Attaches a VPN gateway to a VPC.
242
:type vpn_gateway_id: str
243
:param vpn_gateway_id: The ID of the vpn_gateway to attach
246
:param vpc_id: The ID of the VPC you want to attach the gateway to.
248
:rtype: An attachment
249
:return: a :class:`boto.vpc.vpngateway.Attachment`
251
params = {'VpnGatewayId': vpn_gateway_id,
253
return self.get_object('AttachVpnGateway', params, Attachment)
257
def get_all_subnets(self, subnet_ids=None, filters=None):
259
Retrieve information about your Subnets. You can filter results to
260
return information only about those Subnets that match your search
261
parameters. Otherwise, all Subnets associated with your account
264
:type subnet_ids: list
265
:param subnet_ids: A list of strings with the desired Subnet ID's
267
:type filters: list of tuples
268
:param filters: A list of tuples containing filters. Each tuple
269
consists of a filter key and a filter value.
270
Possible filter keys are:
272
- *state*, the state of the Subnet
274
- *vpdId*, the ID of teh VPC the subnet is in.
275
- *cidrBlock*, CIDR block of the subnet
276
- *availabilityZone*, the Availability Zone
281
:return: A list of :class:`boto.vpc.subnet.Subnet`
285
self.build_list_params(params, subnet_ids, 'SubnetId')
288
for filter in filters:
289
params[('Filter.%d.Key' % i)] = filter[0]
290
params[('Filter.%d.Value.1')] = filter[1]
292
return self.get_list('DescribeSubnets', params, [('item', Subnet)])
294
def create_subnet(self, vpc_id, cidr_block, availability_zone=None):
299
:param vpc_id: The ID of the VPC where you want to create the subnet.
301
:type cidr_block: str
302
:param cidr_block: The CIDR block you want the subnet to cover.
304
:type availability_zone: str
305
:param availability_zone: The AZ you want the subnet in
307
:rtype: The newly created Subnet
308
:return: A :class:`boto.vpc.customergateway.Subnet` object
310
params = {'VpcId' : vpc_id,
311
'CidrBlock' : cidr_block}
312
if availability_zone:
313
params['AvailabilityZone'] = availability_zone
314
return self.get_object('CreateSubnet', params, Subnet)
316
def delete_subnet(self, subnet_id):
321
:param subnet_id: The ID of the subnet to be deleted.
324
:return: True if successful
326
params = {'SubnetId': subnet_id}
327
return self.get_status('DeleteSubnet', params)
332
def get_all_dhcp_options(self, dhcp_options_ids=None):
334
Retrieve information about your DhcpOptions.
336
:type dhcp_options_ids: list
337
:param dhcp_options_ids: A list of strings with the desired DhcpOption ID's
340
:return: A list of :class:`boto.vpc.dhcpoptions.DhcpOptions`
344
self.build_list_params(params, dhcp_options_ids, 'DhcpOptionsId')
345
return self.get_list('DescribeDhcpOptions', params, [('item', DhcpOptions)])
347
def create_dhcp_options(self, vpc_id, cidr_block, availability_zone=None):
349
Create a new DhcpOption
352
:param vpc_id: The ID of the VPC where you want to create the subnet.
354
:type cidr_block: str
355
:param cidr_block: The CIDR block you want the subnet to cover.
357
:type availability_zone: str
358
:param availability_zone: The AZ you want the subnet in
360
:rtype: The newly created DhcpOption
361
:return: A :class:`boto.vpc.customergateway.DhcpOption` object
363
params = {'VpcId' : vpc_id,
364
'CidrBlock' : cidr_block}
365
if availability_zone:
366
params['AvailabilityZone'] = availability_zone
367
return self.get_object('CreateDhcpOption', params, DhcpOptions)
369
def delete_dhcp_options(self, dhcp_options_id):
371
Delete a DHCP Options
373
:type dhcp_options_id: str
374
:param dhcp_options_id: The ID of the DHCP Options to be deleted.
377
:return: True if successful
379
params = {'DhcpOptionsId': dhcp_options_id}
380
return self.get_status('DeleteDhcpOptions', params)
382
def associate_dhcp_options(self, dhcp_options_id, vpc_id):
384
Associate a set of Dhcp Options with a VPC.
386
:type dhcp_options_id: str
387
:param dhcp_options_id: The ID of the Dhcp Options
390
:param vpc_id: The ID of the VPC.
393
:return: True if successful
395
params = {'DhcpOptionsId': dhcp_options_id,
397
return self.get_status('AssociateDhcpOptions', params)
401
def get_all_vpn_connections(self, vpn_connection_ids=None, filters=None):
403
Retrieve information about your VPN_CONNECTIONs. You can filter results to
404
return information only about those VPN_CONNECTIONs that match your search
405
parameters. Otherwise, all VPN_CONNECTIONs associated with your account
408
:type vpn_connection_ids: list
409
:param vpn_connection_ids: A list of strings with the desired VPN_CONNECTION ID's
411
:type filters: list of tuples
412
:param filters: A list of tuples containing filters. Each tuple
413
consists of a filter key and a filter value.
414
Possible filter keys are:
416
- *state*, the state of the VPN_CONNECTION
417
pending,available,deleting,deleted
418
- *type*, the type of connection, currently 'ipsec.1'
419
- *customerGatewayId*, the ID of the customer gateway
420
associated with the VPN
421
- *vpnGatewayId*, the ID of the VPN gateway associated
422
with the VPN connection
425
:return: A list of :class:`boto.vpn_connection.vpnconnection.VpnConnection`
428
if vpn_connection_ids:
429
self.build_list_params(params, vpn_connection_ids, 'Vpn_ConnectionId')
432
for filter in filters:
433
params[('Filter.%d.Key' % i)] = filter[0]
434
params[('Filter.%d.Value.1')] = filter[1]
436
return self.get_list('DescribeVpnConnections', params, [('item', VpnConnection)])
438
def create_vpn_connection(self, type, customer_gateway_id, vpn_gateway_id):
440
Create a new VPN Connection.
443
:param type: The type of VPN Connection. Currently only 'ipsec.1'
446
:type customer_gateway_id: str
447
:param customer_gateway_id: The ID of the customer gateway.
449
:type vpn_gateway_id: str
450
:param vpn_gateway_id: The ID of the VPN gateway.
452
:rtype: The newly created VpnConnection
453
:return: A :class:`boto.vpc.vpnconnection.VpnConnection` object
455
params = {'Type' : type,
456
'CustomerGatewayId' : customer_gateway_id,
457
'VpnGatewayId' : vpn_gateway_id}
458
return self.get_object('CreateVpnConnection', params, VpnConnection)
460
def delete_vpn_connection(self, vpn_connection_id):
462
Delete a VPN Connection.
464
:type vpn_connection_id: str
465
:param vpn_connection_id: The ID of the vpn_connection to be deleted.
468
:return: True if successful
470
params = {'VpnConnectionId': vpn_connection_id}
471
return self.get_status('DeleteVpnConnection', params)