From acf64a1f72bf10e0e332899168e468db5c8ba034 Mon Sep 17 00:00:00 2001 From: Jan Horstmann Date: Thu, 14 Apr 2022 11:44:07 +0200 Subject: [PATCH] Set owner in image module Previously the owner field was not set by module `cloud.openstack.image`, although it is specified as a module parameter. The usual approach in `ansible-collections-openstack` is to accept both names and IDs when referencing openstack resources. Therefore this commit follows the approach taken by `python-openstackclient` in [1] and introduces a `project` and a `project_domain` parameter to identify projects by name or ID and assign the ID to the `owner` attribute of the image. The `owner` parameter is left as an alias to `project` in the module. Story: 2009983 Task: 45012 [1] https://opendev.org/openstack/python-openstackclient/commit/cf2de9af79cedd51ca080f5a6521997c05647418 Change-Id: I3654587df8e40d554aac5126df307961f335332c --- ci/roles/image/tasks/main.yml | 84 ++++++++++++++++++++++++++++++++--- plugins/modules/image.py | 23 ++++++++-- 2 files changed, 99 insertions(+), 8 deletions(-) diff --git a/ci/roles/image/tasks/main.yml b/ci/roles/image/tasks/main.yml index 243f3944..0786da0a 100644 --- a/ci/roles/image/tasks/main.yml +++ b/ci/roles/image/tasks/main.yml @@ -57,11 +57,6 @@ state: absent name: "{{ image_name }}" -- name: Delete test image file - file: - name: "{{ tmp_file.stdout }}" - state: absent - - name: Try to get details of deleted image openstack.cloud.image_info: cloud: "{{ cloud }}" @@ -72,3 +67,82 @@ assert: that: - not deleted_image_info_result.openstack_images + +- name: Create owner project + openstack.cloud.project: + cloud: "{{ cloud }}" + state: present + name: image_owner_project + description: Project owning test image + domain_id: default + enabled: True + register: owner_project + +- name: Create raw image (owner by project name) + openstack.cloud.image: + cloud: "{{ cloud }}" + state: present + name: "{{ image_name }}" + filename: "{{ tmp_file.stdout }}" + disk_format: raw + tags: "{{ image_tags }}" + project: image_owner_project + register: image + +- name: Get details of created image (owner by project name) + openstack.cloud.image_info: + cloud: "{{ cloud }}" + image: "{{ image_name }}" + register: image_info_result + +- name: Verify image owner (owner by project name) + assert: + that: + - image_info_result.openstack_images[0].owner == owner_project.project.id + +- name: Delete raw image (owner by project name) + openstack.cloud.image: + cloud: "{{ cloud }}" + state: absent + name: "{{ image_name }}" + +- name: Create raw image (owner by project name and domain name) + openstack.cloud.image: + cloud: "{{ cloud }}" + state: present + name: "{{ image_name }}" + filename: "{{ tmp_file.stdout }}" + disk_format: raw + tags: "{{ image_tags }}" + project: image_owner_project + project_domain: default + register: image + +- name: Get details of created image (owner by project name and domain name) + openstack.cloud.image_info: + cloud: "{{ cloud }}" + image: "{{ image_name }}" + register: image_info_result + +- name: Verify image owner (owner by project name and domain name) + assert: + that: + - image_info_result.openstack_images[0].owner == owner_project.project.id + +- name: Delete raw image (owner by project name and domain name) + openstack.cloud.image: + cloud: "{{ cloud }}" + state: absent + name: "{{ image_name }}" + +- name: Delete owner project + openstack.cloud.project: + cloud: "{{ cloud }}" + state: absent + name: image_owner_project + domain_id: default + +- name: Delete test image file + file: + name: "{{ tmp_file.stdout }}" + state: absent diff --git a/plugins/modules/image.py b/plugins/modules/image.py index 387197a9..fae13a2e 100644 --- a/plugins/modules/image.py +++ b/plugins/modules/image.py @@ -40,9 +40,15 @@ options: default: bare choices: ['ami', 'aki', 'ari', 'bare', 'ovf', 'ova', 'docker'] type: str - owner: + project: description: - - The owner of the image + - The name or ID of the project owning the image + type: str + aliases: ['owner'] + project_domain: + description: + - The domain the project owning the image belongs to + - May be used to identify a unique project when providing a name to the project argument and multiple projects with such name exist type: str min_disk: description: @@ -169,7 +175,8 @@ class ImageModule(OpenStackModule): disk_format=dict(default='qcow2', choices=['ami', 'ari', 'aki', 'vhd', 'vmdk', 'raw', 'qcow2', 'vdi', 'iso', 'vhdx', 'ploop']), container_format=dict(default='bare', choices=['ami', 'aki', 'ari', 'bare', 'ovf', 'ova', 'docker']), - owner=dict(type='str'), + project=dict(type='str', aliases=['owner']), + project_domain=dict(type='str'), min_disk=dict(type='int', default=0), min_ram=dict(type='int', default=0), is_public=dict(type='bool', default=False), @@ -202,6 +209,16 @@ class ImageModule(OpenStackModule): kwargs = {} if self.params['id'] is not None: kwargs['id'] = self.params['id'] + if self.params['project']: + project_domain = {'id': None} + if self.params['project_domain']: + project_domain = self.conn.get_domain(name_or_id=self.params['project_domain']) + if not project_domain or project_domain['id'] is None: + self.fail(msg='Project domain %s could not be found' % self.params['project_domain']) + project = self.conn.get_project(name_or_id=self.params['project'], domain_id=project_domain['id']) + if not project: + self.fail(msg='Project %s could not be found' % self.params['project']) + kwargs['owner'] = project['id'] image = self.conn.create_image( name=self.params['name'], filename=self.params['filename'],