Debian Codegraph @

lib/jnpr/junos/ofacts/domain.py

raw
1 from jnpr.junos.utils.fs import FS
2 from jnpr.junos.exception import RpcError
3 from jnpr.junos.jxml import INHERIT
4 from lxml.builder import E
5
6
7 def facts_domain(junos, facts):
8 """
9 The following facts are required:
10 facts['hostname']
11
12 The following facts are assigned:
13 facts['domain']
14 facts['fqdn']
15 """
16
17 try:
18 domain_filter_xml = E("configuration", E("system", E("domain-name")))
19 domain = junos.rpc.get_config(filter_xml=domain_filter_xml, options=INHERIT)
20 domain_name = domain.xpath(".//domain-name")
21 if len(domain_name) > 0:
22 facts["domain"] = domain_name[0].text
23 facts["fqdn"] = facts["hostname"] + "." + facts["domain"]
24 return
25 except RpcError:
26 pass
27
28 fs = FS(junos)
29 file_content = fs.cat("/etc/resolv.conf") or fs.cat("/var/etc/resolv.conf")
30 words = file_content.split() if file_content is not None else ""
31 if "domain" not in words:
32 facts["domain"] = None
33 facts["fqdn"] = facts["hostname"]
34 else:
35 idx = words.index("domain") + 1
36 facts["domain"] = words[idx]
37 facts["fqdn"] = facts["hostname"] + "." + facts["domain"]