mirror of
https://opendev.org/openstack/ansible-collections-openstack.git
synced 2026-05-08 06:13:13 +00:00
Update project module to be compatible with new sdk
- Change sdk calls to use proxy objects - Convert return values to dict before updating - Adds additional test values Change-Id: I187a27af4a5b8aa7cd4b60a1a876b5e5e6975144
This commit is contained in:
committed by
Jakob Meng
parent
070e77feca
commit
7ec8e4d087
10
ci/roles/project/defaults/main.yml
Normal file
10
ci/roles/project/defaults/main.yml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
project_fields:
|
||||||
|
- description
|
||||||
|
- domain_id
|
||||||
|
- id
|
||||||
|
- is_domain
|
||||||
|
- is_enabled
|
||||||
|
- name
|
||||||
|
- options
|
||||||
|
- parent_id
|
||||||
|
- tags
|
||||||
@@ -1,28 +1,155 @@
|
|||||||
---
|
---
|
||||||
- name: Create project
|
- name: Ensure project doesn't exist before tests
|
||||||
openstack.cloud.project:
|
openstack.cloud.project:
|
||||||
cloud: "{{ cloud }}"
|
cloud: "{{ cloud }}"
|
||||||
state: present
|
state: absent
|
||||||
name: ansible_project
|
name: ansible_project
|
||||||
description: dummy description
|
|
||||||
domain_id: default
|
|
||||||
enabled: True
|
|
||||||
register: project
|
|
||||||
|
|
||||||
- debug: var=project
|
- block:
|
||||||
|
- name: Create project
|
||||||
|
openstack.cloud.project:
|
||||||
|
cloud: "{{ cloud }}"
|
||||||
|
state: present
|
||||||
|
name: ansible_project
|
||||||
|
description: dummy description
|
||||||
|
domain: default
|
||||||
|
enabled: True
|
||||||
|
register: project
|
||||||
|
|
||||||
- name: Update project
|
- name: Assert project changed
|
||||||
openstack.cloud.project:
|
assert:
|
||||||
cloud: "{{ cloud }}"
|
that: project is changed
|
||||||
state: present
|
|
||||||
name: ansible_project
|
|
||||||
description: new description
|
|
||||||
register: updatedproject
|
|
||||||
|
|
||||||
- debug: var=updatedproject
|
- name: Assert project fields
|
||||||
|
assert:
|
||||||
|
that: item in project['project']
|
||||||
|
loop: "{{ project_fields }}"
|
||||||
|
|
||||||
- name: Delete project
|
- name: Get project
|
||||||
openstack.cloud.project:
|
openstack.cloud.project_info:
|
||||||
cloud: "{{ cloud }}"
|
cloud: "{{ cloud }}"
|
||||||
state: absent
|
name: ansible_project
|
||||||
name: ansible_project
|
register: project_info
|
||||||
|
|
||||||
|
- name: Assert project
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- project_info.openstack_projects | length == 1
|
||||||
|
- project_info.openstack_projects[0]['name'] == 'ansible_project'
|
||||||
|
- project_info.openstack_projects[0]['description'] == 'dummy description'
|
||||||
|
|
||||||
|
- block:
|
||||||
|
- name: Create identical project
|
||||||
|
openstack.cloud.project:
|
||||||
|
cloud: "{{ cloud }}"
|
||||||
|
state: present
|
||||||
|
name: ansible_project
|
||||||
|
description: dummy description
|
||||||
|
domain: default
|
||||||
|
enabled: True
|
||||||
|
register: project
|
||||||
|
|
||||||
|
- name: Assert project not changed
|
||||||
|
assert:
|
||||||
|
that: project is not changed
|
||||||
|
|
||||||
|
- name: Assert project fields
|
||||||
|
assert:
|
||||||
|
that: item in project['project']
|
||||||
|
loop: "{{ project_fields }}"
|
||||||
|
|
||||||
|
|
||||||
|
- block:
|
||||||
|
- name: Update project
|
||||||
|
openstack.cloud.project:
|
||||||
|
cloud: "{{ cloud }}"
|
||||||
|
state: present
|
||||||
|
name: ansible_project
|
||||||
|
description: new description
|
||||||
|
properties:
|
||||||
|
tags:
|
||||||
|
- example_tag
|
||||||
|
register: project
|
||||||
|
|
||||||
|
- name: Assert project changed
|
||||||
|
assert:
|
||||||
|
that: project is changed
|
||||||
|
|
||||||
|
- name: Assert project fields
|
||||||
|
assert:
|
||||||
|
that: item in project['project']
|
||||||
|
loop: "{{ project_fields }}"
|
||||||
|
|
||||||
|
- name: Get project
|
||||||
|
openstack.cloud.project_info:
|
||||||
|
cloud: "{{ cloud }}"
|
||||||
|
name: ansible_project
|
||||||
|
register: project_info
|
||||||
|
|
||||||
|
- name: Assert project
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- project_info.openstack_projects | length == 1
|
||||||
|
- project_info.openstack_projects[0]['description'] == 'new description'
|
||||||
|
|
||||||
|
- block:
|
||||||
|
- name: Delete project
|
||||||
|
openstack.cloud.project:
|
||||||
|
cloud: "{{ cloud }}"
|
||||||
|
state: absent
|
||||||
|
name: ansible_project
|
||||||
|
register: project
|
||||||
|
|
||||||
|
- name: Assert project changed
|
||||||
|
assert:
|
||||||
|
that: project is changed
|
||||||
|
|
||||||
|
- name: Get project
|
||||||
|
openstack.cloud.project_info:
|
||||||
|
cloud: "{{ cloud }}"
|
||||||
|
name: ansible_project
|
||||||
|
register: project_info
|
||||||
|
|
||||||
|
- name: Assert project deleted
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- project_info.openstack_projects | length == 0
|
||||||
|
|
||||||
|
|
||||||
|
- block:
|
||||||
|
- name: Delete non existant project
|
||||||
|
openstack.cloud.project:
|
||||||
|
cloud: "{{ cloud }}"
|
||||||
|
state: absent
|
||||||
|
name: ansible_project
|
||||||
|
register: project
|
||||||
|
|
||||||
|
- name: Assert project not changed
|
||||||
|
assert:
|
||||||
|
that: project is not changed
|
||||||
|
|
||||||
|
- block:
|
||||||
|
- name: Create project with properties
|
||||||
|
openstack.cloud.project:
|
||||||
|
cloud: "{{ cloud }}"
|
||||||
|
state: present
|
||||||
|
name: ansible_project
|
||||||
|
description: dummy description
|
||||||
|
domain: default
|
||||||
|
enabled: True
|
||||||
|
properties:
|
||||||
|
dummy_key: dummy_value
|
||||||
|
register: project
|
||||||
|
|
||||||
|
- block:
|
||||||
|
- name: Update project with properties
|
||||||
|
openstack.cloud.project:
|
||||||
|
cloud: "{{ cloud }}"
|
||||||
|
state: present
|
||||||
|
name: ansible_project
|
||||||
|
description: dummy description
|
||||||
|
domain: default
|
||||||
|
enabled: True
|
||||||
|
properties:
|
||||||
|
dummy_key: other_dummy_value
|
||||||
|
register: project
|
||||||
|
|||||||
@@ -14,37 +14,38 @@ description:
|
|||||||
The value for I(name) cannot be updated without deleting and
|
The value for I(name) cannot be updated without deleting and
|
||||||
re-creating the project.
|
re-creating the project.
|
||||||
options:
|
options:
|
||||||
name:
|
name:
|
||||||
description:
|
description:
|
||||||
- Name for the project
|
- Name for the project
|
||||||
required: true
|
required: true
|
||||||
type: str
|
type: str
|
||||||
description:
|
description:
|
||||||
description:
|
description:
|
||||||
- Description for the project
|
- Description for the project
|
||||||
type: str
|
type: str
|
||||||
domain_id:
|
domain:
|
||||||
description:
|
description:
|
||||||
- Domain id to create the project in if the cloud supports domains.
|
- Domain name or id to create the project in if the cloud supports
|
||||||
aliases: ['domain']
|
domains.
|
||||||
type: str
|
aliases: ['domain_id']
|
||||||
enabled:
|
type: str
|
||||||
description:
|
is_enabled:
|
||||||
- Is the project enabled
|
description:
|
||||||
type: bool
|
- Is the project enabled
|
||||||
default: 'yes'
|
aliases: ['enabled']
|
||||||
properties:
|
type: bool
|
||||||
description:
|
default: 'yes'
|
||||||
- Additional properties to be associated with this project. Requires
|
properties:
|
||||||
openstacksdk>0.45.
|
description:
|
||||||
type: dict
|
- Additional properties to be associated with this project.
|
||||||
required: false
|
type: dict
|
||||||
state:
|
required: false
|
||||||
description:
|
state:
|
||||||
- Should the resource be present or absent.
|
description:
|
||||||
choices: [present, absent]
|
- Should the resource be present or absent.
|
||||||
default: present
|
choices: [present, absent]
|
||||||
type: str
|
default: present
|
||||||
|
type: str
|
||||||
requirements:
|
requirements:
|
||||||
- "python >= 3.6"
|
- "python >= 3.6"
|
||||||
- "openstacksdk"
|
- "openstacksdk"
|
||||||
@@ -61,8 +62,8 @@ EXAMPLES = '''
|
|||||||
state: present
|
state: present
|
||||||
name: demoproject
|
name: demoproject
|
||||||
description: demodescription
|
description: demodescription
|
||||||
domain_id: demoid
|
domain: demoid
|
||||||
enabled: True
|
is_enabled: True
|
||||||
properties:
|
properties:
|
||||||
internal_alias: demo_project
|
internal_alias: demo_project
|
||||||
|
|
||||||
@@ -79,24 +80,40 @@ RETURN = '''
|
|||||||
project:
|
project:
|
||||||
description: Dictionary describing the project.
|
description: Dictionary describing the project.
|
||||||
returned: On success when I(state) is 'present'
|
returned: On success when I(state) is 'present'
|
||||||
type: complex
|
type: dict
|
||||||
contains:
|
contains:
|
||||||
id:
|
|
||||||
description: Project ID
|
|
||||||
type: str
|
|
||||||
sample: "f59382db809c43139982ca4189404650"
|
|
||||||
name:
|
|
||||||
description: Project name
|
|
||||||
type: str
|
|
||||||
sample: "demoproject"
|
|
||||||
description:
|
description:
|
||||||
description: Project description
|
description: Project description
|
||||||
type: str
|
type: str
|
||||||
sample: "demodescription"
|
sample: "demodescription"
|
||||||
enabled:
|
domain_id:
|
||||||
description: Boolean to indicate if project is enabled
|
description: domain to which the project belongs
|
||||||
|
type: str
|
||||||
|
sample: "default"
|
||||||
|
id:
|
||||||
|
description: Project ID
|
||||||
|
type: str
|
||||||
|
sample: "f59382db809c43139982ca4189404650"
|
||||||
|
is_domain:
|
||||||
|
description: Indicates whether the project also acts as a domain.
|
||||||
type: bool
|
type: bool
|
||||||
sample: True
|
is_enabled:
|
||||||
|
description: Indicates whether the project is enabled
|
||||||
|
type: bool
|
||||||
|
name:
|
||||||
|
description: Project name
|
||||||
|
type: str
|
||||||
|
sample: "demoproject"
|
||||||
|
options:
|
||||||
|
description: The resource options for the project
|
||||||
|
type: dict
|
||||||
|
parent_id:
|
||||||
|
description: The ID of the parent of the project
|
||||||
|
type: str
|
||||||
|
tags:
|
||||||
|
description: A list of associated tags
|
||||||
|
type: list
|
||||||
|
elements: str
|
||||||
'''
|
'''
|
||||||
|
|
||||||
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
|
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
|
||||||
@@ -106,109 +123,96 @@ class IdentityProjectModule(OpenStackModule):
|
|||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
name=dict(required=True),
|
name=dict(required=True),
|
||||||
description=dict(required=False),
|
description=dict(required=False),
|
||||||
domain_id=dict(required=False, aliases=['domain']),
|
domain=dict(required=False, aliases=['domain_id']),
|
||||||
|
is_enabled=dict(default=True, type='bool', aliases=['enabled']),
|
||||||
properties=dict(required=False, type='dict', min_ver='0.45.1'),
|
properties=dict(required=False, type='dict', min_ver='0.45.1'),
|
||||||
enabled=dict(default=True, type='bool'),
|
|
||||||
state=dict(default='present', choices=['absent', 'present'])
|
state=dict(default='present', choices=['absent', 'present'])
|
||||||
)
|
)
|
||||||
module_kwargs = dict(
|
module_kwargs = dict(
|
||||||
supports_check_mode=True
|
supports_check_mode=True
|
||||||
)
|
)
|
||||||
|
|
||||||
def _needs_update(self, project):
|
def _needs_update(self, project, update, extra):
|
||||||
keys = ('description', 'enabled')
|
# We cannot update a project name because name find projects by name so
|
||||||
for key in keys:
|
# only a project with an already matching name will be considered for
|
||||||
if self.params[key] is not None and self.params[key] != project.get(key):
|
# updates
|
||||||
return True
|
keys = ('description', 'is_enabled')
|
||||||
|
if any((k in update and update[k] != project[k]) for k in keys):
|
||||||
|
return True
|
||||||
|
|
||||||
properties = self.params['properties']
|
# Additional keys passed by user will be checked completely
|
||||||
if properties:
|
if extra and any(k not in project or extra[k] != project[k]
|
||||||
project_properties = project.get('properties')
|
for k in extra.keys()):
|
||||||
for k, v in properties.items():
|
return True
|
||||||
if v is not None and (k not in project_properties or v != project_properties[k]):
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _system_state_change(self, project):
|
def _get_domain_id(self, domain):
|
||||||
state = self.params['state']
|
dom_obj = self.conn.identity.find_domain(domain)
|
||||||
|
if dom_obj is None:
|
||||||
|
# Ok, let's hope the user is non-admin and passing a sane id
|
||||||
|
return domain
|
||||||
|
return dom_obj.id
|
||||||
|
|
||||||
|
def _system_state_change(self, state, project, attrs, extra_attrs):
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
if project is None:
|
if project is None:
|
||||||
changed = True
|
return True
|
||||||
else:
|
return self._needs_update(project, attrs, extra_attrs)
|
||||||
if self._needs_update(project):
|
# Else state is absent
|
||||||
changed = True
|
return project is not None
|
||||||
else:
|
|
||||||
changed = False
|
|
||||||
|
|
||||||
elif state == 'absent':
|
|
||||||
changed = project is not None
|
|
||||||
|
|
||||||
return changed
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
name = self.params['name']
|
name = self.params['name']
|
||||||
description = self.params['description']
|
domain = self.params['domain']
|
||||||
domain = self.params['domain_id']
|
|
||||||
enabled = self.params['enabled']
|
|
||||||
properties = self.params['properties'] or {}
|
|
||||||
state = self.params['state']
|
state = self.params['state']
|
||||||
|
properties = self.params['properties']
|
||||||
|
enabled = self.params['is_enabled']
|
||||||
|
description = self.params['description']
|
||||||
|
|
||||||
|
find_project_kwargs = {}
|
||||||
|
domain_id = None
|
||||||
if domain:
|
if domain:
|
||||||
try:
|
domain_id = self._get_domain_id(domain)
|
||||||
# We assume admin is passing domain id
|
find_project_kwargs['domain_id'] = domain_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.
|
|
||||||
try:
|
|
||||||
dom = self.conn.search_domains(filters={'name': domain})[0]['id']
|
|
||||||
domain = dom
|
|
||||||
except Exception:
|
|
||||||
# Ok, let's hope the user is non-admin and passing a sane id
|
|
||||||
pass
|
|
||||||
|
|
||||||
if domain:
|
project = None
|
||||||
project = self.conn.get_project(name, domain_id=domain)
|
if name is not None:
|
||||||
else:
|
project = self.conn.identity.find_project(
|
||||||
project = self.conn.get_project(name)
|
name, **find_project_kwargs)
|
||||||
|
|
||||||
if self.ansible.check_mode:
|
project_attrs = {
|
||||||
self.exit_json(changed=self._system_state_change(project))
|
'name': name,
|
||||||
|
'description': description,
|
||||||
|
'is_enabled': enabled,
|
||||||
|
'domain_id': domain_id,
|
||||||
|
}
|
||||||
|
project_attrs = {k: v for k, v in project_attrs.items()
|
||||||
|
if v is not None}
|
||||||
|
# Add in arbitrary properties
|
||||||
|
if properties:
|
||||||
|
project_attrs.update(properties)
|
||||||
|
|
||||||
|
if self.check_mode:
|
||||||
|
self.exit_json(changed=self._system_state_change(state, project,
|
||||||
|
project_attrs,
|
||||||
|
properties))
|
||||||
|
|
||||||
|
changed = False
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
if project is None:
|
if project is None:
|
||||||
project = self.conn.create_project(
|
project = self.conn.identity.create_project(**project_attrs)
|
||||||
name=name, description=description,
|
|
||||||
domain_id=domain,
|
|
||||||
enabled=enabled)
|
|
||||||
changed = True
|
changed = True
|
||||||
|
elif self._needs_update(project, project_attrs, properties):
|
||||||
project = self.conn.update_project(
|
project = self.conn.identity.update_project(
|
||||||
project['id'],
|
project, **project_attrs)
|
||||||
description=description,
|
|
||||||
enabled=enabled,
|
|
||||||
**properties)
|
|
||||||
else:
|
|
||||||
if self._needs_update(project):
|
|
||||||
project = self.conn.update_project(
|
|
||||||
project['id'],
|
|
||||||
description=description,
|
|
||||||
enabled=enabled,
|
|
||||||
**properties)
|
|
||||||
changed = True
|
|
||||||
else:
|
|
||||||
changed = False
|
|
||||||
self.exit_json(changed=changed, project=project)
|
|
||||||
|
|
||||||
elif state == 'absent':
|
|
||||||
if project is None:
|
|
||||||
changed = False
|
|
||||||
else:
|
|
||||||
self.conn.delete_project(project['id'])
|
|
||||||
changed = True
|
changed = True
|
||||||
self.exit_json(changed=changed)
|
self.exit_json(changed=changed,
|
||||||
|
project=project.to_dict(computed=False))
|
||||||
|
elif state == 'absent' and project is not None:
|
||||||
|
self.conn.identity.delete_project(project['id'])
|
||||||
|
changed = True
|
||||||
|
self.exit_json(changed=changed)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user