def __init__(self,search_string): self.ret = [] self.final = {} self.is_system = False self.is_truth = False self.search_string = search_string base = None truth_only = False host_only = False if re.match("host:.*", search_string): host_only = True search_string = search_string.split(":")[1] if re.match("truth:.*", search_string): truth_only = True search_string = search_string.split(":")[1] if truth_only is False: try: system = System.objects.get(hostname=search_string) base = KeyValue.expanded_objects.filter(system=system) self.is_system = True except: self.is_system = False base = None if base is None and host_only is False: try: truth = Truth.objects.get(name=search_string) base = TruthKeyValue.expanded_objects.filter(truth=truth) self.is_truth = True except: self.is_truth = False return None tmp_list = [] #Let's start at our child node, first getting out own keys, these will not get overwritten by those of our parents for row in base: matches = re.match("\$\{(.*)\}", row.value) if not re.search("parent", row.key): if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() ##Now we've got our base keys, lets get those of our parents for row in base: matches = re.match("(parent.*)", row.key) if matches is not None: parent_type = row.value.replace("${","").split(':')[0] parent_name = row.value.replace("}","").split(':')[1] tmp = self.get_parents(parent_type, parent_name, root=True) #Ok now i've got my own parents for host in tmp: #tmp = {'key':host['key'], 'value':host['value']} tmp_list.append(host) #build a list of all of the parents keys as dictionary objects for row in tmp_list: if 'key' in row and 'value' in row: self.final[row['key']] = row['value'] #overwrite the parents kv/store with my own for row in base: self.final[row.key] = row.value
def __init__(self, search_string): self.ret = [] self.final = {} self.is_system = False self.is_truth = False self.search_string = search_string base = None truth_only = False host_only = False if re.match("host:.*", search_string): host_only = True search_string = search_string.split(":")[1] if re.match("truth:.*", search_string): truth_only = True search_string = search_string.split(":")[1] if truth_only is False: try: system = System.objects.get(hostname=search_string) base = KeyValue.expanded_objects.filter(obj=system) self.is_system = True except: self.is_system = False base = None if base is None and host_only is False: try: truth = Truth.objects.get(name=search_string) base = TruthKeyValue.expanded_objects.filter(truth=truth) self.is_truth = True except: self.is_truth = False return None tmp_list = [] #Let's start at our child node, first getting out own keys, these will not get overwritten by those of our parents for row in base: matches = re.match("\$\{(.*)\}", row.value) if not re.search("parent", row.key): if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() ##Now we've got our base keys, lets get those of our parents for row in base: matches = re.match("(parent.*)", row.key) if matches is not None: parent_type = row.value.replace("${", "").split(':')[0] parent_name = row.value.replace("}", "").split(':')[1] tmp = self.get_parents(parent_type, parent_name, root=True) #Ok now i've got my own parents for host in tmp: #tmp = {'key':host['key'], 'value':host['value']} tmp_list.append(host) #build a list of all of the parents keys as dictionary objects for row in tmp_list: if 'key' in row and 'value' in row: self.final[row['key']] = row['value'] #overwrite the parents kv/store with my own for row in base: self.final[row.key] = row.value
def get_parents(self, parent_type, parent_name, root=False): #ret = [] if parent_type is not None and parent_name is not None: obj = None if parent_type == 'host': obj = System.objects.get(hostname=parent_name) base = KeyValue.expanded_objects.filter(system=obj).exclude(key__contains='parent') base_parents = KeyValue.expanded_objects.filter(system=obj,key__contains='parent') parent_compare = obj.hostname elif parent_type == 'truth': obj = Truth.objects.get(name=parent_name) base = TruthKeyValue.expanded_objects.filter(truth=obj).exclude(key__contains='parent') base_parents = TruthKeyValue.expanded_objects.filter(truth=obj,key__contains='parent') parent_compare = obj.name if base is not None and obj is not None: for host in base: if host.value is not None and host.key is not None: matches = re.match("\$\{(.*)\}", host.value) if not re.search("parent", host.key): if matches is not None: m = MacroExpansion(matches.group(1)) host.value = m.output() if host.key is not None and host.value is not None: if self.search_string != parent_compare: host.key = "%s:%s:%s" % (parent_type, parent_name, host.key) tmp = {'key':host.key, 'value':host.value} self.ret.append(tmp) for row in base_parents: parent_type = row.value.replace("${","").split(':')[0] parent_name = row.value.replace("}","").split(':')[1] self.ret.append(self.get_parents(parent_type, parent_name, root=False)) """for host in base: if host.value is not None and host.key is not None: matches = re.match("\$\{(.*)\}", host.value) if not re.search("parent", host.key): if matches is not None: m = MacroExpansion(matches.group(1)) host.value = m.output() if host.key is not None and host.value is not None: #Not sure if i'll keep this or not, but display the parent's name in () if self.search_string != truth.hostname: host.key = "truth:%s:%s" % (parent_name, host.key) tmp = {'key':host.key, 'value':host.value} self.ret.append(tmp) base_parents = TruthKeyValue.expanded_objects.filter(truth=truth,key__contains='parent') for row in base_parents: parent_type = row.value.replace("${","").split(':')[0] parent_name = row.value.replace("}","").split(':')[1] self.ret.append(self.get_parents(parent_type, parent_name, root=False))""" return self.ret else: return None
def read(self, request, key_value_id=None): #if keystore get var is set return the whole keystore if 'keystore' in request.GET: #if key get var is set return the keystore based on the existance of this key if 'key' in request.GET: base = KeyValue.objects.filter(key=request.GET['keystore']).filter(keyvalue_set__contains=request.GET['key']) tmp_list = [] for row in base: matches = re.match("\$\{(.*)\}", row.value) if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() for r in base: key_name = 'host:%s:%s' % (r.system.hostname, r.key) tmp_list[key_name] = r.value if 'key' not in request.GET: tree = KeyValueTree(request.GET['keystore']).final return tree elif 'key_type' in request.GET: key_type = request.GET['key_type'] tmp_list = [] if key_type == 'dhcp_scopes': #Get keystores from truth that have dhcp.is_scope = True base = TruthKeyValue.objects.filter(key='dhcp.is_scope',value='True') #Iterate through the list and get all of the key/value pairs for row in base: keyvalue = TruthKeyValue.objects.filter(truth=row.truth) tmp_dict = {} for kv in keyvalue: tmp_dict[kv.key] = kv.value tmp_list.append(tmp_dict) return tmp_list if key_type == 'system_by_reverse_dns_zone': #Get keystores from truth that have dhcp.is_scope = True keyvalue_pairs = KeyValue.objects.filter(key__contains='reverse_dns_zone',value=request.GET['zone']).filter(key__startswith='nic.') #Iterate through the list and get all of the key/value pairs tmp_list = [] for row in keyvalue_pairs: keyvalue = KeyValue.objects.filter(system=row.system) tmp_dict = {} for kv in keyvalue: tmp_dict[kv.key] = kv.value tmp_dict['hostname'] = row.system.hostname appendable = True for the_items in tmp_list: if 'hostname' not in the_items: appendable = True elif the_items['hostname'] == row.system.hostname: appendable = False if appendable is True: tmp_list.append(tmp_dict) #tmp_list = list(set(tmp_list)) return tmp_list if key_type == 'system_by_scope': #Get keystores from truth that have dhcp.is_scope = True keyvalue_pairs = KeyValue.objects.filter(key__contains='dhcp_scope',value=request.GET['scope']).filter(key__startswith='nic.') #Iterate through the list and get all of the key/value pairs tmp_list = [] for row in keyvalue_pairs: keyvalue = KeyValue.objects.filter(system=row.system) tmp_dict = {} for kv in keyvalue: tmp_dict[kv.key] = kv.value tmp_dict['hostname'] = row.system.hostname appendable = True for the_items in tmp_list: if 'hostname' not in the_items: appendable = True elif the_items['hostname'] == row.system.hostname: appendable = False if appendable is True: tmp_list.append(tmp_dict) #tmp_list = list(set(tmp_list)) return tmp_list if key_type == 'adapters_by_system': #Get keystores from truth that have dhcp.is_scope = True system = None try: system = System.objects.get(hostname=request.GET['system']) except: system = None if not system: try: system = System.objects.get(id=request.GET['system']) except: system = None if not system: resp = rc.NOT_FOUND resp.write('json = {"error_message":"Unable to find system"}') return resp keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(system=system).order_by('key') #Iterate through the list and get all of the key/value pairs tmp_dict = {} adapter_ids = [] final_list = [] for kv in keyvalue_pairs: tmp_dict[kv.key] = kv.value for k in tmp_dict.iterkeys(): matches = re.match('nic\.(\d+).*',k) if matches.group is not None: if matches.group(1) not in adapter_ids: adapter_ids.append(matches.group(1)) adapter_ids.sort() for a in adapter_ids: adapter_name = '' mac_address = '' dhcp_hostname = '' dhcp_scope = '' dhcp_filename = '' ipv4_address = '' dhcp_domain_name_servers = '' option_hostname = "" if 'nic.%s.ipv4_address.0' % a in tmp_dict: ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a] if 'nic.%s.name.0' % a in tmp_dict: adapter_name = tmp_dict['nic.%s.name.0' % a] if 'nic.%s.mac_address.0' % a in tmp_dict: mac_address = tmp_dict['nic.%s.mac_address.0' % a] if 'nic.%s.option_hostname.0' % a in tmp_dict: option_hostname = tmp_dict['nic.%s.option_hostname.0' % a] if 'nic.%s.dhcp_scope.0' % a in tmp_dict: dhcp_scope = tmp_dict['nic.%s.dhcp_scope.0' % a] if 'nic.%s.dhcp_filename.0' % a in tmp_dict: dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a] if 'nic.%s.dhcp_domain_name_servers.0' % a in tmp_dict: dhcp_domain_name_servers = tmp_dict['nic.%s.dhcp_domain_name_servers.0' % a] try: final_list.append({ 'system_hostname':system.hostname, 'ipv4_address':ipv4_address, 'adapter_name':adapter_name, 'mac_address':mac_address, 'option_hostname':option_hostname, 'dhcp_scope':dhcp_scope, 'dhcp_filename':dhcp_filename, 'dhcp_domain_name_servers':dhcp_domain_name_servers, } ) except Exception, e: pass #tmp_list.append(tmp_dict) return final_list if key_type == 'adapters_by_system_and_zone': #Get keystores from truth that have dhcp.is_scope = True zone = request.GET['zone'] system = System.objects.get(hostname=request.GET['system']) keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(system=system).order_by('key') #Iterate through the list and get all of the key/value pairs tmp_dict = {} adapter_ids = [] final_list = [] for kv in keyvalue_pairs: tmp_dict[kv.key] = kv.value for k in tmp_dict.iterkeys(): matches = re.match('nic\.(\d+).*',k) if matches.group is not None: dhcp_scope_match = 'nic.%s.reverse_dns_zone.0' % matches.group(1) if matches.group(1) not in adapter_ids and dhcp_scope_match in tmp_dict and tmp_dict[dhcp_scope_match] == zone: #if matches.group(1) not in adapter_ids and 'nic.%s.dhcp_scope.0' % matches.group(1) in tmp_dict and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] == dhcp_scope: adapter_ids.append(matches.group(1)) adapter_ids.sort() for a in adapter_ids: adapter_name = '' mac_address = '' dhcp_hostname = '' dhcp_filename = '' dhcp_domain_name = '' ipv4_address = '' if 'nic.%s.ipv4_address.0' % a in tmp_dict: ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a] if 'nic.%s.name.0' % a in tmp_dict: adapter_name = tmp_dict['nic.%s.name.0' % a] if 'nic.%s.mac_address.0' % a in tmp_dict: mac_address = tmp_dict['nic.%s.mac_address.0' % a] if 'nic.%s.dhcp_hostname.0' % a in tmp_dict: dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a] if 'nic.%s.dhcp_filename.0' % a in tmp_dict: dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a] if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict: dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a] final_list.append({'system_hostname':system.hostname, 'ipv4_address':ipv4_address}) #tmp_list.append(tmp_dict) return final_list if 'key_type' in request.GET and request.GET['key_type'] == 'key_by_system': try: hostname = request.GET.get('hostname') key = request.GET.get('key') system = System.objects.get(hostname=hostname) objects = KeyValue.objects.filter(key=key, system=system) tmp = [] for obj in objects: tmp.append({'key': obj.key, 'value': obj.value}) resp = rc.ALL_OK resp.write("json = {'data': %s}" % json.dumps(tmp)) except: resp = rc.NOT_FOUND resp.write('json = {"error_message":"Unable to find Key or system"}') return resp if key_type == 'adapters_by_system_and_scope': #Get keystores from truth that have dhcp.is_scope = True dhcp_scope = request.GET['dhcp_scope'] system = System.objects.get(hostname=request.GET['system']) keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(system=system).order_by('key') #Iterate through the list and get all of the key/value pairs tmp_dict = {} adapter_ids = [] final_list = [] for kv in keyvalue_pairs: tmp_dict[kv.key] = kv.value for k in tmp_dict.iterkeys(): matches = re.match('nic\.(\d+).*',k) if matches.group is not None: dhcp_scope_match = 'nic.%s.dhcp_scope.0' % matches.group(1) ip_address_match = 'nic.%s.ipv4_address.0' % matches.group(1) if matches.group(1) not in adapter_ids and ip_address_match in tmp_dict and dhcp_scope_match in tmp_dict and tmp_dict[dhcp_scope_match] == dhcp_scope: #if matches.group(1) not in adapter_ids and 'nic.%s.dhcp_scope.0' % matches.group(1) in tmp_dict and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] == dhcp_scope: adapter_ids.append(matches.group(1)) adapter_ids.sort() for a in adapter_ids: adapter_name = '' mac_address = '' dhcp_hostname = '' dhcp_filename = '' dhcp_domain_name = '' ipv4_address = '' dhcp_domain_name_servers = '' if 'nic.%s.ipv4_address.0' % a in tmp_dict: ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a] if 'nic.%s.name.0' % a in tmp_dict: adapter_name = tmp_dict['nic.%s.name.0' % a] if 'nic.%s.mac_address.0' % a in tmp_dict: mac_address = tmp_dict['nic.%s.mac_address.0' % a] if 'nic.%s.dhcp_hostname.0' % a in tmp_dict and 'nic.%s.option_hostname.0' % a not in tmp_dict: dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a] if 'nic.%s.option_hostname.0' % a in tmp_dict: dhcp_hostname = tmp_dict['nic.%s.option_hostname.0' % a] if 'nic.%s.dhcp_filename.0' % a in tmp_dict: dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a] if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict: dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a] if 'nic.%s.dhcp_domain_name_servers.0' % a in tmp_dict: dhcp_domain_name_servers = tmp_dict['nic.%s.dhcp_domain_name_servers.0' % a] final_list.append({'system_hostname':system.hostname, 'ipv4_address':ipv4_address, 'adapter_name':adapter_name, 'mac_address':mac_address, 'option_hostname': dhcp_hostname, 'dhcp_hostname':dhcp_hostname, 'dhcp_filename':dhcp_filename, 'dhcp_domain_name':dhcp_domain_name, 'dhcp_domain_name_servers':dhcp_domain_name_servers}) #tmp_list.append(tmp_dict) return final_list
if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict: dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a] if 'nic.%s.dhcp_domain_name_servers.0' % a in tmp_dict: dhcp_domain_name_servers = tmp_dict['nic.%s.dhcp_domain_name_servers.0' % a] final_list.append({'system_hostname':system.hostname, 'ipv4_address':ipv4_address, 'adapter_name':adapter_name, 'mac_address':mac_address, 'option_hostname': dhcp_hostname, 'dhcp_hostname':dhcp_hostname, 'dhcp_filename':dhcp_filename, 'dhcp_domain_name':dhcp_domain_name, 'dhcp_domain_name_servers':dhcp_domain_name_servers}) #tmp_list.append(tmp_dict) return final_list elif 'key' in request.GET and request.GET['key'] > '': tmp_list = {} try: base = KeyValue.objects.filter(key=request.GET['key']) for row in base: matches = re.match("\$\{(.*)\}", row.value) if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() for r in base: key_name = 'host:%s:%s' % (r.system.hostname, r.key) tmp_list[key_name] = r.value except Exception, e: pass try: base = TruthKeyValue.objects.filter(key=request.GET['key']) for row in base: matches = re.match("\$\{(.*)\}", row.value) if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() for r in base: key_name = 'truth:%s:%s' % (r.truth.name, r.key) tmp_list[key_name] = r.value
def test_key_value_found(self): m = MacroExpansion("host:fake-hostname2:ip_address") self.assertEqual(m.output(), "10.99.32.1")
def test_key_value_not_found(self): m = MacroExpansion('host:fake-hostname2:ip_address') self.assertEqual(m.output(),'10.99.32.1')
def read(self, request, key_value_id=None): #if keystore get var is set return the whole keystore if 'keystore' in request.GET: #if key get var is set return the keystore based on the existance of this key if 'key' in request.GET: base = KeyValue.objects.filter( key=request.GET['keystore']).filter( keyvalue_set__contains=request.GET['key']) tmp_list = [] for row in base: matches = re.match("\$\{(.*)\}", row.value) if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() for r in base: key_name = 'host:%s:%s' % (r.system.hostname, r.key) tmp_list[key_name] = r.value if 'key' not in request.GET: tree = KeyValueTree(request.GET['keystore']).final return tree elif 'key_type' in request.GET: key_type = request.GET['key_type'] tmp_list = [] if key_type == 'dhcp_scopes': #Get keystores from truth that have dhcp.is_scope = True base = TruthKeyValue.objects.filter(key='dhcp.is_scope', value='True') #Iterate through the list and get all of the key/value pairs for row in base: keyvalue = TruthKeyValue.objects.filter(truth=row.truth) tmp_dict = {} for kv in keyvalue: tmp_dict[kv.key] = kv.value tmp_list.append(tmp_dict) return tmp_list if key_type == 'system_by_reverse_dns_zone': #Get keystores from truth that have dhcp.is_scope = True keyvalue_pairs = KeyValue.objects.filter( key__contains='reverse_dns_zone', value=request.GET['zone']).filter(key__startswith='nic.') #Iterate through the list and get all of the key/value pairs tmp_list = [] for row in keyvalue_pairs: keyvalue = KeyValue.objects.filter(obj=row.system) tmp_dict = {} for kv in keyvalue: tmp_dict[kv.key] = kv.value tmp_dict['hostname'] = row.system.hostname appendable = True for the_items in tmp_list: if 'hostname' not in the_items: appendable = True elif the_items['hostname'] == row.system.hostname: appendable = False if appendable is True: tmp_list.append(tmp_dict) #tmp_list = list(set(tmp_list)) return tmp_list if key_type == 'system_by_scope': #Get keystores from truth that have dhcp.is_scope = True keyvalue_pairs = KeyValue.objects.filter( key__contains='dhcp_scope', value=request.GET['scope']).filter(key__startswith='nic.') #Iterate through the list and get all of the key/value pairs tmp_list = [] for row in keyvalue_pairs: keyvalue = KeyValue.objects.filter(obj=row.system) tmp_dict = {} for kv in keyvalue: tmp_dict[kv.key] = kv.value tmp_dict['hostname'] = row.system.hostname appendable = True for the_items in tmp_list: if 'hostname' not in the_items: appendable = True elif the_items['hostname'] == row.system.hostname: appendable = False if appendable is True: tmp_list.append(tmp_dict) #tmp_list = list(set(tmp_list)) return tmp_list if key_type == 'adapters_by_system': #Get keystores from truth that have dhcp.is_scope = True system = System.objects.get(hostname=request.GET['system']) keyvalue_pairs = KeyValue.objects.filter( key__startswith='nic.').filter(obj=system).order_by('key') #Iterate through the list and get all of the key/value pairs tmp_dict = {} adapter_ids = [] final_list = [] for kv in keyvalue_pairs: tmp_dict[kv.key] = kv.value for k in tmp_dict.iterkeys(): matches = re.match('nic\.(\d+).*', k) if matches.group is not None: if matches.group(1) not in adapter_ids: adapter_ids.append(matches.group(1)) adapter_ids.sort() for a in adapter_ids: adapter_name = '' mac_address = '' dhcp_hostname = '' dhcp_filename = '' ipv4_address = '' if 'nic.%s.ipv4_address.0' % a in tmp_dict: ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a] if 'nic.%s.name.0' % a in tmp_dict: adapter_name = tmp_dict['nic.%s.name.0' % a] if 'nic.%s.mac_address.0' % a in tmp_dict: mac_address = tmp_dict['nic.%s.mac_address.0' % a] if 'nic.%s.dhcp_hostname.0' % a in tmp_dict: dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a] if 'nic.%s.dhcp_filename.0' % a in tmp_dict: dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a] try: final_list.append({ 'system_hostname': system.hostname, 'ipv4_address': ipv4_address, 'adapter_name': adapter_name, 'mac_address': mac_address, 'dhcp_hostname': dhcp_hostname, 'dhcp_filename': dhcp_filename }) except: pass #tmp_list.append(tmp_dict) return final_list if key_type == 'adapters_by_system_and_zone': #Get keystores from truth that have dhcp.is_scope = True zone = request.GET['zone'] system = System.objects.get(hostname=request.GET['system']) keyvalue_pairs = KeyValue.objects.filter( key__startswith='nic.').filter(obj=system).order_by('key') #Iterate through the list and get all of the key/value pairs tmp_dict = {} adapter_ids = [] final_list = [] for kv in keyvalue_pairs: tmp_dict[kv.key] = kv.value for k in tmp_dict.iterkeys(): matches = re.match('nic\.(\d+).*', k) if matches.group is not None: dhcp_scope_match = 'nic.%s.reverse_dns_zone.0' % matches.group( 1) if matches.group( 1 ) not in adapter_ids and dhcp_scope_match in tmp_dict and tmp_dict[ dhcp_scope_match] == zone: #if matches.group(1) not in adapter_ids and 'nic.%s.dhcp_scope.0' % matches.group(1) in tmp_dict and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] == dhcp_scope: adapter_ids.append(matches.group(1)) adapter_ids.sort() for a in adapter_ids: adapter_name = '' mac_address = '' dhcp_hostname = '' dhcp_filename = '' dhcp_domain_name = '' ipv4_address = '' if 'nic.%s.ipv4_address.0' % a in tmp_dict: ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a] if 'nic.%s.name.0' % a in tmp_dict: adapter_name = tmp_dict['nic.%s.name.0' % a] if 'nic.%s.mac_address.0' % a in tmp_dict: mac_address = tmp_dict['nic.%s.mac_address.0' % a] if 'nic.%s.dhcp_hostname.0' % a in tmp_dict: dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a] if 'nic.%s.dhcp_filename.0' % a in tmp_dict: dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a] if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict: dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a] final_list.append({ 'system_hostname': system.hostname, 'ipv4_address': ipv4_address }) #tmp_list.append(tmp_dict) return final_list if key_type == 'adapters_by_system_and_scope': #Get keystores from truth that have dhcp.is_scope = True dhcp_scope = request.GET['dhcp_scope'] system = System.objects.get(hostname=request.GET['system']) keyvalue_pairs = KeyValue.objects.filter( key__startswith='nic.').filter(obj=system).order_by('key') #Iterate through the list and get all of the key/value pairs tmp_dict = {} adapter_ids = [] final_list = [] for kv in keyvalue_pairs: tmp_dict[kv.key] = kv.value for k in tmp_dict.iterkeys(): matches = re.match('nic\.(\d+).*', k) if matches.group is not None: dhcp_scope_match = 'nic.%s.dhcp_scope.0' % matches.group( 1) if matches.group( 1 ) not in adapter_ids and dhcp_scope_match in tmp_dict and tmp_dict[ dhcp_scope_match] == dhcp_scope: #if matches.group(1) not in adapter_ids and 'nic.%s.dhcp_scope.0' % matches.group(1) in tmp_dict and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] == dhcp_scope: adapter_ids.append(matches.group(1)) adapter_ids.sort() for a in adapter_ids: adapter_name = '' mac_address = '' dhcp_hostname = '' dhcp_filename = '' dhcp_domain_name = '' ipv4_address = '' if 'nic.%s.ipv4_address.0' % a in tmp_dict: ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a] if 'nic.%s.name.0' % a in tmp_dict: adapter_name = tmp_dict['nic.%s.name.0' % a] if 'nic.%s.mac_address.0' % a in tmp_dict: mac_address = tmp_dict['nic.%s.mac_address.0' % a] if 'nic.%s.dhcp_hostname.0' % a in tmp_dict and 'nic.%s.option_hostname.0' % a not in tmp_dict: dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a] if 'nic.%s.option_hostname.0' % a not in tmp_dict: dhcp_hostname = tmp_dict['nic.%s.option_hostname.0' % a] if 'nic.%s.dhcp_filename.0' % a in tmp_dict: dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a] if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict: dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a] final_list.append({ 'system_hostname': system.hostname, 'ipv4_address': ipv4_address, 'adapter_name': adapter_name, 'mac_address': mac_address, 'dhcp_hostname': dhcp_hostname, 'dhcp_filename': dhcp_filename, 'dhcp_domain_name': dhcp_domain_name }) #tmp_list.append(tmp_dict) return final_list elif 'key' in request.GET and request.GET['key'] > '': tmp_list = {} try: base = KeyValue.objects.filter(key=request.GET['key']) for row in base: matches = re.match("\$\{(.*)\}", row.value) if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() for r in base: key_name = 'host:%s:%s' % (r.system.hostname, r.key) tmp_list[key_name] = r.value except Exception, e: pass try: base = TruthKeyValue.objects.filter(key=request.GET['key']) for row in base: matches = re.match("\$\{(.*)\}", row.value) if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() for r in base: key_name = 'truth:%s:%s' % (r.truth.name, r.key) tmp_list[key_name] = r.value except Exception, e: pass
for r in base: key_name = 'truth:%s:%s' % (r.truth.name, r.key) tmp_list[key_name] = r.value except Exception, e: pass return tmp_list elif 'value' in request.GET: tmp_list = {} try: base = KeyValue.objects.filter(value=request.GET['value']) for row in base: matches = re.match("\$\{(.*)\}", row.value) if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() for r in base: key_name = 'host:%s:%s' % (r.system.hostname, r.key) tmp_list[key_name] = r.value except Exception, e: pass try: base = TruthKeyValue.objects.filter(value=request.GET['value']) for row in base: matches = re.match("\$\{(.*)\}", row.value) if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() for r in base: key_name = 'truth:%s:%s' % (r.truth.name, r.key) tmp_list[key_name] = r.value
def read(self, request, key_value_id=None): #if keystore get var is set return the whole keystore if 'keystore' in request.GET: #if key get var is set return the keystore based on the existance of this key if 'key' in request.GET: base = KeyValue.objects.filter(key=request.GET['keystore']).filter(keyvalue_set__contains=request.GET['key']) tmp_list = [] for row in base: matches = re.match("\$\{(.*)\}", row.value) if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() for r in base: key_name = 'host:%s:%s' % (r.system.hostname, r.key) tmp_list[key_name] = r.value if 'key' not in request.GET: tree = KeyValueTree(request.GET['keystore']).final return tree elif 'key_type' in request.GET: key_type = request.GET['key_type'] tmp_list = [] if key_type == 'dhcp_scopes': #Get keystores from truth that have dhcp.is_scope = True base = TruthKeyValue.objects.filter(key='dhcp.is_scope',value='True') #Iterate through the list and get all of the key/value pairs for row in base: keyvalue = TruthKeyValue.objects.filter(truth=row.truth) tmp_dict = {} for kv in keyvalue: tmp_dict[kv.key] = kv.value tmp_list.append(tmp_dict) return tmp_list if key_type == 'system_by_scope': #Get keystores from truth that have dhcp.is_scope = True keyvalue_pairs = KeyValue.objects.filter(key__contains='dhcp_scope',value=request.GET['scope']).filter(key__startswith='nic.') #Iterate through the list and get all of the key/value pairs tmp_list = [] for row in keyvalue_pairs: keyvalue = KeyValue.objects.filter(system=row.system) tmp_dict = {} for kv in keyvalue: tmp_dict[kv.key] = kv.value tmp_dict['hostname'] = row.system.hostname appendable = True for the_items in tmp_list: if 'hostname' not in the_items: appendable = True elif the_items['hostname'] == row.system.hostname: appendable = False if appendable is True: tmp_list.append(tmp_dict) #tmp_list = list(set(tmp_list)) return tmp_list if key_type == 'adapters_by_system': #Get keystores from truth that have dhcp.is_scope = True system = System.objects.get(hostname=request.GET['system']) keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(system=system).order_by('key') #Iterate through the list and get all of the key/value pairs tmp_dict = {} adapter_ids = [] final_list = [] for kv in keyvalue_pairs: tmp_dict[kv.key] = kv.value for k in tmp_dict.iterkeys(): matches = re.match('nic\.(\d+).*',k) if matches.group is not None: if matches.group(1) not in adapter_ids: adapter_ids.append(matches.group(1)) adapter_ids.sort() for a in adapter_ids: adapter_name = '' mac_address = '' dhcp_hostname = '' dhcp_filename = '' ipv4_address = '' option_domain_name = '' if 'nic.%s.ipv4_address.0' % a in tmp_dict: ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a] if 'nic.%s.name.0' % a in tmp_dict: adapter_name = tmp_dict['nic.%s.name.0' % a] if 'nic.%s.mac_address.0' % a in tmp_dict: mac_address = tmp_dict['nic.%s.mac_address.0' % a] if 'nic.%s.dhcp_hostname.0' % a in tmp_dict: dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a] if 'nic.%s.dhcp_filename.0' % a in tmp_dict: dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a] try: final_list.append({ 'system_hostname':system.hostname, 'ipv4_address':ipv4_address, 'adapter_name':adapter_name, 'mac_address':mac_address, 'dhcp_hostname':dhcp_hostname, 'dhcp_filename':dhcp_filename} ) except: pass #tmp_list.append(tmp_dict) return final_list if key_type == 'adapters_by_system_and_scope': #Get keystores from truth that have dhcp.is_scope = True dhcp_scope = request.GET['dhcp_scope'] system = System.objects.get(hostname=request.GET['system']) keyvalue_pairs = KeyValue.objects.filter(key__startswith='nic.').filter(system=system).order_by('key') #Iterate through the list and get all of the key/value pairs tmp_dict = {} adapter_ids = [] final_list = [] for kv in keyvalue_pairs: tmp_dict[kv.key] = kv.value for k in tmp_dict.iterkeys(): matches = re.match('nic\.(\d+).*',k) if matches.group is not None: #if matches.group(1) not in adapter_ids and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] is not None and tmp_dict['nic.%s.dhcp_scope.0' % matches.group(1)] == dhcp_scope: dhcp_scope_match = 'nic.%s.dhcp_scope.0' % matches.group(1) if matches.group(1) not in adapter_ids and dhcp_scope_match in tmp_dict and tmp_dict[dhcp_scope_match] == dhcp_scope: adapter_ids.append(matches.group(1)) adapter_ids.sort() for a in adapter_ids: adapter_name = '' mac_address = '' dhcp_hostname = '' dhcp_filename = '' dhcp_domain_name = '' ipv4_address = '' if 'nic.%s.ipv4_address.0' % a in tmp_dict: ipv4_address = tmp_dict['nic.%s.ipv4_address.0' % a] if 'nic.%s.name.0' % a in tmp_dict: adapter_name = tmp_dict['nic.%s.name.0' % a] if 'nic.%s.mac_address.0' % a in tmp_dict: mac_address = tmp_dict['nic.%s.mac_address.0' % a] if 'nic.%s.dhcp_hostname.0' % a in tmp_dict: dhcp_hostname = tmp_dict['nic.%s.dhcp_hostname.0' % a] if 'nic.%s.option_hostname.0' % a in tmp_dict: dhcp_hostname = tmp_dict['nic.%s.option_hostname.0' % a] if 'nic.%s.dhcp_filename.0' % a in tmp_dict: dhcp_filename = tmp_dict['nic.%s.dhcp_filename.0' % a] if 'nic.%s.dhcp_domain_name.0' % a in tmp_dict: dhcp_domain_name = tmp_dict['nic.%s.dhcp_domain_name.0' % a] final_list.append({'system_hostname':system.hostname, 'ipv4_address':ipv4_address, 'adapter_name':adapter_name, 'mac_address':mac_address, 'dhcp_hostname':dhcp_hostname, 'dhcp_filename':dhcp_filename, 'dhcp_domain_name':dhcp_domain_name}) #tmp_list.append(tmp_dict) return final_list elif 'key' in request.GET and request.GET['key'] > '': tmp_list = {} try: base = KeyValue.objects.filter(key=request.GET['key']) for row in base: matches = re.match("\$\{(.*)\}", row.value) if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() for r in base: key_name = 'host:%s:%s' % (r.system.hostname, r.key) tmp_list[key_name] = r.value except Exception, e: pass try: base = TruthKeyValue.objects.filter(key=request.GET['key']) for row in base: matches = re.match("\$\{(.*)\}", row.value) if matches is not None: m = MacroExpansion(matches.group(1)) row.value = m.output() for r in base: key_name = 'truth:%s:%s' % (r.truth.name, r.key) tmp_list[key_name] = r.value except Exception, e: pass
def test_key_value_found(self): m = MacroExpansion('host:fake-hostname2:ip_address') self.assertEqual(m.output(), '10.99.32.1')
def get_parents(self, parent_type, parent_name, root=False): #ret = [] if parent_type is not None and parent_name is not None: obj = None if parent_type == 'host': obj = System.objects.get(hostname=parent_name) base = KeyValue.expanded_objects.filter(obj=obj).exclude( key__contains='parent') base_parents = KeyValue.expanded_objects.filter( obj=obj, key__contains='parent') parent_compare = obj.hostname elif parent_type == 'truth': obj = Truth.objects.get(name=parent_name) base = TruthKeyValue.expanded_objects.filter( truth=obj).exclude(key__contains='parent') base_parents = TruthKeyValue.expanded_objects.filter( truth=obj, key__contains='parent') parent_compare = obj.name if base is not None and obj is not None: for host in base: if host.value is not None and host.key is not None: matches = re.match("\$\{(.*)\}", host.value) if not re.search("parent", host.key): if matches is not None: m = MacroExpansion(matches.group(1)) host.value = m.output() if host.key is not None and host.value is not None: if self.search_string != parent_compare: host.key = "%s:%s:%s" % (parent_type, parent_name, host.key) tmp = {'key': host.key, 'value': host.value} self.ret.append(tmp) for row in base_parents: parent_type = row.value.replace("${", "").split(':')[0] parent_name = row.value.replace("}", "").split(':')[1] self.ret.append( self.get_parents(parent_type, parent_name, root=False)) """for host in base: if host.value is not None and host.key is not None: matches = re.match("\$\{(.*)\}", host.value) if not re.search("parent", host.key): if matches is not None: m = MacroExpansion(matches.group(1)) host.value = m.output() if host.key is not None and host.value is not None: #Not sure if i'll keep this or not, but display the parent's name in () if self.search_string != truth.hostname: host.key = "truth:%s:%s" % (parent_name, host.key) tmp = {'key':host.key, 'value':host.value} self.ret.append(tmp) base_parents = TruthKeyValue.expanded_objects.filter(truth=truth,key__contains='parent') for row in base_parents: parent_type = row.value.replace("${","").split(':')[0] parent_name = row.value.replace("}","").split(':')[1] self.ret.append(self.get_parents(parent_type, parent_name, root=False))""" return self.ret else: return None