def prepare_service_to_post(item, hostid): """ Prepare a service linked with a service template in case the host is in template host + services :param item: the service template source :type item: dict :param hostid: the id of the host where put this new service :type hostid: str :return: The service with right fields :rtype: dict """ ignore_schema_fields = [ '_realm', '_template_fields', '_templates', '_is_template', '_templates_from_host_template' ] schema = service_schema() item['host'] = hostid item['_templates'] = [item['_id']] del item['_etag'] del item['_id'] del item['_created'] del item['_updated'] if '_status' in item: del item['_status'] if '_links' in item: del item['_links'] item['_is_template'] = False item['_templates_from_host_template'] = True item['_template_fields'] = {} for key in schema['schema']: if item not in ignore_schema_fields: item['_template_fields'][key] = 0 return item
def prepare_service_to_post(item, host): """Prepare a service linked with a service template in case the host is in template host + services :param item: the service template source :type item: dict :param host: the host where put this new service :type host: Host :return: The service with right fields :rtype: dict """ schema = service_schema() item['host'] = host['_id'] item['_realm'] = host['_realm'] item['_templates'] = [item['_id']] del item['_etag'] del item['_id'] del item['_created'] del item['_updated'] if '_status' in item: del item['_status'] if '_links' in item: del item['_links'] item['_is_template'] = False item['_templates_from_host_template'] = True item['_template_fields'] = [] for key in schema['schema']: if not key.startswith('_') and not key.startswith('ls_'): item['_template_fields'].append(key) # we must never have 'host' in the _template_fields if 'host' in item['_template_fields']: item['_template_fields'].remove('host') return item
def prepare_service_to_post(item, hostid): """ Prepare a service linked with a service template in case the host is in template host + services :param item: the service template source :type item: dict :param hostid: the id of the host where put this new service :type hostid: str :return: The service with right fields :rtype: dict """ ignore_schema_fields = ['_realm', '_template_fields', '_templates', '_is_template', '_templates_from_host_template'] schema = service_schema() item['host'] = hostid item['_templates'] = [item['_id']] del item['_etag'] del item['_id'] del item['_created'] del item['_updated'] if '_status' in item: del item['_status'] if '_links' in item: del item['_links'] item['_is_template'] = False item['_templates_from_host_template'] = True item['_template_fields'] = {} for key in schema['schema']: if item not in ignore_schema_fields: item['_template_fields'][key] = 0 return item
def fill_template_service(item): """ Prepare fields of service with fields of service templates :param item: field name / values of the service :type item: dict :return: None """ service = current_app.data.driver.db['service'] ignore_fields = ['_id', '_etag', '_updated', '_created', '_template_fields', '_templates', '_is_template', '_realm', 'host', '_templates_from_host_template'] fields_not_update = [] for (field_name, field_value) in iteritems(item): fields_not_update.append(field_name) item['_template_fields'] = {} if ('_is_template' not in item or not item['_is_template']) \ and '_templates' in item and item['_templates'] != []: for service_template in item['_templates']: services = service.find_one({'_id': ObjectId(service_template)}) if services is not None: for (field_name, field_value) in iteritems(services): if field_name not in fields_not_update \ and field_name not in ignore_fields: item[field_name] = field_value item['_template_fields'][field_name] = service_template schema = service_schema() ignore_schema_fields = ['_realm', '_template_fields', '_templates', '_is_template', '_templates_from_host_template'] for key in schema['schema']: if key not in ignore_schema_fields: if key not in item: item['_template_fields'][key] = 0
def fill_template_service(item): """ Prepare fields of service with fields of service templates :param item: field name / values of the service :type item: dict :return: None """ service = current_app.data.driver.db['service'] ignore_fields = [ '_id', '_etag', '_updated', '_created', '_template_fields', '_templates', '_is_template', '_realm', 'host', '_templates_from_host_template' ] fields_not_update = [] for (field_name, field_value) in iteritems(item): fields_not_update.append(field_name) item['_template_fields'] = {} if ('_is_template' not in item or not item['_is_template']) \ and '_templates' in item and item['_templates'] != []: for service_template in item['_templates']: services = service.find_one( {'_id': ObjectId(service_template)}) if services is not None: for (field_name, field_value) in iteritems(services): if field_name not in fields_not_update \ and field_name not in ignore_fields: item[field_name] = field_value item['_template_fields'][ field_name] = service_template schema = service_schema() ignore_schema_fields = [ '_realm', '_template_fields', '_templates', '_is_template', '_templates_from_host_template' ] for key in schema['schema']: if key not in ignore_schema_fields: if key not in item: item['_template_fields'][key] = 0
def fill_template_service(item): # pylint: disable=too-many-locals """Prepare fields of service with fields of service templates :param item: field name / values of the service :type item: dict :return: None """ service_drv = current_app.data.driver.db['service'] # The fields which values may be cumulated: cumulated_fields = { 'tags': [], 'customs': {}, 'users': [], 'usergroups': [] } # The fields which must be ignored: not_updated_fields = [] for (field_name, field_value) in iteritems(item): not_updated_fields.append(field_name) item['_template_fields'] = [] # Whether service is a template or not... is_a_template = False if '_is_template' in item: is_a_template = item['_is_template'] if '_templates' in item and item['_templates'] != []: for service_template in item['_templates']: if not ObjectId.is_valid(service_template): abort( make_response( "The template '%s' is not at the right format" % service_template, 412)) service = service_drv.find_one( {'_id': ObjectId(service_template)}) if service is not None: for (field_name, field_value) in iteritems(service): if field_name not in not_updated_fields \ and field_name not in cumulated_fields \ and not field_name.startswith('_') \ and not field_name.startswith('ls_'): item[field_name] = field_value item['_template_fields'].append(field_name) # Cumulate fields only if item is not a template if not is_a_template: Template.get_inherited_fields(item, cumulated_fields, tpl_type='service') for (field_name, field_value) in iteritems(cumulated_fields): if isinstance(field_value, dict): item[field_name] = field_value elif isinstance(field_value, list): seen = set() seen_add = seen.add item[field_name] = [ x for x in field_value if not (x in seen or seen_add(x)) ] item['_template_fields'].append(field_name) schema = service_schema() for key in schema['schema']: if not key.startswith('_') and not key.startswith('ls_'): if key not in item: item['_template_fields'].append(key) # we must never have 'host' in the _template_fields if 'host' in item['_template_fields']: item['_template_fields'].remove('host') if 'check_command' not in item: # Get default service check commands commands = current_app.data.driver.db['command'] default_service_check_command = commands.find_one( {'name': '_echo'}) item['check_command'] = default_service_check_command['_id'] if '_realm' not in item: # Get default logged-in user realm if g.get('user_realm', None): item['_realm'] = g.get('user_realm')