paulbsd-salt/states/_states/ovhapi.py

84 lines
2.9 KiB
Python
Raw Normal View History

2020-07-10 00:58:55 +02:00
#!/usr/bin/python3
import salt.utils.dictupdate
import salt.utils.dictdiffer
2021-01-17 17:43:56 +01:00
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
2021-02-09 01:29:48 +01:00
def domain_record_present(name="",
zone="",
recordname="",
recordtype="A",
target="",
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'
}
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)
2021-01-17 17:43:56 +01:00
# check if record exists
cur_record = __salt__['ovhapi.domain_get_record'](zone=zone,
fieldType=recordtype,
2021-02-07 16:18:16 +01:00
subDomain=recordname,
target=target)
2021-01-17 17:43:56 +01:00
if cur_record is not None:
res = __salt__['ovhapi.domain_put_record'](zone=zone,
fieldType=recordtype,
2021-02-04 01:03:00 +01:00
subDomain=recordname,
target=target)
2021-01-24 19:01:14 +01:00
new_zone_state = __salt__['ovhapi.domain_get_zone'](zone=zone)
2020-11-09 23:10:56 +01:00
2021-01-24 19:01:14 +01:00
ret['changes'] = {
"diff": salt.utils.stringutils.get_diff(_str_split(cur_zone_state),
_str_split(new_zone_state))
}
res_output = f"Updated record {recordname}"
2021-01-17 17:43:56 +01:00
else:
res = __salt__['ovhapi.domain_post_record'](zone=zone,
subDomain=recordname,
fieldType=recordtype,
target=target,
ttl=ttl)
2021-01-24 19:01:14 +01:00
new_zone_state = __salt__['ovhapi.domain_get_zone'](zone=zone)
2020-07-10 00:58:55 +02:00
2021-01-24 19:01:14 +01:00
ret['changes'] = {
"diff": salt.utils.stringutils.get_diff(_str_split(cur_zone_state),
_str_split(new_zone_state))
}
res_output = f"Created record {recordname}"
2020-07-10 00:58:55 +02:00
2021-01-17 17:43:56 +01:00
cur_zone_refresh = __salt__['ovhapi.domain_refresh_zone'](zone=zone)
2021-01-24 19:01:14 +01:00
ret['comment'] = f'Result is {res_output}'
2020-07-10 00:58:55 +02:00
2020-11-09 23:10:56 +01:00
return ret