Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add project module #238

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ auths
Passw
AUTHS
EDAHTTP
refspec
178 changes: 178 additions & 0 deletions plugins/modules/project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#!/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
"""

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)
Akasurde marked this conversation as resolved.
Show resolved Hide resolved
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
Akasurde marked this conversation as resolved.
Show resolved Hide resolved

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()
121 changes: 121 additions & 0 deletions plugins/modules/project_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/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_info
author:
- Abhijeet Kasurde (@akasurde)
short_description: List projects in EDA Controller
description:
- This module allows user to list project in a EDA controller.
version_added: '2.0.0'
options:
name:
description:
- The name of the project.
- Return information about particular project available on EDA Controller.
type: str
extends_documentation_fragment:
- ansible.eda.eda_controller.auths
"""

EXAMPLES = """
- name: List a particular project
ansible.eda.project_info:
controller_host: https://my_eda_host/
controller_username: admin
controller_password: MySuperSecretPassw0rd
name: "Example"
register: r

- name: List all projects
ansible.eda.project_info:
controller_host: https://my_eda_host/
controller_username: admin
controller_password: MySuperSecretPassw0rd
register: r
"""

RETURN = """
projects:
description: List of dicts containing information about projects
returned: success
type: list
sample: [
{
"created_at": "2024-08-12T20:35:28.367702Z",
"description": "",
"eda_credential_id": null,
"git_hash": "417b4dbe9b3472fd64212ef8233b865585e5ade3",
"id": 17,
"import_error": null,
"import_state": "completed",
"modified_at": "2024-08-12T20:35:28.367724Z",
"name": "Sample Example Project",
"organization_id": 1,
"proxy": "",
"scm_branch": "",
"scm_refspec": "",
"scm_type": "git",
"signature_validation_credential_id": null,
"url": "https://github.com/ansible/ansible-ui",
"verify_ssl": true
},
]
""" # NOQA

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(),
)

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")

ret = {}

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

if ret is None:
ret = []
if not isinstance(ret, list):
ret = [ret]
module.exit_json(projects=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
Akasurde marked this conversation as resolved.
Show resolved Hide resolved
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
Loading