Unverified Commit fffe566c authored by Francesc Guasch's avatar Francesc Guasch Committed by GitHub
Browse files

Fix: remove hardware by index and attribute (#1707)

refactor: remove when searching by attribute and index
parent 59e3f7a8
......@@ -2349,6 +2349,12 @@ sub _find_child($controller, $name) {
}
sub _remove_device($self, $index, $device, $attribute_name0=undef, $attribute_value=undef) {
confess "Error: I need index defined or attribute name=value"
if !defined $index && !defined $attribute_name0;
confess "Error: I attribute value to search must be defined"
if defined $attribute_name0 && !defined $attribute_value;
my $doc = XML::LibXML->load_xml(string => $self->xml_description_inactive);
my ($devices) = $doc->findnodes('/domain/devices');
my $ind=0;
......@@ -2361,11 +2367,17 @@ sub _remove_device($self, $index, $device, $attribute_name0=undef, $attribute_va
my $found_value = $item->getAttribute($attr_name);
push @found,($found_value or '');
$found = 1
if defined $found_value && $found_value =~ $attribute_value;
if ( $found_value =~ $attribute_value ) {
$found=1 if !defined $index || $ind == $index;
$ind++;
}
} else {
$found = 1 if defined $index && $ind == $index;
$ind++;
}
if($found || defined $index && $ind++==$index ){
if($found ){
my ($source ) = $controller->findnodes("source");
my $file;
$file = $source->getAttribute('file') if $source;
......@@ -2420,7 +2432,7 @@ sub _remove_controller_disk($self, $index, $attribute_name=undef, $attribute_va
}
sub _remove_controller_network($self, $index) {
$self->_remove_device($index,'interface', type => qr'(bridge|network)');
$self->_remove_device($index,'interface' );
}
=head2 pre_remove
......
......@@ -782,7 +782,7 @@ sub _validate_start_domain($self) {
next if !$req;
next if $req->at_time;
next if $command eq 'start' && !$req->after_request();
$self->after_request($req->id);
$self->after_request($req->id) if $req->id < $self->id;
}
}
......@@ -798,7 +798,7 @@ sub _validate_change_hardware($self) {
return if !$id_domain;
my $req = $self->_search_request('%_hardware', id_domain => $id_domain);
$self->after_request($req->id) if $req;
$self->after_request($req->id) if $req && $req->id < $self->id;
}
sub _validate_create_domain($self) {
......
......@@ -635,17 +635,13 @@
}
var file = $scope.showmachine.hardware.disk[index].file;
if (typeof(file) != 'undefined' && file) {
$scope.showmachine.requests++;
$http.post('/request/remove_hardware/'
,JSON.stringify({
$scope.request('remove_hardware'
,{
'id_domain': $scope.showmachine.id
,'name': 'disk'
,'option': { 'source/file': file }
})
).then(function(response) {
});
item.remove = false;
return;
}
......@@ -654,10 +650,11 @@
if(typeof(item) == 'object') {
item.remove = false;
}
$http.get('/machine/hardware/remove/'
+$scope.showmachine.id+'/'+hardware+'/'+index).then(function(response) {
});
$scope.request('remove_hardware',{
'id_domain': $scope.showmachine.id
,'name': hardware
,'index': index
});
};
$scope.list_ldap_attributes= function() {
$scope.ldap_entries = 0;
......
......@@ -321,6 +321,84 @@ sub test_add_hardware_custom($domain, $hardware) {
return $exec->($domain);
}
sub _set_three_devices($domain, $hardware) {
my %drivers = map { $_ => 1 } @{$domain->info(user_admin)->{drivers}->{$hardware}};
my $info_hw = $domain->info(user_admin)->{hardware};
my $items = [];
$items = $info_hw->{$hardware};
for my $item (@$items) {
delete $drivers{$item->{driver}} if ref($item);
}
for (1 .. 3-scalar(@$items)) {
my @driver;
if ($hardware eq 'display') {
my ($driver) = keys %drivers;
delete $drivers{$driver};
@driver =( data => { driver => $driver } );
}
Ravada::Request->add_hardware(
uid => user_admin->id
,id_domain => $domain->id
,name => $hardware
,@driver
);
}
wait_request(debug => 0);
}
sub test_remove_hardware_by_index_network_kvm($vm, $hardware) {
return if $hardware ne 'network' || $vm->type ne 'KVM';
my $domain = create_domain($vm);
_set_three_devices($domain, $hardware);
my $info_hw1 = $domain->info(user_admin)->{hardware};
my $items1 = [];
$items1 = $info_hw1->{$hardware};
$domain->_remove_device(1,"interface", type => qr'(bridge|network)');
my $info_hw2 = $domain->info(user_admin)->{hardware};
my $items2 = [];
$items2 = $info_hw2->{$hardware};
is($items2->[0]->{name},$items1->[0]->{name});
is($items2->[1]->{name},$items1->[2]->{name});
remove_domain($domain);
}
sub test_remove_hardware_by_index($vm, $hardware) {
return if $hardware eq 'usb';
my $domain = create_domain($vm);
_set_three_devices($domain, $hardware);
my $info_hw1 = $domain->info(user_admin)->{hardware};
my $items1 = [];
$items1 = $info_hw1->{$hardware};
Ravada::Request->remove_hardware(
uid => user_admin->id
,id_domain => $domain->id
,name => $hardware
,index => 1
);
wait_request();
my $info_hw2 = $domain->info(user_admin)->{hardware};
my $items2 = [];
$items2 = $info_hw2->{$hardware};
if (!ref($items2->[0])) {
is($items2->[0], $items1->[0]);
is($items2->[1], $items1->[2]);
} else {
is($items2->[0]->{name},$items1->[0]->{name});
is($items2->[1]->{name},$items1->[2]->{name});
}
$domain->remove(user_admin);
}
sub test_remove_hardware($vm, $domain, $hardware, $index) {
$domain->shutdown_now(user_admin) if $domain->is_active;
......@@ -783,6 +861,7 @@ ok($rvd_back,"Launch Ravada");# or exit;
ok($Ravada::CONNECTOR,"Expecting conector, got ".($Ravada::CONNECTOR or '<unde>'));
clean();
remove_old_domains();
remove_old_disks();
......@@ -808,6 +887,9 @@ for my $vm_name ( vm_names()) {
for my $hardware ( reverse sort keys %controllers ) {
diag("Testing $hardware controllers for VM $vm_name");
test_remove_hardware_by_index($vm, $hardware);
test_remove_hardware_by_index_network_kvm($vm, $hardware);
test_front_hardware($vm, $domain_b, $hardware);
test_add_hardware_custom($domain_b, $hardware);
......
......@@ -614,8 +614,6 @@ for my $vm_name (vm_names() ) {
}
}
}
test_add_disk_boot_order($vm);
test_cdrom($vm);
for my $iso_name ('Alpine%64 bits', 'Alpine%32 bits') {
......
......@@ -6,8 +6,8 @@
</div>
<div class="col-lg-2">
<select name="device" ng-model="add_disk.device">
<option><%=l 'disk' %></option>
<option><%=l 'cdrom' %></option>
<option value="disk" selected><%=l 'disk' %></option>
<option value="cdrom"><%=l 'cdrom' %></option>
</select>
</div>
</div>
......@@ -50,7 +50,7 @@
<input type="text" name="allocation" ng-model="add_disk.allocation">
</div>
</div>
<div class="row" ng-show="add_disk.device == 'disk'">
<div class="row">
<div class="col-lg-2">
<label for="bus"><%=l 'bus' %></label>
</div>
......
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