| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | package App::CloudTrail; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | use Data::Dumper; | ||
| 10 | use English qw(-no_match_vars); | ||
| 11 | use JSON; | ||
| 12 | |||
| 13 | use Role::Tiny::With; | ||
| 14 | with 'App::AWS'; | ||
| 15 | |||
| 16 | use parent qw(App::Command); | ||
| 17 | |||
| 18 | __PACKAGE__->follow_best_practice; | ||
| 19 | __PACKAGE__->mk_accessors(qw(profile region)); | ||
| 20 | |||
| 21 | ######################################################################## | ||
| 22 | sub lookup_events { | ||
| 23 | ######################################################################## | ||
| 24 | my ( $self, %args ) = @_; | ||
| 25 | |||
| 26 | my ( $attributes, $start_time, $end_time ) = @args{qw(attributes start_time end_time)}; | ||
| 27 | |||
| 28 | my $result = $self->command( | ||
| 29 | 'lookup-events' => [ | ||
| 30 | '--lookup-attributes' => $self->format_attributes($attributes), | ||
| 31 | $start_time ? ( '--start-time' => $start_time ) : (), | ||
| 32 | $end_time ? ( '--end-time' => $end_time ) : (), | ||
| 33 | ] | ||
| 34 | ); | ||
| 35 | |||
| 36 | $self->check_result( message => 'ERROR: unable to lookup events' ); | ||
| 37 | |||
| 38 | foreach ( @{ $result->{Events} } ) { | ||
| 39 | $_->{CloudTrailEvent} = from_json( $_->{CloudTrailEvent} ); | ||
| 40 | } | ||
| 41 | |||
| 42 | return $result->{Events}; | ||
| 43 | } | ||
| 44 | |||
| 45 | ######################################################################## | ||
| 46 | sub format_attributes { | ||
| 47 | ######################################################################## | ||
| 48 | my ( $self, $events ) = @_; | ||
| 49 | |||
| 50 | return map { sprintf 'AttributeKey=%s,AttributeValue=%s', $_, $events->{$_} } keys %{$events}; | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; |