Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Data alignment in Python
#1
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?
Quote
#2
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to parse the data in python sandy 10 344 Jan-15-2019, 05:50 PM
Last Post: sandy
  Storing MySQL BIT Data Type data in python variable krushna 2 212 Dec-31-2018, 01:28 AM
Last Post: krushna
  No Data Transmission with Python / COM neuralcosmonaut 0 153 Dec-13-2018, 09:20 PM
Last Post: neuralcosmonaut
  OpenPyxl Cell.value Alignment pcsailor 0 510 Sep-10-2018, 01:09 AM
Last Post: pcsailor
  Balance Data in Python navindran 1 306 Aug-06-2018, 10:00 AM
Last Post: Larz60+
  Data Validation script in python Tulsi 1 560 Jul-17-2018, 03:54 PM
Last Post: Tulsi
  How to use python to do "for each 365 data, print the largest 18 value? ctliaf 1 394 Apr-28-2018, 08:14 PM
Last Post: snippsat
  Python Selenium (Dropdown-) data Robin_at_Cantelli 1 907 Mar-04-2018, 12:21 PM
Last Post: snippsat
  sending data to second python script Cyberfly 1 632 Jan-29-2018, 10:09 AM
Last Post: Cyberfly
  python data format to c data format tony1812 1 566 Dec-02-2017, 12:34 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)