2017-02-20 23:16:32 +03:00
#!/usr/bin/python
2017-05-11 19:26:36 +03:00
# coding: utf-8 -*-
2017-02-20 23:16:32 +03:00
# (c) 2017, Wayne Witzel III <wayne@riotousliving.com>
Remove wildcard imports
Made the following changes:
* Removed wildcard imports
* Replaced long form of GPL header with short form
* Removed get_exception usage
* Added from __future__ boilerplate
* Adjust division operator to // where necessary
For the following files:
* web_infrastructure modules
* system modules
* linode, lxc, lxd, atomic, cloudscale, dimensiondata, ovh, packet,
profitbricks, pubnub, smartos, softlayer, univention modules
* compat dirs (disabled as its used intentionally)
2017-07-28 08:55:24 +03:00
# 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 ' ,
2017-03-14 19:07:22 +03:00
' status ' : [ ' preview ' ] ,
' supported_by ' : ' community ' }
2017-02-20 23:16:32 +03:00
DOCUMENTATION = '''
- - -
module : tower_team
2017-03-09 19:20:25 +03:00
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
2019-09-30 23:01:44 +03:00
type : str
2020-01-21 22:44:19 +03:00
new_name :
description :
- To use when changing a team ' s name.
required : True
type : str
2019-09-30 23:01:44 +03:00
description :
description :
- The description to use for the team .
type : str
2017-02-20 23:16:32 +03:00
organization :
description :
- Organization the team should be made a member of .
required : True
2019-09-30 23:01:44 +03:00
type : str
2017-02-20 23:16:32 +03:00
state :
description :
- Desired state of the resource .
choices : [ " present " , " absent " ]
2018-03-16 00:15:24 +03:00
default : " present "
2019-09-30 23:01:44 +03:00
type : str
2020-01-21 22:44:19 +03:00
tower_oauthtoken :
description :
- The Tower OAuth token to use .
required : False
type : str
2019-09-18 15:43:36 +03:00
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
2018-08-02 18:17:39 +03:00
argument_spec = dict (
2017-10-02 23:21:24 +03:00
name = dict ( required = True ) ,
2020-01-17 18:21:42 +03:00
new_name = dict ( required = False ) ,
2017-10-02 23:21:24 +03:00
description = dict ( ) ,
organization = dict ( required = True ) ,
state = dict ( choices = [ ' present ' , ' absent ' ] , default = ' present ' ) ,
2018-08-02 18:17:39 +03:00
)
2017-10-02 23:21:24 +03:00
2020-01-17 18:21:42 +03:00
# Create a module for ourselves
2018-08-02 18:17:39 +03:00
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 ' )
2020-01-19 00:13:33 +03:00
# Attempt to lookup 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 lookup team based on the provided name and org ID
team = module . get_one ( ' teams ' , * * {
' data ' : {
' name ' : name ,
' organization ' : org_id
}
} )
2020-01-21 22:44:19 +03:00
# Create data to sent to create and update
team_fields = {
2020-01-24 19:30:16 +03:00
' name ' : new_name if new_name else name ,
2020-01-21 22:44:19 +03:00
' description ' : description ,
' organization ' : org_id
}
2020-01-17 18:21:42 +03:00
if state == ' absent ' and not team :
# If the state was absent and we had no team, we can just return
module . exit_json ( * * module . json_output )
elif state == ' absent ' and team :
# If the state was absent and we had a team, we can try to delete it, the module will handle exiting from this
module . delete_endpoint ( ' teams/ {0} ' . format ( team [ ' id ' ] ) , item_type = ' team ' , item_name = name , * * { } )
elif state == ' present ' and not team :
2020-01-21 22:44:19 +03:00
# if the state was present and we couldn't find a team we can build one, the module will handle exiting from this
module . post_endpoint ( ' teams ' , item_type = ' team ' , item_name = name , * * { ' data ' : team_fields } )
2020-01-17 18:21:42 +03:00
else :
# If the state was present and we had a team we can see if we need to update it
# This will return on its own
module . update_if_needed ( team , team_fields )
2017-02-20 23:16:32 +03:00
if __name__ == ' __main__ ' :
main ( )