« blib/lib/VM/EC2/SecurityGroup/GroupPermission.pm
↑ Summary
blib/lib/VM/EC2/REST/internet_gateway.pm »
| 1 | package VM::EC2::VPC::RouteTable; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::VPC::RouteTable -- A VPC route table | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use VM::EC2; | ||
| 10 | my $ec2 = VM::EC2->new(...); | ||
| 11 | my @tables = $ec2->describe_route_tables; | ||
| 12 | |||
| 13 | for my $rt (@tables) { | ||
| 14 | print $rt->routeTableId,"\n", | ||
| 15 | $rt->vpcId,"\n"; | ||
| 16 | my @routes = $rt->routes; | ||
| 17 | my @associations = $rt->associations; | ||
| 18 | } | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | This object supports the EC2 Virtual Private Cloud route table | ||
| 23 | interface, and is used to control the routing of packets within and | ||
| 24 | between subnets. | ||
| 25 | |||
| 26 | =head1 METHODS | ||
| 27 | |||
| 28 | These object methods are supported: | ||
| 29 | |||
| 30 | routeTableId -- the ID of the route table | ||
| 31 | vpcId -- The ID of the VPC the route table is in. | ||
| 32 | routes -- An array of VM::EC2::VPC::Route objects, | ||
| 33 | each describing a routing rule in the | ||
| 34 | table. | ||
| 35 | associations -- An array of VM::EC2::RouteTable::Association | ||
| 36 | objects, each describing the association | ||
| 37 | between the route table and a subnet. | ||
| 38 | |||
| 39 | This class supports the VM::EC2 tagging interface. See | ||
| 40 | L<VM::EC2::Generic> for information. | ||
| 41 | |||
| 42 | In addition, this object supports the following convenience methods: | ||
| 43 | |||
| 44 | vpc -- The VPC object for this route table. | ||
| 45 | main -- Returns true if this is the VPC's current "main" | ||
| 46 | route table | ||
| 47 | associate($subnet) -- Associate the route table with a subnet ID or object. | ||
| 48 | disassociate($subnet) -- Disassociate the route table with a subnet ID or object. | ||
| 49 | refresh -- Refreshes the object from its current state in EC2. | ||
| 50 | create_route($dest=>$target) -- Create a route in the route table | ||
| 51 | delete_route($dest) -- Delete a route in the route table | ||
| 52 | replace_route($dest=>$target) -- Replace a route in the route table | ||
| 53 | |||
| 54 | =head1 STRING OVERLOADING | ||
| 55 | |||
| 56 | When used in a string context, this object will be interpolated as the | ||
| 57 | route table ID. | ||
| 58 | |||
| 59 | =head1 SEE ALSO | ||
| 60 | |||
| 61 | L<VM::EC2> | ||
| 62 | L<VM::EC2::Generic> | ||
| 63 | |||
| 64 | =head1 AUTHOR | ||
| 65 | |||
| 66 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 67 | |||
| 68 | Copyright (c) 2012 Ontario Institute for Cancer Research | ||
| 69 | |||
| 70 | This package and its accompanying libraries is free software; you can | ||
| 71 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 72 | version 1, or at your option, any later version) or the Artistic | ||
| 73 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 74 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | use strict; | ||
| 79 | use Carp 'croak'; | ||
| 80 | use base 'VM::EC2::Generic'; | ||
| 81 | use VM::EC2::VPC::Route; | ||
| 82 | use VM::EC2::VPC::RouteTable::Association; | ||
| 83 | |||
| 84 | sub valid_fields { | ||
| 85 | my $self = shift; | ||
| 86 | return qw(routeTableId vpcId routeSet associationSet); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub primary_id { shift->routeTableId } | ||
| 90 | |||
| 91 | sub vpc { | ||
| 92 | my $self = shift; | ||
| 93 | return $self->aws->describe_vpcs($self->vpcId); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub routes { | ||
| 97 | my $self = shift; | ||
| 98 | my $set = $self->routeSet or return; | ||
| 99 | return map {VM::EC2::VPC::Route->new($_,$self->aws)} @{$set->{item}}; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub main { | ||
| 103 | my $self = shift; | ||
| 104 | my @a = grep {$_->main} $self->associations; | ||
| 105 | return scalar @a; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub associations { | ||
| 109 | my $self = shift; | ||
| 110 | my $set = $self->associationSet or return; | ||
| 111 | return map {VM::EC2::VPC::RouteTable::Association->new($_,$self->aws)} @{$set->{item}}; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub associate { | ||
| 115 | my $self = shift; | ||
| 116 | my $subnet = shift; | ||
| 117 | $self->aws->associate_route_table($subnet=>$self); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub disassociate { | ||
| 121 | my $self = shift; | ||
| 122 | my $subnet = shift; | ||
| 123 | my @associations = $self->associations; | ||
| 124 | my ($ass) = grep {$_->subnetId eq $subnet} @associations; | ||
| 125 | return unless $ass; | ||
| 126 | $self->aws->disassociate_route_table($ass->routeTableAssociationId); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub refresh { | ||
| 130 | my $self = shift; | ||
| 131 | my $i = shift; | ||
| 132 | local $self->aws->{raise_error} = 1; | ||
| 133 | ($i) = $self->aws->describe_subnets($self->subnetId) unless $i; | ||
| 134 | %$self = %$i if $i; | ||
| 135 | return defined $i; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub create_route { | ||
| 139 | my $self = shift; | ||
| 140 | return $self->aws->create_route($self->routeTableId, @_); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub replace_route { | ||
| 144 | my $self = shift; | ||
| 145 | return $self->aws->replace_route($self->routeTableId, @_); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub delete_route { | ||
| 149 | my $self = shift; | ||
| 150 | return $self->aws->delete_route($self->routeTableId, @_); | ||
| 151 | } | ||
| 152 | |||
| 153 | 1; | ||
| 154 |