API-DirectAdmin

 view release on metacpan or  search on metacpan

lib/API/DirectAdmin.pm  view on Meta::CPAN

    dns    => 'DNS',
    ip     => 'Ip',
);

# init
sub new {
    my $class = shift;
    $class = ref ($class) || $class;
    
    my $self = {
        auth_user   => '',
        auth_passwd => '',
        host        => '',
        ip          => '',
        debug       => $DEBUG,
	    allow_https => 1,
	    fake_answer => $FAKE_ANSWER,
        (@_)
    };

    confess "Required auth_user!"   unless $self->{auth_user};
    confess "Required auth_passwd!" unless $self->{auth_passwd};
    confess "Required host!"        unless $self->{host};

    return bless $self, $class;
}

# initialize components
sub init_components {
    my ( %c ) = @_;
    my $caller = caller;

lib/API/DirectAdmin.pm  view on Meta::CPAN

            $new_hash->{$allowed_key} = $hash->{$allowed_key};
        }
        elsif (exists $hash->{lc $allowed_key}) {
            $new_hash->{$allowed_key} = $hash->{lc $allowed_key};
        };
    }

    return $new_hash;
}

# all params derived from get_auth_hash
sub query {
    my ( $self, %params ) = @_;

    my $command   = delete $params{command};
    my $fields    = $params{allowed_fields} || '';

    my $allowed_fields;
    warn 'query_abstract ' . Dumper( \%params ) if $self->{debug};

    confess "Empty command" unless $command;

    $fields = "host port auth_user auth_passwd method allow_https command $fields";
    @$allowed_fields = split /\s+/, $fields;

    my $params = $self->filter_hash( $params{params}, $allowed_fields );

    my $query_string = $self->mk_full_query_string( {
        command => $command,
        %$params,
    } );

    carp Dumper $query_string if $self->{debug};

lib/API/DirectAdmin.pm  view on Meta::CPAN

                                                        && scalar keys %$params
                                                        && $self->{host}
                                                        && $params->{command};

    my $allow_https = defined $params->{allow_https} ? $params->{allow_https} : $self->{allow_https};
    delete $params->{allow_https};
   
    my $host        = $self->{host};
    my $port        = $self->{port} || 2222;
    my $command     = delete $params->{command};
    my $auth_user   = $self->{auth_user};
    my $auth_passwd = $self->{auth_passwd};

    $self->kill_start_end_slashes();

    my $query_path = ( $allow_https ? 'https' : 'http' ) . "://$auth_user:$auth_passwd\@$host:$port/$command?";
    return $query_path . $self->mk_query_string($params);
}

# Make query string
# STATIC(HASHREF: params)
sub mk_query_string {
    my ($self, $params) = @_;

    return '' unless ref $params eq 'HASH' && scalar keys %$params;

lib/API/DirectAdmin.pm  view on Meta::CPAN



=head1 NAME

API::DirectAdmin - interface to the DirectAdmin Hosting Panel API ( http://www.directadmin.com )

=head1 SYNOPSIS

 use API::DirectAdmin;
 
 my %auth = (
    auth_user   => 'admin_name',
    auth_passwd => 'admin_passwd',
    host        => '11.22.33.44',
 );

 # init
 my $da = API::DirectAdmin->new(%auth);

 ### Get all panel IP
 my $ip_list = $da->ip->list();

 unless ($ip_list && ref $ip_list eq 'ARRAY' && scalar @$ip_list) {
    die 'Cannot get ip list from DirectAdmin';
 }

 my $ip  = $ip_list->[0];
 my $dname  = 'reg.ru';

lib/API/DirectAdmin.pm  view on Meta::CPAN


    my $result = $da->user->change_package( {
        username => 'username',
        package  => 'new_package',
    } );

=item show_packages

Return list of available packages.

Note: If you created packages through administrator user - you must use admin's login and password for authorisation. Obviously, if packages was created by reseller user - use reseller authorisation.

Example:

    my $packages = $da->user->show_packages();

=item show_user_config

Return all user settings.

Example:

lib/API/DirectAdmin.pm  view on Meta::CPAN

Return list of domains on server.

Example:

    my $domains = $da->domain->list();

=item add

Add new domain to user through you connect to server.

Note: For adding domains for customers and you don't khow their password use: auth_user = 'admin_name|customer_name' in auth hash.

Example:

    my %auth = (
        auth_user   => 'admin_name|customer_name',
	auth_passwd => 'admin_passwd',
        host        => '11.22.33.44',
    );

    # init
    my $da = API::DirectAdmin->new(%auth);
    
    $result = $da->domain->add({
    	domain => 'newdomain.com',
    	php    => 'ON',
    	cgi    => 'ON',
    });

=back
    
=head2 API::DirectAdmin::Mysql

lib/API/DirectAdmin.pm  view on Meta::CPAN

Example:

    print $da->mysql->list();

=item adddb

Add database to user. Prefix "username_" will be added to 'name' and 'user';

Example:

    my %auth = (
        auth_user   => 'admin_name|customer',
        auth_passwd => 'admin_passwd',
        host        => '11.22.33.44',
    );

    # init
    my $da = API::DirectAdmin->new(%auth);
    
    my $result = $da->mysql->adddb( {
        name     => 'default', # will be 'customer_default'
        user     => 'default', # will be 'customer_default'
        passwd   => 'password',
        passwd2  => 'password',
    } );

=item deldb

lib/API/DirectAdmin/Mysql.pm  view on Meta::CPAN


use Modern::Perl '2010';
use Data::Dumper;
use Carp;

use base 'API::DirectAdmin::Component';

our $VERSION = 0.05;

# Create database for user
# Connection data MUST BE for user: auth_user => 'admin_login|user_login'
# auth_passwd => 'admin_passwd'
#    INPUT
#    name	=> 'DBNAME',
#    passwd	=> 'DBPASSWD',
#    passwd2	=> 'DBPASSWD',
#    user	=> 'DBLOGIN',
sub adddb {
    my ($self, $params ) = @_;
    
    $params->{action} = 'create';
    

lib/API/DirectAdmin/Mysql.pm  view on Meta::CPAN

    );
    
    carp '$responce ' . Dumper(\$responce) if $self->{debug};
    
    return $responce if $responce;    

    return 'FAIL';
}

# Delete database for user
# Connection data MUST BE for user: auth_user => 'admin_login|user_login'
# auth_passwd => 'admin_passwd'
#    INPUT
#    select0	=> 'DBNAME',
#    domain	=> 'DOMAIN.COM',
sub deldb {
    my ($self, $params ) = @_;
    
    $params->{action} = 'delete';

    carp 'params ' . Dumper($params) if $self->{debug};

lib/API/DirectAdmin/Mysql.pm  view on Meta::CPAN

			   select0',
    );
    
    carp '$responce ' . Dumper(\$responce) if $self->{debug};
    
    return $responce if $responce;    

    return 'FAIL';
}

# Get list of databases for authorized user.
# No params.
sub list {
    my ($self ) = @_;

    my $responce = $self->directadmin->query(
	command        => 'CMD_API_DATABASES',
	method	       => 'GET',
    );

    carp '$responce ' . Dumper($responce) if $self->{debug};

t/01-test.t  view on Meta::CPAN

use strict;
use warnings;
use lib qw( ./lib );


use Data::Dumper;

our $ONLINE;

BEGIN {
    #$ENV{auth_user}   = 'restest';
    #$ENV{auth_passwd} = '123';
    #$ENV{host}        = '192.168.123.1';
    $ONLINE = $ENV{auth_user} && $ENV{auth_passwd} && $ENV{host};
}

my $manipulate_user = 'zsezse';

use Test::More tests => $ONLINE ? 34 : 34;
my %connection_params = (
    host	=> $ENV{host} || '127.0.0.1',
    auth_user	=> $ENV{auth_user} || 'login',
    auth_passwd => $ENV{auth_passwd} || 'passwd',
);

ok(1, 'Test OK');
use_ok('API::DirectAdmin');

my $da = API::DirectAdmin->new(%connection_params);

my $func = 'filter_hash';
is_deeply( $da->filter_hash( {  }, [ ]), {}, $func );
is_deeply( $da->filter_hash( { aaa => 555, bbb => 111 }, [ 'aaa' ]), { aaa => 555 }, $func );

t/01-test.t  view on Meta::CPAN

is( $da->mk_query_string( {  }  ), '', $func );
is( $da->mk_query_string( ''    ), '', $func );
is( $da->mk_query_string( undef ), '', $func );
is( $da->mk_query_string( { aaa => 111, bbb => 222 } ), 'aaa=111&bbb=222', $func );
is( $da->mk_query_string( { bbb => 222, aaa => 111 } ), 'aaa=111&bbb=222', $func );
is( $da->mk_query_string( [ ] ), '', $func );
is( $da->mk_query_string( { dddd => 'dfdf' } ), 'dddd=dfdf', $func );

$func = 'mk_full_query_string';
is( $da->mk_full_query_string( { command => 'CMD' } ), 
    'https://'.$connection_params{auth_user}.':'.$connection_params{auth_passwd}.'@'.$connection_params{host}.':2222/CMD?',
    $func
);

is( $da->mk_full_query_string( { command => 'CMD', allow_https => 0 } ), 
    'http://'.$connection_params{auth_user}.':'.$connection_params{auth_passwd}.'@'.$connection_params{host}.':2222/CMD?',
    $func
);

is( $da->mk_full_query_string( {
	command => 'CMD',
        param1  => 'val1',
        param2  => 'val2',
    } ), 
    'https://'.$connection_params{auth_user}.':'.$connection_params{auth_passwd}.'@'.$connection_params{host}.':2222/CMD?param1=val1&param2=val2',
    $func
);

is( $da->mk_full_query_string( {
        param1      => 'val1',
        param2      => 'val2',
        command     => 'CMD',
	    allow_https => 0, 
    } ), 
    'http://'.$connection_params{auth_user}.':'.$connection_params{auth_passwd}.'@'.$connection_params{host}.':2222/CMD?param1=val1&param2=val2',
    $func
);

# IP

use_ok('API::DirectAdmin::Ip');

$da->{fake_answer} = ! $ONLINE ? { list => ['127.0.0.1'], error => 0 } : undef;

my $ip_list = $da->ip->list();

t/01-test.t  view on Meta::CPAN

    {
	user => $manipulate_user,
    }
);
is_deeply( $result, \%answer , 'API::DirectAdmin::User::delete repeat');

# Mysql

use_ok('API::DirectAdmin::Mysql');

$connection_params{auth_user} .= '|' . $manipulate_user;

%answer = (
    text 	=> 'Database Created',
    error 	=> 0,
    details 	=> 'Database Created',
);

$da->{fake_answer} = ! $ONLINE ? \%answer : undef;

$result = $da->mysql->adddb(



( run in 7.847 seconds using v1.01-cache-2.11-cpan-5c0b1e786e0 )