| 1 | package VM::EC2::VPC; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::VPC | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use VM::EC2; | ||
| 10 | $ec2 - VM::EC2->new(...); | ||
| 11 | $vpc = $ec2->create_vpc('10.0.0.0/16') or die $ec2->error_str; | ||
| 12 | $subnet1 = $vpc->create_subnet('10.0.0.0/24') or die $vpc->error_str; | ||
| 13 | $gateway = $vpc->create_internet_gateway or die $vpc->error_str; | ||
| 14 | $routeTbl = $subnet1->create_route_table or die $vpc->error_str; | ||
| 15 | $routeTbl->create_route('0.0.0.0/0' => $gateway) or die $vpc->error_str; | ||
| 16 | |||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Please see L<VM::EC2::Generic> for methods shared by all VM::EC2 | ||
| 21 | objects. | ||
| 22 | |||
| 23 | =head1 METHODS | ||
| 24 | |||
| 25 | These object methods are supported: | ||
| 26 | |||
| 27 | vpcId -- ID of the VPC | ||
| 28 | state -- Current state of the VPC (pending, available) | ||
| 29 | cidrBlock -- The CIDR block the VPC covers. | ||
| 30 | dhcpOptionsId -- The ID of the set of DHCP options you've associated | ||
| 31 | with the VPC, or "default". | ||
| 32 | instanceTenancy -- Either "dedicated" or "default" | ||
| 33 | isDefault -- Indicates if the VPC is a default VPC | ||
| 34 | |||
| 35 | In addition, this object supports the following convenience methods: | ||
| 36 | |||
| 37 | dhcp_options() -- Return a VM::EC2::VPC::DhcpOptions object. | ||
| 38 | |||
| 39 | current_state() -- Refresh the object and then return its state | ||
| 40 | |||
| 41 | current_status() -- Same as above (for module consistency) | ||
| 42 | |||
| 43 | set_dhcp_options($options) -- Associate the Dhcp option set with | ||
| 44 | this VPC (DhcpOptionsId string or VM::EC2::VPC::DhcpOptions object). | ||
| 45 | Use "default" or pass no arguments to assign no Dhcp options. | ||
| 46 | |||
| 47 | internet_gateways() -- Return the list of internet gateways attached to | ||
| 48 | this VPC as a list of VM::EC2::VPC::InternetGateway. | ||
| 49 | |||
| 50 | create_subnet($cidr_block [,$availability_zone] ) | ||
| 51 | -- Create a subnet with the indicated CIDR block and | ||
| 52 | return the VM::EC2::VPC::Subnet object. The | ||
| 53 | optional $availability_zone argument selects the | ||
| 54 | zone for the subnet. Otherwise Amazon selects one | ||
| 55 | for you. | ||
| 56 | |||
| 57 | create_internet_gateway() | ||
| 58 | -- Create an internet gateway and immediately attach | ||
| 59 | it to this VPC. If successful returns a | ||
| 60 | VM::EC2::VPC::InternetGateway object. | ||
| 61 | |||
| 62 | subnets() -- Return the list of subnets attached to this VPC | ||
| 63 | as a list of VM::EC2::VPC::Subnet. | ||
| 64 | |||
| 65 | route_tables() -- Return the list of route tables attached to this VPC | ||
| 66 | as a list of VM::EC2::VPC::RouteTable. | ||
| 67 | |||
| 68 | =head1 STRING OVERLOADING | ||
| 69 | |||
| 70 | When used in a string context, this object will be interpolated as the | ||
| 71 | VPC ID. | ||
| 72 | |||
| 73 | =head1 SEE ALSO | ||
| 74 | |||
| 75 | L<VM::EC2> | ||
| 76 | L<VM::EC2::Generic> | ||
| 77 | |||
| 78 | =head1 AUTHOR | ||
| 79 | |||
| 80 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 81 | |||
| 82 | Copyright (c) 2012 Ontario Institute for Cancer Research | ||
| 83 | |||
| 84 | This package and its accompanying libraries is free software; you can | ||
| 85 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 86 | version 1, or at your option, any later version) or the Artistic | ||
| 87 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 88 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | use strict; | ||
| 93 | use base 'VM::EC2::Generic'; | ||
| 94 | use Carp 'croak'; | ||
| 95 | |||
| 96 | sub valid_fields { | ||
| 97 | my $self = shift; | ||
| 98 | return qw(vpcId state cidrBlock dhcpOptionsId instanceTenancy isDefault); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub refresh { | ||
| 102 | my $self = shift; | ||
| 103 | my $i = shift; | ||
| 104 | local $self->aws->{raise_error} = 1; | ||
| 105 | ($i) = $self->aws->describe_vpcs(-vpc_id=>$self->vpcId) unless $i; | ||
| 106 | %$self = %$i if $i; | ||
| 107 | return defined $i; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub current_state { | ||
| 111 | my $self = shift; | ||
| 112 | $self->refresh; | ||
| 113 | $self->state; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub current_status {shift->current_state(@_)} | ||
| 117 | |||
| 118 | sub primary_id { shift->vpcId } | ||
| 119 | |||
| 120 | sub dhcp_options { | ||
| 121 | my $self = shift; | ||
| 122 | return $self->aws->describe_dhcp_options($self->dhcpOptionsId); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub set_dhcp_options { | ||
| 126 | my $self = shift; | ||
| 127 | my $options = shift || 'default'; | ||
| 128 | return $self->aws->associate_dhcp_options($self => $options); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub internet_gateways { | ||
| 132 | my $self = shift; | ||
| 133 | return $self->aws->describe_internet_gateways({'attachment.vpc-id'=>$self->vpcId}); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub subnets { | ||
| 137 | my $self = shift; | ||
| 138 | return $self->aws->describe_subnets({'vpc-id'=>$self->vpcId}); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub route_tables { | ||
| 142 | my $self = shift; | ||
| 143 | return $self->aws->describe_route_tables({'vpc-id'=>$self->vpcId}); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub attach_internet_gateway { | ||
| 147 | my $self = shift; | ||
| 148 | my $gw = shift; | ||
| 149 | return $self->aws->attach_internet_gateway($gw => $self->vpcId); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub detach_internet_gateway { | ||
| 153 | my $self = shift; | ||
| 154 | my $gw = shift || ($self->internet_gateways)[0]; | ||
| 155 | return $self->aws->detach_internet_gateway($gw=>$self->vpcId); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub create_subnet { | ||
| 159 | my $self = shift; | ||
| 160 | my $cidr_block = shift or croak "usage: create_subnet(\$cidr_block)"; | ||
| 161 | my $availability_zone = shift; | ||
| 162 | |||
| 163 | my %params = (-vpc_id=>$self->vpcId, -cidr_block=>$cidr_block); | ||
| 164 | $params{-availability_zone} = $availability_zone if $availability_zone; | ||
| 165 | |||
| 166 | my $result = $self->aws->create_subnet(%params); | ||
| 167 | $self->refresh if $result; | ||
| 168 | return $result; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub delete_internet_gateway { | ||
| 172 | my $self = shift; | ||
| 173 | my $gateway = shift || ($self->internet_gateways)[0]; | ||
| 174 | $gateway or return; | ||
| 175 | $self->detach_internet_gateway($gateway) or return; | ||
| 176 | return $self->aws->delete_internet_gateway($gateway); | ||
| 177 | } | ||
| 178 | |||
| 179 | sub create_internet_gateway { | ||
| 180 | my $self = shift; | ||
| 181 | my $gateway = $self->aws->create_internet_gateway() or return; | ||
| 182 | my $attach = $self->attach_internet_gateway($gateway); | ||
| 183 | unless ($attach) { | ||
| 184 | local $self->aws->{error}; # so that we get the error from the attach call | ||
| 185 | $self->aws->delete_internet_gateway($gateway); | ||
| 186 | return; | ||
| 187 | } | ||
| 188 | return $gateway; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub isDefault { | ||
| 192 | my $self = shift; | ||
| 193 | my $default = $self->SUPER::isDefault; | ||
| 194 | return $default eq 'true'; | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 |