2020-07-10 00:58:55 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
2021-01-17 17:43:56 +01:00
|
|
|
# check if record exists
|
|
|
|
cur_record = __salt__['ovhapi.domain_get_record'](zone=zone,
|
2022-08-06 20:23:43 +02:00
|
|
|
fieldType=recordtype,
|
|
|
|
subDomain=recordname,
|
|
|
|
target=target)
|
|
|
|
if cur_record:
|
2022-12-11 13:46:51 +01:00
|
|
|
new_record = __salt__['ovhapi.domain_put_record'](zone=zone,
|
2022-08-06 20:23:43 +02:00
|
|
|
fieldType=recordtype,
|
|
|
|
subDomain=recordname,
|
|
|
|
target=target)
|
2020-11-09 23:10:56 +01:00
|
|
|
|
2021-01-24 19:01:14 +01:00
|
|
|
ret['changes'] = {
|
2022-12-11 13:46:51 +01:00
|
|
|
"diff": f"old record: {cur_record}, new record: {new_record}"
|
2021-01-24 19:01:14 +01:00
|
|
|
}
|
|
|
|
res_output = 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-08-06 20:23:43 +02:00
|
|
|
subDomain=recordname,
|
|
|
|
fieldType=recordtype,
|
|
|
|
target=target,
|
|
|
|
ttl=ttl)
|
2020-07-10 00:58:55 +02:00
|
|
|
|
2021-01-24 19:01:14 +01:00
|
|
|
ret['changes'] = {
|
2022-12-11 13:46:51 +01:00
|
|
|
"diff": f"new record: {new_record}"
|
2021-01-24 19:01:14 +01:00
|
|
|
}
|
|
|
|
res_output = f"Created record {recordname}"
|
2020-07-10 00:58:55 +02:00
|
|
|
|
2022-08-06 20:23:43 +02:00
|
|
|
ret['comment'] = f'Result is {res_output}'
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def domain_zone_refresh(name="", zone=""):
|
|
|
|
res_output = ""
|
|
|
|
ret = {
|
|
|
|
'name': name,
|
|
|
|
'changes': {},
|
|
|
|
'result': True,
|
|
|
|
'comment': 'Zone is refreshed'
|
|
|
|
}
|
|
|
|
|
|
|
|
res_output = __salt__['ovhapi.domain_refresh_zone'](zone=zone)
|
2021-01-17 17:43:56 +01:00
|
|
|
|
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
|