Merge "image: do a web-download import when uri is given"

This commit is contained in:
Zuul
2026-10-26 16:25:54 +00:00
committed by Gerrit Code Review
4 changed files with 107 additions and 17 deletions
@@ -0,0 +1,10 @@
---
bugfixes:
- modules/image.py now performs a web-download import when ``uri`` is given,
instead of ignoring ``uri`` and ``import_method`` and attempting a direct
upload with no data.
- modules/image.py no longer passes ``uri``, ``import_method`` and
``use_import`` to the image create call, where they were stored on the
image as arbitrary properties.
- modules/image.py no longer accepts ``import_method=glance-download``.
The module never implemented this method.
+3
View File
@@ -63,3 +63,6 @@ expected_fields:
- vm_mode - vm_mode
- vmware_adaptertype - vmware_adaptertype
- vmware_ostype - vmware_ostype
# Fetched by the Image service itself in the web-download test.
image_web_download_uri: https://download.cirros-cloud.net/0.6.2/cirros-0.6.2-x86_64-disk.img
+47
View File
@@ -368,6 +368,53 @@
name: "{{ tmp_file.path }}" name: "{{ tmp_file.path }}"
state: absent state: absent
- name: Import an image with web-download
openstack.cloud.image:
cloud: "{{ cloud }}"
state: present
name: ansible_image_web_download
container_format: bare
disk_format: qcow2
import_method: web-download
uri: "{{ image_web_download_uri }}"
register: web_download_image
- name: Assert the image was fetched by the Image service
assert:
that:
- web_download_image is changed
- web_download_image.image.status == 'active'
- web_download_image.image.size > 0
- name: Assert import options were not stored as image properties
assert:
that:
- "'uri' not in web_download_image.image.properties"
- "'import_method' not in web_download_image.image.properties"
- "'use_import' not in web_download_image.image.properties"
- name: Import the same image again
openstack.cloud.image:
cloud: "{{ cloud }}"
state: present
name: ansible_image_web_download
container_format: bare
disk_format: qcow2
import_method: web-download
uri: "{{ image_web_download_uri }}"
register: web_download_image
- name: Assert the second import did not change anything
assert:
that:
- web_download_image is not changed
- name: Delete the web-download image
openstack.cloud.image:
cloud: "{{ cloud }}"
state: absent
name: ansible_image_web_download
always: always:
- name: Remove temporary image file - name: Remove temporary image file
ansible.builtin.file: ansible.builtin.file:
+47 -17
View File
@@ -125,21 +125,29 @@ options:
use_import: use_import:
description: description:
- Use the 'glance-direct' method of the interoperable image import mechanism. - Use the 'glance-direct' method of the interoperable image import mechanism.
- Applies only to uploads from I(filename). I(import_method) does not
affect it.
- Should only be used when needed, such as when the user needs the cloud to - Should only be used when needed, such as when the user needs the cloud to
transform image format. transform image format.
type: bool type: bool
import_method: import_method:
description: description:
- Method to use for importing the image. Not all deployments support all methods. - Interoperable image import method that the Image service uses to
- Supports web-download or glance-download. download the image data.
- copy-image is not supported with create actions. - The only supported method is web-download. The Image service downloads
- glance-direct is removed from the import method so use_import can be used in that case. the image data from I(uri).
- If I(uri) is set and I(import_method) is not, the module uses
web-download.
- For glance-direct uploads from I(filename), use I(use_import).
type: str type: str
choices: [web-download, glance-download] choices: [web-download]
uri: uri:
description: description:
- Required only if using the web-download import method. - URL the Image service downloads the image data from.
- This url is where the data is made available to the Image service. - Requires I(import_method=web-download), which is the default when
I(uri) is given, and a Glance with that import method enabled.
- Mutually exclusive with I(filename) and I(volume). The image data
never passes through the Ansible controller.
type: str type: str
extends_documentation_fragment: extends_documentation_fragment:
@@ -147,6 +155,15 @@ extends_documentation_fragment:
''' '''
EXAMPLES = r''' EXAMPLES = r'''
- name: Have the Image service download an image itself
openstack.cloud.image:
cloud: devstack
name: cirros
container_format: bare
disk_format: qcow2
import_method: web-download
uri: https://download.cirros-cloud.net/0.6.2/cirros-0.6.2-x86_64-disk.img
- name: Upload an image from a local file named cirros-0.3.0-x86_64-disk.img - name: Upload an image from a local file named cirros-0.3.0-x86_64-disk.img
openstack.cloud.image: openstack.cloud.image:
cloud: devstack-admin cloud: devstack-admin
@@ -413,7 +430,7 @@ class ImageModule(OpenStackModule):
visibility=dict(choices=['public', 'private', 'shared', 'community']), visibility=dict(choices=['public', 'private', 'shared', 'community']),
volume=dict(), volume=dict(),
use_import=dict(type='bool'), use_import=dict(type='bool'),
import_method=dict(choices=['web-download', 'glance-download']), import_method=dict(choices=['web-download']),
uri=dict() uri=dict()
) )
@@ -422,13 +439,15 @@ class ImageModule(OpenStackModule):
('filename', 'volume', 'uri'), ('filename', 'volume', 'uri'),
('visibility', 'is_public'), ('visibility', 'is_public'),
], ],
required_if=[
('import_method', 'web-download', ['uri']),
],
) )
# resource attributes obtainable directly from params # resource attributes obtainable directly from params
attr_params = ('id', 'name', 'filename', 'disk_format', attr_params = ('id', 'name', 'filename', 'disk_format',
'container_format', 'wait', 'timeout', 'is_public', 'container_format', 'wait', 'timeout', 'is_public',
'is_protected', 'min_disk', 'min_ram', 'volume', 'tags', 'is_protected', 'min_disk', 'min_ram', 'volume', 'tags')
'use_import', 'import_method', 'uri')
def _resolve_visibility(self): def _resolve_visibility(self):
"""resolve a visibility value to be compatible with older versions""" """resolve a visibility value to be compatible with older versions"""
@@ -497,14 +516,13 @@ class ImageModule(OpenStackModule):
wait=self.params['timeout'], wait=self.params['timeout'],
attribute='status') attribute='status')
def _import_uploaded_image(self, image): def _import_image(self, image, method, **kwargs):
if not hasattr(self.conn.image, 'import_image'): if not hasattr(self.conn.image, 'import_image'):
self.fail_json( self.fail_json(
msg="The installed openstacksdk library does not support " msg="The installed openstacksdk library does not support "
"image import operations required for images in the " "image import operations.")
"'uploading' state.")
self.conn.image.import_image(image, method='glance-direct') self.conn.image.import_image(image, method=method, **kwargs)
return self._wait_for_image_active(self.conn.get_image(image.id)) return self._wait_for_image_active(self.conn.get_image(image.id))
def run(self): def run(self):
@@ -536,15 +554,23 @@ class ImageModule(OpenStackModule):
changed = False changed = False
if self.params['state'] == 'present': if self.params['state'] == 'present':
import_method = self.params['import_method']
if self.params['uri'] and not import_method:
import_method = 'web-download'
attrs = self._build_params(owner) attrs = self._build_params(owner)
if not image: if not image:
# self.conn.image.create_image() cannot be used because it does # self.conn.image.create_image() cannot be used because it does
# not provide self.conn.create_image()'s volume parameter [0]. # not provide self.conn.create_image()'s volume parameter [0].
# [0] https://opendev.org/openstack/openstacksdk/src/commit/ # [0] https://opendev.org/openstack/openstacksdk/src/commit/
# a41d04ea197439c2f134ce3554995693933a46ac/openstack/cloud/_image.py#L306 # a41d04ea197439c2f134ce3554995693933a46ac/openstack/cloud/_image.py#L306
if import_method:
# Without data, the image stays queued until the import
# below starts. Waiting for it here times out.
attrs['wait'] = False
image = self.conn.create_image(**attrs) image = self.conn.create_image(**attrs)
changed = True changed = True
if not self.params['wait']: if not self.params['wait'] and not import_method:
self.exit_json(changed=changed, self.exit_json(changed=changed,
image=self._return_value(image.id)) image=self._return_value(image.id))
@@ -552,7 +578,11 @@ class ImageModule(OpenStackModule):
self.conn.image.reactivate_image(image) self.conn.image.reactivate_image(image)
changed = True changed = True
elif image['status'] == 'queued': elif image['status'] == 'queued':
if ( if import_method == 'web-download':
image = self._import_image(
image, import_method, uri=self.params['uri'])
changed = True
elif (
self.params['filename'] self.params['filename']
and hasattr(self.conn.image, 'stage_image')): and hasattr(self.conn.image, 'stage_image')):
self.conn.image.stage_image( self.conn.image.stage_image(
@@ -570,7 +600,7 @@ class ImageModule(OpenStackModule):
image = self.conn.get_image(image.id) image = self.conn.get_image(image.id)
if image['status'] == 'uploading' and self.params['use_import']: if image['status'] == 'uploading' and self.params['use_import']:
image = self._import_uploaded_image(image) image = self._import_image(image, 'glance-direct')
changed = True changed = True
elif image['status'] == 'importing': elif image['status'] == 'importing':
image = self._wait_for_image_active(image) image = self._wait_for_image_active(image)