| 1 | package VM::EC2::VPC::Subnet; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::VPC::Subnet -- A VPC subnet | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use VM::EC2; | ||
| 10 | my $ec2 = VM::EC2->new(...); | ||
| 11 | my $vpc = $ec2->create_vpc('10.0.0.0/16'); | ||
| 12 | my $subnet = $vpc->create_subnet('10.0.0.0/24') or die $vpc->error_str; | ||
| 13 | @subnets = $ec2->describe_subnets; | ||
| 14 | |||
| 15 | for my $sn (@subnets) { | ||
| 16 | print $sn->subnetId,"\n", | ||
| 17 | $sn->state,"\n", | ||
| 18 | $sn->vpcId,"\n", | ||
| 19 | $sn->cidrBlock,"\n", | ||
| 20 | $sn->availableIpAddressCount,"\n", | ||
| 21 | $sn->availabilityZone,"\n"; | ||
| 22 | } | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This object supports the EC2 Virtual Private Cloud subnet | ||
| 27 | interface. Please see L<VM::EC2::Generic> for methods shared by all | ||
| 28 | VM::EC2 objects. | ||
| 29 | |||
| 30 | =head1 METHODS | ||
| 31 | |||
| 32 | These object methods are supported: | ||
| 33 | |||
| 34 | subnetId -- the ID of the subnet | ||
| 35 | state -- The current state of the subnet, either "pending" or "available" | ||
| 36 | vpcId -- The ID of the VPC the subnet is in. | ||
| 37 | cidrBlock -- The CIDR block assigned to the subnet. | ||
| 38 | availableIpAddressCount -- The number of unused IP addresses in the subnet. | ||
| 39 | availableZone -- This subnet's availability zone. | ||
| 40 | defaultForAz -- Indicates if this is the default subnet for the Availability Zone | ||
| 41 | mapPublicIpOnLaunch -- Indicates if instances launched in this subnet automatically receive a | ||
| 42 | public IP address | ||
| 43 | |||
| 44 | This class supports the VM::EC2 tagging interface. See | ||
| 45 | L<VM::EC2::Generic> for information. | ||
| 46 | |||
| 47 | In addition, this object supports the following convenience methods: | ||
| 48 | |||
| 49 | vpc() -- Return the associated VM::EC2::VPC object. | ||
| 50 | zone() -- Return the associated VM::EC2::AvailabilityZone object. | ||
| 51 | refresh() -- Refreshes the object from its current state in EC2. | ||
| 52 | current_state() -- Refreshes the object and returns its current state. | ||
| 53 | create_route_table() -- Create a new route table, associates it with this subnet, and | ||
| 54 | returns the corresponding VM::EC2::VPC::RouteTable | ||
| 55 | object. | ||
| 56 | associate_route_table($table) | ||
| 57 | -- Associates a route table with this subnet, returning true if | ||
| 58 | sucessful. | ||
| 59 | disassociate_route_table($table) | ||
| 60 | -- Removes the association of a route table with this subnet. Produces | ||
| 61 | a fatal error if $table is not associated with the subnet. Returns true | ||
| 62 | on success. | ||
| 63 | associate_network_acl($network_acl_id) | ||
| 64 | -- Associates a network ACL with this subnet, returning the new | ||
| 65 | association ID on success. | ||
| 66 | disassociate_network_acl() | ||
| 67 | -- Removes the association of a network ACL with this subnet. The subnet | ||
| 68 | will then be associated with the default network ACL. Returns the | ||
| 69 | the association ID. | ||
| 70 | |||
| 71 | =head1 STRING OVERLOADING | ||
| 72 | |||
| 73 | When used in a string context, this object will be interpolated as the | ||
| 74 | subnet ID. | ||
| 75 | |||
| 76 | =head1 SEE ALSO | ||
| 77 | |||
| 78 | L<VM::EC2> | ||
| 79 | L<VM::EC2::Generic> | ||
| 80 | |||
| 81 | =head1 AUTHOR | ||
| 82 | |||
| 83 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 84 | |||
| 85 | Copyright (c) 2012 Ontario Institute for Cancer Research | ||
| 86 | |||
| 87 | This package and its accompanying libraries is free software; you can | ||
| 88 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 89 | version 1, or at your option, any later version) or the Artistic | ||
| 90 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 91 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | use strict; | ||
| 96 | use Carp 'croak'; | ||
| 97 | use base 'VM::EC2::Generic'; | ||
| 98 | |||
| 99 | sub valid_fields { | ||
| 100 | my $self = shift; | ||
| 101 | return qw(subnetId state vpcId cidrBlock availableIpAddressCount | ||
| 102 | availabilityZone defaultForAz mapPublicIpOnLaunch); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub primary_id { shift->subnetId } | ||
| 106 | |||
| 107 | sub vpc { | ||
| 108 | my $self = shift; | ||
| 109 | return $self->aws->describe_vpcs($self->vpcId); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub zone { | ||
| 113 | my $self = shift; | ||
| 114 | return $self->aws->describe_availability_zones($self->availabilityZone); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub refresh { | ||
| 118 | my $self = shift; | ||
| 119 | my $i = shift; | ||
| 120 | local $self->aws->{raise_error} = 1; | ||
| 121 | ($i) = $self->aws->describe_subnets($self->subnetId) unless $i; | ||
| 122 | %$self = %$i if $i; | ||
| 123 | return defined $i; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub current_state { | ||
| 127 | my $self = shift; | ||
| 128 | $self->refresh; | ||
| 129 | $self->state; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub associate_route_table { | ||
| 133 | my $self = shift; | ||
| 134 | my $rt = shift or croak "usage: associate_route_table(\$route_table_id)"; | ||
| 135 | return $self->aws->associate_route_table(-subnet_id => $self->subnetId, | ||
| 136 | -route_table_id => $rt); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub disassociate_route_table { | ||
| 140 | my $self = shift; | ||
| 141 | my $rt = shift or croak "usage: disassociate_route_table(\$route_table_id)"; | ||
| 142 | $rt = $self->aws->describe_route_tables($rt) unless ref $rt; | ||
| 143 | my ($association) = grep {$_->subnetId eq $self->subnetId} $rt->associations; | ||
| 144 | $association or croak "$rt is not associated with this subnet"; | ||
| 145 | return $self->aws->disassociate_route_table($association); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub create_route_table { | ||
| 149 | my $self = shift; | ||
| 150 | my $vpc = $self->vpcId; | ||
| 151 | my $rt = $self->aws->create_route_table($vpc) or return; | ||
| 152 | $self->associate_route_table($rt->routeTableId) or return; | ||
| 153 | return $rt | ||
| 154 | } | ||
| 155 | |||
| 156 | sub disassociate_network_acl { | ||
| 157 | my $self = shift; | ||
| 158 | my $acl = $self->aws->describe_network_acls(-filter=>{ 'association.subnet-id' => $self->subnetId}); | ||
| 159 | if ($acl->default) { | ||
| 160 | print "disassociate_network_acl(): Cannot disassociate subnet from default ACL"; | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | my $default_acl = $self->aws->describe_network_acls(-filter=>{ 'default' => 'true', 'vpc-id' => $self->vpcId}) | ||
| 164 | or croak "disassociate_network_acl(): Cannot determine default ACL"; | ||
| 165 | return $self->associate_network_acl($default_acl->networkAclId); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub associate_network_acl { | ||
| 169 | my $self = shift; | ||
| 170 | my $network_acl_id = shift or croak "usage: associate_network_acl(\$network_acl_id)"; | ||
| 171 | my $acl = $self->aws->describe_network_acls(-filter=>{ 'association.subnet-id' => $self->subnetId}) | ||
| 172 | or croak "associate_network_acl(): Cannot determine current ACL"; | ||
| 173 | my ($association) = grep { $_->subnetId eq $self->subnetId } $acl->associations; | ||
| 174 | my $association_id = $association->networkAclAssociationId; | ||
| 175 | return $self->aws->replace_network_acl_association(-association_id=>$association_id,-network_acl_id=>$network_acl_id); | ||
| 176 | } | ||
| 177 | |||
| 178 | sub defaultForAz { | ||
| 179 | my $self = shift; | ||
| 180 | my $default = $self->SUPER::defaultForAz; | ||
| 181 | return $default eq 'true'; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub mapPublicIpOnLaunch { | ||
| 185 | my $self = shift; | ||
| 186 | my $map_ip = $self->SUPER::mapPublicIpOnLaunch; | ||
| 187 | return $map_ip eq 'true'; | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; | ||
| 191 |