diff --git a/awx_collection/plugins/modules/tower_label.py b/awx_collection/plugins/modules/tower_label.py index c22629c930..848e28431e 100644 --- a/awx_collection/plugins/modules/tower_label.py +++ b/awx_collection/plugins/modules/tower_label.py @@ -22,6 +22,8 @@ short_description: create, update, or destroy Ansible Tower labels. description: - Create, update, or destroy Ansible Tower labels. See U(https://www.ansible.com/tower) for an overview. + - Note, labels can only be created via the Tower API, they can not be deleted. + Once they are fully disassociated the API will clean them up on its own. options: name: description: @@ -31,7 +33,6 @@ options: new_name: description: - Setting this option will change the existing name (looked up via the name field). - required: True type: str organization: description: @@ -42,7 +43,7 @@ options: description: - Desired state of the resource. default: "present" - choices: ["present"] + choices: ["present"] type: str tower_oauthtoken: description: @@ -51,7 +52,6 @@ options: type: str version_added: "3.7" extends_documentation_fragment: awx.awx.auth -note: Labels can only be created via the Tower API, they can not be deleted. Once they are fully disassociated the API will clean them up on its own. ''' EXAMPLES = ''' @@ -70,7 +70,7 @@ def main(): name=dict(required=True, type='str'), new_name=dict(required=False, type='str'), organization=dict(required=True, type='str'), - state=dict(choices=['present', 'absent'], default='present'), + state=dict(choices=['present'], default='present'), ) # Create a module for ourselves @@ -97,7 +97,7 @@ def main(): # Create the data that gets sent for create and update new_fields = {} new_fields['name'] = new_name if new_name else name - if organization != None: + if organization: new_fields['organization'] = organization_id module.create_or_update_if_needed( diff --git a/awx_collection/test/awx/test_label.py b/awx_collection/test/awx/test_label.py new file mode 100644 index 0000000000..9ede40f3aa --- /dev/null +++ b/awx_collection/test/awx/test_label.py @@ -0,0 +1,47 @@ +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import pytest + +from awx.main.models import Label + + +@pytest.mark.django_db +def test_create_label(run_module, admin_user, organization): + result = run_module('tower_label', dict( + name='test-label', + organization=organization.name + ), admin_user) + assert not result.get('failed'), result.get('msg', result) + assert result.get('changed', False) + + assert Label.objects.get(name='test-label').organization == organization + + +@pytest.mark.django_db +def test_create_label_using_org_id(run_module, admin_user, organization): + result = run_module('tower_label', dict( + name='test-label', + organization=organization.id + ), admin_user) + assert not result.get('failed'), result.get('msg', result) + assert result.get('changed', False) + + assert Label.objects.get(name='test-label').organization == organization + + +@pytest.mark.django_db +def test_modify_label(run_module, admin_user, organization): + label = Label.objects.create(name='test-label', organization=organization) + + result = run_module('tower_label', dict( + name='test-label', + new_name='renamed-label', + organization=organization.name + ), admin_user) + assert not result.get('failed'), result.get('msg', result) + assert result.get('changed', False) + + label.refresh_from_db() + assert label.organization == organization + assert label.name == 'renamed-label'