Skip to content

Commit

Permalink
Improve validation for virt volumes (#15474)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qubad786 authored Jan 24, 2025
1 parent 03d7282 commit 348bff7
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/middlewared/middlewared/plugins/virt/volume.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import errno

from middlewared.api import api_method
from middlewared.api.current import (
VirtVolumeEntry, VirtVolumeCreateArgs, VirtVolumeCreateResult, VirtVolumeUpdateArgs,
VirtVolumeUpdateResult, VirtVolumeDeleteArgs, VirtVolumeDeleteResult, VirtVolumeImportISOArgs,
VirtVolumeImportISOResult,
)
from middlewared.service import CallError, CRUDService, job
from middlewared.service import CallError, CRUDService, job, ValidationErrors
from middlewared.utils import filter_list

from .utils import incus_call, incus_call_sync, Status, incus_wait
Expand Down Expand Up @@ -46,6 +48,11 @@ async def query(self, filters, options):
async def do_create(self, data):
await self.middleware.call('virt.global.check_initialized')

verrors = ValidationErrors()
if await self.middleware.call('virt.volume.query', [['id', '=', data['name']]]):
verrors.add('virt_volume_create.name', 'Volume with this name already exists')
verrors.check()

result = await incus_call('1.0/storage-pools/default/volumes/custom', 'post', {
'json': {
'name': data['name'],
Expand Down Expand Up @@ -100,6 +107,9 @@ async def import_iso(self, job, data):
elif data['iso_location'] is None:
raise CallError('Either upload iso or provide iso_location')

if await self.middleware.call('virt.volume.query', [['id', '=', data['name']]]):
raise CallError('Volume with this name already exists', errno=errno.EEXIST)

request_kwargs = {
'headers': {
'X-Incus-type': 'iso',
Expand Down

0 comments on commit 348bff7

Please sign in to comment.