organization module skel

This commit is contained in:
chris 2020-08-02 21:40:22 +02:00
commit 9f45999fde

View file

@ -0,0 +1,147 @@
# 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),
# TODO support "username": "string",
name=dict(type='bool', required=False, default=False)
# TODO support "full_name": "string", <- name
# 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",
}
gitea_token = module.params['auth_token']
req_org = requests.get(
module.params['gitea_url']+'/api/v1/orgs/'+data['username']+'?access_token='+gitea_token,
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:
##TODO create
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:
##TODO delete
result['changed'] = True
result['state'] = 'absent'
module.exit_json(**result)
def main():
run_module()
if __name__ == '__main__':
main()