paulbsd-salt/states/_states/ovhapi.py

97 lines
3.4 KiB
Python
Raw Permalink Normal View History

2020-07-10 00:58:55 +02:00
#!/usr/bin/python3
2022-12-11 15:22:48 +01:00
from salt.utils.dictdiffer import deep_diff
2020-07-10 00:58:55 +02:00
def _error(ret, err_msg):
ret['result'] = False
ret['comment'] = err_msg
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
return ret
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
def _str_split(string):
delim = "\n"
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
return [e + delim for e in string.split(delim) if e]
2021-01-17 17:43:56 +01:00
2022-12-11 13:46:51 +01:00
def domain_record_present(name=None,
zone=None,
recordname=None,
2021-02-09 01:29:48 +01:00
recordtype="A",
2022-12-11 13:46:51 +01:00
target=None,
2020-07-10 00:58:55 +02:00
ttl=0):
2021-01-17 17:43:56 +01:00
res_output = ""
2020-07-10 00:58:55 +02:00
ret = {
'name': name,
'changes': {},
'result': True,
'comment': 'Config is up to date'
}
2022-12-11 13:46:51 +01:00
if type(name) is not str:
2020-07-10 00:58:55 +02:00
return _error(ret, 'Must provide name to ovhapi.domain_record_present')
2022-12-11 13:46:51 +01:00
if type(zone) is not str:
2020-07-10 00:58:55 +02:00
return _error(ret, 'Must provide dns zone to ovhapi.domain_record_present')
2022-12-11 13:46:51 +01:00
if type(recordname) is not str:
2020-07-10 00:58:55 +02:00
return _error(ret, 'Must provide record name to ovhapi.domain_record_present')
2022-12-11 13:46:51 +01:00
if type(recordtype) is not str:
2020-07-10 00:58:55 +02:00
return _error(ret, 'Must provide record type to ovhapi.domain_record_present')
2022-12-11 13:46:51 +01:00
if type(target) is not str:
2020-07-10 00:58:55 +02:00
return _error(ret, 'Must provide target to ovhapi.domain_record_present')
2022-12-11 15:22:48 +01:00
client = __salt__['ovhapi.authenticate']()
2021-01-17 17:43:56 +01:00
# check if record exists
2022-12-11 15:22:48 +01:00
current_record = __salt__['ovhapi.domain_get_record'](zone=zone,
fieldType=recordtype,
subDomain=recordname,
target=target,
client=client)
if current_record:
2022-12-11 13:46:51 +01:00
new_record = __salt__['ovhapi.domain_put_record'](zone=zone,
2022-12-11 15:22:48 +01:00
fieldType=recordtype,
subDomain=recordname,
target=target,
current=current_record,
client=client)
diff = deep_diff(current_record,new_record)
if diff == {}:
ret['comment'] = f"No updates on record {recordname}"
else:
ret['changes'] = {
"diff": diff
}
ret['comment'] = f"Updated record {recordname}"
2021-01-17 17:43:56 +01:00
else:
2022-12-11 13:46:51 +01:00
new_record = __salt__['ovhapi.domain_post_record'](zone=zone,
2022-12-11 15:22:48 +01:00
subDomain=recordname,
fieldType=recordtype,
target=target,
ttl=ttl,
client=client)
2020-07-10 00:58:55 +02:00
2021-01-24 19:01:14 +01:00
ret['changes'] = {
2022-12-11 15:22:48 +01:00
"diff": f"{new_record}"
2021-01-24 19:01:14 +01:00
}
2022-12-11 15:22:48 +01:00
ret['comment'] = f"Created record {recordname}"
2022-08-06 20:23:43 +02:00
return ret
def domain_zone_refresh(name="", zone=""):
ret = {
'name': name,
'changes': {},
'result': True,
'comment': 'Zone is refreshed'
}
2022-12-11 15:22:48 +01:00
client = __salt__['ovhapi.authenticate']()
ret['comment'] = __salt__['ovhapi.domain_refresh_zone'](zone=zone,
client=client)
2020-07-10 00:58:55 +02:00
2020-11-09 23:10:56 +01:00
return ret