Python Forum
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?
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for alignment and font size 1418 0 310 Jan-14-2024, 03:56 AM
Last Post: 1418
  make all text in Right to left alignment in tkinter widgets jalal0034 1 1,316 Sep-27-2022, 06:42 PM
Last Post: Larz60+
  Right to left alignment in python report using Reportlab jalal0034 1 1,826 Sep-27-2022, 04:25 AM
Last Post: jalal0034
  pandas, tabulate, and alignment menator01 3 7,255 Feb-05-2022, 07:04 AM
Last Post: menator01
  Space between list and column alignment rturus 8 5,096 Mar-17-2021, 04:47 PM
Last Post: rturus
  OpenPyxl Cell.value Alignment pcsailor 0 8,816 Sep-10-2018, 01:09 AM
Last Post: pcsailor
  List Alignment Help A3G 1 4,115 Oct-25-2016, 06:59 AM
Last Post: Ofnuts
  matplotlib barh y tick alignment iFunKtion 2 7,610 Oct-06-2016, 11:46 AM
Last Post: iFunKtion

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020