def op_get_device(d42): ''' Get a single device. Required Parameters: device_id Examples: ./d42-cli --get-device -p device_id=449 ./d42-cli --get-device -p device_name=zm1.st1 ./d42-cli --get-device --params device_id=449 ''' if check_deps(d42.params, ['device_id']) and check_deps( d42.params, ['device_name']): d42.err( 'A device requires either device_id or device_name be specified. Not both.', 110) return False elif check_deps(d42.params, ['device_id']): ret = d42.api('/devices/id/{}/'.format(d42.params['device_id'])) elif check_deps(d42.params, ['device_name']): ret = d42.api('/devices/name/{}/'.format(d42.params['device_name'])) else: d42.err('Required options were not found', 110) return False if not ret['result']: d42.err('API Error', 111, ret['data']) return False d42.out(ret['data']) return True
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
def op_request_ip(d42): ''' Reserve or suggest an IP address in a given subnet. Required Parameters: subnet_id|subnet|name Non-optional Parameters: reserve_ip=(yes|No) ''' if not (check_deps(d42.params, ['subnet_id']) or check_deps(d42.params, ['subnet']) or check_deps(d42.params, ['name'])): d42.err('Need one of: subnet_id, subnet, name', 115) return False if not check_deps(d42.params, ['reserve_ip']): stderr('Parameter reserve_ip was missing; default does not reserve', 'WARNING') ret = d42.api('/suggest_ip/', post=d42.params) if not ret['result']: d42.err('API Error', 116, ret['data']) return False d42.out(ret['data']) return True
def op_create_password(d42): ''' Create a new password in D42. Required Parameters: username, password ''' if not check_deps(d42.params, ['username', 'password']): d42.err('Required options were not found: username, password', 125) return False ret = d42.api('/passwords/', post=d42.params) if not ret['result']: d42.err('API Error', 126, ret['data']) return False d42.out(ret)
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
def op_get_vlan(d42): ''' Get a single vlan. Required Parameters: vlan_id ''' if not check_deps(d42.params, ['vlan_id']): d42.err('Required options were not found: vlan_id', 135) return False ret = d42.api('/vlans/{}/'.format(d42.params['vlan_id'])) if not ret['result']: d42.err('API Error', 136, ret['data']) return False d42.out(ret['data']) return True
def op_get_ip(d42): ''' Get a single ip. Required Parameters: ip_id ''' if not check_deps(d42.params, ['ip_id']): d42.err('Required options were not found: ip_id', 115) return False ret = d42.api('/ips/{}/'.format(d42.params['ip_id'])) if not ret['result']: d42.err('API Error', 116, ret['data']) return False d42.out(ret['data']) return True
def op_create_ip(d42): ''' Create a new ip in D42. Required Parameters: number Optional Parameters: http://api.device42.com/#update-ips ''' if not check_deps(d42.params, ['ipaddress']): d42.err('Required options were not found: ipaddress', 115) if _ip_exists(d42, {'ip': d42.params['ipaddress']}): d42.err('An existing ip matched this create request', 115) ret = d42.api('/ips/', post=d42.params) if not ret['result']: d42.err('API Error', 116, ret['data']) d42.out(ret)
def op_update_password(d42): ''' Update an existing password in D42. Required Parameters: id ''' if not check_deps(d42.params, ['id']): d42.err('Required options were not found: id', 125) return False ret = d42.api('/passwords/', post=d42.params) if not ret['result']: d42.err('API Error', 126, ret['data']) return False d42.out(ret) return True
def op_get_subnet(d42): ''' Get a single subnet. Required Parameters: subnet_id ''' if not check_deps(d42.params, ['subnet_id']): d42.err('Required options were not found: subnet_id', 130) return False ret = d42.api('/subnets/{}/'.format(d42.params['subnet_id'])) if not ret['result']: d42.err('API Error', 131, ret['data']) return False d42.out(ret['data']) return True
def op_create_subnet(d42): ''' Create a new subnet in D42. Required Parameters: network, mask_bits, name Optional Parameters: http://api.device42.com/#create-update-subnets ''' if not check_deps(d42.params, ['network', 'mask_bits', 'name']): d42.err('Required options were not found: network, mask_bits, name', 130) if _subnet_exists(d42, d42.params): d42.err('An existing subnet matched this create request', 130) ret = d42.api('/subnets/', post=d42.params) if not ret['result']: d42.err('API Error', 131, ret['data']) d42.out(ret)
def op_create_vlan(d42): ''' Create a new vlan in D42. Required Parameters: number Optional Parameters: http://api.device42.com/#update-vlans ''' if not check_deps(d42.params, ['number']): d42.err('Required options were not found: number', 135) return False if _vlan_exists(d42, {'number': d42.params['number']}): d42.err('An existing vlan matched this create request', 135) return False ret = d42.api('/vlans/', post=d42.params) if not ret['result']: d42.err('API Error', 136, ret['data']) return False d42.out(ret) return True
def op_update_device(d42): ''' Update an existing device in D42. Required Parameters: name (name) Optional Parameters: http://api.device42.com/#create-update-device-by-name ''' if not check_deps(d42.params, ['name']): d42.err('The field name is a required.', 110) return False if not _device_exists(d42, d42.params['name']): d42.err('No device with this name currently exists.', 110) return False ret = d42.api('/device/', post=d42.params) if not ret['result']: d42.err('API Error', 111, ret['data']) return False d42.out(ret) return True
def op_update_ip(d42): ''' Update an existing ip in D42. Required Parameters: id Optional Parameters: http://api.device42.com/#update-ips ''' if not check_deps(d42.params, ['ipaddress']): d42.err('Required options were not found: ipaddress', 115) return False if not _ip_exists(d42, {'ip': d42.params['ipaddress']}): d42.err('No existing ip was found for this update request', 115) return False ret = d42.api('/ips/', post=d42.params) if not ret['result']: d42.err('API Error', 116, ret['data']) return False d42.out(ret) return True
def op_delete_ip(d42): ''' Delete a ip. Required Parameters: ip_id ''' if not check_deps(d42.params, ['ip_id']): d42.err('Required options were not found: ip_id', 115) return False if not d42.opts.misc_yes: if not confirm('Are you sure you want to delete the given ip?'): d42.err('Terminated at user request', 119) return False ret = d42.api('/ips/{}/'.format(d42.params['ip_id']), post=d42.params, delete=True) if not ret['result']: d42.err('API Error', 116, ret['data']) return False d42.out(ret['data']) return True
def op_update_subnet(d42): ''' Update an existing subnet in D42. Required Parameters: network, mask_bits, name Optional Parameters: http://api.device42.com/#create-update-subnets ''' if not check_deps(d42.params, ['network', 'mask_bits']): d42.err('Required options were not found: network, mask_bits', 130) return False if not _subnet_exists(d42, d42.params): d42.err('No existing subnet was found for this update request', 130) return False ret = d42.api('/subnets/', post=d42.params) if not ret['result']: d42.err('API Error', 131, ret['data']) return False d42.out(ret) return True
def op_delete_subnet(d42): ''' Delete a subnet. Required Parameters: subnet_id ''' if not check_deps(d42.params, ['subnet_id']): d42.err('Required options were not found: subnet_id', 130) return False if not d42.opts.misc_yes: if not confirm('Are you sure you want to delete the given subnet?'): d42.err('Terminated at user request', 134) return False ret = d42.api('/subnets/{}/'.format(d42.params['subnet_id']), post=d42.params, delete=True) if not ret['result']: d42.err('API Error', 131, ret['data']) return False d42.out(ret['data']) return True
def op_update_vlan(d42): ''' Update an existing vlan in D42. Required Parameters: number Optional Parameters: http://api.device42.com/#update-vlans ''' if not check_deps(d42.params, ['id']): d42.err('Required options were not found: id', 135) return False if not _vlan_exists(d42, {'vlan_id': d42.params['id']}): d42.err('No existing vlan was found for this update request', 135) return False params = dict(d42.params) del params['id'] ret = d42.api('/vlans/{}/'.format(d42.params['id']), post=params) if not ret['result']: d42.err('API Error', 136, ret['data']) return False d42.out(ret) return True