Skip to content

Commit

Permalink
Few fixes and improvements requested by UI team:
Browse files Browse the repository at this point in the history
 - Add image details
 - virt.global.config event
 - make sure "autostart" is always a boolean
  • Loading branch information
william-gr committed Oct 28, 2024
1 parent 5e2f3fd commit 01b6fec
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/middlewared/middlewared/api/v25_04_0/virt.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ class VirtInstanceAlias(BaseModel):
InstanceType: TypeAlias = Literal['CONTAINER', 'VM']


class Image(BaseModel):
architecture: str | None
description: str | None
os: str | None
release: str | None
serial: str | None
type: str | None
variant: str | None


class VirtInstanceEntry(BaseModel):
id: str
name: Annotated[NonEmptyString, StringConstraints(max_length=200)]
Expand All @@ -165,6 +175,7 @@ class VirtInstanceEntry(BaseModel):
autostart: bool
environment: dict[str, str]
aliases: List[VirtInstanceAlias]
image: Image
raw: dict


Expand All @@ -174,19 +185,19 @@ class VirtInstanceCreateArgs(BaseModel):
image: Annotated[NonEmptyString, StringConstraints(max_length=200)]
remote: REMOTE_CHOICES = 'LINUX_CONTAINERS'
instance_type: InstanceType = 'CONTAINER'
environment: dict | None = None
autostart: bool | None = None
environment: dict[str, str] | None = None
autostart: bool | None = True
cpu: str | None = None
memory: int | None = None
devices: List[DeviceType] = None


class VirtInstanceCreateResult(BaseModel):
result: dict
result: VirtInstanceEntry


class VirtInstanceUpdate(BaseModel, metaclass=ForUpdateMetaclass):
environment: dict | None = None
environment: dict[str, str] | None = None
autostart: bool | None = None
cpu: str | None = None
memory: int | None = None
Expand Down
7 changes: 7 additions & 0 deletions src/middlewared/middlewared/plugins/virt/global.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ async def setup(self, job):
raise
else:
await self.middleware.call('cache.put', 'VIRT_STATE', Status.INITIALIZED.value)
finally:
self.middleware.send_event('virt.global.config', 'CHANGED', fields=await self.config())

async def _setup_impl(self, job):
config = await self.config()
Expand Down Expand Up @@ -395,6 +397,11 @@ async def _event_system_ready(middleware: 'Middleware', event_type, args):


async def setup(middleware: 'Middleware'):
middleware.event_register(
'virt.global.config',
'Sent on virtualziation configuration changes.',
roles=['VIRT_GLOBAL_READ']
)
middleware.event_subscribe('system.ready', _event_system_ready)
# Should only happen if middlewared crashes or during development
failover_licensed = await middleware.call('failover.licensed')
Expand Down
14 changes: 12 additions & 2 deletions src/middlewared/middlewared/plugins/virt/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,21 @@ async def query(self, filters, options):
'type': 'CONTAINER' if i['type'] == 'container' else 'VM',
'status': i['state']['status'].upper(),
'cpu': i['config'].get('limits.cpu'),
'autostart': i['config'].get('boot.autostart') or False,
'autostart': bool(i['config'].get('boot.autostart')) or False,
'environment': {},
'aliases': [],
'image': {
'architecture': i['config'].get('image.architecture'),
'description': i['config'].get('image.description'),
'os': i['config'].get('image.os'),
'release': i['config'].get('image.release'),
'serial': i['config'].get('image.serial'),
'type': i['config'].get('image.type'),
'variant': i['config'].get('image.variant'),
}
}


if options.get('extra').get('raw'):
entry['raw'] = i

Expand Down Expand Up @@ -195,7 +205,7 @@ async def do_update(self, job, id, data):
instance = await self.middleware.call('virt.instance.get_instance', id, {'extra': {'raw': True}})

verrors = ValidationErrors()
await self.validate(data, 'virt_instance_create', verrors, old=instance)
await self.validate(data, 'virt_instance_update', verrors, old=instance)
verrors.check()

instance['raw']['config'].update(self.__data_to_config(data))
Expand Down

0 comments on commit 01b6fec

Please sign in to comment.