~cbehrens/nova/lp844160-build-works-with-zones

« back to all changes in this revision

Viewing changes to vendor/boto/boto/vpc/__init__.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2009 Mitch Garnaat http://garnaat.org/
 
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
"""
 
23
Represents a connection to the EC2 service.
 
24
"""
 
25
 
 
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
 
33
 
 
34
class VPCConnection(EC2Connection):
 
35
 
 
36
    # VPC methods
 
37
        
 
38
    def get_all_vpcs(self, vpc_ids=None, filters=None):
 
39
        """
 
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
 
43
        are returned.
 
44
        
 
45
        :type vpc_ids: list
 
46
        :param vpc_ids: A list of strings with the desired VPC ID's
 
47
        
 
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:
 
52
                        
 
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
 
56
 
 
57
        :rtype: list
 
58
        :return: A list of :class:`boto.vpc.vpc.VPC`
 
59
        """
 
60
        params = {}
 
61
        if vpc_ids:
 
62
            self.build_list_params(params, vpc_ids, 'VpcId')
 
63
        if filters:
 
64
            i = 1
 
65
            for filter in filters:
 
66
                params[('Filter.%d.Key' % i)] = filter[0]
 
67
                params[('Filter.%d.Value.1')] = filter[1]
 
68
                i += 1
 
69
        return self.get_list('DescribeVpcs', params, [('item', VPC)])
 
70
 
 
71
    def create_vpc(self, cidr_block):
 
72
        """
 
73
        Create a new Virtual Private Cloud.
 
74
 
 
75
        :type cidr_block: str
 
76
        :param cidr_block: A valid CIDR block
 
77
 
 
78
        :rtype: The newly created VPC
 
79
        :return: A :class:`boto.vpc.vpc.VPC` object
 
80
        """
 
81
        params = {'CidrBlock' : cidr_block}
 
82
        return self.get_object('CreateVpc', params, VPC)
 
83
        
 
84
    def delete_vpc(self, vpc_id):
 
85
        """
 
86
        Delete a Virtual Private Cloud.
 
87
 
 
88
        :type vpc_id: str
 
89
        :param vpc_id: The ID of the vpc to be deleted.
 
90
 
 
91
        :rtype: bool
 
92
        :return: True if successful
 
93
        """
 
94
        params = {'VpcId': vpc_id}
 
95
        return self.get_status('DeleteVpc', params)
 
96
 
 
97
    # Customer Gateways
 
98
 
 
99
    def get_all_customer_gateways(self, customer_gateway_ids=None, filters=None):
 
100
        """
 
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
 
104
        are returned.
 
105
        
 
106
        :type customer_gateway_ids: list
 
107
        :param customer_gateway_ids: A list of strings with the desired CustomerGateway ID's
 
108
        
 
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:
 
113
                        
 
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
 
119
 
 
120
        :rtype: list
 
121
        :return: A list of :class:`boto.vpc.customergateway.CustomerGateway`
 
122
        """
 
123
        params = {}
 
124
        if customer_gateway_ids:
 
125
            self.build_list_params(params, customer_gateway_ids, 'CustomerGatewayId')
 
126
        if filters:
 
127
            i = 1
 
128
            for filter in filters:
 
129
                params[('Filter.%d.Key' % i)] = filter[0]
 
130
                params[('Filter.%d.Value.1')] = filter[1]
 
131
                i += 1
 
132
        return self.get_list('DescribeCustomerGateways', params, [('item', CustomerGateway)])
 
133
 
 
134
    def create_customer_gateway(self, type, ip_address, bgp_asn):
 
135
        """
 
136
        Create a new Customer Gateway
 
137
 
 
138
        :type type: str
 
139
        :param type: Type of VPN Connection.  Only valid valid currently is 'ipsec.1'
 
140
 
 
141
        :type ip_address: str
 
142
        :param ip_address: Internet-routable IP address for customer's gateway.
 
143
                           Must be a static address.
 
144
 
 
145
        :type bgp_asn: str
 
146
        :param bgp_asn: Customer gateway's Border Gateway Protocol (BGP)
 
147
                        Autonomous System Number (ASN)
 
148
 
 
149
        :rtype: The newly created CustomerGateway
 
150
        :return: A :class:`boto.vpc.customergateway.CustomerGateway` object
 
151
        """
 
152
        params = {'Type' : type,
 
153
                  'IpAddress' : ip_address,
 
154
                  'BgpAsn' : bgp_asn}
 
155
        return self.get_object('CreateCustomerGateway', params, CustomerGateway)
 
156
        
 
157
    def delete_customer_gateway(self, customer_gateway_id):
 
158
        """
 
159
        Delete a Customer Gateway.
 
160
 
 
161
        :type customer_gateway_id: str
 
162
        :param customer_gateway_id: The ID of the customer_gateway to be deleted.
 
163
 
 
164
        :rtype: bool
 
165
        :return: True if successful
 
166
        """
 
167
        params = {'CustomerGatewayId': customer_gateway_id}
 
168
        return self.get_status('DeleteCustomerGateway', params)
 
169
 
 
170
    # VPN Gateways
 
171
 
 
172
    def get_all_vpn_gateways(self, vpn_gateway_ids=None, filters=None):
 
173
        """
 
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
 
177
        are returned.
 
178
 
 
179
        :type vpn_gateway_ids: list
 
180
        :param vpn_gateway_ids: A list of strings with the desired VpnGateway ID's
 
181
        
 
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:
 
186
                        
 
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
 
191
                          VPN gateway is in.
 
192
 
 
193
        :rtype: list
 
194
        :return: A list of :class:`boto.vpc.customergateway.VpnGateway`
 
195
        """
 
196
        params = {}
 
197
        if vpn_gateway_ids:
 
198
            self.build_list_params(params, vpn_gateway_ids, 'VpnGatewayId')
 
199
        if filters:
 
200
            i = 1
 
201
            for filter in filters:
 
202
                params[('Filter.%d.Key' % i)] = filter[0]
 
203
                params[('Filter.%d.Value.1')] = filter[1]
 
204
                i += 1
 
205
        return self.get_list('DescribeVpnGateways', params, [('item', VpnGateway)])
 
206
 
 
207
    def create_vpn_gateway(self, type, availability_zone=None):
 
208
        """
 
209
        Create a new Vpn Gateway
 
210
 
 
211
        :type type: str
 
212
        :param type: Type of VPN Connection.  Only valid valid currently is 'ipsec.1'
 
213
 
 
214
        :type availability_zone: str
 
215
        :param availability_zone: The Availability Zone where you want the VPN gateway.
 
216
 
 
217
        :rtype: The newly created VpnGateway
 
218
        :return: A :class:`boto.vpc.vpngateway.VpnGateway` object
 
219
        """
 
220
        params = {'Type' : type}
 
221
        if availability_zone:
 
222
            params['AvailabilityZone'] = availability_zone
 
223
        return self.get_object('CreateVpnGateway', params, VpnGateway)
 
224
        
 
225
    def delete_vpn_gateway(self, vpn_gateway_id):
 
226
        """
 
227
        Delete a Vpn Gateway.
 
228
 
 
229
        :type vpn_gateway_id: str
 
230
        :param vpn_gateway_id: The ID of the vpn_gateway to be deleted.
 
231
 
 
232
        :rtype: bool
 
233
        :return: True if successful
 
234
        """
 
235
        params = {'VpnGatewayId': vpn_gateway_id}
 
236
        return self.get_status('DeleteVpnGateway', params)
 
237
 
 
238
    def attach_vpn_gateway(self, vpn_gateway_id, vpc_id):
 
239
        """
 
240
        Attaches a VPN gateway to a VPC.
 
241
 
 
242
        :type vpn_gateway_id: str
 
243
        :param vpn_gateway_id: The ID of the vpn_gateway to attach
 
244
 
 
245
        :type vpc_id: str
 
246
        :param vpc_id: The ID of the VPC you want to attach the gateway to.
 
247
 
 
248
        :rtype: An attachment
 
249
        :return: a :class:`boto.vpc.vpngateway.Attachment`
 
250
        """
 
251
        params = {'VpnGatewayId': vpn_gateway_id,
 
252
                  'VpcId' : vpc_id}
 
253
        return self.get_object('AttachVpnGateway', params, Attachment)
 
254
 
 
255
    # Subnets
 
256
 
 
257
    def get_all_subnets(self, subnet_ids=None, filters=None):
 
258
        """
 
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
 
262
        are returned.
 
263
        
 
264
        :type subnet_ids: list
 
265
        :param subnet_ids: A list of strings with the desired Subnet ID's
 
266
        
 
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:
 
271
                        
 
272
                        - *state*, the state of the Subnet
 
273
                          (pending,available)
 
274
                        - *vpdId*, the ID of teh VPC the subnet is in.
 
275
                        - *cidrBlock*, CIDR block of the subnet
 
276
                        - *availabilityZone*, the Availability Zone
 
277
                          the subnet is in.
 
278
 
 
279
 
 
280
        :rtype: list
 
281
        :return: A list of :class:`boto.vpc.subnet.Subnet`
 
282
        """
 
283
        params = {}
 
284
        if subnet_ids:
 
285
            self.build_list_params(params, subnet_ids, 'SubnetId')
 
286
        if filters:
 
287
            i = 1
 
288
            for filter in filters:
 
289
                params[('Filter.%d.Key' % i)] = filter[0]
 
290
                params[('Filter.%d.Value.1')] = filter[1]
 
291
                i += 1
 
292
        return self.get_list('DescribeSubnets', params, [('item', Subnet)])
 
293
 
 
294
    def create_subnet(self, vpc_id, cidr_block, availability_zone=None):
 
295
        """
 
296
        Create a new Subnet
 
297
 
 
298
        :type vpc_id: str
 
299
        :param vpc_id: The ID of the VPC where you want to create the subnet.
 
300
 
 
301
        :type cidr_block: str
 
302
        :param cidr_block: The CIDR block you want the subnet to cover.
 
303
 
 
304
        :type availability_zone: str
 
305
        :param availability_zone: The AZ you want the subnet in
 
306
 
 
307
        :rtype: The newly created Subnet
 
308
        :return: A :class:`boto.vpc.customergateway.Subnet` object
 
309
        """
 
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)
 
315
        
 
316
    def delete_subnet(self, subnet_id):
 
317
        """
 
318
        Delete a subnet.
 
319
 
 
320
        :type subnet_id: str
 
321
        :param subnet_id: The ID of the subnet to be deleted.
 
322
 
 
323
        :rtype: bool
 
324
        :return: True if successful
 
325
        """
 
326
        params = {'SubnetId': subnet_id}
 
327
        return self.get_status('DeleteSubnet', params)
 
328
 
 
329
    
 
330
    # DHCP Options
 
331
 
 
332
    def get_all_dhcp_options(self, dhcp_options_ids=None):
 
333
        """
 
334
        Retrieve information about your DhcpOptions.
 
335
        
 
336
        :type dhcp_options_ids: list
 
337
        :param dhcp_options_ids: A list of strings with the desired DhcpOption ID's
 
338
        
 
339
        :rtype: list
 
340
        :return: A list of :class:`boto.vpc.dhcpoptions.DhcpOptions`
 
341
        """
 
342
        params = {}
 
343
        if dhcp_options_ids:
 
344
            self.build_list_params(params, dhcp_options_ids, 'DhcpOptionsId')
 
345
        return self.get_list('DescribeDhcpOptions', params, [('item', DhcpOptions)])
 
346
 
 
347
    def create_dhcp_options(self, vpc_id, cidr_block, availability_zone=None):
 
348
        """
 
349
        Create a new DhcpOption
 
350
 
 
351
        :type vpc_id: str
 
352
        :param vpc_id: The ID of the VPC where you want to create the subnet.
 
353
 
 
354
        :type cidr_block: str
 
355
        :param cidr_block: The CIDR block you want the subnet to cover.
 
356
 
 
357
        :type availability_zone: str
 
358
        :param availability_zone: The AZ you want the subnet in
 
359
 
 
360
        :rtype: The newly created DhcpOption
 
361
        :return: A :class:`boto.vpc.customergateway.DhcpOption` object
 
362
        """
 
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)
 
368
        
 
369
    def delete_dhcp_options(self, dhcp_options_id):
 
370
        """
 
371
        Delete a DHCP Options
 
372
 
 
373
        :type dhcp_options_id: str
 
374
        :param dhcp_options_id: The ID of the DHCP Options to be deleted.
 
375
 
 
376
        :rtype: bool
 
377
        :return: True if successful
 
378
        """
 
379
        params = {'DhcpOptionsId': dhcp_options_id}
 
380
        return self.get_status('DeleteDhcpOptions', params)
 
381
 
 
382
    def associate_dhcp_options(self, dhcp_options_id, vpc_id):
 
383
        """
 
384
        Associate a set of Dhcp Options with a VPC.
 
385
        
 
386
        :type dhcp_options_id: str
 
387
        :param dhcp_options_id: The ID of the Dhcp Options
 
388
        
 
389
        :type vpc_id: str
 
390
        :param vpc_id: The ID of the VPC.
 
391
        
 
392
        :rtype: bool
 
393
        :return: True if successful
 
394
        """
 
395
        params = {'DhcpOptionsId': dhcp_options_id,
 
396
                  'VpcId' : vpc_id}
 
397
        return self.get_status('AssociateDhcpOptions', params)
 
398
 
 
399
    # VPN Connection
 
400
 
 
401
    def get_all_vpn_connections(self, vpn_connection_ids=None, filters=None):
 
402
        """
 
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
 
406
        are returned.
 
407
        
 
408
        :type vpn_connection_ids: list
 
409
        :param vpn_connection_ids: A list of strings with the desired VPN_CONNECTION ID's
 
410
        
 
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:
 
415
                        
 
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
 
423
 
 
424
        :rtype: list
 
425
        :return: A list of :class:`boto.vpn_connection.vpnconnection.VpnConnection`
 
426
        """
 
427
        params = {}
 
428
        if vpn_connection_ids:
 
429
            self.build_list_params(params, vpn_connection_ids, 'Vpn_ConnectionId')
 
430
        if filters:
 
431
            i = 1
 
432
            for filter in filters:
 
433
                params[('Filter.%d.Key' % i)] = filter[0]
 
434
                params[('Filter.%d.Value.1')] = filter[1]
 
435
                i += 1
 
436
        return self.get_list('DescribeVpnConnections', params, [('item', VpnConnection)])
 
437
 
 
438
    def create_vpn_connection(self, type, customer_gateway_id, vpn_gateway_id):
 
439
        """
 
440
        Create a new VPN Connection.
 
441
 
 
442
        :type type: str
 
443
        :param type: The type of VPN Connection.  Currently only 'ipsec.1'
 
444
                     is supported
 
445
 
 
446
        :type customer_gateway_id: str
 
447
        :param customer_gateway_id: The ID of the customer gateway.
 
448
 
 
449
        :type vpn_gateway_id: str
 
450
        :param vpn_gateway_id: The ID of the VPN gateway.
 
451
 
 
452
        :rtype: The newly created VpnConnection
 
453
        :return: A :class:`boto.vpc.vpnconnection.VpnConnection` object
 
454
        """
 
455
        params = {'Type' : type,
 
456
                  'CustomerGatewayId' : customer_gateway_id,
 
457
                  'VpnGatewayId' : vpn_gateway_id}
 
458
        return self.get_object('CreateVpnConnection', params, VpnConnection)
 
459
        
 
460
    def delete_vpn_connection(self, vpn_connection_id):
 
461
        """
 
462
        Delete a VPN Connection.
 
463
 
 
464
        :type vpn_connection_id: str
 
465
        :param vpn_connection_id: The ID of the vpn_connection to be deleted.
 
466
 
 
467
        :rtype: bool
 
468
        :return: True if successful
 
469
        """
 
470
        params = {'VpnConnectionId': vpn_connection_id}
 
471
        return self.get_status('DeleteVpnConnection', params)
 
472
 
 
473