def main():
    # define available arguments/parameters a user can pass to the module
    module_args = dict(port=dict(required=True),
                       device=dict(type='str', required=True),
                       portconf=dict(type='str'),
                       override=dict(type='dict', required=False, default={}),
                       **UniFi.DEFAULT_ARGS)
    required_if = [('state', 'present', ('portconf', ))]

    # initialize UniFi helper object
    unifi = UniFi(argument_spec=module_args, required_if=required_if)

    device = unifi.get_device()
    port_idx = get_port_idx(unifi.param('port'), device)
    port_overrides = device['port_overrides']
    change_required = update_port_overrides(unifi, port_overrides, port_idx)
    if change_required and not unifi.check_mode:
        unifi.send('/rest/device',
                   _id=device['_id'],
                   data={'port_overrides': port_overrides})
        unifi.result['changed'] = True
    unifi.result['ports'] = port_overrides

    # return the results
    unifi.exit()
Beispiel #2
0
def preprocess_portconf(unifi, portconf):
    site_id = unifi.get_site_id()
    if site_id is None:
        unifi.fail('Could not determine site for port profile {portconf}',
                   portconf=portconf['name'])
    portconf['site_id'] = site_id

    networkconfs = unifi.send('/rest/networkconf')

    portconf['forward'] = 'disabled'

    # lookup native network
    native_networkconf = portconf.pop('native_networkconf', None)
    if native_networkconf:
        native_networkconf_id = get_networkconf_id(native_networkconf,
                                                   networkconfs)
        if native_networkconf_id is None:
            unifi.fail('Could not determine native network for port '
                            'profile {portconf}', portconf=portconf['name'])
        portconf['native_networkconf_id'] = native_networkconf_id
        portconf['forward'] = 'native'
    else:
        portconf['native_networkconf_id'] = ''

    # lookup tagged networks
    tagged_networkconfs = portconf.pop('tagged_networkconfs', None)
    if tagged_networkconfs:
        if tagged_networkconfs == 'all':
            # tagged_networkconf_ids = [n['_id'] for n in networkconfs
            #                           if n['purpose'] == 'corporate']
            # portconf['tagged_networkconf_ids'] = tagged_networkconf_ids
            portconf['forward'] = 'all'
        else:
            tagged_networkconf_ids = [
                get_networkconf_id(tagged_networkconf, networkconfs)
                for tagged_networkconf in tagged_networkconfs
            ]
            if None in tagged_networkconf_ids:
                unifi.fail('Could not determine all tagged networks for '
                           'port profile {portconf}', portconf=portconf['name'])
            if 'native_networkconf_id' in portconf and \
                    portconf['native_networkconf_id'] in tagged_networkconf_ids:
                tagged_networkconf_ids.remove(portconf['native_networkconf_id'])
            portconf['tagged_networkconf_ids'] = tagged_networkconf_ids
            portconf['forward'] = 'customize'
    else:
        portconf['tagged_networkconf_ids'] = []

    # update the PoE mode
    poe_mode = portconf.pop('poe_mode', 'unchanged')
    if UniFi.find_invariant(poe_mode, [None, 'unchanged']):
        UniFi.set_missing(portconf, 'poe_mode')
    elif UniFi.find_invariant(poe_mode, [False, 'off']):
        portconf['poe_mode'] = 'off'
    elif UniFi.find_invariant(poe_mode, [True, 'on', 'poe', 'auto']):
        portconf['poe_mode'] = 'auto'
    else:
        unifi.fail('Could not determine PoE mode for port '
                   'profile {portconf}', portconf=portconf['name'])
def preprocess_portconf(unifi, portconf):
    site = unifi.get_site()
    if site is None:
        unifi.fail('Could not determine site for port profile {portconf}',
                   portconf=portconf['name'])
    portconf['site_id'] = site['_id']

    networkconfs = unifi.get_networkconfs()

    portconf['forward'] = 'disabled'

    # lookup native network
    native_networkconf = portconf.pop('native_networkconf', None)
    if native_networkconf:
        native_networkconf_id = get_networkconf_id(native_networkconf,
                                                   networkconfs)
        if native_networkconf_id is None:
            unifi.fail(
                'Could not determine native network for port '
                'profile {portconf}',
                portconf=portconf['name'])
        portconf['native_networkconf_id'] = native_networkconf_id
        portconf['forward'] = 'native'
    else:
        portconf['native_networkconf_id'] = ''

    # lookup tagged networks
    tagged_networkconfs = portconf.pop('tagged_networkconfs', None)
    if tagged_networkconfs:
        if tagged_networkconfs == 'all':
            # tagged_networkconf_ids = [n['_id'] for n in networkconfs
            #                           if n['purpose'] == 'corporate']
            # portconf['tagged_networkconf_ids'] = tagged_networkconf_ids
            portconf['forward'] = 'all'
        else:
            tagged_networkconf_ids = [
                get_networkconf_id(tagged_networkconf, networkconfs)
                for tagged_networkconf in tagged_networkconfs
            ]
            if None in tagged_networkconf_ids:
                unifi.fail(
                    'Could not determine all tagged networks for '
                    'port profile {portconf}',
                    portconf=portconf['name'])
            if 'native_networkconf_id' in portconf and \
                    portconf['native_networkconf_id'] in tagged_networkconf_ids:
                tagged_networkconf_ids.remove(
                    portconf['native_networkconf_id'])
            portconf['tagged_networkconf_ids'] = tagged_networkconf_ids
            portconf['forward'] = 'customize'
    else:
        portconf['tagged_networkconf_ids'] = []

    # update the PoE mode
    if 'poe_mode' not in portconf:
        UniFi.set_missing(portconf, 'poe_mode')
def main():
    # define available arguments/parameters a user can pass to the module
    module_args = dict(portconf=dict(type='dict', required=True),
                       **UniFi.DEFAULT_ARGS)

    # initialize UniFi helper object
    unifi = UniFi(argument_spec=module_args)

    # ensure that the input item will be reflected in the requested state
    # on the UniFi controller
    unifi.ensure_item('portconf', preprocess_item=preprocess_portconf)

    # return the results
    unifi.exit()
def update_port_overrides(unifi, port_overrides, port_idx):
    port = unifi.param('override')
    port['port_idx'] = port_idx

    portconf = unifi.param('portconf', default=None)
    if portconf is not None:
        portconf = unifi.get_portconf(name=portconf)
        if not portconf:
            unifi.fail('Could not find portconf {portconf}',
                       portconf=unifi.param('portconf'))
        port['portconf_id'] = portconf['_id']

    require_absent = []

    for key in ['name', 'autoneg', 'full_duplex', 'poe_mode', 'speed']:
        if key not in port:
            require_absent.append(key)

    found = False
    change_required = False
    remove_items = []
    for port_override in port_overrides:
        if port_override['port_idx'] == port_idx:
            found = True
            if unifi.param('state') == 'present':
                change_required = UniFi.update_item(port,
                                                    port_override,
                                                    require_absent,
                                                    log=unifi.log)
            elif unifi.param('state') == 'absent':
                remove_items.append(port_override)
                change_required = True

    if unifi.param('state') == 'present':
        if not found:
            port_overrides.append(port)
            change_required = True
    else:
        for item in remove_items:
            port_overrides.remove(item)

    return change_required