#!/usr/share/ucs-test/runner python3
## desc: Validate nagios for all computer roles
## tags: [udm-computers]
## roles: [domaincontroller_master]
## exposure: careful
## packages:
##   - univention-config
##   - univention-directory-manager-tools


import univention.testing.udm as udm_test
import univention.testing.strings as uts
import univention.testing.utils as utils

if __name__ == '__main__':
	ldap = utils.get_ldap_connection()

	for role in udm_test.UCSTestUDM.COMPUTER_MODULES:
		with udm_test.UCSTestUDM() as udm:
			forwardZone = udm.create_object('dns/forward_zone', zone='%s.%s' % (uts.random_name(), uts.random_name()), nameserver=uts.random_string(numeric=False))
			nagiosService = udm.create_object('nagios/service', name=uts.random_name(), checkCommand=uts.random_string(), checkPeriod=uts.random_string(), notificationPeriod=uts.random_string())

			nagiosParentProperties = {
				'options': ['nagios'],
				'name': uts.random_name(),
				'ip': '10.20.30.2'
			}
			# FIXME: workaround for remaining locks
			udm.addCleanupLock('aRecord', nagiosParentProperties['ip'])

			computerProperties = {
				'dnsEntryZoneForward': forwardZone,
				'nagiosServices': nagiosService,
				'nagiosContactEmail': '%s@%s.%s' % (uts.random_name(), uts.random_name(), uts.random_name()),
				'nagiosParents': udm.create_object('computers/domaincontroller_backup', dnsEntryZoneForward=forwardZone, **nagiosParentProperties),
				'name': uts.random_name(),
				'ip': '10.20.30.3',
				'options': ['posix', 'nagios']
			}
			# FIXME: workaround for remaining locks
			udm.addCleanupLock('aRecord', computerProperties['ip'])

			computer = udm.create_object(role, wait_for=True, **computerProperties)

			# validate that nagios related properties of computer are set correctly
			utils.verify_ldap_object(computer, {
				'univentionNagiosEmail': [computerProperties['nagiosContactEmail']],
				'univentionNagiosEnabled': ['1'],
				'univentionNagiosParent': [b'%s.%s' % (nagiosParentProperties['name'].encode('UTF-8'), ldap.getAttr(computerProperties['nagiosParents'], 'associatedDomain')[0])]
			})

			# check if computer has been added to nagios service
			utils.verify_ldap_object(nagiosService, {'univentionNagiosHostname': [b'%s.%s' % (computerProperties['name'].encode('UTF-8'), ldap.getAttr(computer, 'associatedDomain')[0])]})
