AWS-CLIWrapper

 view release on metacpan or  search on metacpan

lib/AWS/CLIWrapper.pm  view on Meta::CPAN

package AWS::CLIWrapper;

use 5.008001;
use strict;
use warnings;

our $VERSION = '1.27';

use version;
use JSON 2;
use IPC::Cmd;
use String::ShellQuote;
use Carp;

our $Error = { Message => '', Code => '' };

our $true  = do { bless \(my $dummy = 1), "AWS::CLIWrapper::Boolean" };
our $false = do { bless \(my $dummy = 0), "AWS::CLIWrapper::Boolean" };

my $AWSCLI_VERSION = undef;
my $DEFAULT_CATCH_ERROR_RETRIES = 3;
my $DEFAULT_CATCH_ERROR_MIN_DELAY = 3;
my $DEFAULT_CATCH_ERROR_MAX_DELAY = 10;

sub new {
    my($class, %param) = @_;

    my $region = $param{region};

    my @opt = ();
    for my $k (qw(region profile endpoint_url)) {
        if (my $v = delete $param{$k}) {
            push @opt, param2opt($k, $v);
        }
    }

    my $self = bless {
        region => $region,
        opt  => \@opt,
        json => JSON->new,
        param => \%param,
        awscli_path => $param{awscli_path} || 'aws',
        croak_on_error => !!$param{croak_on_error},
        timeout => (defined $ENV{AWS_CLIWRAPPER_TIMEOUT}) ? $ENV{AWS_CLIWRAPPER_TIMEOUT} : 30,
    }, $class;

    return $self;
}

sub region { shift->{region} }

sub awscli_path {
    my ($self) = @_;
    return $self->{awscli_path};
}

sub awscli_version {
    my ($self) = @_;
    unless (defined $AWSCLI_VERSION) {
        $AWSCLI_VERSION = do {
            my $awscli_path = $self->awscli_path;
            my $vs = qx($awscli_path --version 2>&1) || '';
            my $v;
            if ($vs =~ m{/([0-9.]+)\s}) {
                $v = $1;
            } else {
                $v = 0;
            }
            version->parse($v);
        };
    }
    return $AWSCLI_VERSION;
}

sub catch_error_pattern {
    my ($self) = @_;

    return $ENV{AWS_CLIWRAPPER_CATCH_ERROR_PATTERN}
        if defined $ENV{AWS_CLIWRAPPER_CATCH_ERROR_PATTERN};

    return $self->{param}->{catch_error_pattern}
        if defined $self->{param}->{catch_error_pattern};
    
    return;
}

sub catch_error_retries {
    my ($self) = @_;

    my $retries = defined $ENV{AWS_CLIWRAPPER_CATCH_ERROR_RETRIES}
        ? $ENV{AWS_CLIWRAPPER_CATCH_ERROR_RETRIES}

lib/AWS/CLIWrapper.pm  view on Meta::CPAN

sub workmail           { shift->_execute('workmail', @_) }
sub workmailmessageflow { shift->_execute('workmailmessageflow', @_) }
sub workspaces         { shift->_execute('workspaces', @_) }
sub workspaces_web     { shift->_execute('workspaces-web', @_) }
sub xray               { shift->_execute('xray', @_) }

1;

__END__

=encoding utf-8

=head1 NAME

AWS::CLIWrapper - Wrapper module for aws-cli

=head1 SYNOPSIS

    use AWS::CLIWrapper;
    
    my $aws = AWS::CLIWrapper->new(
        region => 'us-west-1',
    );
    
    my $res = $aws->ec2(
        'describe-instances' => {
            instance_ids => ['i-XXXXX', 'i-YYYYY'],
        },
        timeout => 18, # optional. default is 30 seconds
    );
    
    if ($res) {
        for my $rs ( @{ $res->{Reservations} }) {
            for my $is (@{ $rs->{Instances} }) {
                print $is->{InstanceId},"\n";
            }
        }
    } else {
        warn $AWS::CLIWrapper::Error->{Code};
        warn $AWS::CLIWrapper::Error->{Message};
    }

=head1 DESCRIPTION

AWS::CLIWrapper is wrapper module for aws-cli (recommend: awscli >= 1.0.0, requires: >= 0.40.0).

AWS::CLIWrapper is a just wrapper module, so you can do everything what you can do with aws-cli.

See note below about making sure AWS credentials are accessible (especially under crond)

=head1 METHODS

=over 4

=item B<new>($param:HashRef)

Constructor of AWS::CLIWrapper. Acceptable AWS CLI params are:

    region       region_name:Str
    profile      profile_name:Str
    endpoint_url endpoint_url:Str

Additionally, the these params can be used to control the wrapper behavior:

    nofork                  Truthy to avoid forking when executing `aws`
    timeout                 `aws` execution timeout
    croak_on_error          Truthy to croak() with the error message when `aws`
                            exits with non-zero code
    catch_error_pattern     Regexp pattern to match for error handling.
    catch_error_retries     Retries for handling errors.
    catch_error_min_delay   Minimal delay before retrying `aws` call
                            when an error was caught.
    catch_error_max_delay   Maximal delay before retrying `aws` call.

See below for more detailed explanation.

=item B<accessanalyzer>($operation:Str, $param:HashRef, %opt:Hash)

=item B<account>($operation:Str, $param:HashRef, %opt:Hash)

=item B<acm>($operation:Str, $param:HashRef, %opt:Hash)

=item B<acm_pca>($operation:Str, $param:HashRef, %opt:Hash)

=item B<alexaforbusiness>($operation:Str, $param:HashRef, %opt:Hash)

=item B<amp>($operation:Str, $param:HashRef, %opt:Hash)

=item B<amplify>($operation:Str, $param:HashRef, %opt:Hash)

=item B<amplifybackend>($operation:Str, $param:HashRef, %opt:Hash)

=item B<amplifyuibuilder>($operation:Str, $param:HashRef, %opt:Hash)

=item B<apigateway>($operation:Str, $param:HashRef, %opt:Hash)

=item B<apigatewaymanagementapi>($operation:Str, $param:HashRef, %opt:Hash)

=item B<apigatewayv2>($operation:Str, $param:HashRef, %opt:Hash)

=item B<appconfig>($operation:Str, $param:HashRef, %opt:Hash)

=item B<appconfigdata>($operation:Str, $param:HashRef, %opt:Hash)

=item B<appfabric>($operation:Str, $param:HashRef, %opt:Hash)

=item B<appflow>($operation:Str, $param:HashRef, %opt:Hash)

=item B<appintegrations>($operation:Str, $param:HashRef, %opt:Hash)

=item B<application_autoscaling>($operation:Str, $param:HashRef, %opt:Hash)

=item B<application_insights>($operation:Str, $param:HashRef, %opt:Hash)

=item B<applicationcostprofiler>($operation:Str, $param:HashRef, %opt:Hash)

=item B<appmesh>($operation:Str, $param:HashRef, %opt:Hash)

=item B<apprunner>($operation:Str, $param:HashRef, %opt:Hash)

=item B<appstream>($operation:Str, $param:HashRef, %opt:Hash)



( run in 1.280 second using v1.01-cache-2.11-cpan-39a47a84364 )