Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Dynamic data acquisition and automatic supplementary recording in the development of operation and maintenance

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Vm_info_dict = virtual_machine_info.objects.get (ip=vm_ip_addr) print (vm_info_dict) if vm_info_dict! = None: physical_server_ip = vm_info_dict.hostip device_info_dict = physical_server_info.objects.get (ip=physical_server_ip) phy_contain_vms = virtual_machine_info.objects.filter (hostip=physical_server_ip) else: token = gettoken ('xxxxx' 'xxxxx') vm_info_dict=getresponse (' https://xxxxx/API/v1/GetServerInfo/'+vm_ip_addr, token) physical_server_ip = vm_info_dict.get ("hostip") print (physical_server_ip) device_info_dict=getresponse ('https://xxxxxx/API/v1/GetDeviceInfo/'+physical_server_ip, token)

The overall idea is to obtain virtual machine information through Django API, and access it through API if it does not exist.

This problem seems strange, according to the wrong way of thinking, it should be dealt with in the exception catching department of exception, which is indeed possible, but if you want to extend the logic, the logic of exception handling will become heavier and heavier and feel strange.

So after repeated testing, we can see that if we use get to get the data, we will throw an error if we don't get it. I feel that this interface is still not very friendly, and we can implement it in another way, such as filter, because the slave table structure can be located to be a primary key or a unique index.

So the part of the code is adjusted according to the way of count, and then improved on this basis, that is, if it does not exist in the local database, then the access interface gets the database and sinks the database locally, the whole process is transparent for us to use, and it is also a relatively friendly way of interaction.

Vm_info_dict_exists = virtual_machine_info.objects.filter (ip=vm_ip_addr). Count () if vm_info_dict_exists > = 1:vm_info_dict = virtual_machine_info.objects.get (ip=vm_ip_addr) print (vm_info_dict) physical_server_ip = vm_info_dict.hostip device_info_dict = physical_server_info.objects.get (ip=physical_server_ip) phy_contain_vms = virtual_machine_info.objects.filter ( Hostip=physical_server_ip) return_dict ["device_info_dict"] = device_info_dictreturn_dict ["vm_info_dict"] = vm_info_dictreturn_dict ["phy_contain_vms"] = phy_contain_vms else:token = gettoken ('xxxx' 'xxxxxx') vm_info_dict = getresponse (' https://xxxxxx/API/v1/GetServerInfo/' + vm_ip_addr, token) physical_server_ip = vm_info_dict.get ("hostip") print (physical_server_ip) device_info_dict = getresponse ('https://xxxxxxxAPI/v1/GetDeviceInfo/' + physical_server_ip) Token) return_dict ["device_info_dict"] = device_info_dictreturn_dict ["vm_info_dict"] = vm_info_dictvm_info = virtual_machine_info () .vm_info.memo = vm_info_dict.get ("memo") vm_info.save () phy_exists = physical_server_info.objects.filter (ip=vm_info.hostip) .count () if phy_exists = = 0:phy_info = physical_server_info () Phy_exists = physical_server_info.objects.filter (ip=vm_info.hostip). Count () if phy_exists = = 0:phy_info = physical_server_info () phy_info.update_date = device_info_dict.get ("update_date"). Phy_info.memo = device_info_dict.get ("memo") phy_info.save () phy_contain_vms = virtual_machine_info.objects.filter (hostip=vm_info.hostip) return_dict ["phy_contain_vms"] = phy_contain_vms

In the future, I am going to make further improvements to these information, which can realize the incremental refresh of metadata, that is, when the data changes, I will call the API to refresh the data locally, otherwise it will remain unchanged.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report