paulbsd-salt/states/_states/ovhapi.py
Paul Lecuq bfd8b6cf72
Some checks failed
continuous-integration/drone/push Build is failing
updated ovhapi module
2022-12-11 15:23:05 +01:00

97 lines
3.4 KiB
Python

#!/usr/bin/python3
from salt.utils.dictdiffer import deep_diff
def _error(ret, err_msg):
ret['result'] = False
ret['comment'] = err_msg
return ret
def _str_split(string):
delim = "\n"
return [e + delim for e in string.split(delim) if e]
def domain_record_present(name=None,
zone=None,
recordname=None,
recordtype="A",
target=None,
ttl=0):
res_output = ""
ret = {
'name': name,
'changes': {},
'result': True,
'comment': 'Config is up to date'
}
if type(name) is not str:
return _error(ret, 'Must provide name to ovhapi.domain_record_present')
if type(zone) is not str:
return _error(ret, 'Must provide dns zone to ovhapi.domain_record_present')
if type(recordname) is not str:
return _error(ret, 'Must provide record name to ovhapi.domain_record_present')
if type(recordtype) is not str:
return _error(ret, 'Must provide record type to ovhapi.domain_record_present')
if type(target) is not str:
return _error(ret, 'Must provide target to ovhapi.domain_record_present')
client = __salt__['ovhapi.authenticate']()
# check if record exists
current_record = __salt__['ovhapi.domain_get_record'](zone=zone,
fieldType=recordtype,
subDomain=recordname,
target=target,
client=client)
if current_record:
new_record = __salt__['ovhapi.domain_put_record'](zone=zone,
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}"
else:
new_record = __salt__['ovhapi.domain_post_record'](zone=zone,
subDomain=recordname,
fieldType=recordtype,
target=target,
ttl=ttl,
client=client)
ret['changes'] = {
"diff": f"{new_record}"
}
ret['comment'] = f"Created record {recordname}"
return ret
def domain_zone_refresh(name="", zone=""):
ret = {
'name': name,
'changes': {},
'result': True,
'comment': 'Zone is refreshed'
}
client = __salt__['ovhapi.authenticate']()
ret['comment'] = __salt__['ovhapi.domain_refresh_zone'](zone=zone,
client=client)
return ret