| 1 | package VM::EC2::REST::route_table; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use VM::EC2 ''; # important not to import anything! | ||
| 5 | package VM::EC2; # add methods to VM::EC2 | ||
| 6 | |||
| 7 | VM::EC2::Dispatch->register( | ||
| 8 | AssociateRouteTable => sub { shift->{associationId} }, | ||
| 9 | CreateRoute => 'boolean', | ||
| 10 | CreateRouteTable => 'fetch_one,routeTable,VM::EC2::VPC::RouteTable', | ||
| 11 | DeleteRoute => 'boolean', | ||
| 12 | DeleteRouteTable => 'boolean', | ||
| 13 | DescribeRouteTables => 'fetch_items,routeTableSet,VM::EC2::VPC::RouteTable', | ||
| 14 | DisassociateRouteTable => 'boolean', | ||
| 15 | ReplaceRoute => 'boolean', | ||
| 16 | ReplaceRouteTableAssociation => sub { shift->{newAssociationId} }, | ||
| 17 | ); | ||
| 18 | |||
| 19 | =head1 NAME VM::EC2::REST::route_table | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use VM::EC2 ':vpc'; | ||
| 24 | |||
| 25 | =head1 METHODS | ||
| 26 | |||
| 27 | These methods allow you to create and manipulate VPC route tables. | ||
| 28 | |||
| 29 | Implemented: | ||
| 30 | AssociateRouteTable | ||
| 31 | CreateRoute | ||
| 32 | CreateRouteTable | ||
| 33 | DeleteRoute | ||
| 34 | DeleteRouteTable | ||
| 35 | DescribeRouteTables | ||
| 36 | DisassociateRouteTable | ||
| 37 | ReplaceRoute | ||
| 38 | ReplaceRouteTableAssociation | ||
| 39 | |||
| 40 | Unimplemented: | ||
| 41 | (none) | ||
| 42 | |||
| 43 | =head2 $table = $ec2->create_route_table($vpc_id) | ||
| 44 | |||
| 45 | =head2 $table = $ec2->create_route_table(-vpc_id=>$id) | ||
| 46 | |||
| 47 | This method creates a new route table within the given VPC and returns | ||
| 48 | a VM::EC2::VPC::RouteTable object. By default, every route table | ||
| 49 | includes a local route that enables traffic to flow within the | ||
| 50 | VPC. You may add additional routes using create_route(). | ||
| 51 | |||
| 52 | This method can be called using a single argument corresponding to VPC | ||
| 53 | ID for the new route table, or with the named argument form. | ||
| 54 | |||
| 55 | Required arguments: | ||
| 56 | |||
| 57 | -vpc_id A VPC ID or previously-created VM::EC2::VPC object. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub create_route_table { | ||
| 62 | my $self = shift; | ||
| 63 | my %args = $self->args(-vpc_id => @_); | ||
| 64 | $args{-vpc_id} | ||
| 65 | or croak "Usage: create_route_table(-vpc_id=>\$id)"; | ||
| 66 | my @parm = $self->single_parm(VpcId => \%args); | ||
| 67 | return $self->call('CreateRouteTable',@parm); | ||
| 68 | } | ||
| 69 | |||
| 70 | =head2 $success = $ec2->delete_route_table($route_table_id) | ||
| 71 | |||
| 72 | =head2 $success = $ec2->delete_route_table(-route_table_id=>$id) | ||
| 73 | |||
| 74 | This method deletes the indicated route table and all the route | ||
| 75 | entries within it. It may not be called on the main route table, or if | ||
| 76 | the route table is currently associated with a subnet. | ||
| 77 | |||
| 78 | The method can be called with a single argument corresponding to the | ||
| 79 | route table's ID, or using the named form with argument -route_table_id. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub delete_route_table { | ||
| 84 | my $self = shift; | ||
| 85 | my %args = $self->args(-route_table_id=>@_); | ||
| 86 | my @parm = $self->single_parm(RouteTableId=>\%args); | ||
| 87 | return $self->call('DeleteRouteTable',@parm); | ||
| 88 | } | ||
| 89 | |||
| 90 | =head2 @tables = $ec2->describe_route_tables(@route_table_ids) | ||
| 91 | |||
| 92 | =head2 @tables = $ec2->describe_route_tables(\%filters) | ||
| 93 | |||
| 94 | =head2 @tables = $ec2->describe_route_tables(-route_table_id=> \@ids, | ||
| 95 | -filter => \%filters); | ||
| 96 | |||
| 97 | This method describes all or some of the route tables available to | ||
| 98 | you. You may use the filter to restrict the search to a particular | ||
| 99 | type of route table using one of the filters described at | ||
| 100 | http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeRouteTables.html. | ||
| 101 | |||
| 102 | Some of the commonly used filters are: | ||
| 103 | |||
| 104 | vpc-id ID of the VPC the route table is in. | ||
| 105 | association.subnet-id ID of the subnet the route table is | ||
| 106 | associated with. | ||
| 107 | route.state State of the route, either 'active' or 'blackhole' | ||
| 108 | tag:<key> Value of a tag | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub describe_route_tables { | ||
| 113 | my $self = shift; | ||
| 114 | my %args = $self->args(-route_table_id => @_); | ||
| 115 | my @parm = $self->list_parm('RouteTableId',\%args); | ||
| 116 | push @parm, $self->filter_parm(\%args); | ||
| 117 | return $self->call('DescribeRouteTables',@parm); | ||
| 118 | } | ||
| 119 | |||
| 120 | =head2 $associationId = $ec2->associate_route_table($subnet_id => $route_table_id) | ||
| 121 | |||
| 122 | =head2 $associationId = $ec2->associate_route_table(-subnet_id => $id, | ||
| 123 | -route_table_id => $id) | ||
| 124 | |||
| 125 | This method associates a route table with a subnet. Both objects must | ||
| 126 | be in the same VPC. You may use either string IDs, or | ||
| 127 | VM::EC2::VPC::RouteTable and VM::EC2::VPC::Subnet objects. | ||
| 128 | |||
| 129 | On success, an associationID is returned, which you may use to | ||
| 130 | disassociate the route table from the subnet later. The association ID | ||
| 131 | can also be found by searching through the VM::EC2::VPC::RouteTable | ||
| 132 | object. | ||
| 133 | |||
| 134 | Required arguments: | ||
| 135 | |||
| 136 | -subnet_id The subnet ID or a VM::EC2::VPC::Subnet object. | ||
| 137 | |||
| 138 | -route_table_id The route table ID or a VM::EC2::VPC::RouteTable object. | ||
| 139 | |||
| 140 | It may be more convenient to call the | ||
| 141 | VM::EC2::VPC::Subnet->associate_route_table() or | ||
| 142 | VM::EC2::VPC::RouteTable->associate_subnet() methods, which are front | ||
| 143 | ends to this method. | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub associate_route_table { | ||
| 148 | my $self = shift; | ||
| 149 | my %args; | ||
| 150 | if ($_[0] !~ /^-/ && @_ == 2) { | ||
| 151 | @args{qw(-subnet_id -route_table_id)} = @_; | ||
| 152 | } else { | ||
| 153 | %args = @_; | ||
| 154 | } | ||
| 155 | $args{-subnet_id} && $args{-route_table_id} | ||
| 156 | or croak "-subnet_id, and -route_table_id arguments required"; | ||
| 157 | my @param = ($self->single_parm(SubnetId=>\%args), | ||
| 158 | $self->single_parm(RouteTableId=>\%args)); | ||
| 159 | return $self->call('AssociateRouteTable',@param); | ||
| 160 | } | ||
| 161 | |||
| 162 | =head2 $success = $ec2->dissociate_route_table($association_id) | ||
| 163 | |||
| 164 | =head2 $success = $ec2->dissociate_route_table(-association_id => $id) | ||
| 165 | |||
| 166 | This method disassociates a route table from a subnet. You must | ||
| 167 | provide the association ID (either returned from | ||
| 168 | associate_route_table() or found among the associations() of a | ||
| 169 | RouteTable object). You may use the short single-argument form, or the | ||
| 170 | longer named argument form with the required argument -association_id. | ||
| 171 | |||
| 172 | The method returns true on success. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub disassociate_route_table { | ||
| 177 | my $self = shift; | ||
| 178 | my %args = $self->args('-association_id',@_); | ||
| 179 | my @params = $self->single_parm('AssociationId',\%args); | ||
| 180 | return $self->call('DisassociateRouteTable',@params); | ||
| 181 | } | ||
| 182 | |||
| 183 | =head2 $new_association = $ec2->replace_route_table_association($association_id=>$route_table_id) | ||
| 184 | |||
| 185 | |||
| 186 | =head2 $new_association = $ec2->replace_route_table_association(-association_id => $id, | ||
| 187 | -route_table_id => $id) | ||
| 188 | |||
| 189 | This method changes the route table associated with a given | ||
| 190 | subnet. You must pass the replacement route table ID and the | ||
| 191 | association ID. To replace the main route table, use its association | ||
| 192 | ID and the ID of the route table you wish to replace it with. | ||
| 193 | |||
| 194 | On success, a new associationID is returned. | ||
| 195 | |||
| 196 | Required arguments: | ||
| 197 | |||
| 198 | -association_id The association ID | ||
| 199 | |||
| 200 | -route_table_id The route table ID or a M::EC2::VPC::RouteTable object. | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | sub replace_route_table_association { | ||
| 205 | my $self = shift; | ||
| 206 | my %args; | ||
| 207 | if ($_[0] !~ /^-/ && @_ == 2) { | ||
| 208 | @args{qw(-association_id -route_table_id)} = @_; | ||
| 209 | } else { | ||
| 210 | %args = @_; | ||
| 211 | } | ||
| 212 | $args{-association_id} && $args{-route_table_id} | ||
| 213 | or croak "-association_id, and -route_table_id arguments required"; | ||
| 214 | my @param = $self->single_parm(AssociationId => \%args), | ||
| 215 | $self->single_parm(RouteTableId => \%args); | ||
| 216 | return $self->call('ReplaceRouteTableAssociation',@param); | ||
| 217 | } | ||
| 218 | |||
| 219 | =head2 $success = $ec2->create_route($route_table_id,$destination,$target) | ||
| 220 | |||
| 221 | =head2 $success = $ec2->create_route(-route_table_id => $id, | ||
| 222 | -destination_cidr_block => $block, | ||
| 223 | -target=>$target) | ||
| 224 | |||
| 225 | This method creates a routing rule in a route table within a VPC. It | ||
| 226 | takes three mandatory arguments consisting of the route table, the | ||
| 227 | CIDR address block to match packet destinations against, and a target | ||
| 228 | to route matching packets to. The target may be an internet gateway, a | ||
| 229 | NAT instance, or a network interface ID. | ||
| 230 | |||
| 231 | Network packets are routed by matching their destination addresses | ||
| 232 | against a CIDR block. For example, 0.0.0.0/0 matches all addresses, | ||
| 233 | while 10.0.1.0/24 matches 10.0.1.* addresses. When a packet matches | ||
| 234 | more than one rule, the most specific matching routing rule is chosen. | ||
| 235 | |||
| 236 | In the named argument form, the following arguments are recognized: | ||
| 237 | |||
| 238 | -route_table_id The ID of a route table, or a VM::EC2::VPC::RouteTable | ||
| 239 | object. | ||
| 240 | |||
| 241 | -destination_cidr_block | ||
| 242 | The CIDR address block to match against packet destinations. | ||
| 243 | |||
| 244 | -destination A shorthand version of -destination_cidr_block. | ||
| 245 | |||
| 246 | -target The destination of matching packets. See below for valid | ||
| 247 | targets. | ||
| 248 | |||
| 249 | The -target value can be any one of the following: | ||
| 250 | |||
| 251 | 1. A VM::EC2::VPC::InternetGateway object, or an internet gateway ID matching | ||
| 252 | the regex /^igw-[0-9a-f]{8}$/ | ||
| 253 | |||
| 254 | 2. A VM::EC2::Instance object, or an instance ID matching the regex | ||
| 255 | /^i-[0-9a-f]{8}$/. | ||
| 256 | |||
| 257 | 3. A VM::EC2::NetworkInterface object, or a network interface ID | ||
| 258 | matching the regex /^eni-[0-9a-f]{8}$/. | ||
| 259 | |||
| 260 | 4. A VM::EC2::VPC::PeeringConnection object, or a VPC peering connection ID | ||
| 261 | matching the regex /^pcx-[0-9a-f]{8}$/. | ||
| 262 | |||
| 263 | On success, this method returns true. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub create_route { | ||
| 268 | my $self = shift; | ||
| 269 | return $self->_manipulate_route('CreateRoute',@_); | ||
| 270 | } | ||
| 271 | |||
| 272 | =head2 $success = $ec2->delete_route($route_table_id,$destination_block) | ||
| 273 | |||
| 274 | This method deletes a route in the specified routing table. The | ||
| 275 | destination CIDR block is used to indicate which route to delete. On | ||
| 276 | success, the method returns true. | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | sub delete_route { | ||
| 281 | my $self = shift; | ||
| 282 | @_ == 2 or croak "Usage: delete_route(\$route_table_id,\$destination_block)"; | ||
| 283 | my %args; | ||
| 284 | @args{qw(-route_table_id -destination_cidr_block)} = @_; | ||
| 285 | my @parm = map {$self->single_parm($_,\%args)} qw(RouteTableId DestinationCidrBlock); | ||
| 286 | return $self->call('DeleteRoute',@parm); | ||
| 287 | } | ||
| 288 | |||
| 289 | =head2 $success = $ec2->replace_route($route_table_id,$destination,$target) | ||
| 290 | |||
| 291 | =head2 $success = $ec2->replace_route(-route_table_id => $id, | ||
| 292 | -destination_cidr_block => $block, | ||
| 293 | -target=>$target) | ||
| 294 | |||
| 295 | This method replaces an existing routing rule in a route table within | ||
| 296 | a VPC. It takes three mandatory arguments consisting of the route | ||
| 297 | table, the CIDR address block to match packet destinations against, | ||
| 298 | and a target to route matching packets to. The target may be an | ||
| 299 | internet gateway, a NAT instance, or a network interface ID. | ||
| 300 | |||
| 301 | Network packets are routed by matching their destination addresses | ||
| 302 | against a CIDR block. For example, 0.0.0.0/0 matches all addresses, | ||
| 303 | while 10.0.1.0/24 matches 10.0.1.* addresses. When a packet matches | ||
| 304 | more than one rule, the most specific matching routing rule is chosen. | ||
| 305 | |||
| 306 | In the named argument form, the following arguments are recognized: | ||
| 307 | |||
| 308 | -route_table_id The ID of a route table, or a VM::EC2::VPC::RouteTable | ||
| 309 | object. | ||
| 310 | |||
| 311 | -destination_cidr_block | ||
| 312 | The CIDR address block to match against packet destinations. | ||
| 313 | |||
| 314 | -destination A shorthand version of -destination_cidr_block. | ||
| 315 | |||
| 316 | -target The destination of matching packets. See below for valid | ||
| 317 | targets. | ||
| 318 | |||
| 319 | The -target value can be any one of the following: | ||
| 320 | |||
| 321 | 1. A VM::EC2::VPC::InternetGateway object, or an internet gateway ID matching | ||
| 322 | the regex /^igw-[0-9a-f]{8}$/ | ||
| 323 | |||
| 324 | 2. A VM::EC2::Instance object, or an instance ID matching the regex | ||
| 325 | /^i-[0-9a-f]{8}$/. | ||
| 326 | |||
| 327 | 3. A VM::EC2::NetworkInterface object, or a network interface ID | ||
| 328 | matching the regex /^eni-[0-9a-f]{8}$/. | ||
| 329 | |||
| 330 | 4. A VM::EC2::VPC::PeeringConnection object, or a VPC peering connection ID | ||
| 331 | matching the regex /^pcx-[0-9a-f]{8}$/. | ||
| 332 | |||
| 333 | On success, this method returns true. | ||
| 334 | |||
| 335 | =cut | ||
| 336 | |||
| 337 | sub replace_route { | ||
| 338 | my $self = shift; | ||
| 339 | return $self->_manipulate_route('ReplaceRoute',@_); | ||
| 340 | } | ||
| 341 | |||
| 342 | sub _manipulate_route { | ||
| 343 | my $self = shift; | ||
| 344 | my $api_call = shift; | ||
| 345 | |||
| 346 | my %args; | ||
| 347 | if ($_[0] !~ /^-/ && @_ == 3) { | ||
| 348 | @args{qw(-route_table_id -destination -target)} = @_; | ||
| 349 | } else { | ||
| 350 | %args = @_; | ||
| 351 | } | ||
| 352 | |||
| 353 | $args{-destination_cidr_block} ||= $args{-destination}; | ||
| 354 | $args{-destination_cidr_block} && $args{-route_table_id} && $args{-target} | ||
| 355 | or croak "-route_table_id, -destination_cidr_block, and -target arguments required"; | ||
| 356 | |||
| 357 | # figure out what the target is. | ||
| 358 | $args{-gateway_id} = $args{-target} if eval{$args{-target}->isa('VM::EC2::VPC::InternetGateway')} | ||
| 359 | || $args{-target} =~ /^igw-[0-9a-f]{8}$/; | ||
| 360 | $args{-instance_id} = $args{-target} if eval{$args{-target}->isa('VM::EC2::Instance')} | ||
| 361 | || $args{-target} =~ /^i-[0-9a-f]{8}$/; | ||
| 362 | $args{-network_interface_id} = $args{-target} if eval{$args{-target}->isa('VM::EC2::NetworkInterface')} | ||
| 363 | || $args{-target} =~ /^eni-[0-9a-f]{8}$/; | ||
| 364 | $args{-vpc_peering_connection_id} = $args{-target} if eval{$args{-target}->isa('VM::EC2::VPC::PeeringConnection')} | ||
| 365 | || $args{-target} =~ /^pcx-[0-9a-f]{8}$/; | ||
| 366 | |||
| 367 | |||
| 368 | my @parm = map {$self->single_parm($_,\%args)} | ||
| 369 | qw(RouteTableId DestinationCidrBlock GatewayId InstanceId | ||
| 370 | NetworkInterfaceId VpcPeeringConnectionId); | ||
| 371 | |||
| 372 | return $self->call($api_call,@parm); | ||
| 373 | } | ||
| 374 | |||
| 375 | |||
| 376 | |||
| 377 | =head1 SEE ALSO | ||
| 378 | |||
| 379 | L<VM::EC2> | ||
| 380 | |||
| 381 | =head1 AUTHOR | ||
| 382 | |||
| 383 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 384 | |||
| 385 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 386 | |||
| 387 | This package and its accompanying libraries is free software; you can | ||
| 388 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 389 | version 1, or at your option, any later version) or the Artistic | ||
| 390 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 391 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 392 | |||
| 393 | =cut | ||
| 394 | |||
| 395 | |||
| 396 | 1; |