def main():
    argument_spec = basic_auth_argument_spec()
    argument_spec.update(
        api_token=dict(type='str', required=True, no_log=True),
        project=dict(type='str', required=True),
        purge=dict(type='bool', required=False, default=False),
        vars=dict(type='dict', required=False, default=dict(), no_log=True),
        state=dict(type='str', default="present", choices=["absent", "present"])
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=[
            ['api_username', 'api_token'],
            ['api_password', 'api_token'],
        ],
        required_together=[
            ['api_username', 'api_password'],
        ],
        required_one_of=[
            ['api_username', 'api_token']
        ],
        supports_check_mode=True
    )

    purge = module.params['purge']
    var_list = module.params['vars']
    state = module.params['state']

    if not HAS_GITLAB_PACKAGE:
        module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR)

    gitlab_instance = gitlabAuthentication(module)

    this_gitlab = GitlabProjectVariables(module=module, gitlab_instance=gitlab_instance)

    change, return_value = native_python_main(this_gitlab, purge, var_list, state)

    module.exit_json(changed=change, project_variable=return_value)
def main():
    argument_spec = basic_auth_argument_spec()
    argument_spec.update(
        dict(
            api_token=dict(type='str', no_log=True),
            group=dict(type='str'),
            name=dict(type='str', required=True),
            path=dict(type='str'),
            description=dict(type='str'),
            issues_enabled=dict(type='bool', default=True),
            merge_requests_enabled=dict(type='bool', default=True),
            wiki_enabled=dict(type='bool', default=True),
            snippets_enabled=dict(default=True, type='bool'),
            visibility=dict(type='str',
                            default="private",
                            choices=["internal", "private", "public"],
                            aliases=["visibility_level"]),
            import_url=dict(type='str'),
            state=dict(type='str',
                       default="present",
                       choices=["absent", "present"]),
        ))

    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=[
            ['api_username', 'api_token'],
            ['api_password', 'api_token'],
        ],
        required_together=[
            ['api_username', 'api_password'],
        ],
        required_one_of=[['api_username', 'api_token']],
        supports_check_mode=True,
    )

    group_identifier = module.params['group']
    project_name = module.params['name']
    project_path = module.params['path']
    project_description = module.params['description']
    issues_enabled = module.params['issues_enabled']
    merge_requests_enabled = module.params['merge_requests_enabled']
    wiki_enabled = module.params['wiki_enabled']
    snippets_enabled = module.params['snippets_enabled']
    visibility = module.params['visibility']
    import_url = module.params['import_url']
    state = module.params['state']

    if not HAS_GITLAB_PACKAGE:
        module.fail_json(msg=missing_required_lib("python-gitlab"),
                         exception=GITLAB_IMP_ERR)

    gitlab_instance = gitlabAuthentication(module)

    # Set project_path to project_name if it is empty.
    if project_path is None:
        project_path = project_name.replace(" ", "_")

    gitlab_project = GitLabProject(module, gitlab_instance)

    if group_identifier:
        group = findGroup(gitlab_instance, group_identifier)
        if group is None:
            module.fail_json(
                msg="Failed to create project: group %s doesn't exists" %
                group_identifier)

        namespace = gitlab_instance.namespaces.get(group.id)
        project_exists = gitlab_project.existsProject(namespace, project_path)
    else:
        user = gitlab_instance.users.list(
            username=gitlab_instance.user.username)[0]
        namespace = gitlab_instance.namespaces.get(user.id)
        project_exists = gitlab_project.existsProject(namespace, project_path)

    if state == 'absent':
        if project_exists:
            gitlab_project.deleteProject()
            module.exit_json(changed=True,
                             msg="Successfully deleted project %s" %
                             project_name)
        else:
            module.exit_json(changed=False,
                             msg="Project deleted or does not exists")

    if state == 'present':
        if gitlab_project.createOrUpdateProject(
                project_name, namespace, {
                    "path": project_path,
                    "description": project_description,
                    "issues_enabled": issues_enabled,
                    "merge_requests_enabled": merge_requests_enabled,
                    "wiki_enabled": wiki_enabled,
                    "snippets_enabled": snippets_enabled,
                    "visibility": visibility,
                    "import_url": import_url
                }):

            module.exit_json(
                changed=True,
                msg="Successfully created or updated the project %s" %
                project_name,
                project=gitlab_project.projectObject._attrs)
        else:
            module.exit_json(changed=False,
                             msg="No need to update the project %s" %
                             project_name,
                             project=gitlab_project.projectObject._attrs)
def main():
    argument_spec = basic_auth_argument_spec()
    argument_spec.update(
        dict(
            api_token=dict(type='str', no_log=True),
            state=dict(type='str',
                       default="present",
                       choices=["absent", "present"]),
            project=dict(type='str', required=True),
            hook_url=dict(type='str', required=True),
            push_events=dict(type='bool', default=True),
            push_events_branch_filter=dict(type='str', default=''),
            issues_events=dict(type='bool', default=False),
            merge_requests_events=dict(type='bool', default=False),
            tag_push_events=dict(type='bool', default=False),
            note_events=dict(type='bool', default=False),
            job_events=dict(type='bool', default=False),
            pipeline_events=dict(type='bool', default=False),
            wiki_page_events=dict(type='bool', default=False),
            hook_validate_certs=dict(type='bool',
                                     default=False,
                                     aliases=['enable_ssl_verification']),
            token=dict(type='str', no_log=True),
        ))

    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=[['api_username', 'api_token'],
                            ['api_password', 'api_token']],
        required_together=[['api_username', 'api_password']],
        required_one_of=[['api_username', 'api_token']],
        supports_check_mode=True,
    )

    state = module.params['state']
    project_identifier = module.params['project']
    hook_url = module.params['hook_url']
    push_events = module.params['push_events']
    push_events_branch_filter = module.params['push_events_branch_filter']
    issues_events = module.params['issues_events']
    merge_requests_events = module.params['merge_requests_events']
    tag_push_events = module.params['tag_push_events']
    note_events = module.params['note_events']
    job_events = module.params['job_events']
    pipeline_events = module.params['pipeline_events']
    wiki_page_events = module.params['wiki_page_events']
    enable_ssl_verification = module.params['hook_validate_certs']
    hook_token = module.params['token']

    if not HAS_GITLAB_PACKAGE:
        module.fail_json(msg=missing_required_lib("python-gitlab"),
                         exception=GITLAB_IMP_ERR)

    gitlab_instance = gitlabAuthentication(module)

    gitlab_hook = GitLabHook(module, gitlab_instance)

    project = findProject(gitlab_instance, project_identifier)

    if project is None:
        module.fail_json(
            msg="Failed to create hook: project %s doesn't exists" %
            project_identifier)

    hook_exists = gitlab_hook.existsHook(project, hook_url)

    if state == 'absent':
        if hook_exists:
            gitlab_hook.deleteHook()
            module.exit_json(changed=True,
                             msg="Successfully deleted hook %s" % hook_url)
        else:
            module.exit_json(changed=False,
                             msg="Hook deleted or does not exists")

    if state == 'present':
        if gitlab_hook.createOrUpdateHook(
                project, hook_url, {
                    "push_events": push_events,
                    "push_events_branch_filter": push_events_branch_filter,
                    "issues_events": issues_events,
                    "merge_requests_events": merge_requests_events,
                    "tag_push_events": tag_push_events,
                    "note_events": note_events,
                    "job_events": job_events,
                    "pipeline_events": pipeline_events,
                    "wiki_page_events": wiki_page_events,
                    "enable_ssl_verification": enable_ssl_verification,
                    "token": hook_token
                }):

            module.exit_json(
                changed=True,
                msg="Successfully created or updated the hook %s" % hook_url,
                hook=gitlab_hook.hookObject._attrs)
        else:
            module.exit_json(changed=False,
                             msg="No need to update the hook %s" % hook_url,
                             hook=gitlab_hook.hookObject._attrs)
Ejemplo n.º 4
0
def main():
    argument_spec = basic_auth_argument_spec()
    argument_spec.update(
        dict(
            api_token=dict(type='str', no_log=True),
            name=dict(type='str', required=True),
            path=dict(type='str'),
            description=dict(type='str'),
            state=dict(type='str',
                       default="present",
                       choices=["absent", "present"]),
            parent=dict(type='str'),
            visibility=dict(type='str',
                            default="private",
                            choices=["internal", "private", "public"]),
        ))

    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=[
            ['api_username', 'api_token'],
            ['api_password', 'api_token'],
        ],
        required_together=[
            ['api_username', 'api_password'],
        ],
        required_one_of=[['api_username', 'api_token']],
        supports_check_mode=True,
    )

    group_name = module.params['name']
    group_path = module.params['path']
    description = module.params['description']
    state = module.params['state']
    parent_identifier = module.params['parent']
    group_visibility = module.params['visibility']

    if not HAS_GITLAB_PACKAGE:
        module.fail_json(msg=missing_required_lib("python-gitlab"),
                         exception=GITLAB_IMP_ERR)

    gitlab_instance = gitlabAuthentication(module)

    # Define default group_path based on group_name
    if group_path is None:
        group_path = group_name.replace(" ", "_")

    gitlab_group = GitLabGroup(module, gitlab_instance)

    parent_group = None
    if parent_identifier:
        parent_group = findGroup(gitlab_instance, parent_identifier)
        if not parent_group:
            module.fail_json(
                msg="Failed create GitLab group: Parent group doesn't exists")

        group_exists = gitlab_group.existsGroup(parent_group.full_path + '/' +
                                                group_path)
    else:
        group_exists = gitlab_group.existsGroup(group_path)

    if state == 'absent':
        if group_exists:
            gitlab_group.deleteGroup()
            module.exit_json(changed=True,
                             msg="Successfully deleted group %s" % group_name)
        else:
            module.exit_json(changed=False,
                             msg="Group deleted or does not exists")

    if state == 'present':
        if gitlab_group.createOrUpdateGroup(
                group_name, parent_group, {
                    "path": group_path,
                    "description": description,
                    "visibility": group_visibility
                }):
            module.exit_json(
                changed=True,
                msg="Successfully created or updated the group %s" %
                group_name,
                group=gitlab_group.groupObject._attrs)
        else:
            module.exit_json(changed=False,
                             msg="No need to update the group %s" % group_name,
                             group=gitlab_group.groupObject._attrs)
def main():
    argument_spec = basic_auth_argument_spec()
    argument_spec.update(dict(
        api_token=dict(type='str', no_log=True),
        state=dict(type='str', default="present", choices=["absent", "present"]),
        project=dict(type='str', required=True),
        key=dict(type='str', required=True),
        can_push=dict(type='bool', default=False),
        title=dict(type='str', required=True)
    ))

    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=[
            ['api_username', 'api_token'],
            ['api_password', 'api_token']
        ],
        required_together=[
            ['api_username', 'api_password']
        ],
        required_one_of=[
            ['api_username', 'api_token']
        ],
        supports_check_mode=True,
    )

    state = module.params['state']
    project_identifier = module.params['project']
    key_title = module.params['title']
    key_keyfile = module.params['key']
    key_can_push = module.params['can_push']

    if not HAS_GITLAB_PACKAGE:
        module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR)

    gitlab_instance = gitlabAuthentication(module)

    gitlab_deploy_key = GitLabDeployKey(module, gitlab_instance)

    project = findProject(gitlab_instance, project_identifier)

    if project is None:
        module.fail_json(msg="Failed to create deploy key: project %s doesn't exists" % project_identifier)

    deployKey_exists = gitlab_deploy_key.existsDeployKey(project, key_title)

    if state == 'absent':
        if deployKey_exists:
            gitlab_deploy_key.deleteDeployKey()
            module.exit_json(changed=True, msg="Successfully deleted deploy key %s" % key_title)
        else:
            module.exit_json(changed=False, msg="Deploy key deleted or does not exists")

    if state == 'present':
        if gitlab_deploy_key.createOrUpdateDeployKey(project, key_title, key_keyfile, {'can_push': key_can_push}):

            module.exit_json(changed=True, msg="Successfully created or updated the deploy key %s" % key_title,
                             deploy_key=gitlab_deploy_key.deployKeyObject._attrs)
        else:
            module.exit_json(changed=False, msg="No need to update the deploy key %s" % key_title,
                             deploy_key=gitlab_deploy_key.deployKeyObject._attrs)
def main():
    argument_spec = basic_auth_argument_spec()
    argument_spec.update(
        dict(
            api_token=dict(type='str', no_log=True),
            name=dict(type='str'),
            state=dict(type='str',
                       default="present",
                       choices=["absent", "present"]),
            username=dict(type='str', required=True),
            password=dict(type='str', no_log=True),
            email=dict(type='str'),
            sshkey_name=dict(type='str'),
            sshkey_file=dict(type='str'),
            group=dict(type='str'),
            access_level=dict(type='str',
                              default="guest",
                              choices=[
                                  "developer", "guest", "maintainer", "master",
                                  "owner", "reporter"
                              ]),
            confirm=dict(type='bool', default=True),
            isadmin=dict(type='bool', default=False),
            external=dict(type='bool', default=False),
        ))

    module = AnsibleModule(argument_spec=argument_spec,
                           mutually_exclusive=[
                               ['api_username', 'api_token'],
                               ['api_password', 'api_token'],
                           ],
                           required_together=[
                               ['api_username', 'api_password'],
                           ],
                           required_one_of=[['api_username', 'api_token']],
                           supports_check_mode=True,
                           required_if=(('state', 'present',
                                         ['name', 'email', 'password']), ))

    user_name = module.params['name']
    state = module.params['state']
    user_username = module.params['username'].lower()
    user_password = module.params['password']
    user_email = module.params['email']
    user_sshkey_name = module.params['sshkey_name']
    user_sshkey_file = module.params['sshkey_file']
    group_path = module.params['group']
    access_level = module.params['access_level']
    confirm = module.params['confirm']
    user_isadmin = module.params['isadmin']
    user_external = module.params['external']

    if not HAS_GITLAB_PACKAGE:
        module.fail_json(msg=missing_required_lib("python-gitlab"),
                         exception=GITLAB_IMP_ERR)

    gitlab_instance = gitlabAuthentication(module)

    gitlab_user = GitLabUser(module, gitlab_instance)
    user_exists = gitlab_user.existsUser(user_username)

    if state == 'absent':
        if user_exists:
            gitlab_user.deleteUser()
            module.exit_json(changed=True,
                             msg="Successfully deleted user %s" %
                             user_username)
        else:
            module.exit_json(changed=False,
                             msg="User deleted or does not exists")

    if state == 'present':
        if gitlab_user.createOrUpdateUser(
                user_username, {
                    "name": user_name,
                    "password": user_password,
                    "email": user_email,
                    "sshkey_name": user_sshkey_name,
                    "sshkey_file": user_sshkey_file,
                    "group_path": group_path,
                    "access_level": access_level,
                    "confirm": confirm,
                    "isadmin": user_isadmin,
                    "external": user_external
                }):
            module.exit_json(
                changed=True,
                msg="Successfully created or updated the user %s" %
                user_username,
                user=gitlab_user.userObject._attrs)
        else:
            module.exit_json(changed=False,
                             msg="No need to update the user %s" %
                             user_username,
                             user=gitlab_user.userObject._attrs)
Ejemplo n.º 7
0
def main():
    argument_spec = basic_auth_argument_spec()
    argument_spec.update(dict(
        api_token=dict(type='str', no_log=True),
        description=dict(type='str', required=True, aliases=["name"]),
        active=dict(type='bool', default=True),
        tag_list=dict(type='list', default=[]),
        run_untagged=dict(type='bool', default=True),
        locked=dict(type='bool', default=False),
        access_level=dict(type='str', default='ref_protected', choices=["not_protected", "ref_protected"]),
        maximum_timeout=dict(type='int', default=3600),
        registration_token=dict(type='str', required=True),
        state=dict(type='str', default="present", choices=["absent", "present"]),
    ))

    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=[
            ['api_username', 'api_token'],
            ['api_password', 'api_token'],
        ],
        required_together=[
            ['api_username', 'api_password'],
        ],
        required_one_of=[
            ['api_username', 'api_token'],
        ],
        supports_check_mode=True,
    )

    state = module.params['state']
    runner_description = module.params['description']
    runner_active = module.params['active']
    tag_list = module.params['tag_list']
    run_untagged = module.params['run_untagged']
    runner_locked = module.params['locked']
    access_level = module.params['access_level']
    maximum_timeout = module.params['maximum_timeout']
    registration_token = module.params['registration_token']

    if not HAS_GITLAB_PACKAGE:
        module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR)

    gitlab_instance = gitlabAuthentication(module)

    gitlab_runner = GitLabRunner(module, gitlab_instance)
    runner_exists = gitlab_runner.existsRunner(runner_description)

    if state == 'absent':
        if runner_exists:
            gitlab_runner.deleteRunner()
            module.exit_json(changed=True, msg="Successfully deleted runner %s" % runner_description)
        else:
            module.exit_json(changed=False, msg="Runner deleted or does not exists")

    if state == 'present':
        if gitlab_runner.createOrUpdateRunner(runner_description, {
                                              "active": runner_active,
                                              "tag_list": tag_list,
                                              "run_untagged": run_untagged,
                                              "locked": runner_locked,
                                              "access_level": access_level,
                                              "maximum_timeout": maximum_timeout,
                                              "registration_token": registration_token}):
            module.exit_json(changed=True, runner=gitlab_runner.runnerObject._attrs,
                             msg="Successfully created or updated the runner %s" % runner_description)
        else:
            module.exit_json(changed=False, runner=gitlab_runner.runnerObject._attrs,
                             msg="No need to update the runner %s" % runner_description)