def fill_template_host(item): """ Prepare fields of host with fields of host templates :param item: field name / values of the host :type item: dict :return: None """ host = current_app.data.driver.db['host'] ignore_fields = ['_id', '_etag', '_updated', '_created', '_template_fields', '_templates', '_is_template', 'realm', '_templates_with_services'] 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 host_template in item['_templates']: hosts = host.find_one({'_id': ObjectId(host_template)}) if hosts is not None: for (field_name, field_value) in iteritems(hosts): 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] = host_template schema = host_schema() ignore_schema_fields = ['realm', '_template_fields', '_templates', '_is_template', '_templates_with_services'] 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_host(item): """ Prepare fields of host with fields of host templates :param item: field name / values of the host :type item: dict :return: None """ host = current_app.data.driver.db['host'] ignore_fields = [ '_id', '_etag', '_updated', '_created', '_template_fields', '_templates', '_is_template', 'realm', '_templates_with_services' ] 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 host_template in item['_templates']: hosts = host.find_one({'_id': ObjectId(host_template)}) if hosts is not None: for (field_name, field_value) in iteritems(hosts): 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] = host_template schema = host_schema() ignore_schema_fields = [ 'realm', '_template_fields', '_templates', '_is_template', '_templates_with_services' ] for key in schema['schema']: if key not in ignore_schema_fields: if key not in item: item['_template_fields'][key] = 0
def test_host_templates(self): """ Test host templates :return: None """ headers = {'Content-Type': 'application/json'} sort_id = {'sort': '_id'} # Add command data = json.loads(open('cfg/command_ping.json').read()) data['_realm'] = self.realm_all requests.post(self.endpoint + '/command', json=data, headers=headers, auth=self.auth) # Check if command right in backend response = requests.get(self.endpoint + '/command', params=sort_id, auth=self.auth) resp = response.json() rc = resp['_items'] self.assertEqual(rc[0]['name'], "ping") data = json.loads(open('cfg/host_srv001.json').read()) data['check_command'] = rc[0]['_id'] if 'realm' in data: del data['realm'] data['_realm'] = self.realm_all data['_is_template'] = True requests.post(self.endpoint + '/host', json=data, headers=headers, auth=self.auth) # Check if host right in backend response = requests.get(self.endpoint + '/host', params=sort_id, auth=self.auth) resp = response.json() rh = resp['_items'] self.assertEqual(rh[0]['name'], "srv001") host_template_id = rh[0]['_id'] data = { 'name': 'host_001', '_templates': [host_template_id], '_realm': self.realm_all } requests.post(self.endpoint + '/host', json=data, headers=headers, auth=self.auth) response = requests.get(self.endpoint + '/host', params=sort_id, auth=self.auth) resp = response.json() rh = resp['_items'] self.assertEqual(rh[0]['name'], "srv001") self.assertEqual(rh[1]['name'], "host_001") self.assertEqual(rh[1]['check_command'], rc[0]['_id']) schema = host_schema() template_fields = {} ignore_fields = ['name', 'realm', '_realm', '_template_fields', '_templates', '_is_template', '_templates_with_services'] for key in schema['schema']: if key not in ignore_fields: template_fields[key] = host_template_id self.assertItemsEqual(rh[1]['_template_fields'], template_fields) datal = [{ 'name': 'host_002', '_templates': [rh[0]['_id']], '_realm': self.realm_all }, { 'name': 'host_003', '_templates': [rh[0]['_id']], '_realm': self.realm_all }] requests.post(self.endpoint + '/host', json=datal, headers=headers, auth=self.auth) response = requests.get(self.endpoint + '/host', params=sort_id, auth=self.auth) resp = response.json() rh = resp['_items'] self.assertEqual(rh[2]['name'], "host_002") self.assertEqual(rh[3]['name'], "host_003")
def fill_template_host(item): # pylint: disable=too-many-locals """Prepare fields of host with fields of host templates. The field _template_fields will contain all fields filled by templates :param item: field name / values of the host :type item: dict :return: None """ host_drv = current_app.data.driver.db['host'] # 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 host 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 host_template in item['_templates']: if not ObjectId.is_valid(host_template): abort( make_response( "The template '%s' is not at the right format" % host_template, 412)) host = host_drv.find_one({'_id': ObjectId(host_template)}) if host is None: continue for (field_name, field_value) in iteritems(host): 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='host') 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 = host_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) if 'check_command' not in item: # Get default host check commands commands = current_app.data.driver.db['command'] default_host_check_command = commands.find_one( {'name': '_internal_host_up'}) item['check_command'] = default_host_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')
def test_host_templates(self): """ Test host templates :return: None """ headers = {'Content-Type': 'application/json'} sort_id = {'sort': '_id'} # Add command data = json.loads(open('cfg/command_ping.json').read()) data['_realm'] = self.realm_all requests.post(self.endpoint + '/command', json=data, headers=headers, auth=self.auth) # Check if command right in backend response = requests.get(self.endpoint + '/command', params=sort_id, auth=self.auth) resp = response.json() rc = resp['_items'] self.assertEqual(rc[0]['name'], "ping") data = json.loads(open('cfg/host_srv001.json').read()) data['check_command'] = rc[0]['_id'] if 'realm' in data: del data['realm'] data['_realm'] = self.realm_all data['_is_template'] = True requests.post(self.endpoint + '/host', json=data, headers=headers, auth=self.auth) # Check if host right in backend response = requests.get(self.endpoint + '/host', params=sort_id, auth=self.auth) resp = response.json() rh = resp['_items'] self.assertEqual(rh[0]['name'], "srv001") host_template_id = rh[0]['_id'] data = { 'name': 'host_001', '_templates': [host_template_id], '_realm': self.realm_all } requests.post(self.endpoint + '/host', json=data, headers=headers, auth=self.auth) response = requests.get(self.endpoint + '/host', params=sort_id, auth=self.auth) resp = response.json() rh = resp['_items'] self.assertEqual(rh[0]['name'], "srv001") self.assertEqual(rh[1]['name'], "host_001") self.assertEqual(rh[1]['check_command'], rc[0]['_id']) schema = host_schema() template_fields = {} ignore_fields = [ 'name', 'realm', '_realm', '_template_fields', '_templates', '_is_template', '_templates_with_services' ] for key in schema['schema']: if key not in ignore_fields: template_fields[key] = host_template_id self.assertItemsEqual(rh[1]['_template_fields'], template_fields) datal = [{ 'name': 'host_002', '_templates': [rh[0]['_id']], '_realm': self.realm_all }, { 'name': 'host_003', '_templates': [rh[0]['_id']], '_realm': self.realm_all }] requests.post(self.endpoint + '/host', json=datal, headers=headers, auth=self.auth) response = requests.get(self.endpoint + '/host', params=sort_id, auth=self.auth) resp = response.json() rh = resp['_items'] self.assertEqual(rh[2]['name'], "host_002") self.assertEqual(rh[3]['name'], "host_003")