Skip to content

Commit

Permalink
project: new module
Browse files Browse the repository at this point in the history
* new module for managing project
* tests

Signed-off-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
Akasurde committed Aug 12, 2024
1 parent 3a75a16 commit d05f89c
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 0 deletions.
181 changes: 181 additions & 0 deletions plugins/modules/project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type

DOCUMENTATION = """
---
module: project
author:
- Nikhil Jain (@jainnikhil30)
- Abhijeet Kasurde (@akasurde)
short_description: Create, update or delete project in EDA Controller
description:
- This module allows user to create, update or delete project in a EDA controller.
version_added: '2.0.0'
options:
name:
description:
- The name of the project.
type: str
required: true
new_name:
description:
- Setting this option will change the existing name.
type: str
description:
description:
- The description of the project.
type: str
url:
description:
- The git URL of the project.
type: str
credential:
description:
- The name of the credential to associate with the project.
type: str
state:
description:
- Desired state of the resource.
default: "present"
choices: ["present", "absent"]
type: str
extends_documentation_fragment:
- ansible.eda.eda_controller.auths
attributes:
check_mode:
support: full
"""

EXAMPLES = """
- name: Create EDA Projects
ansible.eda.project:
controller_host: https://my_eda_host/
controller_username: admin
controller_password: MySuperSecretPassw0rd
name: "Example Project"
description: "Example project description"
url: "https://example.com/project1"
state: present
- name: Update the name of the project
ansible.eda.project:
controller_host: https://my_eda_host/
controller_username: admin
controller_password: MySuperSecretPassw0rd
name: "Example Project"
new_name: "Latest Example Project"
description: "Example project description"
url: "https://example.com/project1"
state: present
- name: Delete the project
ansible.eda.project:
controller_host: https://my_eda_host/
controller_username: admin
controller_password: MySuperSecretPassw0rd
name: "Example Project"
state: absent
"""

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.ansible.eda.plugins.module_utils.arguments import AUTH_ARGSPEC
from ansible_collections.ansible.eda.plugins.module_utils.client import Client
from ansible_collections.ansible.eda.plugins.module_utils.controller import Controller
from ansible_collections.ansible.eda.plugins.module_utils.errors import EDAError


def main():
argument_spec = dict(
name=dict(required=True),
new_name=dict(),
description=dict(),
url=dict(),
credential=dict(),
state=dict(choices=["present", "absent"], default="present"),
)

argument_spec.update(AUTH_ARGSPEC)

module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)

client = Client(
host=module.params.get("controller_host"),
username=module.params.get("controller_username"),
password=module.params.get("controller_password"),
timeout=module.params.get("request_timeout"),
validate_certs=module.params.get("validate_certs"),
)

project_endpoint = "projects"
controller = Controller(client, module)

project_name = module.params.get("name")
new_name = module.params.get("new_name")
description = module.params.get("description")
url = module.params.get("url")
state = module.params.get("state")
credential = module.params.get("credential")
ret = {}

try:
project_type = controller.get_one_or_many(project_endpoint, name=project_name)
except EDAError as eda_err:
module.fail_json(msg=eda_err)

if state == "absent":
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
try:
ret = controller.delete_if_needed(project_type, endpoint=project_endpoint)
except EDAError as eda_err:
module.fail_json(msg=eda_err)
module.exit_json(**ret)

project_type_params = {}
if description:
project_type_params["description"] = description
if url:
project_type_params["url"] = url

credential_id = None
if credential:
try:
credential_id = controller.get_one_or_many("credentials", credential)
except EDAError as eda_err:
module.fail_json(msg=eda_err)

if credential_id is not None:
# this is resolved earlier, so save an API call and don't do it again
# in the loop above
project_type_params["credential"] = credential_id

if new_name:
project_type_params["name"] = new_name
elif project_type:
project_type_params["name"] = controller.get_item_name(project_type)
else:
project_type_params["name"] = project_name

# If the state was present and we can let the module build or update the existing project type,
# this will return on its own
try:
ret = controller.create_or_update_if_needed(
project_type,
project_type_params,
endpoint=project_endpoint,
item_type="project type",
)
except EDAError as eda_err:
module.fail_json(msg=eda_err)

module.exit_json(**ret)


if __name__ == "__main__":
main()
81 changes: 81 additions & 0 deletions tests/integration/targets/project/tasks/create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- name: Create project in check mode
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_user }}"
validate_certs: "{{ validate_certs }}"
name: Example
description: "Example project description"
url: "https://example.com/project1"
state: present
check_mode: true
register: r

- name: Check project creation in check mode
assert:
that:
- r.changed

- name: Create project
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_user }}"
validate_certs: "{{ validate_certs }}"
name: Example
description: "Example project description"
url: "https://example.com/project1"
state: present
register: r

- name: Check project creation
assert:
that:
- r.changed

- name: Create project again
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_user }}"
validate_certs: "{{ validate_certs }}"
name: Example
description: "Example project description"
url: "https://example.com/project1"
state: present
register: r

- name: Check project is not created again
assert:
that:
- not r.changed

- name: Delete project in check mode
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_user }}"
validate_certs: "{{ validate_certs }}"
name: Example
state: absent
check_mode: true
register: r

- name: Check if delete project in check mode
assert:
that:
- r.changed

- name: Delete project
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_user }}"
validate_certs: "{{ validate_certs }}"
name: Example
state: absent
register: r

- name: Check if delete project
assert:
that:
- r.changed
45 changes: 45 additions & 0 deletions tests/integration/targets/project/tasks/delete.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- name: Delete operation without required name parameter
ansible.eda.project:
controller_host: "{{ controller_host }}"
state: absent
ignore_errors: true
register: r

- name: Check if project name is required
assert:
that:
- r.failed
- "'missing required arguments: name' in r.msg"

- name: Delete non-existing project in check mode
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_user }}"
validate_certs: "{{ validate_certs }}"
name: Example
state: absent
check_mode: true
register: r

- name: Check if delete non-existing project in check mode
assert:
that:
- not r.changed

- name: Delete non-existing project
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_user }}"
validate_certs: "{{ validate_certs }}"
name: Example
state: absent
register: r

- name: Check if delete non-existing project
assert:
that:
- not r.changed
20 changes: 20 additions & 0 deletions tests/integration/targets/project/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- block:
- include_tasks: create.yml
- include_tasks: delete.yml
- include_tasks: update.yml
always:
- name: Clean up - project
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_host }}"
validate_certs: "{{ validate_certs }}"
name: "{{ item }}"
state: absent
loop:
- Example
- Example_New
ignore_errors: true
67 changes: 67 additions & 0 deletions tests/integration/targets/project/tasks/update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- name: Create project
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_user }}"
validate_certs: "{{ validate_certs }}"
name: Example
description: "Example project description"
url: "https://example.com/project1"
state: present
register: r

- name: Check project creation
assert:
that:
- r.changed

- name: Update project name
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_user }}"
validate_certs: "{{ validate_certs }}"
name: Example
new_name: Example_New
description: "Example project description"
url: "https://example.com/project1"
state: present
register: r

- name: Check project update
assert:
that:
- r.changed

- name: Update project again
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_user }}"
validate_certs: "{{ validate_certs }}"
name: Example_New
new_name: Example_New
description: "Example project description"
url: "https://example.com/project1"
state: present
register: r

- name: Check project is not updated again
assert:
that:
- not r.changed

- name: Delete updated project
ansible.eda.project:
controller_host: "{{ controller_host }}"
controller_username: "{{ controller_user }}"
validate_certs: "{{ validate_certs }}"
name: Example_New
state: absent
register: r

- name: Check if delete project
assert:
that:
- r.changed

0 comments on commit d05f89c

Please sign in to comment.