221 lines
6.9 KiB
Python
221 lines
6.9 KiB
Python
# Copyright: (c) 2020, Chris Gebhardt <cg@zknt.org>
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
import json
|
|
import requests
|
|
|
|
ANSIBLE_METADATA = {
|
|
'metadata_version': '1.1',
|
|
'status': ['preview'],
|
|
}
|
|
|
|
DOCUMENTATION = '''
|
|
---
|
|
module: gitea_organization
|
|
|
|
short_description: Manage Gitea organizations
|
|
|
|
description:
|
|
- "Manage organization accounts / groups in your Gitea"
|
|
|
|
options:
|
|
state:
|
|
description:
|
|
- The desired state of the organization
|
|
choices: ['present', 'absent']
|
|
default: present
|
|
required: no
|
|
name:
|
|
description:
|
|
- The username for your organization account / group
|
|
required: true
|
|
full_name:
|
|
description:
|
|
- The full name for your organization account / group
|
|
required: false
|
|
visibility:
|
|
description:
|
|
- Visibility of the organization, either private, public, or limited (visible to all logged in users)
|
|
choices: ['private', 'public', 'limited']
|
|
default: private
|
|
required: no
|
|
description:
|
|
description:
|
|
- The organizations description
|
|
required: no
|
|
location:
|
|
description:
|
|
- The organizations location
|
|
required: no
|
|
website:
|
|
description:
|
|
- The organizations website
|
|
required: no
|
|
repo_admin_change_team_access
|
|
description
|
|
- Wether repository admins can add and remove access for teams
|
|
choices: ['yes', 'no']
|
|
default: no
|
|
auth_token:
|
|
description:
|
|
- Authentification token for your gitea account
|
|
required: true
|
|
gitea_url:
|
|
description:
|
|
- Base URL of your gitea API instance (e.g. "https://git.zknt.org")
|
|
required: true
|
|
|
|
author:
|
|
- Chris Gebhardt <cg@zknt.org> (@hnrd)
|
|
'''
|
|
|
|
EXAMPLES = '''
|
|
# Create an organization
|
|
- name: Create organization
|
|
gitea_organization:
|
|
full_name: My Org
|
|
name: myorg
|
|
auth_token: 1234...6789
|
|
gitea_url: https://git.example.com
|
|
|
|
# Update organization
|
|
- name: Change organization
|
|
gitea_organization:
|
|
full_name: My better Org
|
|
name: myorg
|
|
auth_token: 1234...6789
|
|
gitea_url: https://git.example.com
|
|
'''
|
|
|
|
RETURN = '''
|
|
return_code:
|
|
description: The HTTP return code from the Gitea API
|
|
type: int
|
|
returned: always
|
|
gitea_respone:
|
|
description: The JSON output message that Gitea returns
|
|
type: dict
|
|
'''
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
def run_module():
|
|
module_args = dict(
|
|
state=dict(type='str', default='present', choices=['present', 'absent']),
|
|
auth_token=dict(type='str', required=True),
|
|
gitea_url=dict(type='str', required=True),
|
|
|
|
name=dict(type='str', required=True),
|
|
full_name=dict(type='str', default="", required=False),
|
|
visibility=dict(type='str', default="private", choices=['private', 'public', 'limited']),
|
|
|
|
repo_admin_change_team_access=dict(type="bool", required=False, default=False),
|
|
description=dict(type='str', default="", required=False),
|
|
location=dict(type='str', default="", required=False),
|
|
website=dict(type='str', default="", required=False),
|
|
)
|
|
|
|
result = dict(
|
|
changed=False,
|
|
return_code=0,
|
|
gitea_response={},
|
|
)
|
|
|
|
module = AnsibleModule(
|
|
argument_spec=module_args,
|
|
supports_check_mode=True
|
|
)
|
|
|
|
gitea_url = module.params['gitea_url']
|
|
username = module.params['name']
|
|
headers = {
|
|
"Content-Type": "application/json",
|
|
"Authorization": "token {}".format(module.params['auth_token'])
|
|
}
|
|
req_org = requests.get(
|
|
gitea_url + '/api/v1/orgs/' + username,
|
|
headers=headers
|
|
)
|
|
|
|
if module.check_mode:
|
|
module.exit_json(**result)
|
|
|
|
# Prepare org data
|
|
new_data = {
|
|
"username": username,
|
|
"full_name": module.params['full_name'],
|
|
"visibility": module.params['visibility'],
|
|
"repo_admin_change_team_access": module.params['repo_admin_change_team_access'],
|
|
"description": module.params['description'],
|
|
"location": module.params['location'],
|
|
"website": module.params['website'],
|
|
}
|
|
|
|
if module.params['state'] == 'present':
|
|
# org is requested and already exists
|
|
if req_org.status_code == 200:
|
|
result['return_code'] = 200
|
|
result['gitea_response'] = req_org.json()
|
|
old_data = req_org.json()
|
|
# check if org needs to be patched
|
|
if (
|
|
new_data['full_name'] != old_data['full_name'] or
|
|
new_data['description'] != old_data['description'] or
|
|
new_data['location'] != old_data['location'] or
|
|
new_data['repo_admin_change_team_access'] !=
|
|
old_data['repo_admin_change_team_access'] or
|
|
new_data['visibility'] != old_data['visibility'] or
|
|
new_data['website'] != old_data['website']
|
|
):
|
|
new_data.pop('username')
|
|
req_patch = requests.patch(
|
|
gitea_url + '/api/v1/orgs/' + username,
|
|
headers=headers,
|
|
data=json.dumps(new_data),
|
|
)
|
|
result['return_code'] = req_patch.status_code
|
|
result['gitea_response'] = req_patch.json()
|
|
result['changed'] = True
|
|
# all is fine
|
|
result['state'] = 'present'
|
|
if req_org.status_code == 404:
|
|
# org is requesed and does not yet exist, create
|
|
create_req = requests.post(
|
|
gitea_url + '/api/v1/orgs',
|
|
headers=headers,
|
|
data=json.dumps(new_data),
|
|
)
|
|
result['return_code'] = create_req.status_code
|
|
result['gitea_response'] = create_req.json()
|
|
if create_req.status_code != 201:
|
|
module.fail_json(msg="Creation failed", **result)
|
|
result['changed'] = True
|
|
result['state'] = 'present'
|
|
|
|
if module.params['state'] == 'absent':
|
|
if req_org.status_code == 404:
|
|
# org should be absent and does not exist
|
|
result['return_code'] = 404
|
|
result['gitea_response'] = req_org.json()
|
|
result['state'] = 'absent'
|
|
if req_org.status_code == 200:
|
|
# org should be abenst and needs to be deleted
|
|
delete_req = requests.delete(
|
|
gitea_url + '/api/v1/orgs/' + username,
|
|
headers=headers,
|
|
)
|
|
result['return_code'] = delete_req.status_code
|
|
if delete_req.status_code != 204:
|
|
# deletion failed
|
|
result['gitea_response'] = delete_req.json()
|
|
module.fail_json(msg="Deletion failed", **result)
|
|
result['changed'] = True
|
|
result['state'] = 'absent'
|
|
|
|
module.exit_json(**result)
|
|
|
|
def main():
|
|
run_module()
|
|
|
|
if __name__ == '__main__':
|
|
main()
|