Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-132416 / 25.04 / Validate PROXY devices on instance creation #15057

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/middlewared/middlewared/plugins/virt/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,14 @@ async def do_create(self, job, data):

devices = {}
for i in (data['devices'] or []):
await self.middleware.call('virt.instance.validate', i, 'virt_instance_create', verrors)
await self.middleware.call('virt.instance.validate_device', i, 'virt_instance_create', verrors)
if i['name'] is None:
i['name'] = await self.middleware.call('virt.instance.generate_device_name', devices.keys(), i['dev_type'])
devices[i['name']] = await self.middleware.call('virt.instance.device_to_incus', data['instance_type'], i)

if not verrors and data['devices']:
await self.middleware.call('virt.instance.validate_devices', data['devices'], 'virt_instance_create', verrors)

verrors.check()

async def running_cb(data):
Expand Down
26 changes: 23 additions & 3 deletions src/middlewared/middlewared/plugins/virt/instance_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,27 @@ async def generate_device_name(self, device_names: list[str], device_type: str)
i += 1
return name

async def __validate_device(self, device, schema, verrors: ValidationErrors, old: dict = None, instance: str = None):
@private
async def validate_devices(self, devices, schema, verrors: ValidationErrors):
unique_src_proxies = []
unique_dst_proxies = []

for device in devices:
match device['dev_type']:
case 'PROXY':
source = (device['source_proto'], device['source_port'])
if source in unique_src_proxies:
verrors.add(f'{schema}.source_port', 'Source proto/port already in use.')
else:
unique_src_proxies.append(source)
dst = (device['dest_proto'], device['dest_port'])
if dst in unique_dst_proxies:
verrors.add(f'{schema}.dest_port', 'Destination proto/port already in use.')
else:
unique_dst_proxies.append(dst)

@private
async def validate_device(self, device, schema, verrors: ValidationErrors, old: dict = None, instance: str = None):
match device['dev_type']:
case 'PROXY':
# Skip validation if we are updating and port has not changed
Expand Down Expand Up @@ -283,7 +303,7 @@ async def device_add(self, id, device):
device['name'] = await self.generate_device_name(data['devices'].keys(), device['dev_type'])

verrors = ValidationErrors()
await self.__validate_device(device, 'virt_device_add', verrors)
await self.validate_device(device, 'virt_device_add', verrors)
verrors.check()

data['devices'][device['name']] = await self.device_to_incus(instance['type'], device)
Expand All @@ -305,7 +325,7 @@ async def device_update(self, id, device):
raise CallError('Device does not exist.', errno.ENOENT)

verrors = ValidationErrors()
await self.__validate_device(device, 'virt_device_update', verrors, old, instance['name'])
await self.validate_device(device, 'virt_device_update', verrors, old, instance['name'])
verrors.check()

data['devices'][device['name']] = await self.device_to_incus(instance['type'], device)
Expand Down