ansible-gitea/source_control/gitea_organization.py

222 lines
6.9 KiB
Python
Raw Normal View History

2020-08-02 19:40:22 +00:00
# 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)
2020-08-02 20:00:33 +00:00
import json
2020-08-02 19:40:22 +00:00
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:
2020-08-03 09:14:40 +00:00
description:
- The username for your organization account / group
required: true
full_name:
2020-08-02 19:40:22 +00:00
description:
- The full name for your organization account / group
required: false
2020-08-03 09:14:40 +00:00
visibility:
2020-08-02 19:40:22 +00:00
description:
2020-08-03 09:14:40 +00:00
- 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
2020-08-02 19:40:22 +00:00
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:
2020-08-03 09:14:40 +00:00
full_name: My Org
name: myorg
2020-08-02 19:40:22 +00:00
auth_token: 1234...6789
gitea_url: https://git.example.com
# Update organization
- name: Change organization
gitea_organization:
2020-08-03 09:14:40 +00:00
full_name: My better Org
name: myorg
2020-08-02 19:40:22 +00:00
auth_token: 1234...6789
gitea_url: https://git.example.com
'''
RETURN = '''
2020-08-03 09:14:40 +00:00
return_code:
description: The HTTP return code from the Gitea API
type: int
2020-08-02 19:40:22 +00:00
returned: always
2020-08-03 09:14:40 +00:00
gitea_respone:
description: The JSON output message that Gitea returns
type: dict
2020-08-02 19:40:22 +00:00
'''
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),
2020-08-03 09:14:40 +00:00
name=dict(type='str', required=True),
full_name=dict(type='str', default="", required=False),
visibility=dict(type='str', default="private", choices=['private', 'public', 'limited']),
2020-08-02 19:40:22 +00:00
2020-08-03 09:14:40 +00:00
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),
2020-08-02 19:40:22 +00:00
)
result = dict(
changed=False,
2020-08-02 19:58:17 +00:00
return_code=0,
2020-08-02 19:40:22 +00:00
gitea_response={},
)
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)
2020-08-03 10:08:03 +00:00
gitea_url = module.params['gitea_url']
username = module.params['name']
2020-08-02 19:40:22 +00:00
headers = {
"Content-Type": "application/json",
2020-08-03 10:08:03 +00:00
"Authorization": "token {}".format(module.params['auth_token'])
2020-08-02 19:40:22 +00:00
}
req_org = requests.get(
2020-08-03 10:08:03 +00:00
gitea_url + '/api/v1/orgs/' + username,
2020-08-02 19:40:22 +00:00
headers=headers
)
if module.check_mode:
module.exit_json(**result)
2020-08-03 09:14:40 +00:00
# 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'],
}
2020-08-02 19:40:22 +00:00
if module.params['state'] == 'present':
2020-08-03 09:14:40 +00:00
# org is requested and already exists
2020-08-02 19:40:22 +00:00
if req_org.status_code == 200:
result['return_code'] = 200
result['gitea_response'] = req_org.json()
2020-08-03 09:14:40 +00:00
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(
2020-08-03 10:08:03 +00:00
gitea_url + '/api/v1/orgs/' + username,
2020-08-03 09:14:40 +00:00
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
2020-08-02 19:40:22 +00:00
result['state'] = 'present'
if req_org.status_code == 404:
2020-08-03 09:14:40 +00:00
# org is requesed and does not yet exist, create
2020-08-02 19:52:35 +00:00
create_req = requests.post(
2020-08-03 10:08:03 +00:00
gitea_url + '/api/v1/orgs',
2020-08-02 19:52:35 +00:00
headers=headers,
2020-08-03 09:14:40 +00:00
data=json.dumps(new_data),
2020-08-02 19:52:35 +00:00
)
2020-08-02 19:58:17 +00:00
result['return_code'] = create_req.status_code
result['gitea_response'] = create_req.json()
2020-08-02 19:46:50 +00:00
if create_req.status_code != 201:
module.fail_json(msg="Creation failed", **result)
2020-08-02 19:40:22 +00:00
result['changed'] = True
result['state'] = 'present'
if module.params['state'] == 'absent':
if req_org.status_code == 404:
2020-08-03 09:14:40 +00:00
# org should be absent and does not exist
2020-08-02 19:40:22 +00:00
result['return_code'] = 404
result['gitea_response'] = req_org.json()
result['state'] = 'absent'
if req_org.status_code == 200:
2020-08-03 09:14:40 +00:00
# org should be abenst and needs to be deleted
2020-08-02 19:52:35 +00:00
delete_req = requests.delete(
2020-08-03 10:08:03 +00:00
gitea_url + '/api/v1/orgs/' + username,
2020-08-02 19:52:35 +00:00
headers=headers,
)
2020-08-02 19:58:17 +00:00
result['return_code'] = delete_req.status_code
2020-08-02 19:52:35 +00:00
if delete_req.status_code != 204:
2020-08-03 09:14:40 +00:00
# deletion failed
2020-08-09 17:29:50 +00:00
result['gitea_response'] = delete_req.json()
2020-08-02 19:52:35 +00:00
module.fail_json(msg="Deletion failed", **result)
2020-08-02 19:40:22 +00:00
result['changed'] = True
result['state'] = 'absent'
module.exit_json(**result)
def main():
run_module()
if __name__ == '__main__':
2020-08-03 09:14:40 +00:00
main()