| 1 | package VM::EC2::Instance::State::Change; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | VM::EC2::Instance::State::Change - Represent an EC2 instance's change in state. | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | # find all stopped instances | ||
| 10 | @instances = $ec2->describe_instances(-filter=>{'instance-state-name'=>'stopped'}); | ||
| 11 | |||
| 12 | # start them | ||
| 13 | @state_change = $ec2->start_instances(@instances) | ||
| 14 | |||
| 15 | foreach my $sc (@state_change) { | ||
| 16 | my $instanceId = $sc->instanceId; | ||
| 17 | my $currentState = $sc->currentState; | ||
| 18 | my $previousState = $sc->previousState; | ||
| 19 | } | ||
| 20 | |||
| 21 | # poll till the first instance is running | ||
| 22 | sleep 2 until $state_change[0]->current_status eq 'running'; | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This object represents a state change in an Amazon EC2 instance. It | ||
| 27 | is returned by VM::EC2 start_instances(), stop_instances(), | ||
| 28 | terminate_instances(), reboot_instances() and the corresponding | ||
| 29 | VM::EC2::Instance methods. In addition, this object is returned by | ||
| 30 | calls to VM::EC2::Instance->instanceState(). | ||
| 31 | |||
| 32 | =head1 METHODS | ||
| 33 | |||
| 34 | These object methods are supported: | ||
| 35 | |||
| 36 | instanceId -- The instanceId. | ||
| 37 | currentState -- The instanceId's current state AT THE TIME | ||
| 38 | THE STATECHANGE OBJECT WAS CREATED. One of | ||
| 39 | "terminated", "running", "stopped", "stopping", | ||
| 40 | "shutting-down". | ||
| 41 | previousState -- The instanceID's previous state AT THE TIME | ||
| 42 | THE STATECHANGE OBJECT WAS CREATED. | ||
| 43 | |||
| 44 | Note that currentState and previousState return a | ||
| 45 | VM::EC2::Instance::State object, which provides both string-readable | ||
| 46 | forms and numeric codes representing the state. | ||
| 47 | |||
| 48 | In addition, the method provides the following convenience method: | ||
| 49 | |||
| 50 | =head2 $state = $state_change->current_status() | ||
| 51 | |||
| 52 | This method returns the current state of the instance. This is the | ||
| 53 | correct method to call if you are interested in knowing what the | ||
| 54 | instance is doing right now. | ||
| 55 | |||
| 56 | =head2 STRING OVERLOADING | ||
| 57 | |||
| 58 | In a string context, the method will return the string representation of | ||
| 59 | currentState. | ||
| 60 | |||
| 61 | =head1 SEE ALSO | ||
| 62 | |||
| 63 | L<VM::EC2> | ||
| 64 | L<VM::EC2::Generic> | ||
| 65 | L<VM::EC2::State> | ||
| 66 | L<VM::EC2::Instance> | ||
| 67 | |||
| 68 | =head1 AUTHOR | ||
| 69 | |||
| 70 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 71 | |||
| 72 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 73 | |||
| 74 | This package and its accompanying libraries is free software; you can | ||
| 75 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 76 | version 1, or at your option, any later version) or the Artistic | ||
| 77 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 78 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | use strict; | ||
| 83 | use base 'VM::EC2::Generic'; | ||
| 84 | use Carp 'croak'; | ||
| 85 | use VM::EC2::Instance::State; | ||
| 86 | |||
| 87 | use overload '""' => sub {shift()->currentState}, | ||
| 88 | fallback => 1; | ||
| 89 | |||
| 90 | sub valid_fields { | ||
| 91 | my $self = shift; | ||
| 92 | return qw(instanceId currentState previousState); | ||
| 93 | } | ||
| 94 | sub currentState { | ||
| 95 | return VM::EC2::Instance::State->new(shift->SUPER::currentState); | ||
| 96 | } | ||
| 97 | sub previousState { | ||
| 98 | return VM::EC2::Instance::State->new(shift->SUPER::previousState); | ||
| 99 | } | ||
| 100 | sub current_status { | ||
| 101 | my $self = shift; | ||
| 102 | my $ec2 = $self->aws; | ||
| 103 | my $id = $self->instanceId; | ||
| 104 | my ($instance) = $ec2->describe_instances(-instance_id=>$id); | ||
| 105 | $instance or croak "invalid instance: $id"; | ||
| 106 | return $instance->instanceState; | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; |