Update project_info module to new sdk

Make project_info module compatible with the new sdk 1.0.0 and also add
ansible tests for project_info module

Change-Id: I413200cf6a9b8bada7e5d78087246b888d53fac2
This commit is contained in:
Arx Cruz
2022-03-14 16:08:53 +01:00
committed by Jakob Meng
parent 802e46d554
commit bcca2efe1a
4 changed files with 78 additions and 30 deletions

View File

@@ -95,6 +95,7 @@
object_container object_container
port port
project project
project_info
recordset recordset
role_assignment role_assignment
security_group security_group

View File

@@ -0,0 +1,46 @@
---
- name: List admin project
openstack.cloud.project_info:
cloud: "{{ cloud }}"
name: 'admin'
register: project_admin
- name: List admin project with filter
openstack.cloud.project_info:
cloud: "{{ cloud }}"
filters:
name: 'admin'
- name: Check output of list admin project
assert:
that:
- project_admin.openstack_projects | length == 1
- name: List all projects
openstack.cloud.project_info:
cloud: "{{ cloud }}"
register: all_projects
- name: Check output of list all projects
assert:
that:
- all_projects.openstack_projects | length > 0
- name: List admin project with domain
openstack.cloud.project_info:
cloud: "{{ cloud }}"
name: 'admin'
domain: 'default'
register: project_domain
- name: Check output of list admin project with admin domain
assert:
that:
- project_domain.openstack_projects | length == 1
- name: Assert fields on SDK 1.*
assert:
that:
- '["description", "domain_id", "is_domain", "is_enabled", "options",
"parent_id", "id", "name", "tags"] |
difference(project_admin.openstack_projects.0.keys()) | length == 0'

View File

@@ -52,6 +52,7 @@
- { role: object, tags: object } - { role: object, tags: object }
- { role: port, tags: port } - { role: port, tags: port }
- { role: project, tags: project } - { role: project, tags: project }
- { role: project_info, tags: project_info }
- { role: recordset, tags: recordset } - { role: recordset, tags: recordset }
- { role: role_assignment, tags: role_assignment } - { role: role_assignment, tags: role_assignment }
- { role: router, tags: router } - { role: router, tags: router }

View File

@@ -22,8 +22,9 @@ options:
type: str type: str
filters: filters:
description: description:
- A dictionary of meta data to use for further filtering. Elements of - A dictionary of meta data to use for filtering projects. Elements of
this dictionary may be additional dictionaries. this dictionary are parsed as queries for openstack identity api in
the new openstacksdk.
type: dict type: dict
requirements: requirements:
- "python >= 3.6" - "python >= 3.6"
@@ -64,7 +65,7 @@ EXAMPLES = '''
name: demoproject name: demoproject
domain: admindomain domain: admindomain
filters: filters:
enabled: False is_enabled: False
register: result register: result
- debug: - debug:
msg: "{{ result.openstack_projects }}" msg: "{{ result.openstack_projects }}"
@@ -74,8 +75,9 @@ EXAMPLES = '''
RETURN = ''' RETURN = '''
openstack_projects: openstack_projects:
description: has all the OpenStack information about projects description: has all the OpenStack information about projects
returned: always, but can be null elements: dict
type: complex returned: always, but can be empty
type: list
contains: contains:
id: id:
description: Unique UUID. description: Unique UUID.
@@ -89,7 +91,7 @@ openstack_projects:
description: Description of the project description: Description of the project
returned: success returned: success
type: str type: str
enabled: is_enabled:
description: Flag to indicate if the project is enabled description: Flag to indicate if the project is enabled
returned: success returned: success
type: bool type: bool
@@ -97,6 +99,22 @@ openstack_projects:
description: Domain ID containing the project (keystone v3 clouds only) description: Domain ID containing the project (keystone v3 clouds only)
returned: success returned: success
type: bool type: bool
tags:
description: A list of simple strings assigned to a project
returned: success
type: list
parent_id:
description: The ID of the parent for the project
returned: success
type: str
is_domain:
description: Indicates whether the project also acts as a domain.
returned: success
type: bool
options:
description: Set of options for the project
returned: success
type: dict
''' '''
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
@@ -117,34 +135,16 @@ class IdentityProjectInfoModule(OpenStackModule):
def run(self): def run(self):
name = self.params['name'] name = self.params['name']
domain = self.params['domain'] domain = self.params['domain']
filters = self.params['filters'] filters = self.params['filters'] or {}
is_old_facts = self.module_name == 'openstack.cloud.project_facts'
if domain: if domain:
try: filters['domain_id'] = self.conn.identity.find_domain(
# We assume admin is passing domain id domain, ignore_missing=False).id
dom = self.conn.get_domain(domain)['id']
domain = dom
except Exception:
# If we fail, maybe admin is passing a domain name.
# Note that domains have unique names, just like id.
dom = self.conn.search_domains(filters={'name': domain})
if dom:
domain = dom[0]['id']
else:
self.fail_json(msg='Domain name or ID does not exist')
if not filters: projects = self.conn.search_projects(name, filters=filters)
filters = {} projects = [p.to_dict(computed=False) for p in projects]
filters['domain_id'] = domain self.exit_json(changed=False, openstack_projects=projects)
projects = self.conn.search_projects(name, filters)
if is_old_facts:
self.exit_json(changed=False, ansible_facts=dict(
openstack_projects=projects))
else:
self.exit_json(changed=False, openstack_projects=projects)
def main(): def main():