Example #1
0
def op_search_vlans(d42):
    '''
    Search for vlans within D42.
    With no parameters specified, all results are returned.
    '''
    qs = '?{}'.format(encode(d42.params)) if d42.params is not None else ''
    ret = d42.api('/vlans/{}'.format(qs))

    if not ret['result']:
        d42.err('API Error', 136, ret['data'])
        return False

    d42.out(ret['data'])
    return True
Example #2
0
def _ip_exists(d42, params):
    '''
    Checks whether a ip with the given name exists.
    Returns: Boolean
    '''
    qs = '?{}'.format(encode(params))
    ret = d42.api('/ips/{}'.format(qs))
    if not ret['result']:
        d42.err('Failed to check if ip currently exists.', 115)
    if not 'ips' in ret['data']:
        d42.err('Failed to check if ip currently exists.', 115)
    if len(ret['data']['ips']) == 0:
        return False
    return True
Example #3
0
def _subnet_exists(d42, params):
    '''
    Checks whether a subnet with the given name exists.
    Returns: Boolean
    '''
    if not check_deps(params, ['network', 'mask_bits']):
        d42.err('Function requires network and mask_bits', 130)
    qs = '?{}'.format(encode({'network': params['network'], 'mask_bits': params['mask_bits']}))
    ret = d42.api('/subnets/{}'.format(qs))
    if not ret['result']:
        d42.err('Failed to check if subnet currently exists.', 130)
    if not 'subnets' in ret['data']:
        d42.err('Failed to check if subnet currently exists.', 130)
    if len(ret['data']['subnets']) == 0:
        return False
    return True
Example #4
0
def _vlan_exists(d42, params):
    '''
    Checks whether a vlan with the given name exists.
    Returns: Boolean
    '''
    if not check_deps(params, ['number']) and not check_deps(
            params, ['vlan_id']):
        d42.err('Function requires number or id', 135)
    qs = '?{}'.format(encode(params))
    ret = d42.api('/vlans/{}'.format(qs))
    if not ret['result']:
        d42.err('Failed to check if vlan currently exists.', 135)
    if not 'vlans' in ret['data']:
        d42.err('Failed to check if vlan currently exists.', 135)
    if len(ret['data']['vlans']) == 0:
        return False
    return True
Example #5
0
def op_search_devices(d42):
    '''
    Search for devices within D42.
    With no parameters specified, all results are returned.
    Examples:
      ./d42-cli --search-devices -p type=virtual
      ./d42-cli --search-devices -p type=virtual -p building=st1
      ./d42-cli --search-devices
    '''
    qs = '?{}'.format(encode(d42.params)) if d42.params is not None else ''
    vs = 'all/' if d42.opts.misc_verbose else ''
    ret = d42.api('/devices/{}{}'.format(vs, qs))

    if not ret['result']:
        d42.err('API Error', 111, ret['data'])
        return False

    d42.out(ret['data'])
    return True
Example #6
0
def op_get_password(d42):
    '''
    Get a single password.
    Requires search to return a single value.
    Provides only raw or secret output.
    '''
    qs = '?{}'.format(encode(d42.params)) if d42.params is not None else ''
    ret = d42.api('/passwords/{}'.format(qs))

    if not ret['result']:
        d42.err(ret['data'], 125)
        return False

    if not 'Passwords' in ret['data']:
        d42.err('Invalid results returned from API', 125)
        return False
    elif len(ret['data']['Passwords']) > 1:
        d42.err('Too many passwords in D42 match this query', 125)
        return False
    elif len(ret['data']['Passwords']) < 1:
        d42.err('No passwords in D42 match this query', 125)
        return False

    ret = d42.api('/passwords/{}{}'.format(qs, '&plain_text=yes'))

    if not ret['result']:
        d42.err('API Error', 126, ret['data'])
        return False

    pw = ret['data']['Passwords'][0]['password']
    # If raw output was requested, provide the raw password to the shell.
    if d42.opts.output == 'raw':
        d42.out(pw, fmt='raw')
    else:
        d42.out(pw, fmt='secret')

    return True
Example #7
0
def getChunk(x, z):
    x = int(x)
    z = int(z)
    #print 'got chunk coords: (%d, %d)' % (x, z)
    return util.encode(chunk.getChunk(x, z))