def _create_device(channel, deviceinfo): import uuid from xivo_dao.helpers import provd_connector device_manager = provd_connector.device_manager() config_manager = provd_connector.config_manager() if 'id' not in deviceinfo: device_id = str(uuid.uuid4()) else: device_id = deviceinfo['id'] config = { 'id': device_id, 'deletable': True, 'parent_ids': ['base', deviceinfo.get('template_id', 'defaultconfigdevice')], 'configdevice': deviceinfo.get('template_id', 'defaultconfigdevice'), 'raw_config': {} } if 'template_id' in deviceinfo: del deviceinfo['template_id'] device_manager.add(deviceinfo) config_manager.add(config)
def template_id_exists(plugin): config_manager = provd_connector.config_manager() existing_templates = config_manager.find({ 'X_type': 'device', 'id': plugin }) return len(existing_templates) > 0
def _device_config_has_properties(channel, device_id, properties): from xivo_dao.helpers import provd_connector provd_config_manager = provd_connector.config_manager() provd_device_manager = provd_connector.device_manager() device = provd_device_manager.get(device_id) if 'config' in device: config = provd_config_manager.get(device['config']) assert 'sip_lines' in config[ 'raw_config'], "device does not have any SIP lines configured" sip_lines = config['raw_config']['sip_lines'] sip_line = sip_lines['1'] keys = [ u'username', u'auth_username', u'display_name', u'password', u'number' ] for key in keys: if key in properties: message = u"Invalid %s ('%s' instead of '%s')" % ( key, sip_line[key], properties[key]) message = message.encode('utf8') assert sip_line[key] == properties[key], message else: assert False, 'Device has no config key.'
def _get_config_from_provd(config_id): config_manager = provd_connector.config_manager() try: return config_manager.get(config_id) except HTTPError as e: if e.code == 404: raise errors.not_found('Device', config=config_id) raise
def remove_all_line_from_device(device): provd_config_manager = provd_connector.config_manager() try: config = provd_config_manager.get(device.id) provd_converter.reset_config(config) provd_config_manager.update(config) except URLError as e: raise ProvdError('error during remove all lines from device %s' % (device.id), e)
def _get_config_from_provd(config_id): config_manager = provd_connector.config_manager() try: return config_manager.get(config_id) except HTTPError as e: if e.code == 404: raise ElementNotExistsError('device config', id=config_id) raise
def remove_all_line_from_device(device): provd_config_manager = provd_connector.config_manager() try: config = provd_config_manager.get(device.id) provd_converter.reset_config(config) provd_config_manager.update(config) except URLError as e: raise DataError.on_action('dissociate', 'LineDevice', e)
def _create_provd_config(device_id, provd_config): config_manager = provd_connector.config_manager() try: config_manager.add(provd_config) except Exception as e: device_manager = provd_connector.device_manager() device_manager.remove(device_id) raise ElementCreationError('device', e)
def populate_sccp_line(config, confregistrar): provd_config_manager = provd_connector.config_manager() config['raw_config']['sccp_call_managers'] = dict() config['raw_config']['sccp_call_managers'][1] = dict() config['raw_config']['sccp_call_managers'][1]['ip'] = confregistrar['proxy_main'] if 'proxy_backup' in confregistrar and len(confregistrar['proxy_backup']) > 0: config['raw_config']['sccp_call_managers'][2] = dict() config['raw_config']['sccp_call_managers'][2]['ip'] = confregistrar['proxy_backup'] provd_config_manager.update(config)
def _remove_line_from_device_sip(device, line): provd_config_manager = provd_connector.config_manager() config = provd_config_manager.get(device.id) if 'sip_lines' in config['raw_config']: del config['raw_config']['sip_lines'][str(line.device_slot)] if len(config['raw_config']['sip_lines']) == 0: provd_converter.reset_config(config) reset_to_autoprov(device) provd_config_manager.update(config)
def _create_provd_config(device_id, provd_config): config_manager = provd_connector.config_manager() try: config_manager.add(provd_config) except Exception as e: device_manager = provd_connector.device_manager() device_manager.remove(device_id) raise DataError.on_create('Device', e)
def build_line_for_device(device, line): provd_config_manager = provd_connector.config_manager() config = provd_config_manager.get(device.id) confregistrar = provd_config_manager.get(line.configregistrar) line_extension = line_extension_dao.find_by_line_id(line.id) if line_extension: if line.protocol == 'sip': extension = extension_dao.get(line_extension.extension_id) provd_converter.populate_sip_line(config, confregistrar, line, extension) elif line.protocol == 'sccp': provd_converter.populate_sccp_line(config, confregistrar)
def _delete_device_with_ip(channel, ip): from xivo_dao.helpers import provd_connector config_manager = provd_connector.config_manager() device_manager = provd_connector.device_manager() for device in device_manager.find({'ip': ip}): try: config_manager.remove(device['id']) except Exception: pass device_manager.remove(device['id'])
def populate_sccp_line(config, confregistrar): provd_config_manager = provd_connector.config_manager() config['raw_config']['sccp_call_managers'] = dict() config['raw_config']['sccp_call_managers'][1] = dict() config['raw_config']['sccp_call_managers'][1]['ip'] = confregistrar[ 'proxy_main'] if 'proxy_backup' in confregistrar and len( confregistrar['proxy_backup']) > 0: config['raw_config']['sccp_call_managers'][2] = dict() config['raw_config']['sccp_call_managers'][2]['ip'] = confregistrar[ 'proxy_backup'] provd_config_manager.update(config)
def _delete_device(channel, device_id): from xivo_dao.helpers import provd_connector config_manager = provd_connector.config_manager() device_manager = provd_connector.device_manager() try: config_manager.remove(device_id) except Exception: pass try: device_manager.remove(device_id) except Exception: pass
def populate_sip_line(config, confregistrar, line, extension): provd_config_manager = provd_connector.config_manager() if 'sip_lines' not in config['raw_config']: config['raw_config']['sip_lines'] = dict() config['raw_config']['sip_lines'][str(line.device_slot)] = dict() line_dict = config['raw_config']['sip_lines'][str(line.device_slot)] line_dict['auth_username'] = line.name line_dict['username'] = line.name line_dict['password'] = line.secret line_dict['display_name'] = caller_id.extract_displayname(line.callerid) line_dict['number'] = extension.exten line_dict['registrar_ip'] = confregistrar['registrar_main'] line_dict['proxy_ip'] = confregistrar['proxy_main'] if 'proxy_backup' in confregistrar and len(confregistrar['proxy_backup']) > 0: line_dict['backup_registrar_ip'] = confregistrar['registrar_backup'] line_dict['backup_proxy_ip'] = confregistrar['proxy_backup'] provd_config_manager.update(config)
def populate_sip_line(config, confregistrar, line, extension): provd_config_manager = provd_connector.config_manager() if 'sip_lines' not in config['raw_config']: config['raw_config']['sip_lines'] = dict() config['raw_config']['sip_lines'][str(line.device_slot)] = dict() line_dict = config['raw_config']['sip_lines'][str(line.device_slot)] line_dict['auth_username'] = line.name line_dict['username'] = line.name line_dict['password'] = line.secret line_dict['display_name'] = caller_id.extract_displayname(line.callerid) line_dict['number'] = extension.exten line_dict['registrar_ip'] = confregistrar['registrar_main'] line_dict['proxy_ip'] = confregistrar['proxy_main'] if 'proxy_backup' in confregistrar and len( confregistrar['proxy_backup']) > 0: line_dict['backup_registrar_ip'] = confregistrar['registrar_backup'] line_dict['backup_proxy_ip'] = confregistrar['proxy_backup'] provd_config_manager.update(config)
def _add_or_replace_device_template(channel, properties): from xivo_dao.helpers import provd_connector config_manager = provd_connector.config_manager() if 'id' in properties: existing = config_manager.find({'X_type': 'device', 'id': properties['id']}) if len(existing) > 0: return default_properties = { 'X_type': 'device', 'deletable': True, 'parent_ids': [], 'raw_config': {} } properties.update(default_properties) config_manager.add(properties)
def _device_config_has_properties(channel, device_id, properties): from xivo_dao.helpers import provd_connector provd_config_manager = provd_connector.config_manager() provd_device_manager = provd_connector.device_manager() device = provd_device_manager.get(device_id) if 'config' in device: config = provd_config_manager.get(device['config']) assert 'sip_lines' in config['raw_config'], "device does not have any SIP lines configured" sip_lines = config['raw_config']['sip_lines'] sip_line = sip_lines['1'] keys = [u'username', u'auth_username', u'display_name', u'password', u'number'] for key in keys: if key in properties: message = u"Invalid %s ('%s' instead of '%s')" % (key, sip_line[key], properties[key]) message = message.encode('utf8') assert sip_line[key] == properties[key], message else: assert False, 'Device has no config key.'
def _add_or_replace_device_template(channel, properties): from xivo_dao.helpers import provd_connector config_manager = provd_connector.config_manager() if 'id' in properties: existing = config_manager.find({ 'X_type': 'device', 'id': properties['id'] }) if len(existing) > 0: return default_properties = { 'X_type': 'device', 'deletable': True, 'parent_ids': [], 'raw_config': {} } properties.update(default_properties) config_manager.add(properties)
def _update_provd_config(provd_config): config_manager = provd_connector.config_manager() try: config_manager.update(provd_config) except Exception as e: raise ElementEditionError('device', e)
def generate_autoprov_config(): provd_config_manager = provd_connector.config_manager() new_configid = provd_config_manager.autocreate() return new_configid
def _delete_provd_config(device): provd_config_manager = provd_connector.config_manager() try: provd_config_manager.remove(device.id) except Exception: pass
def template_id_exists(plugin): config_manager = provd_connector.config_manager() existing_templates = config_manager.find({ 'X_type': 'device', 'id': plugin}) return len(existing_templates) > 0
def _update_provd_config(provd_config): config_manager = provd_connector.config_manager() try: config_manager.update(provd_config) except Exception as e: raise DataError.on_edit('Device', e)