1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 16:51:11 +03:00
awx/awx_collection/plugins/modules/tower_team.py

125 lines
3.6 KiB
Python
Raw Normal View History

2017-02-20 23:16:32 +03:00
#!/usr/bin/python
# coding: utf-8 -*-
2017-02-20 23:16:32 +03:00
# (c) 2017, Wayne Witzel III <wayne@riotousliving.com>
# 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
2017-02-20 23:16:32 +03:00
2017-08-16 06:16:38 +03:00
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
2017-02-20 23:16:32 +03:00
DOCUMENTATION = '''
---
module: tower_team
author: "Wayne Witzel III (@wwitzel3)"
2017-02-20 23:16:32 +03:00
version_added: "2.3"
short_description: create, update, or destroy Ansible Tower team.
description:
- Create, update, or destroy Ansible Tower teams. See
U(https://www.ansible.com/tower) for an overview.
options:
name:
description:
- Name to use for the team.
required: True
type: str
new_name:
description:
- To use when changing a team's name.
required: False
type: str
version_added: "3.7"
description:
description:
- The description to use for the team.
required: False
type: str
2017-02-20 23:16:32 +03:00
organization:
description:
- Organization the team should be made a member of.
required: True
type: str
2017-02-20 23:16:32 +03:00
state:
description:
- Desired state of the resource.
choices: ["present", "absent"]
default: "present"
type: str
tower_oauthtoken:
description:
- The Tower OAuth token to use.
required: False
type: str
version_added: "3.7"
extends_documentation_fragment: awx.awx.auth
2017-02-20 23:16:32 +03:00
'''
EXAMPLES = '''
- name: Create tower team
tower_team:
name: Team Name
description: Team Description
organization: test-org
state: present
tower_config_file: "~/tower_cli.cfg"
'''
2020-01-17 18:21:42 +03:00
from ..module_utils.tower_api import TowerModule
2017-02-20 23:16:32 +03:00
def main():
2020-01-17 18:21:42 +03:00
# Any additional arguments that are not fields of the item can be added here
argument_spec = dict(
name=dict(required=True),
2020-01-17 18:21:42 +03:00
new_name=dict(required=False),
description=dict(required=False),
organization=dict(required=True),
state=dict(choices=['present', 'absent'], default='present'),
)
2020-01-17 18:21:42 +03:00
# Create a module for ourselves
module = TowerModule(argument_spec=argument_spec, supports_check_mode=True)
2017-02-20 23:16:32 +03:00
2020-01-17 18:21:42 +03:00
# Extract our parameters
2017-02-20 23:16:32 +03:00
name = module.params.get('name')
2020-01-17 18:21:42 +03:00
new_name = module.params.get('new_name')
2017-02-20 23:16:32 +03:00
description = module.params.get('description')
organization = module.params.get('organization')
state = module.params.get('state')
# Attempt to look up the related items the user specified (these will fail the module if not found)
2020-01-17 18:21:42 +03:00
org_id = module.resolve_name_to_id('organizations', organization)
# Attempt to look up team based on the provided name and org ID
2020-01-17 18:21:42 +03:00
team = module.get_one('teams', **{
'data': {
'name': name,
'organization': org_id
}
})
# Create the data that gets sent for create and update
team_fields = {
'name': new_name if new_name else name,
'organization': org_id
}
if description is not None:
team_fields['description'] = description
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
module.delete_if_needed(team)
elif state == 'present':
# If the state was present and we can let the module build or update the existing team, this will return on its own
module.create_or_update_if_needed(team, team_fields, endpoint='teams', item_type='team')
2017-02-20 23:16:32 +03:00
if __name__ == '__main__':
main()