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

zendesk_ticket: new module #8543

Open
wants to merge 42 commits into
base: main
Choose a base branch
from

Conversation

elchico2007
Copy link

SUMMARY

Creating a new module to interface with the Zendesk ticketing system. With this module you are able to utilize the API from Zendesk to Create, Close and Resolve tickets.

ISSUE TYPE
  • New Module/Plugin Pull Request
COMPONENT NAME

zendesk_ticket.py

@ansibullbot ansibullbot added module module new_contributor Help guide this first time contributor plugins plugin (any type) labels Jun 19, 2024
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-9 Automatically create a backport for the stable-9 branch labels Jun 20, 2024
plugins/modules/zendesk_ticket.py Outdated Show resolved Hide resolved
plugins/modules/zendesk_ticket.py Outdated Show resolved Hide resolved
ticket_id = module.params['ticket_id']

if not password and not token:
module.fail_json(msg="Either 'password' or 'token' must be provided.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are using required_one_of, this if block here becomes redundant. Just remove it.

elif status in ['closed', 'resolved']:
if not ticket_id:
module.fail_json(msg="The 'ticket_id' must be provided when the status is 'closed'")
result = zendesk_api.close_ticket(ticket_id, status, body)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any idempotence checks here, like "is the ticket already closed" before closing it, or "does the ticket already exist" before creating it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felixfontein comment still stands - it is not resolved. I believe it could be solved somewhat easily by adding the state as a parameter to check_ticket and verifying whether the ticket state is different.

@felixfontein
Copy link
Collaborator

Also please check out the requirements in https://github.com/ansible-collections/community.general/blob/main/CONTRIBUTING.md#creating-new-modules-or-plugins for new modules. You need to add tests, for example.

@ansibullbot ansibullbot added the new_plugin New plugin label Jul 2, 2024
@ansibullbot ansibullbot removed the stale_ci CI is older than 7 days, rerun before merging label Aug 20, 2024
@elchico2007
Copy link
Author

Added the failing mock code, here is the issue i am seeing on my end:

[gw1] [100%] FAILED tests/unit/plugins/modules/test_zendesk_ticket.py::TestZendeskTicket::test_create_ticket_with_mock 

=================================== FAILURES ===================================
________________ TestZendeskTicket.test_create_ticket_with_mock ________________
[gw1] linux -- Python 3.7.17 /usr/bin/python3.7

self = <ansible_collections.community.general.tests.unit.plugins.modules.test_zendesk_ticket.TestZendeskTicket testMethod=test_create_ticket_with_mock>

    def test_create_ticket_with_mock(self):
        set_module_args({
            'url': 'https://your_company.zendesk.com',
            'username': 'user',
            'token': 'test_token',
            'status': 'new',
            'subject': 'Test Ticket',
            'body': 'This is a test ticket body',
            'priority': 'high'
        })
    
        with HTTMock(zendesk_api_mock):
            with self.assertRaises(AnsibleExitJson) as result:
>               self.module.main()

tests/unit/plugins/modules/test_zendesk_ticket.py:107: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
plugins/modules/zendesk_ticket.py:296: in main
    module.fail_json(**result)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    def fail_json(*args, **kwargs):
        kwargs['failed'] = True
>       raise AnsibleFailJson(kwargs)
E       ansible_collections.community.general.tests.unit.plugins.modules.utils.AnsibleFailJson: {'changed': False, 'msg': '<urlopen error [Errno -3] Temporary failure in name resolution>', 'failed': True}

tests/unit/plugins/modules/utils.py:42: AnsibleFailJson
- generated xml file: /root/ansible_collections/community/general/tests/output/junit/python3.7-modules-units.xml -
=========================== short test summary info ============================
FAILED tests/unit/plugins/modules/test_zendesk_ticket.py::TestZendeskTicket::test_create_ticket_with_mock - ansible_collections.community.general.tests.unit.plugins.modules.utils.Ansi...
========================= 1 failed, 3 passed in 2.91s ==========================
FATAL: Command "pytest -r a -n auto --color yes -p no:cacheprovider -c /root/ansible/test/lib/ansible_test/_data/pytest/config/legacy.ini --junit-xml /root/ansible_collections/community/general/tests/output/junit/python3.7-modules-units.xml --strict-markers --rootdir /root/ansible_collections/community/general --confcutdir /root/ansible_collections/community/general -v tests/unit/plugins/modules/test_zendesk_ticket.py" returned exit status 1.
Run command with stdout: docker exec --user pytest -i ansible-test-controller-KSSkgQIZ sh - ...
Run command with stdin: tar oxzf - -C /home/user/.ansible/collections/ansible_collection ...
FATAL: Command "docker exec --user pytest ansible-test-controller-KSSkgQIZ /usr/bin/env ANSIBLE_TEST_CONTENT_ROOT=/root/ansible_collections/community/general LC_ALL=en_US.UTF-8 /usr/bin/python3.12 /root/ansible/bin/ansible-test units -v tests/unit/plugins/modules/test_zendesk_ticket.py --containers '{}' --requirements-mode skip --truncate 96 --color yes --host-path tests/output/.tmp/host-ktegvo5y --metadata tests/output/.tmp/metadata-nugaecbc.json" returned exit status 1.
Run command: docker stop --time 0 ansible-test-controller-KSSkgQIZ
Run command: docker rm ansible-test-controller-KSSkgQIZ

@ansibullbot ansibullbot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR and removed needs_info This issue requires further information. Please answer any outstanding questions labels Aug 20, 2024
plugins/modules/zendesk_ticket.py Outdated Show resolved Hide resolved
plugins/modules/zendesk_ticket.py Outdated Show resolved Hide resolved
plugins/modules/zendesk_ticket.py Outdated Show resolved Hide resolved
tests/unit/plugins/modules/test_zendesk_ticket.py Outdated Show resolved Hide resolved
@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Aug 26, 2024
@elchico2007
Copy link
Author

Removed the http mock library, let me know if there are any other improvements or changes i can make to it.

Comment on lines +166 to +170
request = self.api_auth()
try:
response = request.get(url)
except Exception:
return False
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is not wrong, however, for the user of this module, information is lost: why has it failed? what was the error message? They will have an error and have little idea about what happened.
This could be an improvement for another PR, no need to hold this one back over this.

Copy link
Collaborator

@russoz russoz Aug 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test only proves that the code handles exceptions, it does not test how the code actually works: that was the idea of mocking.
Since the requests library is not being used (to warrant the use of httmock), maybe the answer is a direct mocking of the ansible.module_utils.urls.Request class, or at the very least, of its methods .get(), .patch(), etc...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I guess we need a better mechanism for this. For open_url and fetch_url in the same module there are some unit test frameworks in https://github.com/ansible-collections/community.internal_test_tools/tree/main/tests/unit/utils, but not (yet) for the Request class...

@russoz russoz changed the title Module(zendesk ticket) adding new module zendesk_ticket: new module Aug 31, 2024
@russoz
Copy link
Collaborator

russoz commented Sep 3, 2024

Removed the http mock library, let me know if there are any other improvements or changes i can make to it.

By the way, sorry for indicating httmock before, I obviously did not know it was specific to requests. My apologies. Hope my snafu has not frustrated you too much. Cheers

@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Sep 5, 2024
@felixfontein
Copy link
Collaborator

@russoz does the module look OK enough for you to merge for 9.4.0?

@russoz
Copy link
Collaborator

russoz commented Sep 5, 2024

@russoz does the module look OK enough for you to merge for 9.4.0?

I think it needs a bit polishing yet, specially in the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-9 Automatically create a backport for the stable-9 branch check-before-release PR will be looked at again shortly before release and merged if possible. module module new_contributor Help guide this first time contributor new_plugin New plugin plugins plugin (any type) stale_ci CI is older than 7 days, rerun before merging tests tests unit tests/unit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants