paulbsd-salt/states/_modules/ovhapi.py

224 lines
5.5 KiB
Python
Raw Permalink Normal View History

2020-07-10 00:58:55 +02:00
#!/usr/bin/python3
import salt
import ovh
from salt.exceptions import CommandExecutionError, ArgumentValueError
from ovh.exceptions import ResourceNotFoundError, APIError
2021-07-11 17:37:42 +02:00
2020-07-10 00:58:55 +02:00
def __virtual__():
return True
2021-07-11 17:37:42 +02:00
2020-07-10 00:58:55 +02:00
def _config():
config = __salt__['config.get']('ovh')
if not config:
raise CommandExecutionError(
'OVH execution module configuration could not be found'
)
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
return config
2021-07-11 17:37:42 +02:00
2022-12-11 15:22:48 +01:00
def authenticate():
2020-07-10 00:58:55 +02:00
cfg = _config()
client = ovh.Client(
endpoint=cfg['endpoint'],
application_key=cfg['application_key'],
application_secret=cfg['application_secret'],
consumer_key=cfg['consumer_key'],
)
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
return client
2021-07-11 17:37:42 +02:00
2022-12-11 15:22:48 +01:00
def domain_get_zone(zone="",
client=None):
2020-07-10 00:58:55 +02:00
'''
Get DNS zone extraction
zone
Zone name to fetch
2022-12-11 15:22:48 +01:00
client
Client to use against API
2020-07-10 00:58:55 +02:00
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
2021-01-17 17:43:56 +01:00
try:
results = client.get(f'/domain/zone/{zone}/export')
except APIError:
return "Query failed in OVH API"
2020-07-10 00:58:55 +02:00
return results
2022-12-11 15:22:48 +01:00
def domain_get_record(zone="",
fieldType="",
subDomain="",
target="",
client=None):
2020-07-10 00:58:55 +02:00
'''
2021-01-17 17:43:56 +01:00
Record of the zone
2020-07-10 00:58:55 +02:00
zone
Zone name to fetch
fieldType
Filter the value of fieldType property (like)
subDomain
Filter the value of subDomain property (like)
2021-02-07 16:18:16 +01:00
target
Resource record target
2022-12-11 15:22:48 +01:00
client
Client to use against API
2020-07-10 00:58:55 +02:00
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
try:
2021-02-07 16:18:16 +01:00
records = client.get(f'/domain/zone/{zone}/record',
2020-07-10 00:58:55 +02:00
fieldType=fieldType,
subDomain=subDomain)
2021-02-07 16:18:16 +01:00
if len(records) > 0:
for rec in records:
res = client.get(f'/domain/zone/{zone}/record/{rec}')
if res['target'] == target:
return res
2020-07-10 00:58:55 +02:00
except APIError:
return "Query failed in OVH API"
2021-01-17 17:43:56 +01:00
return None
2020-07-10 00:58:55 +02:00
2021-07-11 17:37:42 +02:00
2021-01-17 17:43:56 +01:00
def domain_post_record(zone="",
fieldType="",
subDomain="",
target="",
2022-12-11 15:22:48 +01:00
ttl=0,
client=None):
2020-07-10 00:58:55 +02:00
'''
Create a new DNS record
zone
The internal name of your zone
fieldType
Filter the value of fieldType property (like)
subDomain
Filter the value of subDomain property (like)
target
Resource record target
ttl
Resource record ttl
2022-12-11 15:22:48 +01:00
client
Client to use against API
2020-07-10 00:58:55 +02:00
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
2021-01-17 17:43:56 +01:00
try:
req = client.post(f'/domain/zone/{zone}/record',
fieldType=fieldType,
subDomain=subDomain,
target=target,
ttl=ttl)
except APIError:
return "Query failed in OVH API"
2020-07-10 00:58:55 +02:00
return req
2021-07-11 17:37:42 +02:00
2021-01-17 17:43:56 +01:00
def domain_put_record(zone="",
fieldType="",
subDomain="",
target="",
2022-12-11 15:22:48 +01:00
ttl=0,
current=None,
client=None):
2021-01-17 17:43:56 +01:00
'''
Update a DNS record
zone
The internal name of your zone
fieldType
Filter the value of fieldType property (like)
subDomain
Filter the value of subDomain property (like)
target
Resource record target
ttl
Resource record ttl
2022-12-11 15:22:48 +01:00
current
Current DNS record
client
Client to use against API
2021-01-17 17:43:56 +01:00
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
try:
2022-12-11 15:22:48 +01:00
client.put(f'/domain/zone/{zone}/record/{current["id"]}',
subDomain=subDomain,
target=target,
ttl=ttl)
changed = client.get(f'/domain/zone/{zone}/record/{current["id"]}')
2021-01-17 17:43:56 +01:00
except APIError:
return "Query failed in OVH API"
2022-12-11 15:22:48 +01:00
return changed
2021-01-17 17:43:56 +01:00
2021-07-11 17:37:42 +02:00
2022-12-11 15:22:48 +01:00
def domain_delete_record(zone="",
fieldType="",
subDomain="",
client=None):
2020-07-10 00:58:55 +02:00
'''
Delete a DNS record (Don't forget to refresh the zone)
zone
The internal name of your zone
fieldType
Filter the value of fieldType property (like)
subDomain
Filter the value of subDomain property (like)
2022-12-11 15:22:48 +01:00
client
Client to use against API
2020-07-10 00:58:55 +02:00
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
results = []
try:
records = client.get(f'/domain/zone/{zone}/record',
fieldType=fieldType,
subDomain=subDomain)
except APIError:
return "Query failed in OVH API"
for record in records:
try:
req = client.delete(f'/domain/zone/{zone}/record/{record}')
results.append(req)
except ResourceNotFoundError:
return "Resource not found in OVH API"
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
return results
2021-07-11 17:37:42 +02:00
2022-12-11 15:22:48 +01:00
def domain_refresh_zone(zone="",
client=None):
2020-07-10 00:58:55 +02:00
'''
Apply zone modification on DNS servers
zone
The internal name of your zone
2022-12-11 15:22:48 +01:00
client
Client to use against API
2020-07-10 00:58:55 +02:00
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
2021-01-17 17:43:56 +01:00
try:
req = client.post(f'/domain/zone/{zone}/refresh')
except APIError:
return "Query failed in OVH API"
2020-09-15 13:14:34 +02:00
return req