Beispiel #1
0
def switch(module, check_mode):
    try:
        access_token = module.params['access_token'] or os.environ['BIGSWITCH_ACCESS_TOKEN']
    except KeyError as e:
        module.fail_json(msg='Unable to load %s' % e.message, exception=traceback.format_exc())

    name = module.params['name']
    fabric_role = module.params['fabric_role']
    leaf_group = module.params['leaf_group']
    dpid = '00:00:' + module.params['mac']
    state = module.params['state']
    controller = module.params['controller']

    rest = Rest(module,
                {'content-type': 'application/json', 'Cookie': 'session_cookie=' + access_token},
                'https://' + controller + ':8443/api/v1/data/controller/core')

    response = rest.get('switch-config', data={})
    if response.status_code != 200:
        module.fail_json(msg="failed to obtain existing switch config: {}".format(response.json['description']))

    config_present = False
    for switch in response.json:
        if all((switch['name'] == name,
                switch['fabric-role'] == fabric_role,
                switch['dpid'] == dpid)):
            config_present = switch.get('leaf-group', None) == leaf_group
            if config_present:
                break

    if state in ('present') and config_present:
        module.exit_json(changed=False)

    if state in ('absent') and not config_present:
        module.exit_json(changed=False)

    if check_mode:
        module.exit_json(changed=True)

    if state in ('present'):
        data = {'name': name, 'fabric-role': fabric_role, 'leaf-group': leaf_group, 'dpid': dpid}
        response = rest.put('switch-config[name="%s"]' % name, data)
        if response.status_code == 204:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg="error configuring switch '{}': {}".format(name, response.json['description']))

    if state in ('absent'):
        response = rest.delete('switch-config[name="%s"]' % name, data={})
        if response.status_code == 204:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg="error deleting switch '{}': {}".format(name, response.json['description']))
Beispiel #2
0
def chain(module):
    try:
        access_token = module.params['access_token'] or os.environ[
            'BIGSWITCH_ACCESS_TOKEN']
    except KeyError as e:
        module.fail_json(msg='Unable to load %s' % e.message,
                         exception=traceback.format_exc())

    name = module.params['name']
    state = module.params['state']
    controller = module.params['controller']

    rest = Rest(
        module, {
            'content-type': 'application/json',
            'Cookie': 'session_cookie=' + access_token
        }, 'https://' + controller +
        ':8443/api/v1/data/controller/applications/bigchain')

    if None in (name, state, controller):
        module.fail_json(msg='parameter `name` is missing')

    response = rest.get('chain?config=true', data={})
    if response.status_code != 200:
        module.fail_json(msg="failed to obtain existing chain config: {0}".
                         format(response.json['description']))

    config_present = False
    matching = [chain for chain in response.json if chain['name'] == name]
    if matching:
        config_present = True

    if state in ('present') and config_present:
        module.exit_json(changed=False)

    if state in ('absent') and not config_present:
        module.exit_json(changed=False)

    if state in ('present'):
        response = rest.put('chain[name="%s"]' % name, data={'name': name})
        if response.status_code == 204:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg="error creating chain '{0}': {1}".format(
                name, response.json['description']))

    if state in ('absent'):
        response = rest.delete('chain[name="%s"]' % name, data={})
        if response.status_code == 204:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg="error deleting chain '{0}': {1}".format(
                name, response.json['description']))
Beispiel #3
0
def policy(module):
    try:
        access_token = module.params['access_token'] or os.environ['BIGSWITCH_ACCESS_TOKEN']
    except KeyError as e:
        module.fail_json(msg='Unable to load %s' % e.message, exception=traceback.format_exc())

    name = module.params['name']
    policy_description = module.params['policy_description']
    action = module.params['action']
    priority = module.params['priority']
    duration = module.params['duration']
    start_time = module.params['start_time']
    delivery_packet_count = module.params['delivery_packet_count']
    state = module.params['state']
    controller = module.params['controller']

    rest = Rest(module,
                {'content-type': 'application/json', 'Cookie': 'session_cookie=' + access_token},
                'https://' + controller + ':8443/api/v1/data/controller/applications/bigtap')

    if name is None:
        module.fail_json(msg='parameter `name` is missing')

    response = rest.get('policy?config=true', data={})
    if response.status_code != 200:
        module.fail_json(msg="failed to obtain existing policy config: {0}".format(response.json['description']))

    config_present = False

    matching = [policy for policy in response.json
                if policy['name'] == name and
                policy['duration'] == duration and
                policy['delivery-packet-count'] == delivery_packet_count and
                policy['policy-description'] == policy_description and
                policy['action'] == action and
                policy['priority'] == priority]

    if matching:
        config_present = True

    if state in ('present') and config_present:
        module.exit_json(changed=False)

    if state in ('absent') and not config_present:
        module.exit_json(changed=False)

    if state in ('present'):
        data = {'name': name, 'action': action, 'policy-description': policy_description,
                'priority': priority, 'duration': duration, 'start-time': start_time,
                'delivery-packet-count': delivery_packet_count}

        response = rest.put('policy[name="%s"]' % name, data=data)
        if response.status_code == 204:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg="error creating policy '{0}': {1}".format(name, response.json['description']))

    if state in ('absent'):
        response = rest.delete('policy[name="%s"]' % name, data={})
        if response.status_code == 204:
            module.exit_json(changed=True)
        else:
            module.fail_json(msg="error deleting policy '{0}': {1}".format(name, response.json['description']))