'Make calls to foreman and save the output in a cache'

def update_cache(self):

    self.groups = dict()
    self.hosts = dict()
    for host in self._get_hosts():
        dns_name = host['name']
        host_data = self._get_host_data_by_id(host['id'])
        host_params = host_data.get('all_parameters', {})
        group = 'hostgroup'
        val = (host.get(('%s_title' % group)) or host.get(('%s_name' % group)))
        if val:
            safe_key = self.to_safe(('%s%s_%s' % (self.group_prefix, group, val.lower())))
            self.inventory[safe_key].append(dns_name)
        for group in ['environment', 'location', 'organization']:
            val = host.get(('%s_name' % group))
            if val:
                safe_key = self.to_safe(('%s%s_%s' % (self.group_prefix, group, val.lower())))
                self.inventory[safe_key].append(dns_name)
        for group in ['lifecycle_environment', 'content_view']:
            val = host.get('content_facet_attributes', {}).get(('%s_name' % group))
            if val:
                safe_key = self.to_safe(('%s%s_%s' % (self.group_prefix, group, val.lower())))
                self.inventory[safe_key].append(dns_name)
        params = self._resolve_params(host_params)
        groupby = dict()
        for (k, v) in params.items():
            groupby[k] = self.to_safe(str(v))
        for pattern in self.group_patterns:
            try:
                key = pattern.format(**groupby)
                self.inventory[key].append(dns_name)
            except KeyError:
                pass
        if self.want_hostcollections:
            hostcollections = host_data.get('host_collections')
            if hostcollections:
                for hostcollection in hostcollections:
                    safe_key = self.to_safe(('%shostcollection_%s' % (self.group_prefix, hostcollection['name'].lower())))
                    self.inventory[safe_key].append(dns_name)
            self.hostcollections[dns_name] = hostcollections
        self.cache[dns_name] = host
        self.params[dns_name] = params
        self.facts[dns_name] = self._get_facts(host)
        self.inventory['all'].append(dns_name)
    self._write_cache()
