| 1 | package Odoo::Database::Manager; | ||||||||||||
| 2 | 2 | ▶ | use v5.20; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 2 | our $VERSION = '0.02'; | |||||||||||
| 7 | |||||||||||||
| 8 | 2 | ▶ | use JSON::RPC2::Client; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use JSON::XS; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use HTTP::Request; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | |||||||||||||
| 14 | 2 | ▶ | use failures qw/odoo::rpc::http odoo::rpc::http::connection odoo::rpc::response odoo::rpc::method odoo::nopassword/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 2 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 2 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | 2 | has url => (is => 'ro', isa => 'Str', default => 'http://localhost:8069'); | |||||||||||
| 20 | 2 | has password => (is => 'ro', required => 0, isa => 'Str', default => 'admin'); | |||||||||||
| 21 | |||||||||||||
| 22 | 2 | has ua => (is => 'ro', lazy => 1, builder => 'build_ua'); | |||||||||||
| 23 | 2 | has json_rpc_client => (is => 'ro', lazy => 1, builder => 'build_json_rpc_client'); | |||||||||||
| 24 | |||||||||||||
| 25 | sub build_ua { | ||||||||||||
| 26 | 1 | ▶ | return LWP::UserAgent->new(); | ||||||||||
|
Subroutine
build_ua: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | sub build_json_rpc_client { | ||||||||||||
| 30 | 1 | ▶ | return JSON::RPC2::Client->new(); | ||||||||||
|
Subroutine
build_json_rpc_client: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | sub list_databases { | ||||||||||||
| 34 | 1 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
list_databases: called CC=1 SCAR=0.4
Pod
documented
| |||||||||||||
| 35 | 1 | my $dbs = $self->_execute('/web/database/get_list', 'call'); | |||||||||||
| 36 | 0 | return @$dbs; | |||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | sub createdb { | ||||||||||||
| 40 | 0 | ▶ | my ($self, %params) = @_; | ||||||||||
|
Subroutine
createdb: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 41 | 0 | ▶ | my $dbname = $params{dbname} or die 'must specify dbname'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 0 | ▶ | my $lang = $params{lang} or die 'must specify lang'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 0 | ▶ | my $admin_password = $params{admin_password} or die 'must specify admin_password'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | 0 | ▶ | failure::odoo::nopassword->throw('password not specified') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | unless defined $self->password; | ||||||||||||
| 46 | # TODO support demo option | ||||||||||||
| 47 | 0 | $self->_execute('/web/database/create', 'call', [$self->_cvt_params( | |||||||||||
| 48 | super_admin_pwd => $self->password, | ||||||||||||
| 49 | db_name => $dbname, | ||||||||||||
| 50 | db_lang => $lang, | ||||||||||||
| 51 | create_admin_pwd => $admin_password, | ||||||||||||
| 52 | create_confirm_pwd => $admin_password, | ||||||||||||
| 53 | )]); | ||||||||||||
| 54 | # {"jsonrpc":"2.0","method":"call","params":{"fields":[{"name":"super_admin_pwd","value":"admin"},{"name":"db_name","value":"another"},{"name":"db_lang","value":"en_GB"},{"name":"create_admin_pwd","value":"password"},{"name":"create_confirm_pwd","value":"password"}]},"id":776199543} | ||||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | sub _cvt_params { | ||||||||||||
| 58 | 0 | ▶ | my ($self, %params) = @_; | ||||||||||
|
Subroutine
_cvt_params: not called CC=1 SCAR=6.9
| |||||||||||||
| 59 | 0 | return (fields => [ map { { name => $_, value => $params{$_} } } keys(%params) ]); | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | sub dropdb { | ||||||||||||
| 63 | 0 | ▶ | my ($self, $dbname) = @_; | ||||||||||
|
Subroutine
dropdb: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 64 | 0 | ▶ | die 'must specify $dbname' unless $dbname; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 0 | ▶ | failure::odoo::nopassword->throw('password not specified') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | unless defined $self->password; | ||||||||||||
| 67 | 0 | $self->_execute('/web/database/drop', 'call', [ | |||||||||||
| 68 | $self->_cvt_params( drop_db => $dbname, drop_pwd => $self->password ), | ||||||||||||
| 69 | ]); | ||||||||||||
| 70 | #{"jsonrpc":"2.0","method":"call","params":{"fields":[{"name":"drop_db","value":"another"},{"name":"drop_pwd","value":"wrongpass"}]},"id":692816518} | ||||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | sub _execute { | ||||||||||||
| 74 | 1 | ▶ | my ($self, $urlpart, $method, @args) = @_; | ||||||||||
|
Subroutine
_execute: called CC=6 SCAR=22.3
| |||||||||||||
| 75 | |||||||||||||
| 76 | 1 | my $url = $self->url . $urlpart; # TODO Use a URI library | |||||||||||
| 77 | 1 | my $jsonreq = $self->json_rpc_client->call($method, @args); | |||||||||||
| 78 | 1 | my $json = decode_json($jsonreq); | |||||||||||
| 79 | 1 | ▶ | $json->{params} //= {}; | ||||||||||
|
Condition50%1/2
$json->{"params"} //= {}
MC/DC0%0/1
$json->{"params"} //= {}
$json->{"params"}
Truth table50%1/2
A
A= $json->{"params"}
| |||||||||||||
| 80 | 1 | $jsonreq = encode_json($json); | |||||||||||
| 81 | 1 | my $request = HTTP::Request->new(POST => $url); | |||||||||||
| 82 | 1 | $request->content_type('application/json'); | |||||||||||
| 83 | 1 | $request->header(Accept => 'application/json, text/javascript, */*; q=0.01'); | |||||||||||
| 84 | 1 | $request->content($jsonreq); | |||||||||||
| 85 | |||||||||||||
| 86 | 1 | my $res = $self->ua->request($request); | |||||||||||
| 87 | |||||||||||||
| 88 | 1 | ▶ | unless ($res->is_success) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 1 | ▶ | my $subclass = ($res->status_line =~ /^500 Can't connect to/) ? "::connection" : ""; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 1 | "failure::odoo::rpc::http$subclass"->throw({ | |||||||||||
| 91 | msg => $res->status_line, | ||||||||||||
| 92 | payload => { result => $res } | ||||||||||||
| 93 | }); | ||||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 0 | my ($failed, $result, $error) = $self->json_rpc_client->response($res->content); | |||||||||||
| 97 | 0 | ▶ | failure::odoo::rpc::response->throw($failed) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | if $failed; | ||||||||||||
| 99 | |||||||||||||
| 100 | 0 | ▶ | if ($error) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | 0 | my $code = $error->{code}; | |||||||||||
| 102 | 0 | my $message = $error->{message}; | |||||||||||
| 103 | 0 | failure::odoo::rpc::method->throw({ | |||||||||||
| 104 | msg => qq/method $method failed with code=$code: $message/, | ||||||||||||
| 105 | payload => $error, | ||||||||||||
| 106 | }); | ||||||||||||
| 107 | } | ||||||||||||
| 108 | 0 | return $result; | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | 2 | 1; | |||||||||||
| 112 | |||||||||||||
| 113 | __END__ | ||||||||||||
| 114 | |||||||||||||
| 115 | =pod | ||||||||||||
| 116 | |||||||||||||
| 117 | =encoding UTF-8 | ||||||||||||
| 118 | |||||||||||||
| 119 | =head1 NAME | ||||||||||||
| 120 | |||||||||||||
| 121 | Odoo::Database::Manager | ||||||||||||
| 122 | |||||||||||||
| 123 | =head1 VERSION | ||||||||||||
| 124 | |||||||||||||
| 125 | version 0.02 | ||||||||||||
| 126 | |||||||||||||
| 127 | =head1 SYNOPSIS | ||||||||||||
| 128 | |||||||||||||
| 129 | use v5.20; | ||||||||||||
| 130 | use Odoo::Database::Manager; | ||||||||||||
| 131 | |||||||||||||
| 132 | my $dbman = Odoo::Database::Manager->new( | ||||||||||||
| 133 | url => 'http://localhost:8069', | ||||||||||||
| 134 | password => 'manager_admin_password'); | ||||||||||||
| 135 | |||||||||||||
| 136 | say "Current databases:"; | ||||||||||||
| 137 | say for $dbman->list_databases; | ||||||||||||
| 138 | |||||||||||||
| 139 | say "Creating new database foo:"; | ||||||||||||
| 140 | $dbman->createdb(dbname => 'foo', lang => 'en_GB', admin_password => 'admin'); | ||||||||||||
| 141 | |||||||||||||
| 142 | say "Dropping foo again:"; | ||||||||||||
| 143 | $dbman->dropdb('foo'); | ||||||||||||
| 144 | |||||||||||||
| 145 | =head1 DESCRIPTION | ||||||||||||
| 146 | |||||||||||||
| 147 | Create and drop Odoo databases from your Perl scripts | ||||||||||||
| 148 | |||||||||||||
| 149 | =head1 NAME | ||||||||||||
| 150 | |||||||||||||
| 151 | Odoo::Database::Manager - database management for Odoo (EXPERIMENTAL) | ||||||||||||
| 152 | |||||||||||||
| 153 | =head1 STATUS | ||||||||||||
| 154 | |||||||||||||
| 155 | EXPERIMENTAL | ||||||||||||
| 156 | |||||||||||||
| 157 | =head1 METHODS | ||||||||||||
| 158 | |||||||||||||
| 159 | =head2 list_databases | ||||||||||||
| 160 | |||||||||||||
| 161 | Return list of Odoo databases. | ||||||||||||
| 162 | |||||||||||||
| 163 | my @dbs = $dbman->list_databases; | ||||||||||||
| 164 | |||||||||||||
| 165 | =head2 createdb | ||||||||||||
| 166 | |||||||||||||
| 167 | Create a database. No meaningful return value as yet. | ||||||||||||
| 168 | |||||||||||||
| 169 | $dbman->createdb(dbname => 'my_db', lang => 'en_GB', admin_password => 'password'); | ||||||||||||
| 170 | |||||||||||||
| 171 | =head2 dropdb | ||||||||||||
| 172 | |||||||||||||
| 173 | Drop the database. No meaningful return value as yet. | ||||||||||||
| 174 | |||||||||||||
| 175 | $dbman->dropdb('my_db'); | ||||||||||||
| 176 | |||||||||||||
| 177 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 178 | |||||||||||||
| 179 | Copyright (C) 2016 Nick Booker | ||||||||||||
| 180 | |||||||||||||
| 181 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 182 | under the terms of either: the GNU General Public License as published | ||||||||||||
| 183 | by the Free Software Foundation; or the Artistic License. | ||||||||||||
| 184 | |||||||||||||
| 185 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||
| 186 | |||||||||||||
| 187 | =head1 AUTHOR | ||||||||||||
| 188 | |||||||||||||
| 189 | Nicholas Booker <nmb+cpan@nickbooker.uk> | ||||||||||||
| 190 | |||||||||||||
| 191 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 192 | |||||||||||||
| 193 | This software is copyright (c) 2016 by Nicholas Booker. | ||||||||||||
| 194 | |||||||||||||
| 195 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 196 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 197 | |||||||||||||
| 198 | =cut | ||||||||||||