dataset/seed/base/funcs/base.py

48 lines
1.5 KiB
Python
Raw Normal View History

2022-08-18 10:19:43 +02:00
from risotto.utils import multi_function as _multi_function
2022-12-25 17:08:52 +01:00
from typing import List as _List
2022-06-29 11:44:01 +02:00
2022-08-18 10:19:43 +02:00
@_multi_function
2022-12-25 17:08:52 +01:00
def get_ip(zones: dict,
server_name: str,
) -> str:
2022-08-18 10:19:43 +02:00
if server_name is None:
return
if isinstance(server_name, list):
return_list = True
else:
return_list = False
server_name = [server_name]
lst = []
for s_name in server_name:
host_name, domain_name = s_name.split('.', 1)
2022-12-25 17:08:52 +01:00
for zone in zones.values():
if domain_name == zone['domain_name']:
break
else:
2022-08-18 10:19:43 +02:00
raise ValueError(f'cannot find IP in domain name "{domain_name}" (for "{s_name}")')
2022-12-25 17:08:52 +01:00
ret = zone['hosts'][host_name]
2022-08-18 10:19:43 +02:00
if not return_list:
return ret
if ret not in lst:
lst.append(ret)
return lst
2022-12-25 17:08:52 +01:00
@_multi_function
def get_zones_info(zones: dict,
type: str,
zone_names: _List[str]=None,
zone_name: str=None,
index: int=None,
) -> str:
if type == 'host_ip' and index != 0:
return
if zone_name:
if zone_name not in zones:
raise ValueError(f"cannot get zone informations in unknown zone '{zone_name}'")
if type == 'cidr':
return zones[zone_name]['host_ip'] + '/' + zones[zone_name]['network'].split('/')[-1]
return zones[zone_name][type]
return [data[type] for zone_name, data in zones.items() if not zone_names or zone_name in zone_names]