84 lines
2.9 KiB
Python
84 lines
2.9 KiB
Python
#!/usr/bin/python3
|
|
|
|
import salt.utils.dictupdate
|
|
import salt.utils.dictdiffer
|
|
|
|
|
|
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="",
|
|
zone="",
|
|
recordname="",
|
|
recordtype="A",
|
|
target="",
|
|
ttl=0):
|
|
|
|
res_output = ""
|
|
ret = {
|
|
'name': name,
|
|
'changes': {},
|
|
'result': True,
|
|
'comment': 'Config is up to date'
|
|
}
|
|
|
|
if name is None:
|
|
return _error(ret, 'Must provide name to ovhapi.domain_record_present')
|
|
if zone is None:
|
|
return _error(ret, 'Must provide dns zone to ovhapi.domain_record_present')
|
|
if recordname is None:
|
|
return _error(ret, 'Must provide record name to ovhapi.domain_record_present')
|
|
if recordtype is None:
|
|
return _error(ret, 'Must provide record type to ovhapi.domain_record_present')
|
|
if target is None:
|
|
return _error(ret, 'Must provide target to ovhapi.domain_record_present')
|
|
|
|
cur_zone_state = __salt__['ovhapi.domain_get_zone'](zone=zone)
|
|
|
|
# check if record exists
|
|
cur_record = __salt__['ovhapi.domain_get_record'](zone=zone,
|
|
fieldType=recordtype,
|
|
subDomain=recordname,
|
|
target=target)
|
|
if cur_record is not None:
|
|
res = __salt__['ovhapi.domain_put_record'](zone=zone,
|
|
fieldType=recordtype,
|
|
subDomain=recordname,
|
|
target=target)
|
|
new_zone_state = __salt__['ovhapi.domain_get_zone'](zone=zone)
|
|
|
|
ret['changes'] = {
|
|
"diff": salt.utils.stringutils.get_diff(_str_split(cur_zone_state),
|
|
_str_split(new_zone_state))
|
|
}
|
|
res_output = f"Updated record {recordname}"
|
|
else:
|
|
res = __salt__['ovhapi.domain_post_record'](zone=zone,
|
|
subDomain=recordname,
|
|
fieldType=recordtype,
|
|
target=target,
|
|
ttl=ttl)
|
|
new_zone_state = __salt__['ovhapi.domain_get_zone'](zone=zone)
|
|
|
|
ret['changes'] = {
|
|
"diff": salt.utils.stringutils.get_diff(_str_split(cur_zone_state),
|
|
_str_split(new_zone_state))
|
|
}
|
|
res_output = f"Created record {recordname}"
|
|
|
|
cur_zone_refresh = __salt__['ovhapi.domain_refresh_zone'](zone=zone)
|
|
|
|
ret['comment'] = f'Result is {res_output}'
|
|
|
|
return ret
|