From 2c28b44206600494627f77a386e252ca216036b9 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sat, 6 Aug 2022 20:23:43 +0200 Subject: [PATCH] updated ovh state and modules --- states/_modules/ovhapi.py | 3 +-- states/_states/ovhapi.py | 52 +++++++++++++++++++++++++-------------- states/ovh/domain.sls | 7 ++++++ 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/states/_modules/ovhapi.py b/states/_modules/ovhapi.py index d57589d..e76d3c7 100644 --- a/states/_modules/ovhapi.py +++ b/states/_modules/ovhapi.py @@ -154,11 +154,10 @@ def domain_put_record(zone="", target=target, ttl=ttl) return req - return "Error updating record" except APIError: return "Query failed in OVH API" - return None + return "Error updating record" def domain_delete_record(zone="", fieldType="", subDomain=""): diff --git a/states/_states/ovhapi.py b/states/_states/ovhapi.py index b47fb3e..74450bf 100644 --- a/states/_states/ovhapi.py +++ b/states/_states/ovhapi.py @@ -32,29 +32,29 @@ def domain_record_present(name="", 'comment': 'Config is up to date' } - if name is None: + if not name: return _error(ret, 'Must provide name to ovhapi.domain_record_present') - if zone is None: + if not zone: return _error(ret, 'Must provide dns zone to ovhapi.domain_record_present') - if recordname is None: + if not recordname: return _error(ret, 'Must provide record name to ovhapi.domain_record_present') - if recordtype is None: + if not recordtype: return _error(ret, 'Must provide record type to ovhapi.domain_record_present') - if target is None: + if not target: 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) + fieldType=recordtype, + subDomain=recordname, + target=target) + if cur_record: + __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'] = { @@ -63,11 +63,11 @@ def domain_record_present(name="", } res_output = f"Updated record {recordname}" else: - res = __salt__['ovhapi.domain_post_record'](zone=zone, - subDomain=recordname, - fieldType=recordtype, - target=target, - ttl=ttl) + __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'] = { @@ -76,7 +76,21 @@ def domain_record_present(name="", } 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 + + +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) ret['comment'] = f'Result is {res_output}' diff --git a/states/ovh/domain.sls b/states/ovh/domain.sls index 50182cd..efc1151 100644 --- a/states/ovh/domain.sls +++ b/states/ovh/domain.sls @@ -12,3 +12,10 @@ ovh-domain-{{ domain }}-{{ "{}-{}-{}".format(record.name, record.type, record.ta - ttl: {{ record.ttl|default(0) }} {%- endfor %} {%- endfor %} + +{%- for domain in ovh.domain.zones.keys() %} +ovh-domain-refresh-{{ domain }}: + ovhapi.domain_zone_refresh: + - name: ovh-domain-refresh-{{ domain }} + - zone: {{ domain }} +{%- endfor %}