def _update_nameservers(self, session, host, ansibledict): """Update nameservers for a host. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession :param host: Host object :type host: HostEntity :param ansibledict: Ansible fact dict :type ansibledict: dict """ nameserver_list = complex_get('ansible_dns:nameservers', ansibledict) # Return early if no nameservers. if nameserver_list is None: return # Iterate over each nameserver in the list nameservers = [] for nameserver_item in nameserver_list: nameserver = NameServerEntity(ip=nameserver_item) nameserver.update(session, self.time_in_ms) nameservers.append(nameserver) # Update edges from host to nameservers. host.nameservers.update(session, nameservers, self.time_in_ms)
def _update_interfaces(self, session, host, ansibledict): """Update host interfaces in graph. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession :param host: Host object :type host: HostEntity :param ansibledict: Ansible fact dict :type ansibledict: dict """ interfaces = [] # Obtain list of interfaces from ansible names = ansibledict.get('ansible_interfaces', []) for name in names: interfacedict = ansibledict.get('ansible_{}'.format(name)) if interfacedict is None: continue interfacekwargs = {'device': name, 'host': host.identity} for ansible_key, interface_key in _INTERFACE_KEY_MAP.items(): ansible_key = ansible_key.format(name) val = complex_get(ansible_key, ansibledict) if val is not None: interfacekwargs[interface_key] = val interface = InterfaceEntity(**interfacekwargs) interface.update(session, self.time_in_ms) interfaces.append(interface) host.interfaces.update(session, interfaces, self.time_in_ms)
def _host_from_tuple(self, session, env, host_tuple): """Load hostdata from json file and create HostEntity instance. :param session: neo4j driver session :type session: neo4j.v1.session.BoltSession :param env: Environment entity hosts belong to. :type env: EnvironmentEntity :param host_tuple: (hostname, filename) :type host_tuple: tuple :returns: Host object :rtype: HostEntity """ hostname, filename = host_tuple with open(filename, 'r') as f: fulldict = json.loads(f.read()) fulldict = fulldict.get('data', {}) # Start kwargs for making the host entity hostkwargs = {} # Remove anything not prefixed with 'ansible_' ansibledict = {} for k, v in fulldict.items(): if k.startswith('ansible_'): ansibledict[k] = v # Create properties that require little intervention for ansible_key, host_key in _EASY_KEY_MAP.items(): val = ansibledict.get(ansible_key) if val is not None: hostkwargs[host_key] = val # Create properties that can be found by path for complexkey, host_key in _COMPLEX_KEY_MAP.items(): val = complex_get(complexkey, ansibledict) if val is not None: hostkwargs[host_key] = val host = HostEntity(hostname=hostname, environment=env.identity, **hostkwargs) host.update(session, self.time_in_ms) # Update nameservers subgraph self._update_nameservers(session, host, ansibledict) # Update mounts subgraph self._update_mounts(session, host, ansibledict) # Update devices self._update_devices(session, host, ansibledict) # Update interfaces self._update_interfaces(session, host, ansibledict) return host