Commit 48bab647 authored by Francesc Guasch's avatar Francesc Guasch
Browse files

added id_owner mandatory for creating a new domain

parent 40d60811
......@@ -3,7 +3,7 @@ package Ravada;
use warnings;
use strict;
use Carp qw(carp);
use Carp qw(carp croak);
use Data::Dumper;
use DBIx::Connector;
use Moose;
......@@ -167,6 +167,9 @@ sub create_domain {
my %args = @_;
croak "Argument id_owner required "
if !$args{id_owner};
my $vm_name = $args{vm};
delete $args{vm};
......
......@@ -4,6 +4,7 @@ use warnings;
use strict;
use Carp qw(confess croak cluck);
use Data::Dumper;
use Moose::Role;
our $TIMEOUT_SHUTDOWN = 20;
......@@ -104,7 +105,8 @@ sub _prepare_base_db {
my $file_img = shift;
if (!$self->_select_domain_db) {
$self->_insert_db( name => $self->name );
confess "CRITICAL: The data should be already inserted";
# $self->_insert_db( name => $self->name, id_owner => $self->id_owner );
}
my $sth = $$CONNECTOR->dbh->prepare(
"UPDATE domains set is_base=1,file_base_img=? "
......@@ -118,8 +120,12 @@ sub _prepare_base_db {
sub _insert_db {
my $self = shift;
my %field = @_;
croak "Field name is mandatory ".Dumper(\%field)
if !exists $field{name};
for (qw(name id_owner)) {
confess "Field $_ is mandatory ".Dumper(\%field)
if !exists $field{$_};
}
my $query = "INSERT INTO domains "
."(" . join(",",sort keys %field )." )"
." VALUES (". join(",", map { '?' } keys %field )." ) "
......@@ -159,4 +165,10 @@ sub is_base {
return $self->_data('is_base');
};
sub id_owner {
my $self = shift;
return $self->_data('id_owner',@_);
}
1;
......@@ -3,7 +3,7 @@ package Ravada::Domain::KVM;
use warnings;
use strict;
use Carp qw(cluck croak);
use Carp qw(cluck confess croak);
use Data::Dumper;
use IPC::Run3 qw(run3);
use Moose;
......
......@@ -10,13 +10,21 @@ use Moose;
with 'Ravada::Domain';
sub display {}
sub is_active {}
has 'domain' => (
is => 'ro'
,isa => 'Str'
,required => 1
);
#######################################3
sub name {
sub name {
my $self = shift;
return $self->domain;
}
};
sub display {}
sub is_active {}
sub pause {}
sub remove {}
......
......@@ -26,6 +26,7 @@ our %VALID_ARG = (
,name => 1
,id_iso => 1
,id_base => 1
,id_owner => 1
,id_template => 1
}
);
......
......@@ -132,6 +132,7 @@ sub create_domain {
$args{active} = 1 if !defined $args{active};
croak "argument name required" if !$args{name};
croak "argument id_owner required" if !$args{id_owner};
croak "argument id_iso or id_base required ".Dumper(\%args)
if !$args{id_iso} && !$args{id_base};
......@@ -274,8 +275,10 @@ sub _domain_create_from_iso {
my $self = shift;
my %args = @_;
croak "argument id_iso required"
if !$args{id_iso};
for (qw(id_iso id_owner)) {
croak "argument $_ required"
if !$args{$_};
}
die "Domain $args{name} already exists"
if $self->search_domain($args{name});
......@@ -298,7 +301,7 @@ sub _domain_create_from_iso {
$dom->create if $args{active};
my $domain = Ravada::Domain::KVM->new(domain => $dom , storage => $self->storage_pool);
$domain->_insert_db(name => $args{name});
$domain->_insert_db(name => $args{name}, id_owner => $args{id_owner});
return $domain;
}
sub _create_disk {
......@@ -393,7 +396,7 @@ sub _domain_create_from_base {
my $domain = Ravada::Domain::KVM->new(domain => $dom , storage => $self->storage_pool);
$domain->_insert_db(name => $args{name}, id_base => $base->id);
$domain->_insert_db(name => $args{name}, id_base => $base->id, id_owner => $args{id_owner});
return $domain;
}
......
......@@ -28,9 +28,12 @@ sub create_domain {
$args{active} = 1 if !defined $args{active};
croak "argument name required" if !$args{name};
croak "argument id_owner required" if !$args{id_owner};
my $domain = Ravada::Domain::Void->new(name => $args{name});
$domain->_insert_db(name => $args{name});
my $domain = Ravada::Domain::Void->new(name => $args{name}, domain => $args{name}
, id_owner => $args{id_owner}
);
$domain->_insert_db(name => $args{name} , id_owner => $args{id_owner});
return $domain;
}
......
......@@ -7,6 +7,7 @@ CREATE TABLE `domains` (
`uri` varchar(250) DEFAULT NULL,
`is_base` int(1) NOT NULL DEFAULT '0',
`file_base_img` varchar(255) DEFAULT NULL,
`id_owner` int(11) NOT NULL,
UNIQUE (`id_base`,`name`),
UNIQUE (`name`)
);
......@@ -17,6 +17,7 @@ my $DOMAIN_NAME_SON=$DOMAIN_NAME."_son";
my @ARG_CREATE_DOM = (
id_iso => 1
,id_owner => 1
);
......@@ -182,10 +183,10 @@ ok($ravada,"I can't launch a new Ravada");# or exit;
my ($vm_kvm, $vm_lxc);
eval { $vm_kvm = $ravada->search_vm('kvm') if $ravada;
@ARG_CREATE_DOM = ( id_iso => 1, vm => 'kvm' ) if $vm_kvm;
@ARG_CREATE_DOM = ( id_iso => 1, vm => 'kvm', id_owner => 1 ) if $vm_kvm;
};
eval { $vm_lxc = $ravada->search_vm('lxc') if $ravada;
@ARG_CREATE_DOM = ( id_template => 1, vm => 'LXC' ) if $vm_lxc;
@ARG_CREATE_DOM = ( id_template => 1, vm => 'LXC', id_owner => 1 ) if $vm_lxc;
};
SKIP: {
......
......@@ -153,11 +153,11 @@ my $vmm;
eval {
$vmm = $RAVADA->search_vm('kvm');
@ARG_CREATE_DOM = ( id_iso => 1, vm => 'kvm' ) if $vmm;
@ARG_CREATE_DOM = ( id_iso => 1, vm => 'kvm', id_owner => 1 ) if $vmm;
if (!$vmm) {
$vmm = $RAVADA->search_vm('lxc');
@ARG_CREATE_DOM = ( id_template => 1, vm => 'LXC' );
@ARG_CREATE_DOM = ( id_template => 1, vm => 'LXC', id_owner => 1 );
}
} if $RAVADA;
......
......@@ -43,8 +43,8 @@ sub test_empty {
sub test_add_domain_db {
my $sth = $test->dbh->prepare("INSERT INTO domains "
."(name) VALUES (?)");
$sth->execute('a');
."(name, id_owner) VALUES (?,?)");
$sth->execute('a',1);
my $domains = $RVD_FRONT->list_domains();
ok($domains,"No domains list returned");
......
......@@ -24,8 +24,8 @@ my $RVD_FRONT = Ravada::Front->new( @rvd_args
);
my %CREATE_ARGS = (
kvm => { id_iso => 1 }
,lxc => { id_template => 1 }
kvm => { id_iso => 1, id_owner => 1 }
,lxc => { id_template => 1, id_owner => 1 }
);
###################################################################
......
......@@ -81,6 +81,7 @@ sub test_new_domain {
diag("Creating domain $name");
my $domain = $RAVADA->create_domain(name => $name, id_iso => 1, active => $active
, id_owner => 1
, vm => $BACKEND
);
......@@ -224,9 +225,6 @@ sub test_prepare_import {
if (ok($domain,"test domain not created")) {
my $sth = $test->connector->dbh->prepare("DELETE FROM domains WHERE id=?");
$sth->execute($domain->id);
test_prepare_base($domain);
ok($domain->is_base,"Domain should be base"
.Dumper($domain->_select_domain_db())
......
......@@ -66,6 +66,7 @@ sub test_new_domain_from_iso {
eval { $domain = $RAVADA->create_domain(name => $name
, id_iso => 1
,vm => $BACKEND
,id_owner => 1
)
};
ok(!$@,"Domain $name not created: $@");
......@@ -123,6 +124,7 @@ sub test_new_domain_from_base {
my $domain = $RAVADA->create_domain(
name => $name
,id_base => $base->id
,id_owner => 1
,vm => $BACKEND
);
ok($domain,"Domain not created");
......
......@@ -70,7 +70,7 @@ $name .= "_0";
test_remove_domain($name);
my $domain = $VMM->create_domain(name => $name, id_iso => 1 , active => 0);
my $domain = $VMM->create_domain(name => $name, id_iso => 1 , active => 0, id_owner => 1);
ok($domain,"Domain not created") and do {
......
......@@ -63,6 +63,7 @@ sub test_req_clone {
my $req = Ravada::Request->create_domain(
name => $name
,id_base => $domain_father->id
,id_owner => 1
,vm => $BACKEND
);
ok($req);
......@@ -100,6 +101,7 @@ sub test_req_create_domain_iso {
my $req = Ravada::Request->create_domain(
name => $name
,id_iso => 1
,id_owner => 1
,vm => $BACKEND
);
ok($req);
......@@ -129,6 +131,7 @@ sub test_force_kvm {
my $req = Ravada::Request->create_domain(
name => $name
,id_iso => 1
,id_owner => 1
,vm => 'kvm'
);
ok($req);
......
......@@ -14,7 +14,7 @@ use_ok('Ravada::Auth::SQL');
my $test = Test::SQL::Data->new(config => 't/etc/sql.conf');
my $ravada = Ravada->new(connector => $test->connector);
my $ravada = rvd_back($test->connector , 't/etc/ravada.conf');
my $name = 'foo';
Ravada::Auth::SQL::add_user($name, 'bar',);
......@@ -26,9 +26,14 @@ my $vm = Ravada::VM::Void->new();
ok($vm,"I can't create void VM");
my $domain_name = new_domain_name();
my $domain = $vm->create_domain(name => $domain_name, owner => $user->id);
my $domain = $vm->create_domain(name => $domain_name, id_owner => $user->id);
ok($domain,"No domain $domain_name created");
ok($domain->name eq $domain_name, "Expecting domain name $domain_name , got "
.($domain->name or '<UNDEF>'));
ok($domain->id_owner
&& $domain->id_owner eq $user->id,"Expecting owner=".$user->id." , got ".$domain->id_owner);
done_testing();
......@@ -68,9 +68,17 @@ sub test_create_domain {
my @arg_create = @{$ARG_CREATE_DOM{$vm_name}};
my $domain;
eval { $domain = $vm->create_domain(name => $name, @{$ARG_CREATE_DOM{$vm_name}}) };
eval { $domain = $vm->create_domain(name => $name
, id_owner => 1
, @{$ARG_CREATE_DOM{$vm_name}})
};
ok($domain,"No domain $name created with ".ref($vm)." ".($@ or '')) or exit;
ok($domain->name
&& $domain->name eq $name,"Expecting domain name '$name' , got "
.($domain->name or '<UNDEF>')
." for VM $vm_name"
);
}
#######################################################
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment