API-PleskExpand

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

Build.PL
Changes
lib/API/PleskExpand.pm
lib/API/PleskExpand/Accounts.pm
lib/API/PleskExpand/Domains.pm
Makefile.PL
MANIFEST			This list of files
README
t/expand.t
t/TestData.pm
META.yml                                 Module meta-data (added by MakeMaker)

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


=head1 SYNOPSIS

    use API::PleskExpand;
    use API::Plesk::Response;

    my $expand_client = API::PleskExpand->new(%params);
    my $res = $expand_client->Func_Module->operation_type(%params);

    if ($res->is_success) {
        $res->get_data; # return arr ref of answer blocks
    }

=head1 DESCRIPTION

At present the module provides interaction with Plesk Expand 2.2.4 (API 2.2.4.1). Complete support of operations with Accounts, partial support of work with domains. Support of addition of domains to user Accounts.

API::PleskExpand module gives the convenient interface for addition of new functions. Extensions represent modules in a folder Plesk with definitions of demanded functions. Each demanded operation is described by two functions: op and op_response_par...

For example, here the set of subs in the Accounts module is those.

lib/API/PleskExpand/Accounts.pm  view on Meta::CPAN

    'max_diskspace' -- Least Disk Space (% used) registers a client on the server with the minimum disk space used.
    '' -- Select manually, Specify the target Plesk server by selecting its name from the list.
 	
When choosing a 'manual' (select => '') option you should set server_id!
For 'optimal', 'min_domains', 'max_diskspace' you can ask additional server group id ('group_id' params) or server keyword ('server_keyword' param);


Return (Data::Dumper output):

  VAR1 = bless( {
    'answer_data'   => [ {
      'server_id'   => '1',
      'status'      => 'ok',
      'expiration'  => '-1',
      'tmpl_id'     => '1',
      'id'          => '15'
    } ], 
    'error_codes' => ''
  }, 'API::Plesk::Response' );

=cut

lib/API/PleskExpand/Accounts.pm  view on Meta::CPAN

                $select = create_node( 'server_id', $params{'server_id'} );
            } else {
                return ''; # server_id required!
            }
        }
        
        return create_node( 'add_use_template',
            generate_info_block('gen_info', %{ $params{'general_info'} } ) . '<!-- create_client -->' . $template . $select);

    } else {
        return '';  # not enought data
    }
}


# Parse XML response
# STATIC
sub create_response_parse {
    return abstract_parser('add_use_template', +shift, [ ]);
}

lib/API/PleskExpand/Accounts.pm  view on Meta::CPAN

Changes the account params.

Params:
  general_info -- hashref`s with new user details
  id           -- client id 


Return:

  $VAR1 = bless( {
    'answer_data' => [ {
        'server_id'       => '1',
        'status'          => 'ok',
        'tmpl_id'         => '1',
        'id'              => '15',
        'plesk_client_id' => '384',
        'login'           => 'suxdffffxx'
    } ],
        'error_codes' => ''
  }, 'API::Plesk::Response' );

lib/API/PleskExpand/Accounts.pm  view on Meta::CPAN

    

        return create_node('set', $filter . '<!-- modify_client -->' . create_node('values',
            generate_info_block('gen_info', %{ $params{'general_info'} } ) ) );

    } else {
        return ''; # general_info field required !
    }

    # выключение клиента
    my $data=<<DOC;
<?xml version="1.0"?>
<packet version="0.0.0.110">
    <set>
        <filter>
            <id>1</id>
        </filter>
        <values>
            <gen_info>
                <status>16</status>
            </gen_info>
        </values>
    </set>
</packet
DOC
    # включаем клиента
    my $data1 = <<DOC;
<?xml version="1.0"?>
<packet version="0.0.0.110">
    <set>
       <filter>
            <id>1</id>
        </filter>
        <values>
            <gen_info>
                <status>0</status>
            </gen_info>

lib/API/PleskExpand/Accounts.pm  view on Meta::CPAN

=item delete(%params)

Delete accounts.

Params:
  id -- client id in Plesk

Return:

    $VAR1 = bless( {
        'answer_data' => [ {
            'server_id' => '1',
            'status' => 'ok',
            'id' => '15'
        } ],
        'error_codes' => ''
    }, 'API::Plesk::Response' );


Example:
  print Dumper $client->Accounts->delete( id => 11 );

lib/API/PleskExpand/Accounts.pm  view on Meta::CPAN

}


# DEL response handler
# STATIC
sub delete_response_parse {
    return abstract_parser('del', +shift, [ ]);
}


# Get all element data
# STATIC
sub get {
    my %params = @_;

    unless ($params{all}) {
        return '';
    }

    return create_node( 'get',
        create_node('filter', '') . create_node( 'dataset', create_node('gen_info') )
    ) . '<!-- get_client -->';
}


# GET response handler
# STATIC
sub get_response_parse {
    my $answer = abstract_parser('get', +shift, [ ], 'system_error' );

    if (ref $answer eq 'ARRAY') {
        for my $domain (@$answer) {
            $domain->{data} = xml_extract_values($domain->{data} =~ m#<gen_info>(.*?)</gen_info>#);
        }
    } elsif ($answer) {
        $answer->{data} = xml_extract_values($answer->{data} =~ m#<gen_info>(.*?)</gen_info>#);
    }

    return $answer;
}


1;

__END__
=head1 SEE ALSO

lib/API/PleskExpand/Domains.pm  view on Meta::CPAN

  dname              => 'yandex.ru',   # domain name
  client_id          => 9,             # add domain to client with certain id
  'template-id'      => 1,             # domain template id
  ftp_login          => 'nrgsdasd',    # username for ftp 
  ftp_password       => 'dasdasd',     # password for ftp account
  attach_to_template => 1,             # attach domain to template ? 1 -- yes, 0 -- no

Return:

  $VAR1 = bless( {
    'answer_data' => [ {
        'server_id'     => '1',
        'status'        => 'ok',
        'expiration'    => '-1',
        'tmpl_id'       => '1',
        'client_id'     => '16',
        'id' => '15'
    } ],
        'error_codes' => ''
  }, 'API::Plesk::Response' );

lib/API/PleskExpand/Domains.pm  view on Meta::CPAN

}


# DEL response handler
# STATIC
sub delete_response_parse {
    # stub
}


# Get all element data
# STATIC
sub get {
    my %params = @_;

    unless ($params{all}) {
        return '';
    }

    #return '<get><filter></filter><dataset><gen_info/></dataset></get><!-- create_domain -->';

    return create_node( 'get',
        create_node('filter', '') . create_node( 'dataset', create_node('gen_info') )
    ) . '<!-- create_domain -->';
}


# GET response handler 
# STATIC
sub get_response_parse {
    my $answer = abstract_parser('get', +shift, [ ], 'system_error' );

    if (ref $answer eq 'ARRAY') {
        for my $domain (@$answer) {
            $domain->{data} = xml_extract_values($domain->{data} =~ m#<gen_info>(.*?)</gen_info>#);
        }
    } elsif ($answer) {
        $answer->{data} = xml_extract_values($answer->{data} =~ m#<gen_info>(.*?)</gen_info>#);
    }

    return $answer;
}


1;
__END__
=head1 SEE ALSO

t/expand.t  view on Meta::CPAN


{
    our $our_warning;
    local $SIG{__DIE__} = sub { $our_warning = shift; }; # confess <=> die
    eval { API::PleskExpand->new(%TestData::online_expand_valid_params)->aaa__bbbccc() };
    like($our_warning, qr/aaa__bbbccc/,
         'Checking AUTOLOAD by calling undefined method.');
}


my %create_account_data = (
    'select'        => 'optimal',
    'template-id'   =>  1,
    'general_info'  => {
        login   => 'suxdffffxx',
        pname   => 'stdsdffafff',
        passwd  => '1234d5678',
        status  => 0,
        cname   => '',
        phone   => '',
        fax     => '',
        email   => '',
        address => '',
        city    => '',
        state   => '',
        pcode   => '',
        country => 'RU',
    }
);


my $create_request = API::PleskExpand::Accounts::create( %create_account_data );

is_string($create_request, $_, 'create account test') for
'<add_use_template><gen_info><address></address><city></city><cname></cname>'          .
'<country>RU</country><email></email><fax></fax><login>suxdffffxx</login>'             .
'<passwd>1234d5678</passwd><pcode></pcode><phone></phone><pname>stdsdffafff</pname>'   .
'<state></state><status>0</status></gen_info><!-- create_client -->'                   .
'<tmpl_id>1</tmpl_id><server_auto><optimal></optimal></server_auto></add_use_template>';

$create_request = API::PleskExpand::Accounts::create(
    %create_account_data,
    'attach_to_template' => 1 
);

is_string($create_request, $_, 'create account test') for 
'<add_use_template><gen_info><address></address><city></city><cname></cname>'        .
'<country>RU</country><email></email><fax></fax><login>suxdffffxx</login>'           .
'<passwd>1234d5678</passwd><pcode></pcode><phone></phone><pname>stdsdffafff</pname>' .
'<state></state><status>0</status></gen_info><!-- create_client -->'                 .
'<tmpl_id>1</tmpl_id><attach_to_template></attach_to_template><server_auto>'         .
'<optimal></optimal></server_auto></add_use_template>';


is_deeply(
    API::PleskExpand::Accounts::create( %create_account_data, select => ''),  
    '',
    'Manual select without server_id param'
);

like(
    API::PleskExpand::Accounts::create( %create_account_data, select => '', server_id => 5),  
    qr#<tmpl_id>1</tmpl_id><server_id>5</server_id></add_use_template>$#,
    'Manual select without server_id param'
);

like(
    API::PleskExpand::Accounts::create(
        %create_account_data,
        select   => 'optimal',
        group_id => 2
    ),
    qr#<tmpl_id>1</tmpl_id><server_auto><optimal></optimal><group_id>2</group_id></server_auto></add_use_template>$#x,
    'Select "Optimal server" with group_id',
);

like(
    API::PleskExpand::Accounts::create(
        %create_account_data,
        select         => 'optimal',
        server_keyword => 'Hosting'
    ),
    qr#<server_auto><optimal></optimal></server_auto><server_keyword>Hosting</server_keyword></add_use_template>$#,
    'Select "Optimal server" with keyword',
);

like(
    API::PleskExpand::Accounts::create( %create_account_data, select => 'min_domains', server_keyword => 'Hosting'),
    qr#<server_auto><min_domains></min_domains></server_auto><server_keyword>Hosting</server_keyword></add_use_template>$#x,
    'Select server has "max_diskspace" with keyword',
);


like(
    API::PleskExpand::Accounts::create( %create_account_data, select => 'max_diskspace', server_keyword => 'Hosting'),
    qr#<server_auto><max_diskspace></max_diskspace></server_auto><server_keyword>Hosting</server_keyword></add_use_template>$#,
    'Select server has "min_domains" with keyword',
);



my $delete_query = API::PleskExpand::Accounts::delete( id => 15 );

is_deeply( $delete_query . "\n", <<DOC, 'delete account test');
<del><!-- del_client --><filter><id>15</id></filter></del>

t/expand.t  view on Meta::CPAN

my $modify_query_alter = API::PleskExpand::Accounts::modify(
    id => 5, 
    general_info => { status => 0 } # deactivate!
);


is_string( $modify_query_alter, $_, 'modify account test') for 
'<set><filter><id>5</id></filter><!-- modify_client --><values>' .
'<gen_info><status>0</status></gen_info></values></set>';

my %new_domain_data = (
    dname               => 'y2a1ddsdfandex.ru',
    client_id           => 16,
    'template-id'       => 1,
    ftp_login           => 'nrddgddsdasd',
    ftp_password        => 'dadsdasd',
);

my $create_domain = API::PleskExpand::Domains::create( %new_domain_data );


is_string( $create_domain, $_, 'modify account test') for 
'<add_use_template><gen_setup><name>y2a1ddsdfandex.ru</name>'  .
'<client_id>16</client_id><status>0</status></gen_setup>'      .
'<hosting><vrt_hst><ftp_login>nrddgddsdasd</ftp_login>'        .
'<ftp_password>dadsdasd</ftp_password></vrt_hst></hosting>'    .
'<!-- create_domain --><tmpl_id>1</tmpl_id></add_use_template>';

$create_domain = API::PleskExpand::Domains::create( %new_domain_data, attach_to_template  => 1 );

is_string( $create_domain, $_, 'modify account test') for
'<add_use_template><gen_setup><name>y2a1ddsdfandex.ru</name>' .
'<client_id>16</client_id><status>0</status></gen_setup>'     .
'<hosting><vrt_hst><ftp_login>nrddgddsdasd</ftp_login>'       .
'<ftp_password>dadsdasd</ftp_password></vrt_hst></hosting>'   .
'<!-- create_domain --><tmpl_id>1</tmpl_id>'                  .
'<attach_to_template></attach_to_template></add_use_template>';

$expand_client->{dump_headers} = 1; # debugg =)

t/expand.t  view on Meta::CPAN

        server_id   => 1,
    },
    'parse fail add domain xml response')
for '<?xml version="1.0" encoding="UTF-8" standalone="no" ?><packet version="2.2.4.1"><add_use_template>' .
    '<result><status>error</status><errcode>4304</errcode><errtext>[Operator] Domain already exists.'     .
    " Plesk domain 'yandex.ru' is exist.</errtext><client_id>40</client_id><server_id>1</server_id>"      .
    '<tmpl_id>1</tmpl_id><expiration>-1</expiration></result></add_use_template></packet>';

is_string(
    API::PleskExpand::Domains::get(all => 1),
    '<get><filter></filter><dataset><gen_info/></dataset></get><!-- create_domain -->',
    'Domains get'
);



exit unless $ENV{'online_stress_tests'};

my ($domain_template_id, $client_template_id);
$domain_template_id = $client_template_id = $ENV{template_id} || 1;

t/expand.t  view on Meta::CPAN

        city    => '',
        state   => '',
        pcode   => '',
        country => 'RU',
    }
);

if ($create_account_result->is_success) {
    #warn Dumper $create_account_result;

    my $client_id =  $create_account_result->{answer_data}->[0]->{id};
    my $server_id =  $create_account_result->{answer_data}->[0]->{server_id};

    pass "Account succcessful created!";

    my $deactivate_result = $expand_client->Accounts->modify(
        general_info => { status => 16 }, # deactivate! 
        id           => $client_id,
    );

    if ($deactivate_result->is_success) {
        pass "Deactivation success!";

        my $activate_result = $expand_client->Accounts->modify(
            general_info => { status => 0 }, # activate! 
            id           => $client_id,
        );

        my $plesk_id = $activate_result->get_data->[0]->{plesk_client_id};

        if ($activate_result->is_success) {
            pass "Activation success!";

            
            my $create_domain = $expand_client->Domains->create(
                dname                => $login . '.ru',
                client_id            => $client_id,
                'template-id'        => $domain_template_id,
                'attach_to_template' => 1,

t/expand.t  view on Meta::CPAN

        
            if ($create_domain->is_success) {
   
                pass "Create domain successful";

                my $nop_result = $expand_client->Accounts->modify(
                    general_info => { },         # blank operation
                    id           => $client_id,
                );

                if ($nop_result->is_success && $nop_result->get_data->[0]->{plesk_client_id} eq $plesk_id) {
                    pass "Get plesk_id $plesk_id success";
                        
                    my $delete_result = $expand_client->Accounts->delete(
                        id => $client_id,
                    );
        
                    if ( $delete_result->is_success ) {
                        pass "Delete account success";
                    } else {
                        fail "Remove account failed";   



( run in 0.354 second using v1.01-cache-2.11-cpan-496ff517765 )