Python Forum
Data alignment in Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/Forum-General-Coding-Help)
+--- Thread: Data alignment in Python (/Thread-Data-alignment-in-Python)



Data alignment in Python - Nirmal - Feb-11-2019

I am new to Python . I am writhing a code to generate a excel file having the data sourced by calling API and correlate those to get desired result.

basically taking input from one database and search that in others and fetch related information.

The 4 databases have below data :

EEp
---------------------
{u'data': [{u'_id': u'5c30702c8ca9f51da8178df4',
            u'encap': u'vlan-24',
            u'ip': u'7.12.12.16',
            u'mac': u'5B:P9:01:9E:42:08'}]}

PathEp
-----------
{u'data': [{u'_id': u'5c54a81a8ca9f51da84ae08e',
            u'paths': u'paths-1507',
            u'endpoint': u'eth1/10',
            u'cep': u'5B:P9:01:9E:42:08',
            u'tenant': u'ESX'}]}
ip4_address
-----------------------

{u'data': [{u'Allocation': u'Build_Reserved',
            u'address': u'7.12.12.16',
            u'name': u'fecitrix-1',
            u'state': u'RESERVED'}]}
asset
---------------
{u'data': [{u'_id': u'57ccce8110dd54f02881fedc', 
            u'client': u'CES',
            u'hostname': u'fecitrix-1'
            u'os_team': u'Window'}]}
Logic:

1. if "mac" of EEp and "cep" of PathEp is same than take "encap","ip" ,"mac"
   "paths" ,'endpoint","cep" and "tenant" (these values need to be exported
    to excel)

2.take ip of EEp and search in "ip4_address" 
and get the "name" from ip4_address ( name need to be exported to excel).

3.if "name" of ip4_address is equal to "hostname" os asset then take 
  "client" and "os_team" ( export that to excel)
i have written the script but not getting the desired result.

def get_host_details(self):
    data = {
        "find": {
            "hostname": self.controller
            },
    "projection":{
        "tenant": 1,
        "paths": 1,
        "endpoint":1
        }
    }
    host_details = self.post("https://database.app.com/api/data/devices/PathEp/find", data)
    #print host_details
    hosts = []
    for record in host_details:
        if "mig" not in record["endpoint"]:
            hosts.append(record)
    return hosts


def get_ipaddress(self, controller):

    host_record = {"tenant": "UNKNOWN",
                   "paths": "UNKNOWN",
                   "endpoint": "UNKNOWN",
                   "ip": "UNKNOWN",
                   "mac": "UNKNOWN",
                   "encap": "UNKNOWN"}
    data = {
        "find": {
            "hostname": controller,
            "ip": {
                "$ne": "0.0.0.0"
            }
        },
        "projection": {
            "ip": 1,
            "mac":1,
            "encap":1,
        }
    }
    endpoints = self.post("https://database.app.com/api/data/devices/EEp/find", data)
    IPAM = self.get_dns()
    print endpoints
    host_details = self.get_host_details()
    host_details_record = []
    for record in endpoints:
        for host in host_details:
            if record["mac"] == host["cep"]:
                host_record = {"tenant": host["tenant"],
                               "paths": host["paths"],                                  
                               "endpoint": host["endpoint"],
                               "ip": record["ip"],
                               "mac": record["mac"],
                               "encap": record["encap"]}
                host_details_record.append(host_record)
    self.get_excel(host_details_record)


def get_dns(self, endpoints):
    ip_dns_record = []
    for each_endpoint in endpoints:
        data = {
            "find":
                {
                    "address": {
                        "$eq": each_endpoint["ip"]
                    },
                },
            "projection":
                {
                    "name": 1
                }

        }

        dns_record = {"client":"UNKNOWN",
                      "os_team":"UNKNOWN",


        ipam_record = self.post("https://database.app.com/api/data/"
                                "internal/ip4_address/find", data)
        if ipam_record:
            dns_record["ip_address"] = each_endpoint["ip"]
            dns_record["hostname"] = ipam_record[0]["name"]
            dns_record = self.get_remedy_details(ipam_record[0]["name"],
                                                 dns_record)
            ip_dns_record.append(dns_record)
        else:
            dns_record["ip_address"] = each_endpoint["ip"]
            dns_record["hostname"] = "UNKNOWN"
            ip_dns_record.append(dns_record)
    self.get_excel(ip_dns_record)



def get_remedy_details(self, hostname, dns_record):

    data = {
        "find":
            {
                "hostname": hostname.upper(),

            }
    }
    remedy_data = self.post("https://database.app.com/api/data/internal/asset/find", data)
    print(remedy_data)
    #remedy_data = remedy_data["data"]
    if remedy_data:
        dns_record["client"] = remedy_data[0].get("client","UNKNOWN")
        dns_record["os_team"] = remedy_data[0].get("os_team", "UNKNOWN")

    else:
        dns_record["client"] = "UNKNOWN"
        dns_record["os_team"] = "UNKNOWN"

    return dns_record

def get_excel(self, ip_dns_record):
    filename = self.controller + ".xls"
    excel_file = xlwt.Workbook()
    sheet = excel_file.add_sheet('HOSTLIST')
    sheet.write(0, 0, "IP Address")
    sheet.write(0, 1, "HostName")
    sheet.write(0, 2, "Client")
    sheet.write(0, 3, "OS Team")

    for count in xrange(1, len(ip_dns_record)+1):
        sheet.write(count, 0,ip_dns_record[count - 1]["ip_address"])
        sheet.write(count, 1,ip_dns_record[count - 1]["hostname"])
        sheet.write(count, 2,ip_dns_record[count - 1]["client"])
        sheet.write(count, 3,ip_dns_record[count - 1]["os_team"])

    excel_file.save(filename)
if __name__ == "__main__":
    controller = sys.argv[1]
    OBJ = ACIHostList(controller)
    print "SCRIPT COMPLETED"
No idea where i am going wrong and what needs to be done. Any idea?


RE: Data alignment in Python - nilamo - Feb-12-2019

You didn't include the __init__ method, so unless that does more than just initialization, your script doesn't do anything because you aren't calling any of it's methods.