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}")')
|
2023-02-14 14:24:16 +01:00
|
|
|
if not host_name in zone['hosts']:
|
|
|
|
continue
|
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)
|
2023-02-14 14:24:16 +01:00
|
|
|
if return_list:
|
|
|
|
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,
|
2023-06-23 08:12:05 +02:00
|
|
|
uniq: bool=False,
|
2022-12-25 17:08:52 +01:00
|
|
|
) -> 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]
|
2023-06-23 08:12:05 +02:00
|
|
|
ret = []
|
|
|
|
for zone_name, data in zones.items():
|
|
|
|
if zone_names and zone_name not in zone_names:
|
|
|
|
continue
|
|
|
|
val = data[type]
|
|
|
|
if uniq and val in ret:
|
|
|
|
continue
|
|
|
|
ret.append(val)
|
|
|
|
return ret
|