55 lines
2.4 KiB
Python
55 lines
2.4 KiB
Python
|
import sys
|
||
|
import time
|
||
|
import socket
|
||
|
from dataclasses import fields
|
||
|
import ovh
|
||
|
import yaml
|
||
|
import json
|
||
|
import click
|
||
|
import click_config_file
|
||
|
|
||
|
@click.command()
|
||
|
@click.option("--application-key", "-a", required=True, help='Your OVH application key.')
|
||
|
@click.option("--application-secret", "-s", required=True, help='Your OVH application secret. Use better a configuration file.')
|
||
|
@click.option("--consumer-key", "-c", required=True, help='Your OVH consumer key.')
|
||
|
@click.option("--endpoint", "-e", default='ovh-eu', help='OVH endpoint to use.', type=click.Choice(
|
||
|
['ovh-eu', 'ovh-us', 'ovh-ca', 'soyoustart-eu', 'soyoustart-ca', 'kimsufi-eu', 'kimsufi-ca'],
|
||
|
case_sensitive=True,
|
||
|
))
|
||
|
@click.option("--format", "-f", default='bind', help='Format to show the information', type=click.Choice(
|
||
|
['json', 'yaml', 'bind'],
|
||
|
case_sensitive=True,
|
||
|
))
|
||
|
@click.option('--output-file', '-o', type=click.File('wb'), default=sys.stdout)
|
||
|
@click_config_file.configuration_option()
|
||
|
def main(application_key, application_secret, consumer_key, endpoint, format, output_file):
|
||
|
client = ovh.Client(config_file=None, endpoint=endpoint, application_key=application_key, application_secret=application_secret, consumer_key=consumer_key)
|
||
|
dns_config = {
|
||
|
"records":[],
|
||
|
"timestamp": time.time(),
|
||
|
"endpoint": endpoint,
|
||
|
"hostname": socket.gethostname(),
|
||
|
}
|
||
|
for zone in client.get('/domain'):
|
||
|
if format == 'bind':
|
||
|
zone_raw = client.get(f"/domain/zone/{zone}/export")
|
||
|
output_file.write(f"Zone '{zone}':\n{zone_raw}\n".encode())
|
||
|
else:
|
||
|
for record in client.get(f"/domain/zone/{zone}/record"):
|
||
|
record_dict = client.get(f"/domain/zone/{zone}/record/{record}")
|
||
|
field_type = record_dict['fieldType'].lower()
|
||
|
my_record_dict = {
|
||
|
"name": record_dict['subDomain'],
|
||
|
"value": record_dict['target'],
|
||
|
"record_ttl": record_dict['ttl'],
|
||
|
"domain": zone,
|
||
|
"record_type": record_dict['fieldType'],
|
||
|
}
|
||
|
dns_config['records'].append(my_record_dict)
|
||
|
if format == 'yaml':
|
||
|
output_file.write(yaml.dump(dns_config).encode())
|
||
|
elif format == 'json':
|
||
|
output_file.write(json.dumps(dns_config, indent=2).encode())
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|