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)
|
|
|
|
|
|
|
|
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 full name for your organization account / group
|
|
|
|
required: false
|
|
|
|
username:
|
|
|
|
description:
|
|
|
|
- The username for your organization account / group
|
|
|
|
required: true
|
|
|
|
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:
|
|
|
|
name: My Org
|
|
|
|
username: myorg
|
|
|
|
auth_token: 1234...6789
|
|
|
|
gitea_url: https://git.example.com
|
|
|
|
|
|
|
|
# Update organization
|
|
|
|
- name: Change organization
|
|
|
|
gitea_organization:
|
|
|
|
name: My better Org
|
|
|
|
username: myorg
|
|
|
|
auth_token: 1234...6789
|
|
|
|
gitea_url: https://git.example.com
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
original_message:
|
|
|
|
description: The original name param that was passed in
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
message:
|
|
|
|
description: The output message that the test module generates
|
|
|
|
type: str
|
|
|
|
returned: always
|
|
|
|
'''
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
|
|
|
username=dict(type='str', required=True),
|
|
|
|
name=dict(type='bool', required=False, default=False)
|
|
|
|
|
|
|
|
# TODO support "description": "string",
|
|
|
|
# TODO support "location": "string",
|
|
|
|
# TODO support "repo_admin_change_team_access": true,
|
|
|
|
# TODO support "visibility": "public",
|
|
|
|
# TODO support "website": "string"
|
|
|
|
)
|
|
|
|
|
|
|
|
result = dict(
|
|
|
|
changed=False,
|
|
|
|
return_code='',
|
|
|
|
gitea_response={},
|
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=module_args,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
}
|
2020-08-02 19:52:35 +00:00
|
|
|
gitea_url = module.params['gitea_url']
|
2020-08-02 19:40:22 +00:00
|
|
|
gitea_token = module.params['auth_token']
|
2020-08-02 19:52:35 +00:00
|
|
|
username = module.params['username']
|
2020-08-02 19:40:22 +00:00
|
|
|
req_org = requests.get(
|
2020-08-02 19:52:35 +00:00
|
|
|
gitea_url + '/api/v1/orgs/' + username +
|
|
|
|
'?access_token=' + gitea_token,
|
2020-08-02 19:40:22 +00:00
|
|
|
headers=headers
|
|
|
|
)
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
if module.params['state'] == 'present':
|
|
|
|
if req_org.status_code == 200:
|
|
|
|
##TODO support modify modifyable params
|
|
|
|
result['return_code'] = 200
|
|
|
|
result['gitea_response'] = req_org.json()
|
|
|
|
result['state'] = 'present'
|
|
|
|
if req_org.status_code == 404:
|
2020-08-02 19:46:50 +00:00
|
|
|
data = {
|
|
|
|
#TODO "description": "string",
|
|
|
|
"full_name": module.params['name'],
|
|
|
|
#TODO "location": "string",
|
|
|
|
#TODO "repo_admin_change_team_access": true,
|
2020-08-02 19:52:35 +00:00
|
|
|
"username": username,
|
2020-08-02 19:46:50 +00:00
|
|
|
#TODO "visibility": "public",
|
|
|
|
#TODO "website": "string"
|
|
|
|
}
|
2020-08-02 19:52:35 +00:00
|
|
|
create_req = requests.post(
|
|
|
|
gitea_url + '/api/v1/orgs' + '?access_token=' + gitea_token,
|
|
|
|
headers=headers,
|
|
|
|
data=json.dumps(data),
|
|
|
|
)
|
2020-08-02 19:46:50 +00:00
|
|
|
if create_req.status_code != 201:
|
|
|
|
result['gitea_response'] = create_req.json()
|
|
|
|
module.fail_json(msg="Creation failed", **result)
|
|
|
|
else:
|
|
|
|
result['gitea_response'] = create_req.json()
|
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:
|
|
|
|
result['return_code'] = 404
|
|
|
|
result['gitea_response'] = req_org.json()
|
|
|
|
result['state'] = 'absent'
|
|
|
|
if req_org.status_code == 200:
|
2020-08-02 19:52:35 +00:00
|
|
|
delete_req = requests.delete(
|
|
|
|
gitea_url + '/api/v1/orgs/' + username +
|
|
|
|
'?access_token=' + gitea_token,
|
|
|
|
headers=headers,
|
|
|
|
)
|
|
|
|
if delete_req.status_code != 204:
|
|
|
|
result['gitea_response'] = create_req.json()
|
|
|
|
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__':
|
|
|
|
main()
|