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

ovirt_template: add ova import #304

Merged
merged 10 commits into from
Mar 19, 2022
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Requirements
------------

* Ansible version 2.9.21 or higher
* Python SDK version 4.4 or higher
* Python SDK version 4.4.15 or higher
* Python netaddr library on the ansible controller node

Upstream oVirt documentation
Expand Down
2 changes: 1 addition & 1 deletion README.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Requirements
------------

* Ansible version 2.9.11 or higher
* Python SDK version 4.4 or higher
* Python SDK version 4.4.15 or higher
* Python netaddr library on the ansible controller node

Content of the collection
Expand Down
3 changes: 3 additions & 0 deletions changelogs/fragments/304-ovirt_template-add-ova-import.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- ovirt_template - Add ova import of template (https://github.com/oVirt/ovirt-ansible-collection/pull/304).
2 changes: 1 addition & 1 deletion ovirt-ansible-collection.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Url: http://www.ovirt.org
# TODO: Require ansible-core when both engine and hosts are ready for it
# Requires: ansible-core >= 2.12.0
Requires: ovirt-imageio-client
Requires: python3-ovirt-engine-sdk4 >= 4.4.11
Requires: python3-ovirt-engine-sdk4 >= 4.4.15
Requires: python3-netaddr
Requires: python3-jmespath
Requires: python3-passlib
Expand Down
81 changes: 81 additions & 0 deletions plugins/modules/ovirt_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@
description:
- "If I(true), use smart card authentication."
type: bool
kvm:
description:
- Dictionary of values to be used to connect to kvm and import
a template to oVirt.
type: dict
suboptions:
url:
description:
- The URL to be passed to the I(virt-v2v) tool for conversion.
- For example I(qemu:///system). This is required parameter.
storage_domain:
description:
- Specifies the target storage domain for converted disks. This is required parameter.
host:
description:
- The host name from which the template will be imported.
clone:
description:
- Indicates if the identifiers of the imported template should be regenerated.
cloud_init:
description:
- Dictionary with values for Unix-like Virtual Machine initialization using cloud init.
Expand Down Expand Up @@ -524,6 +543,18 @@
host_name: windowsad.example.com
user_name: Administrator
root_password: SuperPassword123

- name: Import external ova template
@NAMESPACE@.@NAME@.ovirt_template:
cluster: mycluster
name: mytemplate
state: present
timeout: 1800
poll_interval: 30
kvm:
host: myhost
url: ova:///tmp/test.ova
storage_domain: mystorage
'''

RETURN = '''
Expand Down Expand Up @@ -560,6 +591,7 @@
ovirt_full_argument_spec,
search_by_attributes,
search_by_name,
wait,
)


Expand Down Expand Up @@ -850,6 +882,51 @@ def _get_vnic_profile_mappings(module):
return vnicProfileMappings


def import_template(module, connection):
templates_service = connection.system_service().templates_service()
if search_by_name(templates_service, module.params['name']) is not None:
return False

events_service = connection.system_service().events_service()
last_event = events_service.list(max=1)[0]

external_template = module.params['kvm']
imports_service = connection.system_service().external_template_imports_service()
imported_template = imports_service.add(
otypes.ExternalTemplateImport(
template=otypes.Template(
name=module.params['name']
),
url=external_template.get('url'),
cluster=otypes.Cluster(
name=module.params['cluster'],
) if module.params['cluster'] else None,
storage_domain=otypes.StorageDomain(
name=external_template.get('storage_domain'),
) if external_template.get('storage_domain') else None,
host=otypes.Host(
name=external_template.get('host'),
) if external_template.get('host') else None,
clone=external_template.get('clone', None),
)
mnecas marked this conversation as resolved.
Show resolved Hide resolved
)

# Wait until event with code 1158 for our template:
templates_service = connection.system_service().templates_service()
wait(
service=templates_service.template_service(imported_template.template.id),
condition=lambda tmp: len(events_service.list(
from_=int(last_event.id),
search='type=1158 and message=*%s*' % tmp.name,
mnecas marked this conversation as resolved.
Show resolved Hide resolved
)
) > 0 if tmp is not None else False,
fail_condition=lambda tmp: tmp is None,
timeout=module.params['timeout'],
poll_interval=module.params['poll_interval'],
)
return True


def find_subversion_template(module, templates_service):
version = module.params.get('version')
templates = templates_service.list()
Expand Down Expand Up @@ -899,6 +976,7 @@ def main():
export_domain=dict(default=None),
storage_domain=dict(default=None),
exclusive=dict(type='bool'),
kvm=dict(type='dict'),
clone_name=dict(default=None),
image_provider=dict(default=None),
soundcard_enabled=dict(type='bool', default=None),
Expand Down Expand Up @@ -949,6 +1027,9 @@ def main():
if entity is None and module.params['version'] is not None:
force_create = True

if module.params['kvm']:
templates_module.changed = import_template(module, connection)

ret = templates_module.create(
entity=entity,
# When user want to create new template subversion, we must make sure
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ovirt-engine-sdk-python>=4.4.11
ovirt-imageio
ovirt-engine-sdk-python>=4.4.15
ovirt-imageio