예제 #1
0
def delete(context, args):
    return test.delete(context, id=args.id)
예제 #2
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(default='present', choices=['present', 'absent'], type='str'),
            # Authentication related parameters
            #
            dci_login=dict(required=False, type='str'),
            dci_password=dict(required=False, type='str'),
            dci_cs_url=dict(required=False, type='str'),
            # Resource related parameters
            #
            id=dict(type='str'),
            name=dict(type='str'),
            data=dict(type='dict'),
            team_id=dict(type='str'),
        ),
    )

    if not dciclient_found:
        module.fail_json(msg='The python dciclient module is required')

    login, password, url = get_details(module)
    if not login or not password:
        module.fail_json(msg='login and/or password have not been specified')

    ctx = dci_context.build_dci_context(url, login, password, 'Ansible')

    # Action required: Delete the test matching test id
    # Endpoint called: /tests/<test_id> DELETE via dci_test.delete()
    #
    # If the test exists and it has been succesfully deleted the changed is
    # set to true, else if the test does not exist changed is set to False
    if module.params['state'] == 'absent':
        if not module.params['id']:
            module.fail_json(msg='id parameter is required')
        res = dci_test.delete(ctx, module.params['id'])

    # Action required: Retrieve test informations
    # Endpoint called: /tests/<test_id> GET via dci_test.get()
    #
    # Get test informations
    elif module.params['id']:
        res = dci_test.get(ctx, module.params['id'])

    # Action required: Create a test with the specified content
    # Endpoint called: /tests POST via dci_test.create()
    #
    # Create the new test.
    else:
        if not module.params['name']:
            module.fail_json(msg='name parameter must be specified')
        if not module.params['team_id']:
            module.fail_json(msg='team_id parameter must be specified')

        kwargs = {
            'name': module.params['name'],
            'team_id': module.params['team_id'],
        }
        if module.params['data']:
            kwargs['data'] = module.params['data']

        res = dci_test.create(ctx, **kwargs)

    try:
        result = res.json()
        if res.status_code == 404:
            module.fail_json(msg='The resource does not exist')
        if res.status_code == 409:
            result['changed'] = False
        else:
            result['changed'] = True
    except:
        result = {}
        result['changed'] = True

    module.exit_json(**result)