Skip to content

Commit

Permalink
Update rhevm template kwargs, handle disk attachments
Browse files Browse the repository at this point in the history
draft state

add_disk kwargs for name/format/sparse updated

disk_attachments handling for template deploy. needs some consideration of the tradeoffs, and whether the kwarg should contain the attachments that should exist - or just modifications to attachments on the template that match by name.
  • Loading branch information
mshriver committed Jun 11, 2019
1 parent 8f107e5 commit 0bd1bcf
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions wrapanapi/systems/rhevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ def _is_disk_ok(self, disk_id):
if self.system.api.follow_link(disk_attach.disk).id == disk_id].pop()
return getattr(disk, 'status', None) == types.DiskStatus.OK

def add_disk(self, storage_domain=None, size=None, interface='VIRTIO', format=None,
active=True):
def add_disk(self, storage_domain=None, size=None, interface='virtio', format='cow',
active=True, sparse=True, name=None):
"""
Add disk to VM
Expand All @@ -442,6 +442,8 @@ def add_disk(self, storage_domain=None, size=None, interface='VIRTIO', format=No
interface: string disk interface type
format: string disk format type
active: boolean whether the disk is active
sparse: boolean whether the disk is preallocated or thin-provisioned
name: string name of the disk
Returns: None
Notes:
Disk format and interface type definitions, and their valid values,
Expand All @@ -451,10 +453,12 @@ def add_disk(self, storage_domain=None, size=None, interface='VIRTIO', format=No
"""
disk_attachments_service = self.api.disk_attachments_service()
disk_attach = types.DiskAttachment(
disk=types.Disk(format=types.DiskFormat(format),
disk=types.Disk(name=str(name),
format=types.DiskFormat(format.lower()),
provisioned_size=size,
storage_domains=[types.StorageDomain(name=storage_domain)]),
interface=getattr(types.DiskInterface, interface),
storage_domains=[types.StorageDomain(name=storage_domain)],
sparse=bool(sparse)),
interface=types.DiskInterface(interface.lower()),
active=active
)
disk_attachment = disk_attachments_service.add(disk_attach)
Expand Down Expand Up @@ -591,11 +595,15 @@ def deploy(self, vm_name, cluster, timeout=900, power_on=True, **kwargs):
sockets (optional) -- numbner of cpu sockets
ram (optional) -- memory in GB
storage_domain (optional) -- storage domain name to which VM should be deployed
disk_attachments (optional) -- list of dicts defining (name (partial), format, sparse)
name of template disks will be partial matched
format defaults to COW
sparse defaults to True (thin provisioning)
Returns:
wrapanapi.systems.rhevm.RHEVMVirtualMachine
"""
self.logger.debug(' Deploying RHEV template %s to VM %s', self.name, vm_name)
self.logger.debug('Deploying RHEV template %s to VM %s', self.name, vm_name)
vm_kwargs = {
'name': vm_name,
'cluster': self.system.get_cluster(cluster),
Expand All @@ -604,22 +612,32 @@ def deploy(self, vm_name, cluster, timeout=900, power_on=True, **kwargs):
clone = None
domain_name = kwargs.get('storage_domain')
if domain_name:
target_attachments = kwargs.get('disk_attachments', [])
# need to specify storage domain, if its different than the template's disks location
# then additional options required. disk allocation mode in UI required to be clone
clone = True
target_storage_domain = self.system.get_storage_domain(domain_name)
disk_attachments = []
for template_attachment in self.api.disk_attachments_service().list():
new_attachment = types.DiskAttachment(
disk=types.Disk(
id=template_attachment.id,
format=types.DiskFormat.COW,
storage_domains=[target_storage_domain]
)
)
disk_attachments.append(new_attachment)
for template_attachment in self.api.disk_attachments_service().list(follow='disk'):
disk_kwargs = dict(id=template_attachment.id,
storage_domains=[target_storage_domain])
for target_disk in target_attachments:
target_name = target_disk.get('name')
if target_name in template_attachment.disk.name:
self.logger.debug('Matched partial disk from template: %s', target_name)
disk_kwargs.update(
dict(
format=types.DiskFormat(target_disk.get('format', 'raw').lower()),
sparse=bool(target_disk.get('sparse', True))
)
)
else:
self.logger.info('Template disk name not matching any disk_attachment names')

disk_attachments.append(disk_kwargs)

vm_kwargs['disk_attachments'] = disk_attachments
vm_kwargs['disk_attachments'] = [types.DiskAttachment(disk=types.Disk(**dkwargs))
for dkwargs in disk_attachments]

# Placement requires two args
if 'placement_policy_host' in kwargs and 'placement_policy_affinity' in kwargs:
Expand Down

0 comments on commit 0bd1bcf

Please sign in to comment.