paulbsd-salt/states/_modules/ovhapi.py

210 lines
5.1 KiB
Python
Raw 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
def __virtual__():
2021-01-17 17:43:56 +01:00
2020-07-10 00:58:55 +02:00
return True
2021-01-17 17:43:56 +01: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
def _auth():
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
def domain_get_zone(zone=""):
'''
Get DNS zone extraction
zone
Zone name to fetch
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
client = _auth()
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
def domain_get_record(zone="", fieldType="", subDomain=""):
'''
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)
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
2021-01-17 17:43:56 +01:00
res = None
2020-07-10 00:58:55 +02:00
client = _auth()
try:
2021-01-17 17:43:56 +01:00
record = client.get(f'/domain/zone/{zone}/record',
2020-07-10 00:58:55 +02:00
fieldType=fieldType,
subDomain=subDomain)
2021-01-17 17:43:56 +01:00
if len(record) > 0:
req = client.get(f'/domain/zone/{zone}/record/{record}')
return req
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-01-17 17:43:56 +01:00
def domain_post_record(zone="",
fieldType="",
subDomain="",
target="",
ttl=0):
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
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
client = _auth()
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-01-17 17:43:56 +01:00
def domain_put_record(zone="",
fieldType="",
subDomain="",
target="",
ttl=0):
'''
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
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
client = _auth()
try:
records = client.get(f'/domain/zone/{zone}/record',
fieldType=fieldType,
subDomain=subDomain)
if len(records) > 0:
record = client.get(f'/domain/zone/{zone}/record/{records[0]}')
2021-01-24 19:01:14 +01:00
req = client.put(f'/domain/zone/{zone}/record/{record["id"]}',
2021-01-17 17:43:56 +01:00
subDomain=subDomain,
target=target,
ttl=ttl)
return req
else:
return "Error updating record"
except APIError:
return "Query failed in OVH API"
return None
2020-07-10 00:58:55 +02:00
def domain_delete_record(zone="", fieldType="", subDomain=""):
'''
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)
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
results = []
client = _auth()
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
def domain_refresh_zone(zone=""):
'''
Apply zone modification on DNS servers
zone
The internal name of your zone
'''
if zone == "":
raise ArgumentValueError("Zone is not defined")
client = _auth()
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