RT-Extension-AWS-Assets

 view release on metacpan or  search on metacpan

bin/rt-import-aws-assets.in  view on Meta::CPAN


        } while ( $token )
    }

    if ( $args{update} ) {
        my ($aws_resources, $token);

        my %asset;
        do {
            ($aws_resources, $token) =
                RT::Extension::AWS::Assets::FetchMultipleAssetsFromAWS(
                    Region => $args{'region'},
                    ServiceType => $args{'type'},
                    ReservedInstances => $args{'reserved'},
                    Token => $token,
                    MaxResults => $args{'results'} );

                RT::Extension::AWS::Assets::UpdateAWSAssets(
                    AWSResources => $aws_resources,
                    Region => $args{'region'},
                    ServiceType => $args{'type'},
                    ReservedInstances => $args{'reserved'},
                    $args{delete} ? ( updated => \%asset ) : (),
                    CurrentUser => GetCurrentUser() );

        } while ( $token );

        if ( $args{delete} && %asset ) {
            my $catalog = RT->Config->Get('AWSAssetsInstanceCatalog');
            my $assets = RT::Assets->new( RT->SystemUser );
            $assets->FromSQL(<<"EOF");
Catalog = '$catalog' AND 'CF.{Service Type}' = '$args{type}' AND 'CF.Region' = '$args{region}' AND Status = '__Active__'
EOF
            $assets->Limit( FIELD => 'id', VALUE => [ keys %asset ], OPERATOR => 'NOT IN' );
            while ( my $asset = $assets->Next ) {
                my ( $ret, $msg ) = $asset->SetStatus('removed');
                if ( !$ret ) {
                    RT->Logger->error("Couldn't update status of asset #@{[$asset->Id]} to removed: $msg");
                }
                my $links = $asset->DependsOn;
                while ( my $link = $links->Next ) {
                    my $target = $link->TargetObj;
                    if (   $target->isa('RT::Asset')
                        && $target->CatalogObj->Name eq RT->Config->Get('AWSAssetsReservedInstancesCatalog') )
                    {
                        my ( $ret, $msg ) = $asset->DeleteLink( Type => $link->Type, Target => $link->Target );
                        if (!$ret) {
                            RT->Logger->error("Couldn't delete reservation link #@{[$link->Id]}: $msg");
                        }
                    }
                }
            }
        }
    }

    return;
}

sub process_args {
    require Getopt::Long;
    local @ARGV = @_;

    my %opt;
    RT::Interface::CLI::Init( \%opt, 'help|h', 'insert|i', 'update|u', 'delete', 'type=s', 'reserved', 'region=s', 'results=i' );

    if ( delete $opt{help} ) {
        require Pod::Usage;
        Pod::Usage::pod2usage( { verbose => 2 } );
        exit;
    }

    if ( $opt{'insert'} && $opt{'update'} ) {
        print "Running insert and update at the same time is not supported, exiting.\n";
        exit;
    }

    if ( $opt{'insert'} && $opt{'delete'} ) {
        print "Running insert and delete at the same time is not supported, exiting.\n";
        exit;
    }

    if ( $opt{'reserved'} && $opt{'delete'} ) {
        print "Running reserved and delete at the same time is not supported, exiting.\n";
        exit;
    }

    unless ( $opt{'results'} ) {
        $opt{'results'} = 20;
    }

    return %opt;
}

1;

__END__

=head1 NAME

rt-import-aws-assets - import asset data from AWS

=head1 SYNOPSIS

    rt-import-aws-assets --region='us-east-1' --type=EC2 --insert

=head1 DESCRIPTION

This utility pulls data from the configured AWS account and inserts
or updates asset records in Request Tracker.

See the main extension documentation for key configuration options
including the asset catalogs to use.

=head1 OPTIONS

=over

=item B<--region>

Required. The region from which to pull AWS resource information.



( run in 1.286 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )