Skip to content

Commit

Permalink
Add credential_type module (#240)
Browse files Browse the repository at this point in the history
Signed-off-by: Alina Buzachis <[email protected]>
Co-authored-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
alinabuzachis and Akasurde authored Aug 15, 2024
1 parent 9a593af commit 379959e
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ Passw
AUTHS
EDAHTTP
refspec
Alina
Buzachis
alinabuzachis
4 changes: 4 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
# https://access.redhat.com/support/policy/updates/ansible-automation-platform
# https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html#ansible-core-support-matrix
requires_ansible: ">=2.15.0" # AAP 2.4 or newer
action_groups:
eda:
- credential_type
- credential_type_info
16 changes: 16 additions & 0 deletions plugins/module_utils/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-

# Copyright: Contributors to the Ansible project
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)

from __future__ import absolute_import, division, print_function

__metaclass__ = type


def to_list_of_dict(result):
if result is None:
return []
if not isinstance(result, list):
return [result]
return result
173 changes: 173 additions & 0 deletions plugins/modules/credential_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/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: credential_type
author:
- Alina Buzachis (@alinabuzachis)
short_description: Manage credential types in EDA Controller
description:
- This module allows the user to create, update or delete a credential type in EDA controller.
version_added: 2.0.0
options:
name:
description:
- The name of the credential type.
type: str
required: true
description:
description:
- The description of the credential type to give more detail about it.
type: str
new_name:
description:
- Setting this option will change the existing name.
type: str
inputs:
description:
- Inputs of the credential type.
type: dict
injectors:
description:
- Injectors of the credential type.
type: dict
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 a credential type
ansible.eda.credential_type:
name: "Test"
state: present
description: "A test credential type"
inputs:
fields:
- id: "Field1"
type: "string"
label: "Label1"
injectors:
extra_vars:
field1: "field1"
- name: Delete a credential type
ansible.eda.credential_type:
name: "Test"
state: absent
"""


RETURN = """
id:
description: ID of the credential type.
returned: when exists
type: int
sample: 37
"""


from ansible.module_utils.basic import AnsibleModule

from ..module_utils.arguments import AUTH_ARGSPEC
from ..module_utils.client import Client
from ..module_utils.controller import Controller
from ..module_utils.errors import EDAError


def main():
argument_spec = dict(
name=dict(type="str", required=True),
new_name=dict(type="str"),
description=dict(type="str"),
inputs=dict(type="dict"),
injectors=dict(type="dict"),
state=dict(choices=["present", "absent"], default="present"),
)

argument_spec.update(AUTH_ARGSPEC)

required_if = [("state", "present", ("inputs", "injectors"))]

module = AnsibleModule(
argument_spec=argument_spec, required_if=required_if, 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"),
)

name = module.params.get("name")
new_name = module.params.get("new_name")
description = module.params.get("description")
inputs = module.params.get("inputs")
injectors = module.params.get("injectors")
state = module.params.get("state")

credential_type_params = {}
if description:
credential_type_params["description"] = description
if inputs:
credential_type_params["inputs"] = inputs
if injectors:
credential_type_params["injectors"] = injectors

controller = Controller(client, module)

# Attempt to look up credential_type based on the provided name
try:
credential_type = controller.get_one_or_many("credential-types", name=name)
except EDAError as e:
module.fail_json(msg=f"Failed to get credential type: {e}")

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:
result = controller.delete_if_needed(
credential_type, endpoint="credential-types"
)
module.exit_json(**result)
except EDAError as e:
module.fail_json(msg=f"Failed to delete credential type: {e}")

credential_type_params["name"] = (
new_name
if new_name
else (controller.get_item_name(credential_type) if credential_type else name)
)

# If the state was present and we can let the module build or update the existing credential type,
# this will return on its own
try:
result = controller.create_or_update_if_needed(
credential_type,
credential_type_params,
endpoint="credential-types",
item_type="credential type",
)
module.exit_json(**result)
except EDAError as e:
module.fail_json(msg=f"Failed to create/update credential type: {e}")


if __name__ == "__main__":
main()
116 changes: 116 additions & 0 deletions plugins/modules/credential_type_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/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: credential_type_info
author:
- Alina Buzachis (@alinabuzachis)
short_description: List credential types in EDA Controller
description:
- List credential types in EDA controller.
version_added: 2.0.0
options:
name:
description:
- The name of the credential type.
type: str
required: false
extends_documentation_fragment:
- ansible.eda.eda_controller.auths
"""


EXAMPLES = """
- name: Get information about a credential type
ansible.eda.credential_type_info:
name: "Test"
- name: List all credential types
ansible.eda.credential_type_info:
"""


RETURN = """
credential_types:
description: Information about the credential types.
returned: always
type: list
elements: dict
sample: [{
"created_at": "2024-08-14T08:30:14.806638Z",
"description": "A test credential type",
"id": 37,
"injectors": {
"extra_vars": {
"field1": "field1"
}
},
"inputs": {
"fields": [
{
"id": "field1",
"label": "Field 5",
"type": "string"
}
]
},
"kind": "cloud",
"managed": false,
"modified_at": "2024-08-14T08:30:14.807549Z",
"name": "Example",
"namespace": null
}]
"""


from ansible.module_utils.basic import AnsibleModule

from ..module_utils.arguments import AUTH_ARGSPEC
from ..module_utils.client import Client
from ..module_utils.common import to_list_of_dict
from ..module_utils.controller import Controller
from ..module_utils.errors import EDAError


def main():
argument_spec = dict(
name=dict(type="str", required=False),
)

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

name = module.params.get("name")
controller = Controller(client, module)

# Attempt to look up credential_type based on the provided name
try:
result = controller.get_one_or_many(
"credential-types", name=name, want_one=False
)
except EDAError as e:
module.fail_json(msg=f"Failed to get credential type: {e}")

module.exit_json(credential_types=to_list_of_dict(result))


if __name__ == "__main__":
main()
9 changes: 5 additions & 4 deletions plugins/modules/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@
"""

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

from ..module_utils.arguments import AUTH_ARGSPEC
from ..module_utils.client import Client
from ..module_utils.controller import Controller
from ..module_utils.errors import EDAError


def main():
Expand Down
9 changes: 5 additions & 4 deletions plugins/modules/project_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@
""" # 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

from ..module_utils.arguments import AUTH_ARGSPEC
from ..module_utils.client import Client
from ..module_utils.controller import Controller
from ..module_utils.errors import EDAError


def main():
Expand Down
Loading

0 comments on commit 379959e

Please sign in to comment.