| 1 | package VM::EC2::REST::ebs; | ||
| 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 | AttachVolume => 'VM::EC2::BlockDevice::Attachment', | ||
| 9 | CopySnapshot => sub { shift->{snapshotId} }, | ||
| 10 | CreateSnapshot => 'VM::EC2::Snapshot', | ||
| 11 | CreateVolume => 'VM::EC2::Volume', | ||
| 12 | DeleteSnapshot => 'boolean', | ||
| 13 | DeleteVolume => 'boolean', | ||
| 14 | DescribeAvailabilityZones => 'fetch_items,availabilityZoneInfo,VM::EC2::AvailabilityZone', | ||
| 15 | DescribeImages => 'fetch_items,imagesSet,VM::EC2::Image', | ||
| 16 | DescribeInstanceStatus => 'fetch_items_iterator,instanceStatusSet,VM::EC2::Instance::StatusItem,instance_status', | ||
| 17 | DescribeRegions => 'fetch_items,regionInfo,VM::EC2::Region', | ||
| 18 | DescribeSecurityGroups => 'fetch_items,securityGroupInfo,VM::EC2::SecurityGroup', | ||
| 19 | DescribeSnapshots => 'fetch_items,snapshotSet,VM::EC2::Snapshot', | ||
| 20 | DescribeVolumeStatus => 'fetch_items_iterator,volumeStatusSet,VM::EC2::Volume::StatusItem,volume_status', | ||
| 21 | DescribeVolumes => 'fetch_items,volumeSet,VM::EC2::Volume', | ||
| 22 | DetachVolume => 'VM::EC2::BlockDevice::Attachment', | ||
| 23 | EnableVolumeIO => 'boolean', | ||
| 24 | ModifySnapshotAttribute => 'boolean', | ||
| 25 | ResetSnapshotAttribute => 'boolean', | ||
| 26 | ); | ||
| 27 | |||
| 28 | my $VEP = 'VM::EC2::ParmParser'; | ||
| 29 | |||
| 30 | =head1 NAME | ||
| 31 | |||
| 32 | VM::EC2::REST::ebs - Modules for EC2 EBS volumes | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | use VM::EC2 ':standard'; | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | The methods in this section allow you to query and manipulate EC2 EBS | ||
| 41 | volumes and snapshots. See L<VM::EC2::Volume> and L<VM::EC2::Snapshot> | ||
| 42 | for additional functionality provided through the object interface. | ||
| 43 | |||
| 44 | Implemented: | ||
| 45 | AttachVolume | ||
| 46 | CopySnapshot | ||
| 47 | CreateSnapshot | ||
| 48 | CreateVolume | ||
| 49 | DeleteSnapshot | ||
| 50 | DeleteVolume | ||
| 51 | DescribeSnapshotAttribute | ||
| 52 | DescribeSnapshots | ||
| 53 | DescribeVolumes | ||
| 54 | DescribeVolumeAttribute | ||
| 55 | DescribeVolumeStatus | ||
| 56 | DetachVolume | ||
| 57 | EnableVolumeIO | ||
| 58 | ModifySnapshotAttribute | ||
| 59 | ModifyVolumeAttribute | ||
| 60 | ResetSnapshotAttribute | ||
| 61 | |||
| 62 | Unimplemented: | ||
| 63 | (none) | ||
| 64 | |||
| 65 | =head2 @v = $ec2->describe_volumes(-volume_id=>\@ids,-filter=>\%filters) | ||
| 66 | |||
| 67 | =head2 @v = $ec2->describe_volumes(@volume_ids) | ||
| 68 | |||
| 69 | Return a series of VM::EC2::Volume objects. Optional arguments: | ||
| 70 | |||
| 71 | -volume_id The id of the volume to fetch, either a string | ||
| 72 | scalar or an arrayref. | ||
| 73 | |||
| 74 | -filter One or more filters to apply to the search | ||
| 75 | |||
| 76 | The -filter argument name can be omitted if there are no other | ||
| 77 | arguments you wish to pass. | ||
| 78 | |||
| 79 | The full list of volume filters can be found at: | ||
| 80 | http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeVolumes.html | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub describe_volumes { | ||
| 85 | my $self = shift; | ||
| 86 | my %args = $VEP->args(-volume_id,@_); | ||
| 87 | my @params = $VEP->format_parms(\%args, | ||
| 88 | { | ||
| 89 | list_parm => 'VolumeId', | ||
| 90 | filter_parm => 'Filter' | ||
| 91 | }); | ||
| 92 | return $self->call('DescribeVolumes',@params); | ||
| 93 | } | ||
| 94 | |||
| 95 | =head2 $v = $ec2->create_volume(-availability_zone=>$zone,-snapshot_id=>$snapshotId,-size=>$size,-volume_type=>$type,-iops=>$iops,-encrypted=>$boolean) | ||
| 96 | |||
| 97 | Create a volume in the specified availability zone and return | ||
| 98 | information about it. | ||
| 99 | |||
| 100 | Arguments: | ||
| 101 | |||
| 102 | -availability_zone -- An availability zone from | ||
| 103 | describe_availability_zones (required) | ||
| 104 | |||
| 105 | -snapshot_id -- ID of a snapshot to use to build volume from. | ||
| 106 | |||
| 107 | -size -- Size of the volume, in GB (between 1 and 1024). | ||
| 108 | |||
| 109 | One or both of -snapshot_id or -size are required. For convenience, | ||
| 110 | you may abbreviate -availability_zone as -zone, and -snapshot_id as | ||
| 111 | -snapshot. | ||
| 112 | |||
| 113 | Optional Arguments: | ||
| 114 | |||
| 115 | -volume_type -- The volume type. "standard", "io1", or "gp2" | ||
| 116 | Default is "standard" | ||
| 117 | |||
| 118 | -iops -- The number of I/O operations per second (IOPS) that | ||
| 119 | the volume supports. Range is 100 to 4000. Required | ||
| 120 | when volume type is io1. IOPS must be 30-to-1 ratio | ||
| 121 | to size. ie: 3000 IOPS volume must be at least 100GB. | ||
| 122 | |||
| 123 | -encrypted -- Specifies whether the volume should be encrypted. | ||
| 124 | Encrypted Amazon EBS volumes may only be attached to | ||
| 125 | instances that support them. Volumes created from | ||
| 126 | encrypted snapshots are also encrypted using the same | ||
| 127 | key as the original volume. | ||
| 128 | See: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html | ||
| 129 | |||
| 130 | The returned object is a VM::EC2::Volume object. | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | sub create_volume { | ||
| 135 | my $self = shift; | ||
| 136 | my %args = @_; | ||
| 137 | |||
| 138 | $args{-availability_zone} ||= $args{-zone} or croak "-availability_zone argument is required"; | ||
| 139 | $args{-snapshot_id} ||= $args{-snapshot}; | ||
| 140 | $args{-snapshot_id} || $args{-size} or croak "One or both of -snapshot_id or -size are required"; | ||
| 141 | if (exists $args{-volume_type} && $args{-volume_type} eq 'io1') { | ||
| 142 | $args{-iops} or croak "Argument -iops required when -volume_type is 'io1'"; | ||
| 143 | } | ||
| 144 | elsif ($args{-iops}) { | ||
| 145 | croak "Argument -iops cannot be used when volume type is 'standard'"; | ||
| 146 | } | ||
| 147 | my @params = $VEP->format_parms(\%args, | ||
| 148 | { | ||
| 149 | single_parm => [qw(AvailabilityZone SnapshotId Size VolumeType Iops)], | ||
| 150 | boolean_parm => 'Encrypted' | ||
| 151 | }); | ||
| 152 | return $self->call('CreateVolume',@params); | ||
| 153 | } | ||
| 154 | |||
| 155 | =head2 $result = $ec2->delete_volume($volume_id); | ||
| 156 | |||
| 157 | Deletes the specified volume. Returns a boolean indicating success of | ||
| 158 | the delete operation. Note that a volume will remain in the "deleting" | ||
| 159 | state for some time after this call completes. | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | sub delete_volume { | ||
| 164 | my $self = shift; | ||
| 165 | my %args = $VEP->args(-volume_id => @_); | ||
| 166 | my @param = $VEP->format_parms(\%args, | ||
| 167 | { | ||
| 168 | single_parm => 'VolumeId' | ||
| 169 | }); | ||
| 170 | |||
| 171 | return $self->call('DeleteVolume',@param); | ||
| 172 | } | ||
| 173 | |||
| 174 | =head2 $attachment = $ec2->attach_volume($volume_id,$instance_id,$device); | ||
| 175 | |||
| 176 | =head2 $attachment = $ec2->attach_volume(-volume_id=>$volume_id,-instance_id=>$instance_id,-device=>$device); | ||
| 177 | |||
| 178 | Attaches the specified volume to the instance using the indicated | ||
| 179 | device. All arguments are required: | ||
| 180 | |||
| 181 | -volume_id -- ID of the volume to attach. The volume must be in | ||
| 182 | "available" state. | ||
| 183 | -instance_id -- ID of the instance to attach to. Both instance and | ||
| 184 | attachment must be in the same availability zone. | ||
| 185 | -device -- How the device is exposed to the instance, e.g. | ||
| 186 | '/dev/sdg'. | ||
| 187 | |||
| 188 | The result is a VM::EC2::BlockDevice::Attachment object which | ||
| 189 | you can monitor by calling current_status(): | ||
| 190 | |||
| 191 | my $a = $ec2->attach_volume('vol-12345','i-12345','/dev/sdg'); | ||
| 192 | while ($a->current_status ne 'attached') { | ||
| 193 | sleep 2; | ||
| 194 | } | ||
| 195 | print "volume is ready to go\n"; | ||
| 196 | |||
| 197 | or more simply | ||
| 198 | |||
| 199 | my $a = $ec2->attach_volume('vol-12345','i-12345','/dev/sdg'); | ||
| 200 | $ec2->wait_for_attachments($a); | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | sub attach_volume { | ||
| 205 | my $self = shift; | ||
| 206 | my %args; | ||
| 207 | if ($_[0] !~ /^-/ && @_ == 3) { | ||
| 208 | @args{qw(-volume_id -instance_id -device)} = @_; | ||
| 209 | } else { | ||
| 210 | %args = @_; | ||
| 211 | } | ||
| 212 | $args{-volume_id} && $args{-instance_id} && $args{-device} or | ||
| 213 | croak "-volume_id, -instance_id and -device arguments must all be specified"; | ||
| 214 | my @param = $VEP->format_parms(\%args, | ||
| 215 | { | ||
| 216 | single_parm => [qw(VolumeId InstanceId Device)] | ||
| 217 | }); | ||
| 218 | return $self->call('AttachVolume',@param); | ||
| 219 | } | ||
| 220 | |||
| 221 | =head2 $attachment = $ec2->detach_volume($volume_id) | ||
| 222 | |||
| 223 | =head2 $attachment = $ec2->detach_volume(-volume_id=>$volume_id,-instance_id=>$instance_id, | ||
| 224 | -device=>$device, -force=>$force); | ||
| 225 | |||
| 226 | Detaches the specified volume from an instance. | ||
| 227 | |||
| 228 | -volume_id -- ID of the volume to detach. (required) | ||
| 229 | -instance_id -- ID of the instance to detach from. (optional) | ||
| 230 | -device -- How the device is exposed to the instance. (optional) | ||
| 231 | -force -- Force detachment, even if previous attempts were | ||
| 232 | unsuccessful. (optional) | ||
| 233 | |||
| 234 | |||
| 235 | The result is a VM::EC2::BlockDevice::Attachment object which | ||
| 236 | you can monitor by calling current_status(): | ||
| 237 | |||
| 238 | my $a = $ec2->detach_volume('vol-12345'); | ||
| 239 | while ($a->current_status ne 'detached') { | ||
| 240 | sleep 2; | ||
| 241 | } | ||
| 242 | print "volume is ready to go\n"; | ||
| 243 | |||
| 244 | Or more simply: | ||
| 245 | |||
| 246 | my $a = $ec2->detach_volume('vol-12345'); | ||
| 247 | $ec2->wait_for_attachments($a); | ||
| 248 | print "volume is ready to go\n" if $a->current_status eq 'detached'; | ||
| 249 | |||
| 250 | |||
| 251 | =cut | ||
| 252 | |||
| 253 | sub detach_volume { | ||
| 254 | my $self = shift; | ||
| 255 | my %args = $VEP->args(-volume_id => @_); | ||
| 256 | my @param = $VEP->format_parms(\%args, | ||
| 257 | { | ||
| 258 | single_parm => [qw(VolumeId InstanceId Device)], | ||
| 259 | boolean_parm => 'Force' | ||
| 260 | }); | ||
| 261 | return $self->call('DetachVolume',@param); | ||
| 262 | } | ||
| 263 | |||
| 264 | =head2 $ec2->wait_for_attachments(@attachment) | ||
| 265 | |||
| 266 | Wait for all members of the provided list of | ||
| 267 | VM::EC2::BlockDevice::Attachment objects to reach some terminal state | ||
| 268 | ("attached" or "detached"), and then return a hash reference that maps | ||
| 269 | each attachment to its final state. | ||
| 270 | |||
| 271 | Typical usage: | ||
| 272 | |||
| 273 | my $i = 0; | ||
| 274 | my $instance = 'i-12345'; | ||
| 275 | my @attach; | ||
| 276 | foreach (@volume) { | ||
| 277 | push @attach,$_->attach($instance,'/dev/sdf'.$i++; | ||
| 278 | } | ||
| 279 | my $s = $ec2->wait_for_attachments(@attach); | ||
| 280 | my @failed = grep($s->{$_} ne 'attached'} @attach; | ||
| 281 | warn "did not attach: ",join ', ',@failed; | ||
| 282 | |||
| 283 | If no terminal state is reached within a set timeout, then this method | ||
| 284 | returns undef and sets $ec2->error_str() to a suitable message. The | ||
| 285 | timeout, which defaults to 10 minutes (600 seconds), can be get or set | ||
| 286 | with $ec2->wait_for_timeout(). | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub wait_for_attachments { | ||
| 291 | my $self = shift; | ||
| 292 | $self->wait_for_terminal_state(\@_, | ||
| 293 | ['attached','detached'], | ||
| 294 | $self->wait_for_timeout); | ||
| 295 | } | ||
| 296 | |||
| 297 | =head2 @v = $ec2->describe_volume_status(@volume_ids) | ||
| 298 | |||
| 299 | =head2 @v = $ec2->describe_volume_status(\%filters) | ||
| 300 | |||
| 301 | =head2 @v = $ec2->describe_volume_status(-volume_id=>\@ids,-filter=>\%filters) | ||
| 302 | |||
| 303 | Return a series of VM::EC2::Volume::StatusItem objects. Optional arguments: | ||
| 304 | |||
| 305 | -volume_id The id of the volume to fetch, either a string | ||
| 306 | scalar or an arrayref. | ||
| 307 | |||
| 308 | -filter One or more filters to apply to the search | ||
| 309 | |||
| 310 | -max_results Maximum number of items to return (must be more than | ||
| 311 | 5). | ||
| 312 | |||
| 313 | The -filter argument name can be omitted if there are no other | ||
| 314 | arguments you wish to pass. | ||
| 315 | |||
| 316 | The full list of volume filters can be found at: | ||
| 317 | http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeVolumeStatus.html | ||
| 318 | |||
| 319 | If -max_results is specified, then the call will return at most the | ||
| 320 | number of volume status items you requested. You may see whether there | ||
| 321 | are additional results by calling more_volume_status(), and then | ||
| 322 | retrieve the next set of results with additional call(s) to | ||
| 323 | describe_volume_status(): | ||
| 324 | |||
| 325 | my @results = $ec2->describe_volume_status(-max_results => 10); | ||
| 326 | do_something(\@results); | ||
| 327 | while ($ec2->more_volume_status) { | ||
| 328 | @results = $ec2->describe_volume_status; | ||
| 329 | do_something(\@results); | ||
| 330 | } | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | sub more_volume_status { | ||
| 335 | my $self = shift; | ||
| 336 | return $self->{volume_status_token} && | ||
| 337 | !$self->{volume_status_stop}; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub describe_volume_status { | ||
| 341 | my $self = shift; | ||
| 342 | my @parms; | ||
| 343 | |||
| 344 | if (!@_ && $self->{volume_status_token} && $self->{volume_status_args}) { | ||
| 345 | @parms = (@{$self->{volume_status_args}},NextToken=>$self->{volume_status_token}); | ||
| 346 | } | ||
| 347 | |||
| 348 | else { | ||
| 349 | my %args = $VEP->args('-volume_id',@_); | ||
| 350 | my @parms = $VEP->format_parms(\%args, | ||
| 351 | { | ||
| 352 | list_parm => 'VolumeId', | ||
| 353 | filter_parm => 'Filter', | ||
| 354 | single_parm => 'MaxResults' | ||
| 355 | }); | ||
| 356 | if ($args{-max_results}) { | ||
| 357 | $self->{volume_status_token} = 'xyzzy'; # dummy value | ||
| 358 | $self->{volume_status_args} = \@parms; | ||
| 359 | } | ||
| 360 | |||
| 361 | } | ||
| 362 | return $self->call('DescribeVolumeStatus',@parms); | ||
| 363 | } | ||
| 364 | |||
| 365 | =head2 $ec2->wait_for_volumes(@volumes) | ||
| 366 | |||
| 367 | Wait for all members of the provided list of volumes to reach some | ||
| 368 | terminal state ("available", "in-use", "deleted" or "error"), and then | ||
| 369 | return a hash reference that maps each volume ID to its final state. | ||
| 370 | |||
| 371 | If no terminal state is reached within a set timeout, then this method | ||
| 372 | returns undef and sets $ec2->error_str() to a suitable message. The | ||
| 373 | timeout, which defaults to 10 minutes (600 seconds), can be get or set | ||
| 374 | with $ec2->wait_for_timeout(). | ||
| 375 | |||
| 376 | =cut | ||
| 377 | |||
| 378 | sub wait_for_volumes { | ||
| 379 | my $self = shift; | ||
| 380 | $self->wait_for_terminal_state(\@_, | ||
| 381 | ['available','in-use','deleted','error'], | ||
| 382 | $self->wait_for_timeout); | ||
| 383 | } | ||
| 384 | |||
| 385 | =head2 @data = $ec2->describe_volume_attribute($volume_id,$attribute) | ||
| 386 | |||
| 387 | This method returns volume attributes. Only one attribute can be | ||
| 388 | retrieved at a time. The following is the list of attributes that can be | ||
| 389 | retrieved: | ||
| 390 | |||
| 391 | autoEnableIO -- boolean | ||
| 392 | productCodes -- list of scalar | ||
| 393 | |||
| 394 | These values can be retrieved more conveniently from the | ||
| 395 | L<VM::EC2::Volume> object returned from describe_volumes(): | ||
| 396 | |||
| 397 | $volume->auto_enable_io(1); | ||
| 398 | @codes = $volume->product_codes; | ||
| 399 | |||
| 400 | =cut | ||
| 401 | |||
| 402 | sub describe_volume_attribute { | ||
| 403 | my $self = shift; | ||
| 404 | @_ == 2 or croak "Usage: describe_volume_attribute(\$instance_id,\$attribute_name)"; | ||
| 405 | my ($instance_id,$attribute) = @_; | ||
| 406 | my @param = (VolumeId=>$instance_id,Attribute=>$attribute); | ||
| 407 | my $result = $self->call('DescribeVolumeAttribute',@param); | ||
| 408 | return $result && $result->attribute($attribute); | ||
| 409 | } | ||
| 410 | |||
| 411 | sub modify_volume_attribute { | ||
| 412 | my $self = shift; | ||
| 413 | my $volume_id = shift or croak "Usage: modify_volume_attribute(\$volumeId,%param)"; | ||
| 414 | my %args = @_; | ||
| 415 | my @param = (VolumeId=>$volume_id); | ||
| 416 | push @param,('AutoEnableIO.Value'=>$args{-auto_enable_io} ? 'true':'false'); | ||
| 417 | return $self->call('ModifyVolumeAttribute',@param); | ||
| 418 | } | ||
| 419 | |||
| 420 | =head2 $boolean = $ec2->enable_volume_io($volume_id) | ||
| 421 | |||
| 422 | =head2 $boolean = $ec2->enable_volume_io(-volume_id=>$volume_id) | ||
| 423 | |||
| 424 | Given the ID of a volume whose I/O has been disabled (e.g. due to | ||
| 425 | hardware degradation), this method will reenable the I/O and return | ||
| 426 | true if successful. | ||
| 427 | |||
| 428 | =cut | ||
| 429 | |||
| 430 | sub enable_volume_io { | ||
| 431 | my $self = shift; | ||
| 432 | my %args = $self->args('-volume_id',@_); | ||
| 433 | $args{-volume_id} or croak "Usage: enable_volume_io(\$volume_id)"; | ||
| 434 | my @param = $self->single_parm('VolumeId',\%args); | ||
| 435 | return $self->call('EnableVolumeIO',@param); | ||
| 436 | } | ||
| 437 | |||
| 438 | =head2 @snaps = $ec2->describe_snapshots(@snapshot_ids) | ||
| 439 | |||
| 440 | =head2 @snaps = $ec2->describe_snapshots(-snapshot_id=>\@ids,%other_args) | ||
| 441 | |||
| 442 | Returns a series of VM::EC2::Snapshot objects. All arguments | ||
| 443 | are optional: | ||
| 444 | |||
| 445 | -snapshot_id ID of the snapshot | ||
| 446 | |||
| 447 | -owner Filter by owner ID | ||
| 448 | |||
| 449 | -restorable_by Filter by IDs of a user who is allowed to restore | ||
| 450 | the snapshot | ||
| 451 | |||
| 452 | -filter Tags and other filters | ||
| 453 | |||
| 454 | The -filter argument name can be omitted if there are no other | ||
| 455 | arguments you wish to pass. | ||
| 456 | |||
| 457 | The full list of applicable filters can be found at | ||
| 458 | http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSnapshots.html | ||
| 459 | |||
| 460 | =cut | ||
| 461 | |||
| 462 | sub describe_snapshots { | ||
| 463 | my $self = shift; | ||
| 464 | my %args = $VEP->args('-snapshot_id',@_); | ||
| 465 | |||
| 466 | my @params = $VEP->format_parms(\%args, | ||
| 467 | { | ||
| 468 | list_parm => [qw(SnapshotId Owner RestorableBy)], | ||
| 469 | filter_parm => 'Filter' | ||
| 470 | }); | ||
| 471 | return $self->call('DescribeSnapshots',@params); | ||
| 472 | } | ||
| 473 | |||
| 474 | =head2 @data = $ec2->describe_snapshot_attribute($snapshot_id,$attribute) | ||
| 475 | |||
| 476 | This method returns snapshot attributes. The first argument is the | ||
| 477 | snapshot ID, and the second is the name of the attribute to | ||
| 478 | fetch. Currently Amazon defines two attributes: | ||
| 479 | |||
| 480 | createVolumePermission -- return a list of user Ids who are | ||
| 481 | allowed to create volumes from this snapshot. | ||
| 482 | productCodes -- product codes for this snapshot | ||
| 483 | |||
| 484 | The result is a raw hash of attribute values. Please see | ||
| 485 | L<VM::EC2::Snapshot> for a more convenient way of accessing and | ||
| 486 | modifying snapshot attributes. | ||
| 487 | |||
| 488 | =cut | ||
| 489 | |||
| 490 | sub describe_snapshot_attribute { | ||
| 491 | my $self = shift; | ||
| 492 | @_ == 2 or croak "Usage: describe_snapshot_attribute(\$instance_id,\$attribute_name)"; | ||
| 493 | my ($snapshot_id,$attribute) = @_; | ||
| 494 | my @param = (SnapshotId=>$snapshot_id,Attribute=>$attribute); | ||
| 495 | my $result = $self->call('DescribeSnapshotAttribute',@param); | ||
| 496 | return $result && $result->attribute($attribute); | ||
| 497 | } | ||
| 498 | |||
| 499 | =head2 $boolean = $ec2->modify_snapshot_attribute($snapshot_id,-$argument=>$value) | ||
| 500 | |||
| 501 | This method changes snapshot attributes. The first argument is the | ||
| 502 | snapshot ID, and this is followed by an attribute modification command | ||
| 503 | and the value to change it to. | ||
| 504 | |||
| 505 | Currently the only attribute that can be changed is the | ||
| 506 | createVolumeAttribute. This is done through the following arguments | ||
| 507 | |||
| 508 | -createvol_add_user -- scalar or arrayref of UserIds to grant create volume permissions to | ||
| 509 | -createvol_add_group -- scalar or arrayref of Groups to remove create volume permissions from | ||
| 510 | (only currently valid value is "all") | ||
| 511 | -createvol_remove_user -- scalar or arrayref of UserIds to remove from create volume permissions | ||
| 512 | -createvol_remove_group -- scalar or arrayref of Groups to remove from create volume permissions | ||
| 513 | |||
| 514 | You can abbreviate these to -add_user, -add_group, -remove_user, -remove_group, etc. | ||
| 515 | |||
| 516 | See L<VM::EC2::Snapshot> for more convenient methods for interrogating | ||
| 517 | and modifying the create volume permissions. | ||
| 518 | |||
| 519 | =cut | ||
| 520 | |||
| 521 | sub modify_snapshot_attribute { | ||
| 522 | my $self = shift; | ||
| 523 | my $snapshot_id = shift or croak "Usage: modify_snapshot_attribute(\$snapshotId,%param)"; | ||
| 524 | my %args = @_; | ||
| 525 | |||
| 526 | # shortcuts | ||
| 527 | foreach (qw(add_user remove_user add_group remove_group)) { | ||
| 528 | $args{"-createvol_$_"} ||= $args{"-$_"}; | ||
| 529 | } | ||
| 530 | |||
| 531 | my @param = (SnapshotId=>$snapshot_id); | ||
| 532 | push @param,$self->create_volume_perm_parm('Add','UserId', $args{-createvol_add_user}); | ||
| 533 | push @param,$self->create_volume_perm_parm('Remove','UserId',$args{-createvol_remove_user}); | ||
| 534 | push @param,$self->create_volume_perm_parm('Add','Group', $args{-createvol_add_group}); | ||
| 535 | push @param,$self->create_volume_perm_parm('Remove','Group', $args{-createvol_remove_group}); | ||
| 536 | return $self->call('ModifySnapshotAttribute',@param); | ||
| 537 | } | ||
| 538 | |||
| 539 | =head2 $boolean = $ec2->reset_snapshot_attribute($snapshot_id,$attribute) | ||
| 540 | |||
| 541 | This method resets an attribute of the given snapshot to its default | ||
| 542 | value. The only valid attribute at this time is | ||
| 543 | "createVolumePermission." | ||
| 544 | |||
| 545 | =cut | ||
| 546 | |||
| 547 | sub reset_snapshot_attribute { | ||
| 548 | my $self = shift; | ||
| 549 | @_ == 2 or | ||
| 550 | croak "Usage: reset_snapshot_attribute(\$snapshotId,\$attribute_name)"; | ||
| 551 | my ($snapshot_id,$attribute) = @_; | ||
| 552 | my %valid = map {$_=>1} qw(createVolumePermission); | ||
| 553 | $valid{$attribute} or croak "attribute to reset must be 'createVolumePermission'"; | ||
| 554 | return $self->call('ResetSnapshotAttribute', | ||
| 555 | SnapshotId => $snapshot_id, | ||
| 556 | Attribute => $attribute); | ||
| 557 | } | ||
| 558 | |||
| 559 | |||
| 560 | =head2 $snapshot = $ec2->create_snapshot($volume_id) | ||
| 561 | |||
| 562 | =head2 $snapshot = $ec2->create_snapshot(-volume_id=>$vol,-description=>$desc) | ||
| 563 | |||
| 564 | Snapshot the EBS volume and store it to S3 storage. To ensure a | ||
| 565 | consistent snapshot, the volume should be unmounted prior to | ||
| 566 | initiating this operation. | ||
| 567 | |||
| 568 | Arguments: | ||
| 569 | |||
| 570 | -volume_id -- ID of the volume to snapshot (required) | ||
| 571 | |||
| 572 | -description -- A description to add to the snapshot (optional) | ||
| 573 | |||
| 574 | The return value is a VM::EC2::Snapshot object that can be queried | ||
| 575 | through its current_status() interface to follow the progress of the | ||
| 576 | snapshot operation. | ||
| 577 | |||
| 578 | Another way to accomplish the same thing is through the | ||
| 579 | VM::EC2::Volume interface: | ||
| 580 | |||
| 581 | my $volume = $ec2->describe_volumes(-filter=>{'tag:Name'=>'AccountingData'}); | ||
| 582 | $s = $volume->create_snapshot("Backed up at ".localtime); | ||
| 583 | while ($s->current_status eq 'pending') { | ||
| 584 | print "Progress: ",$s->progress,"% done\n"; | ||
| 585 | } | ||
| 586 | print "Snapshot status: ",$s->current_status,"\n"; | ||
| 587 | |||
| 588 | =cut | ||
| 589 | |||
| 590 | sub create_snapshot { | ||
| 591 | my $self = shift; | ||
| 592 | my %args = $VEP->args('-volume_id',@_); | ||
| 593 | my @params = $VEP->format_parms(\%args, | ||
| 594 | { | ||
| 595 | single_parm => ['VolumeId','Description'] | ||
| 596 | }); | ||
| 597 | return $self->call('CreateSnapshot',@params); | ||
| 598 | } | ||
| 599 | |||
| 600 | =head2 $boolean = $ec2->delete_snapshot($snapshot_id) | ||
| 601 | |||
| 602 | Delete the indicated snapshot and return true if the request was | ||
| 603 | successful. | ||
| 604 | |||
| 605 | =cut | ||
| 606 | |||
| 607 | sub delete_snapshot { | ||
| 608 | my $self = shift; | ||
| 609 | my %args = $VEP->args('-snapshot_id',@_); | ||
| 610 | my @params = $VEP->format_parms(\%args, | ||
| 611 | { | ||
| 612 | single_parm => 'SnapshotId' | ||
| 613 | }); | ||
| 614 | return $self->call('DeleteSnapshot',@params); | ||
| 615 | } | ||
| 616 | |||
| 617 | =head2 $snapshot = $ec2->copy_snapshot(-source_region=>$region,-source_snapshot_id=>$id,-description=>$desc) | ||
| 618 | |||
| 619 | =head2 $snapshot = $ec2->copy_snapshot(-region=>$region,-snapshot_id=>$id,-description=>$desc) | ||
| 620 | |||
| 621 | Copies an existing snapshot within the same region or from one region to another. | ||
| 622 | |||
| 623 | Required arguments: | ||
| 624 | |||
| 625 | -source_region -- The region the existing snapshot to copy resides in | ||
| 626 | -source_snapshot_id -- The snapshot ID of the snapshot to copy | ||
| 627 | |||
| 628 | -region -- Alias for -source_region | ||
| 629 | -snapshot_id -- Alias for -source_snapshot_id | ||
| 630 | |||
| 631 | Currently, the DestinationRegion and PresignedUrl API arguments are not | ||
| 632 | supported. Therefore, copying encrypted snapshots between regions is | ||
| 633 | not yet possible. | ||
| 634 | |||
| 635 | Optional arguments: | ||
| 636 | |||
| 637 | -description -- A description of the new snapshot | ||
| 638 | |||
| 639 | The return value is a VM::EC2::Snapshot object that can be queried | ||
| 640 | through its current_status() interface to follow the progress of the | ||
| 641 | snapshot operation. | ||
| 642 | |||
| 643 | =cut | ||
| 644 | |||
| 645 | sub copy_snapshot { | ||
| 646 | my $self = shift; | ||
| 647 | my %args = @_; | ||
| 648 | $args{-description} ||= $args{-desc}; | ||
| 649 | $args{-source_region} ||= $args{-region}; | ||
| 650 | $args{-source_snapshot_id} ||= $args{-snapshot_id}; | ||
| 651 | $args{-source_region} or croak "copy_snapshot(): -source_region argument required"; | ||
| 652 | $args{-source_snapshot_id} or croak "copy_snapshot(): -source_snapshot_id argument required"; | ||
| 653 | my @params = $VEP->format_parms(\%args, | ||
| 654 | { | ||
| 655 | single_parm => [qw(SourceRegion SourceSnapshotId Description)] | ||
| 656 | }); | ||
| 657 | my $snap_id = $self->call('CopySnapshot',@params) or return; | ||
| 658 | return eval { | ||
| 659 | my $snapshot; | ||
| 660 | local $SIG{ALRM} = sub {die "timeout"}; | ||
| 661 | alarm(60); | ||
| 662 | until ($snapshot = $self->describe_snapshots($snap_id)) { sleep 1 } | ||
| 663 | alarm(0); | ||
| 664 | $snapshot; | ||
| 665 | }; | ||
| 666 | } | ||
| 667 | |||
| 668 | =head2 $ec2->wait_for_snapshots(@snapshots) | ||
| 669 | |||
| 670 | Wait for all members of the provided list of snapshots to reach some | ||
| 671 | terminal state ("completed", "error"), and then return a hash | ||
| 672 | reference that maps each snapshot ID to its final state. | ||
| 673 | |||
| 674 | This method may potentially wait forever. It has no set timeout. Wrap | ||
| 675 | it in an eval{} and set alarm() if you wish to timeout. | ||
| 676 | |||
| 677 | =cut | ||
| 678 | |||
| 679 | sub wait_for_snapshots { | ||
| 680 | my $self = shift; | ||
| 681 | $self->wait_for_terminal_state(\@_, | ||
| 682 | ['completed','error'], | ||
| 683 | 0); # no timeout on snapshots -- they may take days | ||
| 684 | } | ||
| 685 | |||
| 686 | =head1 SEE ALSO | ||
| 687 | |||
| 688 | L<VM::EC2> | ||
| 689 | |||
| 690 | =head1 AUTHOR | ||
| 691 | |||
| 692 | Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>. | ||
| 693 | |||
| 694 | Copyright (c) 2011 Ontario Institute for Cancer Research | ||
| 695 | |||
| 696 | This package and its accompanying libraries is free software; you can | ||
| 697 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 698 | version 1, or at your option, any later version) or the Artistic | ||
| 699 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 700 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 701 | |||
| 702 | =cut | ||
| 703 | |||
| 704 | |||
| 705 | 1; |